(v protoreflect.Value, fd protoreflect.FieldDescriptor)
| 366 | } |
| 367 | |
| 368 | func (p *textParser) unmarshalSingularValue(v protoreflect.Value, fd protoreflect.FieldDescriptor) (protoreflect.Value, error) { |
| 369 | tok := p.next() |
| 370 | if tok.err != nil { |
| 371 | return v, tok.err |
| 372 | } |
| 373 | if tok.value == "" { |
| 374 | return v, p.errorf("unexpected EOF") |
| 375 | } |
| 376 | |
| 377 | switch fd.Kind() { |
| 378 | case protoreflect.BoolKind: |
| 379 | switch tok.value { |
| 380 | case "true", "1", "t", "True": |
| 381 | return protoreflect.ValueOfBool(true), nil |
| 382 | case "false", "0", "f", "False": |
| 383 | return protoreflect.ValueOfBool(false), nil |
| 384 | } |
| 385 | case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: |
| 386 | if x, err := strconv.ParseInt(tok.value, 0, 32); err == nil { |
| 387 | return protoreflect.ValueOfInt32(int32(x)), nil |
| 388 | } |
| 389 | |
| 390 | // The C++ parser accepts large positive hex numbers that uses |
| 391 | // two's complement arithmetic to represent negative numbers. |
| 392 | // This feature is here for backwards compatibility with C++. |
| 393 | if strings.HasPrefix(tok.value, "0x") { |
| 394 | if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil { |
| 395 | return protoreflect.ValueOfInt32(int32(-(int64(^x) + 1))), nil |
| 396 | } |
| 397 | } |
| 398 | case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: |
| 399 | if x, err := strconv.ParseInt(tok.value, 0, 64); err == nil { |
| 400 | return protoreflect.ValueOfInt64(int64(x)), nil |
| 401 | } |
| 402 | |
| 403 | // The C++ parser accepts large positive hex numbers that uses |
| 404 | // two's complement arithmetic to represent negative numbers. |
| 405 | // This feature is here for backwards compatibility with C++. |
| 406 | if strings.HasPrefix(tok.value, "0x") { |
| 407 | if x, err := strconv.ParseUint(tok.value, 0, 64); err == nil { |
| 408 | return protoreflect.ValueOfInt64(int64(-(int64(^x) + 1))), nil |
| 409 | } |
| 410 | } |
| 411 | case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: |
| 412 | if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil { |
| 413 | return protoreflect.ValueOfUint32(uint32(x)), nil |
| 414 | } |
| 415 | case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: |
| 416 | if x, err := strconv.ParseUint(tok.value, 0, 64); err == nil { |
| 417 | return protoreflect.ValueOfUint64(uint64(x)), nil |
| 418 | } |
| 419 | case protoreflect.FloatKind: |
| 420 | // Ignore 'f' for compatibility with output generated by C++, |
| 421 | // but don't remove 'f' when the value is "-inf" or "inf". |
| 422 | v := tok.value |
| 423 | if strings.HasSuffix(v, "f") && v != "-inf" && v != "inf" { |
| 424 | v = v[:len(v)-len("f")] |
| 425 | } |
no test coverage detected