Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: assert switch type from valueT in struct case (#747)
* fix: switch type from valueT in struct case In a struct case in type assertion, if the source is a valueT, we still need to take the struct type to allow method and field resolution. * fix: handle all ptr structs as well
- Loading branch information
Showing
3 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"sync/atomic" | ||
) | ||
|
||
type wrappedError struct { | ||
wrapped error | ||
} | ||
|
||
func (e wrappedError) Error() string { | ||
return "some outer error" | ||
} | ||
|
||
func (e wrappedError) Unwrap() error { | ||
return e.wrapped | ||
} | ||
|
||
var err atomic.Value | ||
|
||
func getWrapped() *wrappedError { | ||
if v := err.Load(); v != nil { | ||
err := v.(wrappedError) | ||
if err.wrapped != nil { | ||
return &err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func main() { | ||
err.Store(wrappedError{wrapped: errors.New("test")}) | ||
|
||
e := getWrapped() | ||
if e != nil { | ||
println(e.Error()) | ||
println(e.wrapped.Error()) | ||
} | ||
} | ||
|
||
// Output: | ||
// some outer error | ||
// test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"sync/atomic" | ||
) | ||
|
||
type wrappedError struct { | ||
wrapped error | ||
} | ||
|
||
func (e *wrappedError) Error() string { | ||
return "some outer error" | ||
} | ||
|
||
func (e *wrappedError) Unwrap() error { | ||
return e.wrapped | ||
} | ||
|
||
var err atomic.Value | ||
|
||
func getWrapped() *wrappedError { | ||
if v := err.Load(); v != nil { | ||
err := v.(*wrappedError) | ||
if err.wrapped != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func main() { | ||
err.Store(&wrappedError{wrapped: errors.New("test")}) | ||
|
||
e := getWrapped() | ||
if e != nil { | ||
println(e.Error()) | ||
println(e.wrapped.Error()) | ||
} | ||
} | ||
|
||
// Output: | ||
// some outer error | ||
// test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters