Repository navigation
Expand file tree
/
Copy pathencode_map_interface_key_test.go
More file actions
203 lines (179 loc) · 8.02 KB
/
Copy pathencode_map_interface_key_test.go
File metadata and controls
203 lines (179 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package json_test
import (
"bytes"
stdjson "encoding/json"
"errors"
"fmt"
"math"
"net/netip"
"reflect"
"testing"
"time"
json "github.com/goccy/go-json"
)
type ifaceKeyName string
type ifaceKeyInt int16
type ifaceKeyText struct{ v string }
func (t ifaceKeyText) MarshalText() ([]byte, error) { return []byte("text:" + t.v), nil }
type ifaceKeyPtrText struct{ v string }
func (t *ifaceKeyPtrText) MarshalText() ([]byte, error) {
if t == nil {
return []byte("nil"), nil
}
return []byte("ptr:" + t.v), nil
}
type ifaceKeyTextError struct{}
func (ifaceKeyTextError) MarshalText() ([]byte, error) { return nil, fmt.Errorf("no text") }
type ifaceKeyJSON struct{}
func (ifaceKeyJSON) MarshalJSON() ([]byte, error) { return []byte(`"json"`), nil }
type ifaceKeyNamedText string
func (ifaceKeyNamedText) MarshalText() ([]byte, error) { return []byte("named-text"), nil }
type ifaceKeyEscapedText string
func (t ifaceKeyEscapedText) MarshalText() ([]byte, error) { return []byte(t), nil }
type ifaceKeyStringer interface{ String() string }
type ifaceKeyStringerValue string
func (s ifaceKeyStringerValue) String() string { return string(s) }
// ifaceKeyErrorKind is the kind of an error, which the errors of go-json and of encoding/json are compared by.
func ifaceKeyErrorKind(err error, std bool) string {
switch {
case err == nil:
return ""
case std && errors.As(err, new(*stdjson.UnsupportedTypeError)), !std && errors.As(err, new(*json.UnsupportedTypeError)):
return "unsupported type"
case std && errors.As(err, new(*stdjson.UnsupportedValueError)), !std && errors.As(err, new(*json.UnsupportedValueError)):
return "unsupported value"
case std && errors.As(err, new(*stdjson.MarshalerError)), !std && errors.As(err, new(*json.MarshalerError)):
return "marshaler"
}
return fmt.Sprintf("%T", err)
}
func TestEncodeMapInterfaceKeys(t *testing.T) {
// A map whose keys are of an interface type is encoded as encoding/json of the Go it is built with encodes
// it: refused, or, where encoding/json is built on encoding/json/v2, with the names of the dynamic values of
// its keys, whatever they hold, by Marshal, MarshalIndent and an Encoder.
var nilKey any
var nilPtr *string
var nilText *ifaceKeyPtrText
s := "pointed"
ps := &s
cases := []struct {
name string
value any
}{
{"string", map[any]any{"x": 1}},
{"invalid UTF-8 string", map[any]any{"a\xffb": 1, "\xe3\x81": 2, "<\u2028": 3}},
{"named string", map[any]any{ifaceKeyName("n"): 1}},
{"int", map[any]any{1: "a"}},
{"int kinds", map[any]any{int8(-8): 1, int16(-16): 2, int32(-32): 3, int64(math.MinInt64): 4, ifaceKeyInt(5): 5}},
{"uint kinds", map[any]any{uint(1): 1, uint8(8): 2, uint16(16): 3, uint32(32): 4, uint64(math.MaxUint64): 5, uintptr(9): 6}},
{"float32", map[any]any{float32(1.5): 1, float32(2): 2, float32(1e21): 3, float32(1e-7): 4}},
{"float64", map[any]any{1.5: 1}},
{"float64 nan", map[any]any{math.NaN(): 1}},
{"bool", map[any]any{true: 1}},
{"nil", map[any]any{nilKey: 1}},
{"struct", map[any]any{struct{}{}: 1}},
{"array", map[any]any{[2]int{1, 2}: 1}},
{"complex", map[any]any{complex(1, 2): 1}},
{"pointer", map[any]any{ps: 1}},
{"pointer to pointer", map[any]any{&ps: 1}},
{"nil pointer", map[any]any{nilPtr: 1}},
{"text marshaler", map[any]any{ifaceKeyText{"a"}: 1}},
{"pointer text marshaler", map[any]any{&ifaceKeyPtrText{"b"}: 1}},
{"value of pointer text marshaler", map[any]any{ifaceKeyPtrText{"c"}: 1}},
{"nil pointer text marshaler", map[any]any{nilText: 1}},
{"text marshaler error", map[any]any{ifaceKeyTextError{}: 1}},
{"json marshaler", map[any]any{ifaceKeyJSON{}: 1}},
{"named text marshaler", map[any]any{ifaceKeyNamedText("x"): 1}},
{"time", map[any]any{time.Date(2026, 9, 25, 12, 0, 0, 5, time.UTC): 1}},
{"netip", map[any]any{netip.MustParseAddr("192.0.2.1"): 1}},
{"sorted", map[any]any{"b": 1, 10: 2, "a": 3, 2: 4, ifaceKeyName("c"): 5, -1: 6, float32(0.5): 7}},
{"escaped", map[any]any{"<&>": 1, "\u2028": 2, "a\"b": 3, "\n": 4, "\u00e9": 5, "\\": 6}},
{"values", map[any]string{"a": "x", 2: "y"}},
{"struct values", map[any]struct{ A int }{"a": {1}, 2: {2}}},
{"nested", map[string]any{"m": map[any]any{"x": map[any]any{3: []any{true, nil}}}}},
{"in a struct", struct {
M map[any]int `json:"m"`
N int
}{M: map[any]int{"k": 1}, N: 2}},
{"empty", map[any]any{}},
{"nil map", map[any]any(nil)},
{"text marshaler keys with escapes", map[ifaceKeyEscapedText]int{"<&>": 1, "\u2028": 2, "a\"b": 3, "\t": 4, "b": 5}},
{"stringer keys", map[ifaceKeyStringer]int{ifaceKeyStringerValue("s"): 1}},
{"stringer float64 keys", map[fmt.Stringer]int{time.Duration(3): 1}},
{"text marshaler keys", map[interface{ MarshalText() ([]byte, error) }]int{ifaceKeyText{"t"}: 1, &ifaceKeyPtrText{"p"}: 2}},
{"text marshaler keys of a string type", map[ifaceKeyNamedText]int{"y": 1, "z": 2}},
{"text marshaler keys of a string type in a struct", struct{ M map[ifaceKeyNamedText]int }{map[ifaceKeyNamedText]int{"y": 1}}},
{"text marshaler of a string type in a text marshaler key", map[interface{ MarshalText() ([]byte, error) }]int{ifaceKeyNamedText("w"): 1}},
{"pointer to a text marshaler of a string type", func() any { s := ifaceKeyNamedText("v"); return map[any]any{&s: 1} }()},
{"pointer to a text marshaler of a string type in a text marshaler key", func() any {
s := ifaceKeyNamedText("u")
p := &s
return map[interface{ MarshalText() ([]byte, error) }]int{p: 1}
}()},
{"pointers to a text marshaler of a string type", func() any { s := ifaceKeyNamedText("t"); p := &s; return map[any]any{&p: 1} }()},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
want, wantErr := stdjson.Marshal(tc.value)
got, gotErr := json.Marshal(tc.value)
if ifaceKeyErrorKind(gotErr, false) != ifaceKeyErrorKind(wantErr, true) {
t.Fatalf("Marshal: got the error %v, want %v", gotErr, wantErr)
}
if wantErr == nil && !bytes.Equal(got, want) {
t.Fatalf("Marshal: got %s, want %s", got, want)
}
want, wantErr = stdjson.MarshalIndent(tc.value, ">", " ")
got, gotErr = json.MarshalIndent(tc.value, ">", " ")
if ifaceKeyErrorKind(gotErr, false) != ifaceKeyErrorKind(wantErr, true) {
t.Fatalf("MarshalIndent: got the error %v, want %v", gotErr, wantErr)
}
if wantErr == nil && !bytes.Equal(got, want) {
t.Fatalf("MarshalIndent: got %s, want %s", got, want)
}
// the VMs of the colors and of the options: the same entries, in no order when they are not sorted.
for _, opts := range [][]json.EncodeOptionFunc{
{json.Colorize(json.DefaultColorScheme)},
{json.UnorderedMap()},
} {
_, gotErr := json.MarshalWithOption(tc.value, opts...)
if ifaceKeyErrorKind(gotErr, false) != ifaceKeyErrorKind(wantErr, true) {
t.Fatalf("MarshalWithOption: got the error %v, want %v", gotErr, wantErr)
}
if wantErr != nil || len(opts) == 0 {
continue
}
if _, err := json.MarshalIndentWithOption(tc.value, "", " ", opts...); err != nil {
t.Fatalf("MarshalIndentWithOption: %v", err)
}
}
if wantErr == nil {
want, _ := stdjson.Marshal(tc.value)
got, err := json.MarshalWithOption(tc.value, json.UnorderedMap())
if err != nil {
t.Fatal(err)
}
var wantValue, gotValue any
if err := stdjson.Unmarshal(want, &wantValue); err != nil {
t.Fatal(err)
}
if err := stdjson.Unmarshal(got, &gotValue); err != nil {
t.Fatalf("UnorderedMap: %s: %v", got, err)
}
if !reflect.DeepEqual(gotValue, wantValue) {
t.Fatalf("UnorderedMap: got %s, want %s", got, want)
}
}
var wantBuf, gotBuf bytes.Buffer
wantEnc, gotEnc := stdjson.NewEncoder(&wantBuf), json.NewEncoder(&gotBuf)
wantEnc.SetEscapeHTML(false)
gotEnc.SetEscapeHTML(false)
wantErr, gotErr = wantEnc.Encode(tc.value), gotEnc.Encode(tc.value)
if ifaceKeyErrorKind(gotErr, false) != ifaceKeyErrorKind(wantErr, true) {
t.Fatalf("Encoder: got the error %v, want %v", gotErr, wantErr)
}
if wantErr == nil && !bytes.Equal(gotBuf.Bytes(), wantBuf.Bytes()) {
t.Fatalf("Encoder: got %s, want %s", gotBuf.Bytes(), wantBuf.Bytes())
}
})
}
}