-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/analysis/passes/unmarshal: allow unmarshalling to a type parameter
We can also unmarshal data to a type parameter, in addition to a pointer and an interface. This analyzer probably requires more discussion, but this solution should be sufficient for now. Updates golang/go#48704 Change-Id: I333f919109295e80a04e59df131713553cdbe612 Reviewed-on: https://go-review.googlesource.com/c/tools/+/353210 Run-TryBot: Rebecca Stambler <rstambler@golang.org> Trust: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> gopls-CI: kokoro <noreply+kokoro@google.com>
- Loading branch information
1 parent
0ebff1a
commit ccaa907
Showing
3 changed files
with
30 additions
and
2 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
go/analysis/passes/unmarshal/testdata/src/typeparams/typeparams.go
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,22 @@ | ||
package typeparams | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
func unmarshalT[T any](data []byte) T { | ||
var x T | ||
json.Unmarshal(data, x) | ||
return x | ||
} | ||
|
||
func unmarshalT2[T any](data []byte, t T) { | ||
json.Unmarshal(data, t) | ||
} | ||
|
||
func main() { | ||
x := make(map[string]interface{}) | ||
unmarshalT2([]byte(`{"a":1}`), &x) | ||
fmt.Println(x) | ||
} |
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
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