Navigation Menu

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

encoding/json: fails to ignore fields with more than one ignore tag #26062

Closed
siennathesane opened this issue Jun 26, 2018 · 4 comments
Closed

Comments

@siennathesane
Copy link

siennathesane commented Jun 26, 2018

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

$ go version
go version go1.10.3 linux/amd64

Does this issue reproduce with the latest release?

Yes. I can replicate this with go1.10.3 linux/amd64 and go1.10.2 windows/amd64.

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

GOARCH="amd64"
GOBIN="/mnt/p/go//bin"
GOCACHE="/home/mike/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/mnt/p/go/"
GORACE=""
GOROOT="/home/mike/.linuxbrew/Cellar/go/1.10.3/libexec"
GOTMPDIR=""
GOTOOLDIR="/home/mike/.linuxbrew/Cellar/go/1.10.3/libexec/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc-5"
CXX="g++-5"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build284313999=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"github.com/globalsign/mgo/bson"
	"fmt"
	"encoding/json"
)

type T1 struct {
	Foo chan string `json:"-",bson:"-"`
}

type T2 struct {
	Bar chan string `bson:"-",json:"-"`
}

type T3 struct {
	Foo chan string `bson:"-"`
}

type T4 struct {
	Bar chan string `json:"-"`
}

func main() {
	_, err := bson.Marshal(&T1{Foo: make(chan string, 0)})
	if err != nil {
		fmt.Printf("json and bson fails due to multiple ignored tags: %s\n", err)
	}
	_, err = json.Marshal(&T2{Bar: make(chan string, 0)})
	if err != nil {
		fmt.Printf("bson and json fails due to multiple ignored tags: %s\n", err)
	}
	_, err = bson.Marshal(&T3{Foo: make(chan string, 0)})
	if err == nil {
		fmt.Println("bson passes due to one ignore tag.")
	}
	_, err = json.Marshal(&T4{Bar: make(chan string, 0)})
	if err == nil {
		fmt.Println("json passes due to one ignore tag.")
	}
}

What did you expect to see?

All four use cases should work. Struct field tags denoted by "-" should be ignored by all marshallers.

What did you see instead?

json and bson fails due to multiple ignored tags: Can't marshal chan string in a BSON document
bson and json fails due to multiple ignored tags: json: unsupported type: chan string
bson passes due to one ignore tag.
json passes due to one ignore tag.

My use case is having types pull double-duty as both MongoDB objects as well as JSON objects, but I want to be able to ignore unsupported types, such as channels, with struct tags and have both struct tags be ignored properly.

@josharian
Copy link
Contributor

Try using a space to separate the struct tags instead of a comma. See https://golang.org/pkg/reflect/#StructTag.

@urandom
Copy link

urandom commented Jun 26, 2018

This is another illustration of how confusing unstructured tags are: #23637

@FiloSottile
Copy link
Contributor

The code works as expected when using spaces to separate tags. https://play.golang.org/p/0Fa7VU60y0G

@siennathesane
Copy link
Author

Thanks @josharian & @FiloSottile, didn't realise it had to be spaces, thanks for the doc references as well!

@golang golang locked and limited conversation to collaborators Jun 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants