types/opt: fix test to be agnostic to omitzero support (#14401)

The omitzero tag option has been backported to v1 "encoding/json"
from the "encoding/json/v2" prototype and will land in Go1.24.
Until we fully upgrade to Go1.24, adjust the test to be agnostic
to which version of Go someone is using.

Updates tailscale/corp#25406

Signed-off-by: Joe Tsai <joetsai@digital-static.net>
This commit is contained in:
Joe Tsai 2024-12-16 10:56:55 -08:00 committed by GitHub
parent cc168d9f6b
commit 5883ca72a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,6 +9,8 @@ import (
"testing"
jsonv2 "github.com/go-json-experiment/json"
"tailscale.com/types/bools"
"tailscale.com/util/must"
)
type testStruct struct {
@ -87,7 +89,14 @@ func TestValue(t *testing.T) {
False: ValueOf(false),
ExplicitUnset: Value[bool]{},
},
want: `{"True":true,"False":false,"Unset":null,"ExplicitUnset":null}`,
want: bools.IfElse(
// Detect whether v1 "encoding/json" supports `omitzero` or not.
// TODO(Go1.24): Remove this after `omitzero` is supported.
string(must.Get(json.Marshal(struct {
X int `json:",omitzero"`
}{}))) == `{}`,
`{"True":true,"False":false}`, // omitzero supported
`{"True":true,"False":false,"Unset":null,"ExplicitUnset":null}`), // omitzero not supported
wantBack: struct {
True Value[bool] `json:",omitzero"`
False Value[bool] `json:",omitzero"`