MCPcopy
hub / github.com/google/mangle / ProtoValueToConstant

Function ProtoValueToConstant

proto2struct/proto2struct.go:92–121  ·  view source on GitHub ↗

ProtoValueToConstant uses reflection to convert a proto field value to a Mangle value.

(fd protoreflect.FieldDescriptor, val protoreflect.Value)

Source from the content-addressed store, hash-verified

90
91// ProtoValueToConstant uses reflection to convert a proto field value to a Mangle value.
92func 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.
124func ProtoEnumToConstant(ed protoreflect.EnumDescriptor, val protoreflect.Value) (ast.Constant, error) {

Callers 1

ProtoToStructFunction · 0.85

Calls 6

NumberFunction · 0.92
Float64Function · 0.92
StringFunction · 0.92
ProtoToStructFunction · 0.85
ProtoEnumToConstantFunction · 0.85
StringMethod · 0.65

Tested by

no test coverage detected