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/xml: cannot mark parent of nested element omitempty #57938

Closed
stbenjam opened this issue Jan 20, 2023 · 2 comments
Closed

encoding/xml: cannot mark parent of nested element omitempty #57938

stbenjam opened this issue Jan 20, 2023 · 2 comments

Comments

@stbenjam
Copy link

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

$ go version
go version go1.19.5 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/stbenjam/.cache/go-build"
GOENV="/home/stbenjam/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/stbenjam/.gvm/pkgsets/go1.19.5/global/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/stbenjam/.gvm/pkgsets/go1.19.5/global"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/stbenjam/.gvm/gos/go1.19.5"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/stbenjam/.gvm/gos/go1.19.5/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/stbenjam/git/ci-tools/go.mod"
GOWORK=""
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 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2862434727=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Trying to marshal/unmarshal junit XML testsuites. I cannot get omitempty to work on the parent of a potentially nested field.

For this example XML:

<testsuite>
  <properties>
    <property name="go.version" value="go1.17.5 linux/amd64"/>
  </properties>
</testsuite>

If there is no properties element, like this:

<testsuite>
</testsuite>

I would like properties to NOT be marshalled.

Example: https://go.dev/play/p/iYsJBSIKAQl

I tried to put omitempty in two different ways:

	Properties []*Property `xml:"properties>property,omitempty"`

and

	Properties []*Property `xml:"properties,omitempty>property,omitempty"`

The former does not work for properties, the latter is invalid because > is split after splitting on , (https://cs.opensource.google/go/go/+/refs/tags/go1.19.5:src/encoding/xml/typeinfo.go;l=113)

What did you expect to see?

I expected <properties></properties> to not be in the marshalled output when specifying omitempty

What did you see instead?

It was included.

@stbenjam stbenjam changed the title encoding/xml: cannot mark nested element as omitempty encoding/xml: cannot mark parent of nested element omitempty Jan 20, 2023
@seankhliao
Copy link
Member

Duplicate of #7233

@seankhliao seankhliao marked this as a duplicate of #7233 Jan 20, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jan 20, 2023
@deltamualpha
Copy link

Might it be worth adding some clarity to the documentation that modifiers like omitempty or any are only allowed at the end of a struct tag, not interwoven with nested tags in the XML case?

@golang golang locked and limited conversation to collaborators Jan 20, 2024
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

4 participants