Repository navigation
Expand file tree
/
Copy pathdecode_pointer_test.go
More file actions
97 lines (86 loc) · 2.51 KB
/
Copy pathdecode_pointer_test.go
File metadata and controls
97 lines (86 loc) · 2.51 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
package json_test
import (
stdjson "encoding/json"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
"time"
json "github.com/goccy/go-json"
)
type pointeeLevel int8
type pointeeName string
func (n pointeeName) Upper() string { return strings.ToUpper(string(n)) }
// pointeeComplex and pointeeComplex64 are of the complex kinds, which are decoded only by their methods.
type pointeeComplex complex128
func (c *pointeeComplex) UnmarshalJSON(b []byte) error {
f, err := strconv.ParseFloat(string(b), 64)
*c = pointeeComplex(complex(f, -f))
return err
}
type pointeeComplex64 complex64
func (c *pointeeComplex64) UnmarshalText(b []byte) error {
f, err := strconv.ParseFloat(string(b), 32)
*c = pointeeComplex64(complex(float32(f), 1))
return err
}
type pointeeFields struct {
Bool *bool
Int *int
Int8 *int8
Int16 *int16
Int32 *int32
Int64 *int64
Uint *uint
Uint8 *uint8
Uint16 *uint16
Uint32 *uint32
Uint64 *uint64
Uintptr *uintptr
Float32 *float32
Float64 *float64
String *string
Time *time.Time
Level *pointeeLevel
Name *pointeeName
Names []*pointeeName
PtrPtr **string
Struct *struct{ A *string }
StringKind *json.Number
Complex *pointeeComplex
Complex64 *pointeeComplex64
}
func TestDecodePointees(t *testing.T) {
// The values which pointers are set to are allocated by the kinds of their types: the named types of the
// basic kinds are decoded as encoding/json does, and are still intact after the collections of the garbage
// which the decoding of other values causes.
doc := `{"Bool":true,"Int":-1,"Int8":-8,"Int16":-16,"Int32":-32,"Int64":-64,"Uint":1,"Uint8":8,"Uint16":16,
"Uint32":32,"Uint64":64,"Uintptr":7,"Float32":1.5,"Float64":2.5,"String":"string",
"Time":"2026-09-25T12:00:00Z","Level":3,"Name":"name","Names":["a","b",null,"c"],"PtrPtr":"pp",
"Struct":{"A":"a"},"StringKind":12.5,"Complex":1.5,"Complex64":"2.5"}`
var want pointeeFields
if err := stdjson.Unmarshal([]byte(doc), &want); err != nil {
t.Fatal(err)
}
var values []*pointeeFields
for i := 0; i < 64; i++ {
var got pointeeFields
if err := json.Unmarshal([]byte(doc), &got); err != nil {
t.Fatal(err)
}
values = append(values, &got)
if i%8 == 0 {
runtime.GC()
}
}
runtime.GC()
for _, got := range values {
if !reflect.DeepEqual(*got, want) {
t.Fatalf("got %+v, want %+v", *got, want)
}
if got.Name.Upper() != "NAME" {
t.Fatalf("got the method of the name %q", got.Name.Upper())
}
}
}