[protobuf] defval: support hex and octal default values for numeric fields

1 view
Skip to first unread message

Eric Salo (Gerrit)

unread,
12:46 AM (5 hours ago) 12:46 AM
to Michael Stapelberg, Nicolas Hillegeer, goph...@pubsubhelper.golang.org, Sandy Zhang, golang-co...@googlegroups.com
Attention needed from Michael Stapelberg and Nicolas Hillegeer

Eric Salo has uploaded the change for review

Eric Salo would like Michael Stapelberg and Nicolas Hillegeer to review this change.

Commit message

defval: support hex and octal default values for numeric fields

Update defval.Unmarshal to use base 0 for strconv.ParseInt and strconv.ParseUint, allowing it to handle hex (0x) and octal (0) string representations in dynamic descriptors, matching the flexibility of C++ and Java runtimes.
Change-Id: I43e59ed2c0ab6aebc5554be7de342811b0115f17

Change diff

diff --git a/internal/encoding/defval/default.go b/internal/encoding/defval/default.go
index 328dc73..079a53d 100644
--- a/internal/encoding/defval/default.go
+++ b/internal/encoding/defval/default.go
@@ -69,19 +69,19 @@
}
}
case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
- if v, err := strconv.ParseInt(s, 10, 32); err == nil {
+ if v, err := strconv.ParseInt(s, 0, 32); err == nil {
return protoreflect.ValueOfInt32(int32(v)), nil, nil
}
case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
- if v, err := strconv.ParseInt(s, 10, 64); err == nil {
+ if v, err := strconv.ParseInt(s, 0, 64); err == nil {
return protoreflect.ValueOfInt64(int64(v)), nil, nil
}
case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
- if v, err := strconv.ParseUint(s, 10, 32); err == nil {
+ if v, err := strconv.ParseUint(s, 0, 32); err == nil {
return protoreflect.ValueOfUint32(uint32(v)), nil, nil
}
case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
- if v, err := strconv.ParseUint(s, 10, 64); err == nil {
+ if v, err := strconv.ParseUint(s, 0, 64); err == nil {
return protoreflect.ValueOfUint64(uint64(v)), nil, nil
}
case protoreflect.FloatKind, protoreflect.DoubleKind:
diff --git a/internal/encoding/defval/default_test.go b/internal/encoding/defval/default_test.go
index bde8ca2..bcdb9c5 100644
--- a/internal/encoding/defval/default_test.go
+++ b/internal/encoding/defval/default_test.go
@@ -103,3 +103,57 @@
})
}
}
+
+func TestUnmarshalNonDecimal(t *testing.T) {
+ V := protoreflect.ValueOf
+ tests := []struct {
+ str string
+ kind protoreflect.Kind
+ want protoreflect.Value
+ }{
+ {str: "0x10", kind: protoreflect.Int32Kind, want: V(int32(16))},
+ {str: "-0x10", kind: protoreflect.Int32Kind, want: V(int32(-16))},
+ {str: "0x10", kind: protoreflect.Int64Kind, want: V(int64(16))},
+ {str: "-0x10", kind: protoreflect.Int64Kind, want: V(int64(-16))},
+ {str: "0x10", kind: protoreflect.Uint32Kind, want: V(uint32(16))},
+ {str: "0x10", kind: protoreflect.Uint64Kind, want: V(uint64(16))},
+ {str: "020", kind: protoreflect.Int32Kind, want: V(int32(16))},
+ {str: "-020", kind: protoreflect.Int32Kind, want: V(int32(-16))},
+ {str: "020", kind: protoreflect.Int64Kind, want: V(int64(16))},
+ {str: "-020", kind: protoreflect.Int64Kind, want: V(int64(-16))},
+ {str: "020", kind: protoreflect.Uint32Kind, want: V(uint32(16))},
+ {str: "020", kind: protoreflect.Uint64Kind, want: V(uint64(16))},
+ {str: "0x100000000", kind: protoreflect.Int64Kind, want: V(int64(4294967296))},
+ {str: "-0x100000000", kind: protoreflect.Int64Kind, want: V(int64(-4294967296))},
+ {str: "0x100000000", kind: protoreflect.Uint64Kind, want: V(uint64(4294967296))},
+ {str: "040000000000", kind: protoreflect.Int64Kind, want: V(int64(4294967296))},
+ {str: "-040000000000", kind: protoreflect.Int64Kind, want: V(int64(-4294967296))},
+ {str: "040000000000", kind: protoreflect.Uint64Kind, want: V(uint64(4294967296))},
+ // MaxInt32
+ {str: "0x7fffffff", kind: protoreflect.Int32Kind, want: V(int32(2147483647))},
+ {str: "017777777777", kind: protoreflect.Int32Kind, want: V(int32(2147483647))},
+ // MaxInt64
+ {str: "0x7fffffffffffffff", kind: protoreflect.Int64Kind, want: V(int64(9223372036854775807))},
+ {str: "0777777777777777777777", kind: protoreflect.Int64Kind, want: V(int64(9223372036854775807))},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.str+"_"+tt.kind.String(), func(t *testing.T) {
+ gotVal, _, err := defval.Unmarshal(tt.str, tt.kind, nil, defval.Descriptor)
+ if err != nil {
+ t.Fatalf("Unmarshal(%q, %v, Descriptor) failed: %v", tt.str, tt.kind, err)
+ }
+ if !reflect.DeepEqual(gotVal.Interface(), tt.want.Interface()) {
+ t.Errorf("Unmarshal(%q, %v, Descriptor) = %v, want %v", tt.str, tt.kind, gotVal, tt.want)
+ }
+
+ gotVal, _, err = defval.Unmarshal(tt.str, tt.kind, nil, defval.GoTag)
+ if err != nil {
+ t.Fatalf("Unmarshal(%q, %v, GoTag) failed: %v", tt.str, tt.kind, err)
+ }
+ if !reflect.DeepEqual(gotVal.Interface(), tt.want.Interface()) {
+ t.Errorf("Unmarshal(%q, %v, GoTag) = %v, want %v", tt.str, tt.kind, gotVal, tt.want)
+ }
+ })
+ }
+}

