-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
220 lines (178 loc) · 5.64 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package protolua
import (
"errors"
"fmt"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
// Marshal converts the given proto.Message into a lua table using default options.
// Supports only proto3 messages currently.
func Marshal(m proto.Message) ([]byte, error) {
return LuaMarshalOption{}.Marshal(m)
}
type (
EncodingRun struct {
*Encoder
options LuaMarshalOption
}
MarshalFunc func(EncodingRun, protoreflect.Message) error
LuaMarshalOption struct {
Format struct {
// If set to false the output is formated as one line (default)
// If set to true the output is formated in multiple line with indent (better for humans)
Multiline bool
}
// Defines how the name of message are crated
// If set to nil, [protolua.JsonName] will be used
KeyName interface {
keyName(protoreflect.FieldDescriptor) string
}
// Additional Marshalers for non standard proto messages
// see [protolua.GoogleWellKnownTypesMarshaler] for an example
AdditionalMarshalers []interface {
Handle(fullName protoreflect.FullName) (MarshalFunc, error)
}
}
)
// Marshal convert the given proto.Message into a lua table using the given options.
func (o LuaMarshalOption) Marshal(m proto.Message) ([]byte, error) {
return o.marshal(m)
}
func (option LuaMarshalOption) marshal(m proto.Message) ([]byte, error) {
if m == nil {
return nil, errors.New("message can not be nil")
}
setDefaults(&option)
indent := ""
if option.Format.Multiline {
indent = DefaultIndent
}
encoder, err := NewEncoder(indent)
if err != nil {
return nil, err
}
encodingRun := EncodingRun{encoder, option}
bytes, err2 := marshalRootMessage(m.ProtoReflect(), encodingRun)
if err2 != nil {
return bytes, err2
}
return encodingRun.Encoder.Bytes(), nil
}
func setDefaults(option *LuaMarshalOption) {
if option.KeyName == nil {
option.KeyName = JsonName{}
}
}
func marshalRootMessage(m protoreflect.Message, encodingRun EncodingRun) ([]byte, error) {
// The json name is not populated, so the Protobuf name is used hereX
encodingRun.Encoder.WriteKey(string(m.Descriptor().Name()))
if err := encodingRun.marshalMessage(m); err != nil {
return nil, err
}
return nil, nil
}
// marshalMessage marshals the message and fields in the given protoreflect.Message.
func (e EncodingRun) marshalMessage(m protoreflect.Message) error {
if m.Descriptor().Syntax() != protoreflect.Proto3 {
return errors.New("only proto3 syntax is supported")
}
for _, marshaler := range e.options.AdditionalMarshalers {
marshalFunc, unsupportedTypeError := marshaler.Handle(m.Descriptor().FullName())
shouldReturn, returnValue := isHandledByOtherMarshaler(unsupportedTypeError, marshalFunc, e, m)
if shouldReturn {
return returnValue
}
}
marshalFunc, unsupportedTypeError := GoogleWellKnownTypesMarshaler{}.Handle(m.Descriptor().FullName())
shouldReturn, returnValue := isHandledByOtherMarshaler(unsupportedTypeError, marshalFunc, e, m)
if shouldReturn {
return returnValue
}
if !m.IsValid() {
e.Encoder.WriteNull()
return nil
}
var err error
e.Encoder.StartObject()
defer e.Encoder.EndObject()
fields := m.Descriptor().Fields()
upper := fields.Len()
for i := 0; i < upper; i++ {
currentField := fields.Get(i)
name := e.options.KeyName.keyName(currentField)
if err = e.Encoder.WriteKey(name); err != nil {
return err
}
if err = e.marshalValue(m.Get(currentField), currentField); err != nil {
return err
}
}
return err
}
func isHandledByOtherMarshaler(unsupportedTypeError error, marshalFunc MarshalFunc, e EncodingRun, m protoreflect.Message) (bool, error) {
if unsupportedTypeError != nil {
return true, unsupportedTypeError
}
if marshalFunc != nil {
return true, marshalFunc(e, m)
}
return false, nil
}
func (e EncodingRun) marshalValue(val protoreflect.Value, fd protoreflect.FieldDescriptor) error {
switch {
case fd.IsList():
return e.marshalList(val.List(), fd)
case fd.IsMap():
return e.marshalMap(val.Map(), fd)
default:
return e.marshalSingular(val, fd)
}
}
func (e EncodingRun) marshalSingular(val protoreflect.Value, fd protoreflect.FieldDescriptor) error {
if !val.IsValid() {
return nil
}
switch kind := fd.Kind(); kind {
case protoreflect.BoolKind:
e.WriteBool(val.Bool())
case protoreflect.StringKind:
if err := e.Encoder.WriteString(val.String()); err != nil {
return err
}
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind,
protoreflect.Uint32Kind, protoreflect.Fixed32Kind,
protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Uint64Kind,
protoreflect.Sfixed64Kind, protoreflect.Fixed64Kind:
e.Encoder.WriteNumber(val.String())
case protoreflect.FloatKind:
return errors.New("float is not supported yet")
case protoreflect.DoubleKind:
return errors.New("float is not supported yet")
case protoreflect.BytesKind:
return errors.New("byte is not supported yet")
case protoreflect.EnumKind:
return errors.New("enum is not supported yet")
case protoreflect.MessageKind, protoreflect.GroupKind:
if err := e.marshalMessage(val.Message()); err != nil {
return err
}
default:
panic(fmt.Sprintf("%v has unknown kind: %v", fd.FullName(), kind))
}
return nil
}
func (e EncodingRun) marshalList(list protoreflect.List, fd protoreflect.FieldDescriptor) error {
e.Encoder.StartArray()
defer e.Encoder.EndArray()
for i := 0; i < list.Len(); i++ {
item := list.Get(i)
e.Encoder.WriteIndexedList(i + 1)
if err := e.marshalSingular(item, fd); err != nil {
return err
}
}
return nil
}
func (e EncodingRun) marshalMap(mmap protoreflect.Map, fd protoreflect.FieldDescriptor) error {
return errors.New("maps are not supported yet")
}