-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.go
141 lines (112 loc) · 3.05 KB
/
marshal.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
package jbtf
import (
"bytes"
"encoding/base64"
"encoding/json"
"reflect"
)
func isStruct(v any) bool {
value := reflect.ValueOf(v)
for value.Kind() == reflect.Pointer {
value = value.Elem()
}
return value.Kind() == reflect.Struct
}
func buildBufferView(buf *bytes.Buffer, currentStructure, jsonInterprettedData map[string]any, v any, views []BufferView, bufferIndex int) ([]BufferView, error) {
value := reflect.ValueOf(v)
for value.Kind() == reflect.Pointer {
value = value.Elem()
}
if value.Kind() != reflect.Struct {
return views, nil
}
var err error
valueType := value.Type()
for i := 0; i < valueType.NumField(); i++ {
viewFieldValue := value.Field(i)
structField := valueType.Field(i)
if viewFieldValue.CanInterface() {
i := viewFieldValue.Interface()
perm, isSerializable := i.(Serializable)
if !isSerializable {
jsonTagName := jsonNameTag(structField)
// Golang's own JSON package didn't serialize it. The only case
// I can think of this occuring is that the 'omitempty' is
// present and we had the 0 value present.
nestedInterprettedData, ok := jsonInterprettedData[jsonTagName]
if !ok {
continue
}
nestedInterprettedDataAsStruct, nestedJsonIsStruct := nestedInterprettedData.(map[string]any)
if isStruct(i) && nestedJsonIsStruct {
m := make(map[string]any)
currentStructure[jsonTagName] = m
views, err = buildBufferView(buf, m, nestedInterprettedDataAsStruct, i, views, bufferIndex)
if err != nil {
return views, err
}
} else {
currentStructure[jsonTagName] = i
}
continue
}
if IsNilish(perm) {
continue
}
start := buf.Len()
err := perm.Serialize(buf)
if err != nil {
return views, err
}
currentStructure["$"+structField.Name] = len(views)
views = append(views, BufferView{
Buffer: bufferIndex,
ByteOffset: start,
ByteLength: buf.Len() - start,
})
continue
}
}
return views, nil
}
func toJsonMap(v any) (map[string]any, error) {
jsonData, err := json.Marshal(v)
if err != nil {
return nil, err
}
jsonInterprettedData := make(map[string]any)
err = json.Unmarshal(jsonData, &jsonInterprettedData)
if err != nil {
return nil, err
}
return jsonInterprettedData, nil
}
func Marshal(v any) ([]byte, error) {
structure := &schema{
Data: v,
Buffers: make([]Buffer, 0),
BufferViews: make([]BufferView, 0),
}
buf := &bytes.Buffer{}
if isStruct(v) {
jsonInterprettedData, err := toJsonMap(v)
if err != nil {
return nil, err
}
builtDataView := make(map[string]any)
views, err := buildBufferView(buf, builtDataView, jsonInterprettedData, v, nil, 0)
if err != nil {
return nil, err
}
structure.Data = builtDataView
structure.BufferViews = views
}
if len(structure.BufferViews) > 0 {
bufData := buf.Bytes()
structure.Buffers = append(structure.Buffers, Buffer{
ByteLength: len(bufData),
URI: "data:application/octet-stream;base64," + base64.StdEncoding.EncodeToString(bufData),
})
}
return json.MarshalIndent(structure, "", "\t")
}