MCPcopy
hub / github.com/grpc-ecosystem/grpc-gateway / parseField

Function parseField

runtime/query.go:208–286  ·  view source on GitHub ↗
(fieldDescriptor protoreflect.FieldDescriptor, value string)

Source from the content-addressed store, hash-verified

206}
207
208func parseField(fieldDescriptor protoreflect.FieldDescriptor, value string) (protoreflect.Value, error) {
209 switch fieldDescriptor.Kind() {
210 case protoreflect.BoolKind:
211 v, err := strconv.ParseBool(value)
212 if err != nil {
213 return protoreflect.Value{}, err
214 }
215 return protoreflect.ValueOfBool(v), nil
216 case protoreflect.EnumKind:
217 enum, err := protoregistry.GlobalTypes.FindEnumByName(fieldDescriptor.Enum().FullName())
218 if err != nil {
219 if errors.Is(err, protoregistry.NotFound) {
220 return protoreflect.Value{}, fmt.Errorf("enum %q is not registered", fieldDescriptor.Enum().FullName())
221 }
222 return protoreflect.Value{}, fmt.Errorf("failed to look up enum: %w", err)
223 }
224 // Look for enum by name
225 v := enum.Descriptor().Values().ByName(protoreflect.Name(value))
226 if v == nil {
227 i, err := strconv.Atoi(value)
228 if err != nil {
229 return protoreflect.Value{}, fmt.Errorf("%q is not a valid value", value)
230 }
231 // Look for enum by number
232 if v = enum.Descriptor().Values().ByNumber(protoreflect.EnumNumber(i)); v == nil {
233 return protoreflect.Value{}, fmt.Errorf("%q is not a valid value", value)
234 }
235 }
236 return protoreflect.ValueOfEnum(v.Number()), nil
237 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
238 v, err := strconv.ParseInt(value, 10, 32)
239 if err != nil {
240 return protoreflect.Value{}, err
241 }
242 return protoreflect.ValueOfInt32(int32(v)), nil
243 case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
244 v, err := strconv.ParseInt(value, 10, 64)
245 if err != nil {
246 return protoreflect.Value{}, err
247 }
248 return protoreflect.ValueOfInt64(v), nil
249 case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
250 v, err := strconv.ParseUint(value, 10, 32)
251 if err != nil {
252 return protoreflect.Value{}, err
253 }
254 return protoreflect.ValueOfUint32(uint32(v)), nil
255 case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
256 v, err := strconv.ParseUint(value, 10, 64)
257 if err != nil {
258 return protoreflect.Value{}, err
259 }
260 return protoreflect.ValueOfUint64(v), nil
261 case protoreflect.FloatKind:
262 v, err := strconv.ParseFloat(value, 32)
263 if err != nil {
264 return protoreflect.Value{}, err
265 }

Callers 3

populateFieldFunction · 0.85
populateRepeatedFieldFunction · 0.85
populateMapFieldFunction · 0.85

Calls 5

BytesFunction · 0.85
parseMessageFunction · 0.85
EnumMethod · 0.45
DescriptorMethod · 0.45
NumberMethod · 0.45

Tested by

no test coverage detected