2023-06-30 10:04:17 +00:00
|
|
|
package pblite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2023-07-15 13:11:36 +00:00
|
|
|
"encoding/json"
|
2023-06-30 10:04:17 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-07-15 13:11:36 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-06-30 10:04:17 +00:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
)
|
|
|
|
|
2023-07-15 13:11:36 +00:00
|
|
|
func Unmarshal(data []byte, m proto.Message) error {
|
|
|
|
var anyData any
|
|
|
|
if err := json.Unmarshal(data, &anyData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
anyDataArr, ok := anyData.([]any)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("expected array in JSON, got %T", anyData)
|
|
|
|
}
|
|
|
|
return deserializeFromSlice(anyDataArr, m.ProtoReflect())
|
|
|
|
}
|
|
|
|
|
2023-09-04 11:25:00 +00:00
|
|
|
func deserializeOne(val any, index int, ref protoreflect.Message, insideList protoreflect.List, fieldDescriptor protoreflect.FieldDescriptor) (protoreflect.Value, error) {
|
2023-07-15 13:11:36 +00:00
|
|
|
var num float64
|
|
|
|
var expectedKind, str string
|
|
|
|
var boolean, ok bool
|
|
|
|
var outputVal protoreflect.Value
|
2023-09-04 11:25:00 +00:00
|
|
|
if fieldDescriptor.IsList() && insideList == nil {
|
|
|
|
nestedData, ok := val.([]any)
|
|
|
|
if !ok {
|
|
|
|
return outputVal, fmt.Errorf("expected untyped array at index %d for repeated field %s, got %T", index, fieldDescriptor.FullName(), val)
|
|
|
|
}
|
|
|
|
list := ref.NewField(fieldDescriptor).List()
|
|
|
|
list.NewElement()
|
|
|
|
for i, nestedVal := range nestedData {
|
|
|
|
nestedParsed, err := deserializeOne(nestedVal, i, ref, list, fieldDescriptor)
|
|
|
|
if err != nil {
|
|
|
|
return outputVal, err
|
|
|
|
}
|
|
|
|
list.Append(nestedParsed)
|
|
|
|
}
|
|
|
|
return protoreflect.ValueOfList(list), nil
|
|
|
|
}
|
2023-07-15 13:11:36 +00:00
|
|
|
switch fieldDescriptor.Kind() {
|
|
|
|
case protoreflect.MessageKind:
|
|
|
|
ok = true
|
|
|
|
nestedData, ok := val.([]any)
|
|
|
|
if !ok {
|
|
|
|
return outputVal, fmt.Errorf("expected untyped array at index %d for field %s, got %T", index, fieldDescriptor.FullName(), val)
|
|
|
|
}
|
2023-09-04 11:25:00 +00:00
|
|
|
var nestedMessage protoreflect.Message
|
|
|
|
if insideList != nil {
|
|
|
|
nestedMessage = insideList.NewElement().Message()
|
|
|
|
} else {
|
|
|
|
nestedMessage = ref.NewField(fieldDescriptor).Message()
|
|
|
|
}
|
2023-07-15 13:11:36 +00:00
|
|
|
if err := deserializeFromSlice(nestedData, nestedMessage); err != nil {
|
|
|
|
return outputVal, err
|
|
|
|
}
|
|
|
|
outputVal = protoreflect.ValueOfMessage(nestedMessage)
|
|
|
|
case protoreflect.BytesKind:
|
|
|
|
ok = true
|
|
|
|
bytesBase64, ok := val.(string)
|
|
|
|
if !ok {
|
|
|
|
return outputVal, fmt.Errorf("expected string at index %d for field %s, got %T", index, fieldDescriptor.FullName(), val)
|
|
|
|
}
|
|
|
|
bytes, err := base64.StdEncoding.DecodeString(bytesBase64)
|
|
|
|
if err != nil {
|
|
|
|
return outputVal, fmt.Errorf("failed to decode base64 at index %d for field %s: %w", index, fieldDescriptor.FullName(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
outputVal = protoreflect.ValueOfBytes(bytes)
|
|
|
|
case protoreflect.EnumKind:
|
|
|
|
num, ok = val.(float64)
|
|
|
|
expectedKind = "float64"
|
|
|
|
outputVal = protoreflect.ValueOfEnum(protoreflect.EnumNumber(int32(num)))
|
|
|
|
case protoreflect.Int32Kind:
|
|
|
|
num, ok = val.(float64)
|
|
|
|
expectedKind = "float64"
|
|
|
|
outputVal = protoreflect.ValueOfInt32(int32(num))
|
|
|
|
case protoreflect.Int64Kind:
|
|
|
|
num, ok = val.(float64)
|
|
|
|
expectedKind = "float64"
|
|
|
|
outputVal = protoreflect.ValueOfInt64(int64(num))
|
|
|
|
case protoreflect.Uint32Kind:
|
|
|
|
num, ok = val.(float64)
|
|
|
|
expectedKind = "float64"
|
|
|
|
outputVal = protoreflect.ValueOfUint32(uint32(num))
|
|
|
|
case protoreflect.Uint64Kind:
|
|
|
|
num, ok = val.(float64)
|
|
|
|
expectedKind = "float64"
|
|
|
|
outputVal = protoreflect.ValueOfUint64(uint64(num))
|
|
|
|
case protoreflect.FloatKind:
|
|
|
|
num, ok = val.(float64)
|
|
|
|
expectedKind = "float64"
|
|
|
|
outputVal = protoreflect.ValueOfFloat32(float32(num))
|
|
|
|
case protoreflect.DoubleKind:
|
|
|
|
num, ok = val.(float64)
|
|
|
|
expectedKind = "float64"
|
|
|
|
outputVal = protoreflect.ValueOfFloat64(num)
|
|
|
|
case protoreflect.StringKind:
|
|
|
|
str, ok = val.(string)
|
|
|
|
expectedKind = "string"
|
|
|
|
outputVal = protoreflect.ValueOfString(str)
|
|
|
|
case protoreflect.BoolKind:
|
|
|
|
boolean, ok = val.(bool)
|
|
|
|
expectedKind = "bool"
|
|
|
|
outputVal = protoreflect.ValueOfBool(boolean)
|
|
|
|
default:
|
|
|
|
return outputVal, fmt.Errorf("unsupported field type %s in %s", fieldDescriptor.Kind(), fieldDescriptor.FullName())
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return outputVal, fmt.Errorf("expected %s at index %d for field %s, got %T", expectedKind, index, fieldDescriptor.FullName(), val)
|
|
|
|
}
|
|
|
|
return outputVal, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deserializeFromSlice(data []any, ref protoreflect.Message) error {
|
|
|
|
for i := 0; i < ref.Descriptor().Fields().Len(); i++ {
|
|
|
|
fieldDescriptor := ref.Descriptor().Fields().Get(i)
|
2023-06-30 10:04:17 +00:00
|
|
|
index := int(fieldDescriptor.Number()) - 1
|
|
|
|
if index < 0 || index >= len(data) || data[index] == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
val := data[index]
|
2023-09-04 11:25:00 +00:00
|
|
|
outputVal, err := deserializeOne(val, index, ref, nil, fieldDescriptor)
|
2023-07-15 13:11:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-06-30 10:04:17 +00:00
|
|
|
}
|
2023-07-15 13:11:36 +00:00
|
|
|
ref.Set(fieldDescriptor, outputVal)
|
2023-06-30 10:04:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|