2023-06-30 10:04:17 +00:00
|
|
|
package pblite
|
2023-06-30 09:54:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
in protobuf, a message looks like this:
|
|
|
|
|
|
|
|
message SomeMessage {
|
|
|
|
string stringField1 = 1;
|
|
|
|
int64 intField = 6;
|
|
|
|
bytes byteField = 9;
|
|
|
|
}
|
|
|
|
|
|
|
|
but when this function is done serializing this protobuf message into a slice, it should look something like this:
|
|
|
|
|
|
|
|
[
|
|
|
|
"someString",
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
6,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
"\x9\x91\x942"
|
|
|
|
]
|
|
|
|
|
|
|
|
Any integer should be translated into int64, it doesn't matter if it's defined as int32 in the proto schema.
|
|
|
|
In the finished serialized slice it should be int64.
|
|
|
|
Let's also take in count where there is a message nested inside a message:
|
|
|
|
message SomeMessage {
|
|
|
|
string stringField1 = 1;
|
|
|
|
NestedMessage1 nestedMessage1 = 2;
|
|
|
|
int64 intField = 6;
|
|
|
|
bytes byteField = 9;
|
|
|
|
}
|
|
|
|
|
|
|
|
message NestedMessage1 {
|
|
|
|
string msg1 = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Then the serialized output would be:
|
|
|
|
[
|
|
|
|
"someString",
|
|
|
|
["msg1FieldValue"],
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
6,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
"\x9\x91\x942"
|
|
|
|
]
|
|
|
|
This means that any slice inside of the current slice, indicates another message nested inside of it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import (
|
2023-06-30 10:04:17 +00:00
|
|
|
"encoding/base64"
|
2023-07-15 12:49:51 +00:00
|
|
|
"encoding/json"
|
2023-06-30 10:04:17 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-07-15 12:49:51 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-06-30 09:54:08 +00:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
)
|
|
|
|
|
2023-07-15 12:49:51 +00:00
|
|
|
func serializeOneOrList(fieldDescriptor protoreflect.FieldDescriptor, fieldValue protoreflect.Value) (any, error) {
|
|
|
|
switch {
|
|
|
|
case fieldDescriptor.IsList():
|
|
|
|
var serializedList []any
|
|
|
|
list := fieldValue.List()
|
|
|
|
for i := 0; i < list.Len(); i++ {
|
|
|
|
serialized, err := serializeOne(fieldDescriptor, list.Get(i))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
serializedList = append(serializedList, serialized)
|
|
|
|
}
|
|
|
|
return serializedList, nil
|
|
|
|
default:
|
|
|
|
return serializeOne(fieldDescriptor, fieldValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serializeOne(fieldDescriptor protoreflect.FieldDescriptor, fieldValue protoreflect.Value) (any, error) {
|
|
|
|
switch fieldDescriptor.Kind() {
|
|
|
|
case protoreflect.MessageKind:
|
2024-02-22 20:37:49 +00:00
|
|
|
if isPbliteBinary(fieldDescriptor) {
|
|
|
|
serializedMsg, err := proto.Marshal(fieldValue.Message().Interface())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return base64.StdEncoding.EncodeToString(serializedMsg), nil
|
|
|
|
} else {
|
|
|
|
serializedMsg, err := SerializeToSlice(fieldValue.Message().Interface())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return serializedMsg, nil
|
2023-07-15 12:49:51 +00:00
|
|
|
}
|
|
|
|
case protoreflect.BytesKind:
|
|
|
|
return base64.StdEncoding.EncodeToString(fieldValue.Bytes()), nil
|
|
|
|
case protoreflect.Int32Kind, protoreflect.Int64Kind:
|
|
|
|
return fieldValue.Int(), nil
|
|
|
|
case protoreflect.Uint32Kind, protoreflect.Uint64Kind:
|
|
|
|
return fieldValue.Uint(), nil
|
|
|
|
case protoreflect.FloatKind, protoreflect.DoubleKind:
|
|
|
|
return fieldValue.Float(), nil
|
|
|
|
case protoreflect.EnumKind:
|
|
|
|
return int(fieldValue.Enum()), nil
|
|
|
|
case protoreflect.BoolKind:
|
|
|
|
return fieldValue.Bool(), nil
|
|
|
|
case protoreflect.StringKind:
|
2024-02-22 20:37:49 +00:00
|
|
|
if isPbliteBinary(fieldDescriptor) {
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(fieldValue.String())), nil
|
|
|
|
} else {
|
|
|
|
return fieldValue.String(), nil
|
|
|
|
}
|
2023-07-15 12:49:51 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported field type %s in %s", fieldDescriptor.Kind(), fieldDescriptor.FullName())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:56:55 +00:00
|
|
|
func SerializeToSlice(msg proto.Message) ([]any, error) {
|
|
|
|
ref := msg.ProtoReflect()
|
2023-06-30 09:55:49 +00:00
|
|
|
maxFieldNumber := 0
|
2023-07-15 12:56:55 +00:00
|
|
|
for i := 0; i < ref.Descriptor().Fields().Len(); i++ {
|
|
|
|
fieldNumber := int(ref.Descriptor().Fields().Get(i).Number())
|
2023-06-30 09:55:49 +00:00
|
|
|
if fieldNumber > maxFieldNumber {
|
|
|
|
maxFieldNumber = fieldNumber
|
|
|
|
}
|
|
|
|
}
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-06-30 10:04:17 +00:00
|
|
|
serialized := make([]any, maxFieldNumber)
|
2023-07-15 12:56:55 +00:00
|
|
|
for i := 0; i < ref.Descriptor().Fields().Len(); i++ {
|
|
|
|
fieldDescriptor := ref.Descriptor().Fields().Get(i)
|
|
|
|
fieldValue := ref.Get(fieldDescriptor)
|
2023-06-30 09:55:49 +00:00
|
|
|
fieldNumber := int(fieldDescriptor.Number())
|
2023-07-15 12:56:55 +00:00
|
|
|
if !ref.Has(fieldDescriptor) {
|
2023-06-30 10:04:17 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-07-15 12:49:51 +00:00
|
|
|
serializedVal, err := serializeOneOrList(fieldDescriptor, fieldValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-06-30 09:55:49 +00:00
|
|
|
}
|
2023-07-15 12:49:51 +00:00
|
|
|
serialized[fieldNumber-1] = serializedVal
|
2023-06-30 09:55:49 +00:00
|
|
|
}
|
2023-06-30 09:54:08 +00:00
|
|
|
|
2023-06-30 09:55:49 +00:00
|
|
|
return serialized, nil
|
|
|
|
}
|
2023-07-15 12:49:51 +00:00
|
|
|
|
2023-07-15 12:56:55 +00:00
|
|
|
func Marshal(m proto.Message) ([]byte, error) {
|
|
|
|
serialized, err := SerializeToSlice(m)
|
2023-07-15 12:49:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.Marshal(serialized)
|
|
|
|
}
|