Change information

Files:
  • M internal/encoding/defval/default.go
  • M internal/encoding/defval/default_test.go
Change size: M
Delta: 2 files changed, 58 insertions(+), 4 deletions(-)
Open in Gerrit

Related details

Attention is currently required from:
  • Michael Stapelberg
  • Nicolas Hillegeer
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: protobuf
Gerrit-Branch: master
Gerrit-Change-Id: I43e59ed2c0ab6aebc5554be7de342811b0115f17
Gerrit-Change-Number: 812620
Gerrit-PatchSet: 1
Gerrit-Owner: Eric Salo <sa...@google.com>
Gerrit-Reviewer: Michael Stapelberg <stape...@google.com>
Gerrit-Reviewer: Nicolas Hillegeer <ak...@google.com>
Gerrit-CC: Sandy Zhang <sandy...@google.com>
Gerrit-Attention: Michael Stapelberg <stape...@google.com>
Gerrit-Attention: Nicolas Hillegeer <ak...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
12:51 AM (5 hours ago) 12:51 AM
to Eric Salo, goph...@pubsubhelper.golang.org, Nicolas Hillegeer, Michael Stapelberg, Sandy Zhang, golang-co...@googlegroups.com
Attention needed from Michael Stapelberg and Nicolas Hillegeer

Message from Gopher Robot

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

Open in Gerrit

Related details

Attention is currently required from:
  • Michael Stapelberg
  • Nicolas Hillegeer
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: protobuf
Gerrit-Branch: master
Gerrit-Change-Id: I43e59ed2c0ab6aebc5554be7de342811b0115f17
Gerrit-Change-Number: 812620
Gerrit-PatchSet: 1
Gerrit-Owner: Eric Salo <sa...@google.com>
Gerrit-Reviewer: Michael Stapelberg <stape...@google.com>
Gerrit-Reviewer: Nicolas Hillegeer <ak...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-CC: Sandy Zhang <sandy...@google.com>
Gerrit-Attention: Michael Stapelberg <stape...@google.com>
Gerrit-Attention: Nicolas Hillegeer <ak...@google.com>
Gerrit-Comment-Date: Mon, 10 Aug 2026 04:50:56 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
unsatisfied_requirement
satisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages