-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.go
More file actions
208 lines (194 loc) · 3.97 KB
/
encode.go
File metadata and controls
208 lines (194 loc) · 3.97 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
204
205
206
207
package json
import (
"github.com/tinywasm/fmt"
"io"
)
// Encode serializes a Fielder to JSON.
// output: *[]byte | *string | io.Writer.
func Encode(data fmt.Fielder, output any) error {
b := fmt.GetConv()
defer b.PutConv()
if err := encodeFielder(b, data); err != nil {
return err
}
if b.GetString(fmt.BuffErr) != "" {
return fmt.Err(b.GetString(fmt.BuffErr))
}
switch out := output.(type) {
case *[]byte:
res := b.Bytes()
cpy := make([]byte, len(res))
copy(cpy, res)
*out = cpy
case *string:
*out = b.GetString(fmt.BuffOut)
case io.Writer:
_, err := out.Write(b.Bytes())
return err
default:
return fmt.Err("json", "encode", "output must be *[]byte, *string, or io.Writer")
}
return nil
}
func encodeFielder(b *fmt.Conv, f fmt.Fielder) error {
schema := f.Schema()
ptrs := f.Pointers()
if ptrs == nil && schema != nil {
return fmt.Err("json", "encode", "failed to get pointers")
}
b.WriteByte('{')
first := true
for i, field := range schema {
if field.OmitEmpty && isZeroPtr(ptrs[i], field.Type) {
continue
}
if !first {
b.WriteByte(',')
}
first = false
b.WriteByte('"')
fmt.JSONEscape(field.Name, b)
b.WriteByte('"')
b.WriteByte(':')
encodeFromPtr(b, ptrs[i], field.Type)
}
b.WriteByte('}')
return nil
}
// encodeFromPtr writes a JSON value by reading directly from a typed pointer.
// Avoids interface boxing — the value is never wrapped in any.
func encodeFromPtr(b *fmt.Conv, ptr any, ft fmt.FieldType) {
switch ft {
case fmt.FieldText:
if p, ok := ptr.(*string); ok {
b.WriteByte('"')
fmt.JSONEscape(*p, b)
b.WriteByte('"')
} else {
b.WriteString("null")
}
case fmt.FieldInt:
switch p := ptr.(type) {
case *int64:
b.WriteInt(*p)
case *int:
b.WriteInt(int64(*p))
case *int32:
b.WriteInt(int64(*p))
case *int8:
b.WriteInt(int64(*p))
case *int16:
b.WriteInt(int64(*p))
case *uint:
b.WriteInt(int64(*p))
case *uint8:
b.WriteInt(int64(*p))
case *uint16:
b.WriteInt(int64(*p))
case *uint32:
b.WriteInt(int64(*p))
case *uint64:
b.WriteInt(int64(*p))
default:
b.WriteByte('0')
}
case fmt.FieldFloat:
switch p := ptr.(type) {
case *float64:
b.WriteFloat(*p)
case *float32:
b.WriteFloat(float64(*p))
default:
b.WriteByte('0')
}
case fmt.FieldBool:
if p, ok := ptr.(*bool); ok && *p {
b.WriteString("true")
} else {
b.WriteString("false")
}
case fmt.FieldBlob:
if p, ok := ptr.(*[]byte); ok {
b.WriteByte('"')
fmt.JSONEscape(string(*p), b)
b.WriteByte('"')
} else {
b.WriteString("null")
}
case fmt.FieldStruct:
if nested, ok := ptr.(fmt.Fielder); ok {
if err := encodeFielder(b, nested); err != nil {
b.WrString(fmt.BuffErr, err.Error())
}
} else {
b.WriteString("null")
}
case fmt.FieldIntSlice:
if p, ok := ptr.(*[]int); ok {
b.WriteByte('[')
for i, v := range *p {
if i > 0 {
b.WriteByte(',')
}
b.WriteInt(int64(v))
}
b.WriteByte(']')
} else {
b.WriteString("null")
}
default:
b.WriteString("null")
}
}
// isZeroPtr checks if a field value is zero by reading through its pointer.
func isZeroPtr(ptr any, ft fmt.FieldType) bool {
switch ft {
case fmt.FieldText:
if p, ok := ptr.(*string); ok {
return *p == ""
}
case fmt.FieldInt:
switch p := ptr.(type) {
case *int:
return *p == 0
case *int8:
return *p == 0
case *int16:
return *p == 0
case *int32:
return *p == 0
case *int64:
return *p == 0
case *uint:
return *p == 0
case *uint8:
return *p == 0
case *uint16:
return *p == 0
case *uint32:
return *p == 0
case *uint64:
return *p == 0
}
case fmt.FieldFloat:
switch p := ptr.(type) {
case *float64:
return *p == 0
case *float32:
return *p == 0
}
case fmt.FieldBool:
if p, ok := ptr.(*bool); ok {
return !*p
}
case fmt.FieldBlob:
if p, ok := ptr.(*[]byte); ok {
return len(*p) == 0
}
case fmt.FieldIntSlice:
if p, ok := ptr.(*[]int); ok {
return len(*p) == 0
}
}
return false
}