Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: struct field tag not working when use generics #48317

Closed
nzlov opened this issue Sep 10, 2021 · 2 comments
Closed

cmd/compile: struct field tag not working when use generics #48317

nzlov opened this issue Sep 10, 2021 · 2 comments

Comments

@nzlov
Copy link

@nzlov nzlov commented Sep 10, 2021

What version of Go are you using (go version)?

$ go version
go version devel go1.18-c69f5c0d76 Fri Sep 10 10:45:59 2021 +0000 darwin/amd64

Does this issue reproduce with the latest release?

No

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/nzlov/Library/Caches/go-build"
GOENV="/Users/nzlov/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/nzlov/workspace/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/nzlov/workspace/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/Users/nzlov/program/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/nzlov/program/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="devel go1.18-c69f5c0d76 Fri Sep 10 10:45:59 2021 +0000"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/nzlov/workspace/vgo/generices/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/71/4t6lk06x5y77f2wyln9vqnlw0000gn/T/go-build147050757=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"encoding/json"
	"fmt"
)

type A[T any] struct {
	Name string `json:"name"`
	Data T `json:"d"`
	ErrResp ErrResp `json:"errorResponse"`
}

type ErrResp struct {
	Age int `json:"age"`
}

func main() {
	fmt.Printf("%+v\n", *a[int]())
}

func a[T any]() *T {

	data :=`{"name":"name","d":1,"errorResponse":{"age":10}}`
	a1 := A[T]{}
	 err := json.Unmarshal([]byte(data), &a1)
	if err!=nil{
		panic(err)
	}
	fmt.Printf("%+v\n",a1)
	return &a1.Data
}

What did you expect to see?

{Name:name Data:1 ErrResp:{Age:10}}

What did you see instead?

{Name:name Data:0 ErrResp:{Age:0}}

Fix:

  1. use env GOEXPERIMENT=unified
  2. return Unmarshal obj. like ex1
  3. use full field name. like ex2

Work examples:

package main

import (
	"encoding/json"
	"fmt"
)

type A[T any] struct {
	Name string `json:"name"`
        Data T `json:"d"`
	ErrResp ErrResp `json:"errorResponse"`
}

type ErrResp struct {
	Age int `json:"age"`
}

func main() {
	fmt.Printf("%+v\n", *a[int]())
}

func a[T any]() *A[T] {
	data :=`{"name":"name","d":1,"errorResponse":{"age":10}}
	a1 := A[T]{}
	err := json.Unmarshal([]byte(data), &a1)
	if err!=nil{
		panic(err)
	}
	fmt.Printf("%+v\n",a1)
	return &a1  // <---- return Unmarshal obj
}
package main

import (
	"encoding/json"
	"fmt"
)

type A[T any] struct {
	Name string `json:"name"`
        Data T `json:"data"`
	ErrorResponse ErrResp `json:"errorResponse"`  // <---- use full name
}

type ErrResp struct {
	Age int `json:"age"`
}

func main() {
	fmt.Printf("%+v\n", *a[int]())
}

func a[T any]() *T {

	data :=`{"name":"name","data":1,"errorResponse":{"age":10}}`
 	a1 := A[T]{}
 	err := json.Unmarshal([]byte(data), &a1)
 	if err!=nil{
		panic(err)
 	}
 	fmt.Printf("%+v\n",a1)
 	return &a1.Data
}
@nzlov nzlov changed the title encoding/json: struct field tag no work encoding/json: struct field tag no work when use generic Sep 10, 2021
@gopherbot
Copy link

@gopherbot gopherbot commented Sep 10, 2021

Change https://golang.org/cl/349013 mentions this issue: cmd/compile: save the note of fields when translating struct

Loading

@thanm thanm added this to the Go1.18 milestone Sep 10, 2021
@thanm
Copy link
Contributor

@thanm thanm commented Sep 10, 2021

Loading

@dsnet dsnet changed the title encoding/json: struct field tag no work when use generic cmd/compile: struct field tag not working when use generics Sep 11, 2021
@gopherbot gopherbot closed this in 42057e9 Sep 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants