ProtoValueToConstant uses reflection to convert a proto field value to a Mangle value.
(fd protoreflect.FieldDescriptor, val protoreflect.Value)
| 90 | |
| 91 | // ProtoValueToConstant uses reflection to convert a proto field value to a Mangle value. |
| 92 | func ProtoValueToConstant(fd protoreflect.FieldDescriptor, val protoreflect.Value) (ast.Constant, error) { |
| 93 | switch fd.Kind() { |
| 94 | case protoreflect.BoolKind: |
| 95 | if val.Bool() { |
| 96 | return ast.TrueConstant, nil |
| 97 | } |
| 98 | return ast.FalseConstant, nil |
| 99 | case protoreflect.Sint32Kind: |
| 100 | fallthrough |
| 101 | case protoreflect.Sint64Kind: |
| 102 | fallthrough |
| 103 | case protoreflect.Int32Kind: |
| 104 | fallthrough |
| 105 | case protoreflect.Int64Kind: |
| 106 | return ast.Number(val.Int()), nil |
| 107 | case protoreflect.FloatKind: |
| 108 | fallthrough |
| 109 | case protoreflect.DoubleKind: |
| 110 | return ast.Float64(val.Float()), nil |
| 111 | case protoreflect.BytesKind: |
| 112 | return ast.String(string(val.Bytes())), nil |
| 113 | case protoreflect.StringKind: |
| 114 | return ast.String(val.String()), nil |
| 115 | case protoreflect.MessageKind: |
| 116 | return ProtoToStruct(val.Message()) |
| 117 | case protoreflect.EnumKind: |
| 118 | return ProtoEnumToConstant(fd.Enum(), val) |
| 119 | } |
| 120 | return ast.Constant{}, fmt.Errorf("proto field %v of unsupported kind: %v", fd, fd.Kind()) |
| 121 | } |
| 122 | |
| 123 | // ProtoEnumToConstant uses reflection to convert a proto field value to a Mangle value. |
| 124 | func ProtoEnumToConstant(ed protoreflect.EnumDescriptor, val protoreflect.Value) (ast.Constant, error) { |
no test coverage detected