-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
197 lines (157 loc) · 4.5 KB
/
encode.go
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
package oxygen
import (
"bytes"
"errors"
"fmt"
"reflect"
"strconv"
"sync"
)
const marshalError = "encode data from"
// Marshal encodes the value v and returns the encoded data.
// If v is nil, Marshal returns an encoder error.
func (e *engine[T]) Marshal(v any) ([]byte, error) {
s := e.newEncodeState()
defer encodeStatePool.Put(s)
if s.marshal(v); s.err != nil {
return nil, s.err
}
return append([]byte(nil), s.Bytes()...), nil
}
type encodeState[T any] struct {
*engine[T]
context[T]
*bytes.Buffer // accumulated output
scratch [64]byte
}
var encodeStatePool sync.Pool
func (e *engine[T]) newEncodeState() *encodeState[T] {
if p := encodeStatePool.Get(); p != nil {
s := p.(*encodeState[T])
s.err = nil
s.Reset()
return s
}
s := &encodeState[T]{engine: e, Buffer: new(bytes.Buffer)}
s.field = new(field[T])
return s
}
func (s *encodeState[T]) marshal(v any) {
if err := s.reflectValue(reflect.ValueOf(v)); err != nil {
if !errors.Is(err, errExist) {
if s.field.typ == nil {
s.field.typ = unPoint(reflect.TypeOf(v))
}
s.setError(s.name, marshalError, err)
}
s.Reset()
}
}
func (s *encodeState[T]) reflectValue(v reflect.Value) error {
return s.cachedCoders(v.Type()).encoderFunc(s, v)
}
type encoderFunc[T any] func(*encodeState[T], reflect.Value) error
func valueFromPtr(v reflect.Value) reflect.Value {
if v.Kind() != reflect.Pointer {
return v
}
if v.IsNil() {
v = reflect.New(v.Type().Elem())
}
return v.Elem()
}
func (f *structFields[T]) encode(s *encodeState[T], v reflect.Value, wrap bool) (err error) {
var sep bool
if wrap {
s.Write(s.structOpener)
}
for _, s.field = range *f {
rv := v.Field(s.field.index)
// Ignore the field if empty values can be omitted.
if s.field.omitempty && isEmptyValue(rv) {
continue
}
if sep {
s.Write(s.valueSeparator)
}
sep = s.separate
if s.field.embedded != nil {
if err = s.field.embedded.encode(s, valueFromPtr(rv), false); err != nil {
return
}
continue
}
s.structName = v.Type().Name()
if err = s.field.functions.encoderFunc(s, rv); err != nil {
return
}
}
if wrap {
s.Write(s.structCloser)
}
return
}
func marshallerEncoder[T any](s *encodeState[T], v reflect.Value) error {
tmp := reflect.ValueOf(v.Interface())
v = reflect.New(v.Type())
v.Elem().Set(tmp)
f, ok := s.IsMarshaller(v)
if !ok {
return nil
}
p, err := f()
if err != nil {
return err
}
return s.Encode(s.field.name, s.field.tag, p, s.Buffer)
}
func boolEncoder[T any](s *encodeState[T], v reflect.Value) error {
return s.Encode(s.field.name, s.field.tag, strconv.AppendBool(s.scratch[:0], v.Bool()), s.Buffer)
}
func intEncoder[T any](s *encodeState[T], v reflect.Value) error {
return s.Encode(s.field.name, s.field.tag, strconv.AppendInt(s.scratch[:0], v.Int(), 10), s.Buffer)
}
func uintEncoder[T any](s *encodeState[T], v reflect.Value) error {
return s.Encode(s.field.name, s.field.tag, strconv.AppendUint(s.scratch[:0], v.Uint(), 10), s.Buffer)
}
func floatEncoder[T any](s *encodeState[T], v reflect.Value) error {
return s.Encode(s.field.name, s.field.tag, strconv.AppendFloat(s.scratch[:0], v.Float(), 'g', -1, bitSize(v.Kind())), s.Buffer)
}
//func arrayEncoder[T any](s *encodeState[T], v reflect.Value) error {
// return nil
//}
func interfaceEncoder[T any](s *encodeState[T], v reflect.Value) error {
if v.IsNil() {
return nil
}
return s.reflectValue(v.Elem())
}
//func mapEncoder[T any](s *encodeState[T], v reflect.Value) error {
// return nil
//}
func pointerEncoder[T any](s *encodeState[T], v reflect.Value) error {
return s.reflectValue(valueFromPtr(v))
}
func bytesEncoder[T any](s *encodeState[T], v reflect.Value) error {
return s.Encode(s.field.name, s.field.tag, v.Bytes(), s.Buffer)
}
//func sliceEncoder[T any](s *encodeState[T], v reflect.Value) error {
// return nil
//}
func stringEncoder[T any](s *encodeState[T], v reflect.Value) error {
return s.Encode(s.field.name, s.field.tag, append(s.scratch[:0], v.String()...), s.Buffer)
}
func structEncoder[T any](s *encodeState[T], v reflect.Value) error {
f := s.cachedFields(v.Type())
return f.encode(s, reflect.ValueOf(v.Interface()), s.wrap)
}
func unsupportedTypeEncoder[T any](s *encodeState[T], _ reflect.Value) error {
s.err = ErrNotSupportType
return errExist
}
func invalidTagEncoder[T any](tag string, err error) encoderFunc[T] {
return func(s *encodeState[T], _ reflect.Value) error {
s.err = fmt.Errorf("%s: tag %s of struct field %s.%s: %w", s.name, tag, s.structName, s.field.name, err)
return nil
}
}