-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshalizer.go
404 lines (336 loc) · 12 KB
/
marshalizer.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// -------------------------------------------------------------------------------------------------------
package pprint
import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"reflect"
"runtime"
"sort"
"strings"
"sync"
"testing"
)
type Serializer func(val reflect.Value, mr Marshalizer) any
type MarshalizerContext map[uintptr]int
func (ctx MarshalizerContext) Contains(objectId uintptr) bool {
_, exists := ctx[objectId]
return exists
}
func (ctx MarshalizerContext) Set(objectId uintptr) {
ctx[objectId] = 1
}
func (ctx MarshalizerContext) Del(objectId uintptr) {
delete(ctx, objectId)
}
type MarshalizerInterface interface {
Serialize(object any) ([]byte, error)
}
type Marshalizer struct {
context MarshalizerContext
escapeHTML bool
includePrivateFields bool
includeImplements bool
registry SerializersRegistry
}
func NewMarshalizer(includePrivateFields bool, escapeHTML bool, emptyRegistry bool, includeImplements bool) MarshalizerInterface {
registry := SerializersRegistry{
kindSerializers: make(KindSerializerMap),
typeSerializers: make(TypeSerializerMap),
knownInterfaces: make(KnownInterface),
}
if !emptyRegistry {
registry.AddKind(reflect.Slice, SerializeSlice)
registry.AddKind(reflect.Map, SerializeMap)
registry.AddKind(reflect.Struct, SerializeStruct)
registry.AddKind(reflect.Func, SerializeFuncSignature)
registry.AddKind(reflect.Pointer, SerializePointer)
registry.AddKnownInterface(reflect.TypeOf((*fmt.Stringer)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*fmt.Scanner)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*fmt.Formatter)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*error)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*io.Reader)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*io.Writer)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*io.Closer)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*io.ReadWriter)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*io.ReadSeeker)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*io.Seeker)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*io.WriteSeeker)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*io.ReadWriteSeeker)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*io.ReadWriteCloser)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*io.WriterAt)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*io.ReaderAt)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*sync.Locker)(nil)).Elem())
// registry.AddKnownInterface(reflect.TypeOf((*sync.Mutex)(nil)).Elem()) // non interface
// registry.AddKnownInterface(reflect.TypeOf((*sync.RWMutex)(nil)).Elem()) // non interface
// registry.AddKnownInterface(reflect.TypeOf((*sync.Atomic)(nil)).Elem()) // unimplemented ?
// registry.AddKnownInterface(reflect.TypeOf((*sync.WaitGroup)(nil)).Elem()) // non interface
registry.AddKnownInterface(reflect.TypeOf((*http.RoundTripper)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*http.Handler)(nil)).Elem())
// registry.AddKnownInterface(reflect.TypeOf((*http.ServeHTTP)(nil)).Elem()) // unimplemented ?
registry.AddKnownInterface(reflect.TypeOf((*context.Context)(nil)).Elem())
// registry.AddKnownInterface(reflect.TypeOf((*context.CancelFunc)(nil)).Elem()) // non interface
registry.AddKnownInterface(reflect.TypeOf((*sort.Interface)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*testing.TB)(nil)).Elem())
// registry.AddKnownInterface(reflect.TypeOf((*sql.Scanner)(nil)).Elem()) // unimplemented ?
// registry.AddKnownInterface(reflect.TypeOf((*sql.Valuer)(nil)).Elem()) // unimplemented ?
// registry.AddKnownInterface(reflect.TypeOf((*strconv.NumError)(nil)).Elem()) // non interface
// registry.AddKnownInterface(reflect.TypeOf((*os.File)(nil)).Elem()) // non interface
registry.AddKnownInterface(reflect.TypeOf((*net.Conn)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*MarshalizerInterface)(nil)).Elem())
registry.AddKnownInterface(reflect.TypeOf((*SerializerRegistryInterface)(nil)).Elem())
}
mr := &Marshalizer{
context: make(MarshalizerContext),
escapeHTML: escapeHTML,
includePrivateFields: includePrivateFields,
includeImplements: includeImplements,
registry: registry,
}
return mr
}
func (mr Marshalizer) String() string {
result, err := mr.Serialize(mr)
if err != nil {
panic("can't serialize Marshalizer itself")
}
return string(result)
}
func (mr Marshalizer) Serialize(object any) ([]byte, error) {
// Marshal data with custom serialization
serializedData := serialize(object, mr)
// Marshal data with indentation and optional HTML escaping
var result []byte
var err error
if mr.escapeHTML {
// If escapeHTML is true, use standard MarshalIndent
result, err = json.MarshalIndent(serializedData, "", " ")
} else {
// If escapeHTML is false, use encoder with SetEscapeHTML(false)
encoder := json.NewEncoder(nil)
encoder.SetEscapeHTML(false)
result, err = json.MarshalIndent(serializedData, "", " ")
}
if err != nil {
return nil, err
}
return result, nil
}
func (mr Marshalizer) AddKind(kind reflect.Kind, serializer Serializer) {
mr.registry.AddKind(kind, serializer)
}
func (mr Marshalizer) RemoveKind(kind reflect.Kind) {
mr.registry.RemoveKind(kind)
}
func (mr Marshalizer) AddType(typ reflect.Type, serializer Serializer) {
mr.registry.AddType(typ, serializer)
}
func (mr Marshalizer) RemoveType(typ reflect.Type) {
mr.registry.RemoveType(typ)
}
func (mr Marshalizer) AddKnownInterface(typ reflect.Type) {
mr.registry.AddKnownInterface(typ)
}
func (mr Marshalizer) RemoveKnownInterface(typ reflect.Type) {
mr.registry.RemoveKnownInterface(typ)
}
// serialize replaces unsupported types like functions with string descriptors.
func serialize(object any, mr Marshalizer) any {
if object == nil {
return nil
}
objectId := id(object)
if mr.context.Contains(objectId) {
// return fmt.Sprintf("(%T=%p)[Recursion Exceeded]", object, object)
return fmt.Sprintf("(%T=%p)[Recursion Exceeded]", object, object)
}
mr.context.Set(objectId)
val := reflect.ValueOf(object)
if serializer, exists := mr.registry.typeSerializers[val.Type()]; exists {
r := serializer(val, mr)
mr.context.Del(objectId)
return r
}
if serializer, exists := mr.registry.kindSerializers[val.Kind()]; exists {
r := serializer(val, mr)
mr.context.Del(objectId)
return r
}
mr.context.Del(objectId)
return object
}
func SerializePointer(val reflect.Value, mr Marshalizer) any {
// Handle pointer types
if val.IsNil() {
return nil
}
// Create a structure for pointers
ptrData := map[string]any{
"address": fmt.Sprintf("%p", val.Interface()), // Pointer address
"type": fmt.Sprintf("%T", val.Interface()), // Pointer type
}
if mr.includeImplements {
interfaces := GetImplementedInterfacesDescriptor(val, mr)
ptrData["implements"] = interfaces
}
// Serialize the dereferenced value first
value := serialize(val.Elem().Interface(), mr)
// If the dereferenced value is a map (structure-like), add pointer data
if reflect.ValueOf(value).Kind() == reflect.Map {
// Add pointer metadata to the map
value.(map[string]any)["*"] = ptrData
return value
} else {
output := map[string]any{
"*": ptrData,
"_value": value,
}
return output
}
// If it's not a map, just return the value as is
// return value
}
// DiscoverInterfaces dynamically finds all interfaces implemented by a given struct.
func DiscoverInterfaces(structType reflect.Type, interfaces KnownInterface) []reflect.Type {
implemented := []reflect.Type{}
// Iterate over the known interfaces map
for ifaceType := range interfaces {
if structType.Implements(ifaceType) {
implemented = append(implemented, ifaceType)
}
}
return implemented
}
func SerializeStruct(val reflect.Value, mr Marshalizer) any {
// Handle structs
m := make(map[string]any)
typ := val.Type()
// typ := reflect.TypeOf(object)
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
fieldType := typ.Field(i)
if !field.CanInterface() {
// optional
if mr.includePrivateFields {
m[fieldType.Name] = "[Private Field]"
}
continue // Skip unexported fields
}
m[fieldType.Name] = serialize(field.Interface(), mr)
}
return m
}
func SerializeSlice(val reflect.Value, mr Marshalizer) any {
// Handle slices
result := make([]any, val.Len())
for i := 0; i < val.Len(); i++ {
result[i] = serialize(val.Index(i).Interface(), mr)
}
return result
}
func SerializeMap(val reflect.Value, mr Marshalizer) any {
// Handle maps
result := make(map[string]any)
for _, key := range val.MapKeys() {
result[fmt.Sprintf("%v", key.Interface())] = serialize(val.MapIndex(key).Interface(), mr)
}
return result
}
func SerializeFuncSignature(val reflect.Value, mr Marshalizer) any {
// Automatically generate a function descriptor
funcType := val.Type()
// Get parameter types
params := []string{}
for i := 0; i < funcType.NumIn(); i++ {
if typeString := funcType.In(i).String(); len(typeString) > 0 {
params = append(params, removeSpaces(typeString))
}
}
// Get return types
results := []string{}
for i := 0; i < funcType.NumOut(); i++ {
if typeString := funcType.Out(i).String(); len(typeString) > 0 {
results = append(results, removeSpaces(typeString))
}
}
// resolve func name
funcName := runtime.FuncForPC(val.Pointer()).Name()
return joinFuncSignature(funcName, params, results)
}
func SerializeMethodSignature(method reflect.Method, mr Marshalizer) string {
// Access the method's type
methodType := method.Type
methodName := method.Name
// Get parameter types (excluding the receiver)
params := []string{}
for i := 1; i < methodType.NumIn(); i++ { // Skip the first parameter, which is the receiver
paramType := methodType.In(i).String()
params = append(params, removeSpaces(paramType))
}
// Get return types
results := []string{}
for i := 0; i < methodType.NumOut(); i++ {
resultType := methodType.Out(i).String()
results = append(results, removeSpaces(resultType))
}
return joinFuncSignature(methodName, params, results)
}
func GetImplementedInterfacesDescriptor(val reflect.Value, mr Marshalizer) map[string][]string {
implementedInterfaces := DiscoverInterfaces(val.Type(), mr.registry.knownInterfaces)
serializedInterfaces := map[string][]string{}
if len(implementedInterfaces) > 0 {
for _, intf := range implementedInterfaces {
// if serializedInterface := GetInterfaceDescriptor(intf, mr); serializedInterface != nil {
// serializedInterfaces[intf.String()] = serializedInterface
// }
serializedInterfaces[intf.String()] = GetInterfaceDescriptor(intf, mr)
}
}
if len(serializedInterfaces) > 0 {
return serializedInterfaces
}
return nil
}
func GetInterfaceDescriptor(typ reflect.Type, mr Marshalizer) []string {
if typ.Kind() == reflect.Interface {
// If the interface is not nil, inspect the methods implemented by it
methods := []string{}
// Loop through all methods of the interface
numMethods := typ.NumMethod()
for i := 0; i < numMethods; i++ {
method := typ.Method(i)
// Handle the method signature separately
methodSignature := SerializeMethodSignature(method, mr)
methods = append(methods, methodSignature)
}
return methods
}
return nil
}
func joinFuncSignature(name string, params, results []string) string {
signature := ""
paramList := strings.Join(params, ", ")
resultList := strings.Join(results, ", ")
if len(name) > 0 {
signature += cleanFuncName(name) + " "
}
signature += fmt.Sprintf("func(%s)", paramList)
if len(results) == 1 {
signature += fmt.Sprintf(" %s", resultList)
} else if len(results) > 1 {
signature += fmt.Sprintf(" (%s)", resultList)
}
return strings.TrimSpace(signature)
}
// Function to clean up spaces between type names
func removeSpaces(typeStr string) string {
return strings.ReplaceAll(typeStr, " ", "")
}
func cleanFuncName(fullName string) string {
// Split the full name by "." and return the last part
parts := strings.Split(fullName, "/")
return parts[len(parts)-1]
}