-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
256 lines (224 loc) · 7.6 KB
/
engine.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package oxygen
import (
"reflect"
"sync"
)
// Engine represents the main functions that the package implements.
type Engine interface {
// Marshal encodes the value v and returns the encoded data.
Marshal(v any) ([]byte, error)
// Unmarshal decodes the encoded data and stores the result in the value pointed to by v.
Unmarshal(data []byte, v any) error
}
type Writer interface {
Write(p []byte) (n int, err error)
WriteByte(c byte) error
WriteString(s string) (n int, err error)
}
// Tag describes what functions an entity should implement to use when creating a new Engine entity.
// The entity must include the oxygen.Default that implements following default Parse method,
// so it may not implement this method.
type Tag[T any] interface {
// Parse gets a tagValue string, parses the tagValue into tag *T,
// returns a flag indicating that the field is skipped if it's empty,
// and if parsing fails, it returns an error.
Parse(tagValue string, tag *T) (bool, error)
// Encode takes encoded data and performs secondary encoding.
// It's a mandatory function.
Encode(fieldName string, tag *T, in []byte, out Writer) error
// Decode takes the raw encoded data and performs a primary decode.
// It's a mandatory function.
Decode(fieldName string, tag *T, in []byte, out Writer) error
// IsMarshaller attempts to cast the value to a Marshaller interface,
// if so, returns a marshal function.
IsMarshaller(v reflect.Value) (func() ([]byte, error), bool)
// IsUnmarshaler attempts to cast the value to an Unmarshaler interface,
// if so, returns an unmarshal function.
IsUnmarshaler(v reflect.Value) (func([]byte) error, bool)
f()
}
type Config struct {
// Name of the tag.
Name string
// StructOpener a byte array that denotes the beginning of a structure.
// Will be automatically added when encoding.
StructOpener []byte
// StructCloser a byte array that denotes the end of a structure.
// Will be automatically added when encoding.
StructCloser []byte
// UnwrapWhenDecoding this flag tells the library whether to remove the StructOpener and StructCloser bytes of a structure.
UnwrapWhenDecoding bool
// ValueSeparator a byte array separating values.
// Will be automatically added when encoding.
ValueSeparator []byte
// RemoveSeparatorWhenDecoding this flag tells the library whether to remove the ValueSeparator.
RemoveSeparatorWhenDecoding bool
// Marshaller is used to check if a type implements a type of the Marshaller interface.
Marshaller reflect.Type
// Unmarshaler is used to check if a type implements a type of the Unmarshaler interface.
Unmarshaler reflect.Type
}
// New returns a new entity that implements the Engine interface.
func New[T any](tag Tag[T], cfg Config) Engine {
return &engine[T]{
Tag: tag,
name: cfg.Name,
wrap: len(cfg.StructOpener) != 0 || len(cfg.StructCloser) != 0,
removeWrapper: (len(cfg.StructOpener) != 0 || len(cfg.StructCloser) != 0) && cfg.UnwrapWhenDecoding,
separate: len(cfg.ValueSeparator) != 0,
removeSeparator: len(cfg.ValueSeparator) != 0 && cfg.RemoveSeparatorWhenDecoding,
structOpener: cfg.StructOpener,
structCloser: cfg.StructCloser,
valueSeparator: cfg.ValueSeparator,
marshaller: cfg.Marshaller,
unmarshaler: cfg.Unmarshaler,
}
}
type engine[T any] struct {
Tag[T]
name string
wrap, removeWrapper, separate, removeSeparator bool
structOpener, structCloser, valueSeparator []byte
marshaller, unmarshaler reflect.Type
}
type coders[T any] struct {
encoderFunc[T]
decoderFunc[T]
}
var coderCache sync.Map // map[reflect.Type]*coders[T]
// cachedCoders is like typeCoders but uses a cache to avoid repeated work.
func (e *engine[T]) cachedCoders(t reflect.Type) *coders[T] {
if c, ok := coderCache.Load(t); ok {
return c.(*coders[T])
}
c, _ := coderCache.LoadOrStore(t, e.typeCoders(t))
return c.(*coders[T])
}
// typeCoders returns coders for a type.
func (e *engine[T]) typeCoders(t reflect.Type) *coders[T] {
f := new(coders[T])
switch t.Kind() {
case reflect.Bool:
f.encoderFunc = boolEncoder[T]
f.decoderFunc = boolDecoder[T]
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
f.encoderFunc = intEncoder[T]
f.decoderFunc = intDecoder[T]
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
f.encoderFunc = uintEncoder[T]
f.decoderFunc = uintDecoder[T]
case reflect.Float32, reflect.Float64:
f.encoderFunc = floatEncoder[T]
f.decoderFunc = floatDecoder[T]
//case reflect.Array:
// f.encoderFunc = arrayEncoder[T]
// f.decoderFunc = arrayDecoder[T]
case reflect.Interface:
f.encoderFunc = interfaceEncoder[T]
f.decoderFunc = interfaceDecoder[T]
//case reflect.Map:
// f.encoderFunc = mapEncoder[T]
// f.decoderFunc = mapDecoder[T])
case reflect.Pointer:
f.encoderFunc = pointerEncoder[T]
f.decoderFunc = pointerDecoder[T]
case reflect.Slice:
if t.Elem().Kind() == reflect.Uint8 {
f.encoderFunc = bytesEncoder[T]
f.decoderFunc = bytesDecoder[T]
} else {
f.encoderFunc = unsupportedTypeEncoder[T]
f.decoderFunc = unsupportedTypeDecoder[T]
}
case reflect.String:
f.encoderFunc = stringEncoder[T]
f.decoderFunc = stringDecoder[T]
case reflect.Struct:
f.encoderFunc = structEncoder[T]
f.decoderFunc = structDecoder[T]
default:
f.encoderFunc = unsupportedTypeEncoder[T]
f.decoderFunc = unsupportedTypeDecoder[T]
}
if t.Kind() != reflect.Pointer {
p := reflect.PointerTo(t)
if p.Implements(e.marshaller) {
f.encoderFunc = marshallerEncoder[T]
}
if p.Implements(e.unmarshaler) {
f.decoderFunc = unmarshalerDecoder[T]
}
}
return f
}
// field represents a single field found in a struct.
type field[T any] struct {
index int
name string
typ reflect.Type
tag *T
omitempty bool
functions *coders[T]
embedded structFields[T]
}
type structFields[T any] []*field[T]
var fieldCache sync.Map // map[reflect.Type]structFields[T]
// cachedFields is like typeFields but uses a cache to avoid repeated work.
func (e *engine[T]) cachedFields(t reflect.Type) structFields[T] {
if c, ok := fieldCache.Load(t); ok {
return c.(structFields[T])
}
c, _ := fieldCache.LoadOrStore(t, e.typeFields(t))
return c.(structFields[T])
}
// typeFields returns a list of fields that the encoder/decoder should recognize for the given type.
func (e *engine[T]) typeFields(t reflect.Type) structFields[T] {
var err error
fs := make(structFields[T], 0, t.NumField())
// Scan type for fields to encode/decode.
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
ft := sf.Type
f := &field[T]{
index: i,
name: sf.Name,
typ: ft,
}
if sf.Anonymous {
if ft.Kind() == reflect.Pointer {
ft = ft.Elem()
}
// Ignore embedded fields of unexported non-struct types.
if !sf.IsExported() && ft.Kind() != reflect.Struct {
continue
}
// Do not ignore embedded fields of unexported struct types since they may have exported fields.
f.embedded = e.cachedFields(ft)
if f.embedded == nil {
continue
}
fs = append(fs, f)
continue
} else if !sf.IsExported() {
// Ignore unexported non-embedded fields.
continue
}
if tag, ok := sf.Tag.Lookup(e.name); ok {
// Ignore the field if the tag has a skip value.
if tag == "-" {
continue
}
f.tag = new(T)
if f.omitempty, err = e.Parse(tag, f.tag); err != nil {
f.functions = &coders[T]{
encoderFunc: invalidTagEncoder[T](tag, err),
decoderFunc: invalidTagDecoder[T](tag, err),
}
return append(fs, f)
}
}
f.functions = e.cachedCoders(ft)
fs = append(fs, f)
}
return fs
}