internal/filedesc, internal/filetype: initial commit
The internal/fileinit package is split apart into two packages:
* internal/filedesc constructs descriptors from the raw proto.
It is very similar to the previous internal/fileinit package.
* internal/filetype wraps descriptors with Go type information
Overview:
* The internal/fileinit package will be deleted in a future CL.
It is kept around since the v1 repo currently depends on it.
* The internal/prototype package is deleted. All former usages of it
are now using internal/filedesc instead. Most significantly,
the reflect/protodesc package was almost entirely re-written.
* The internal/impl package drops support for messages that do not
have a Descriptor method (pre-2016). This removes a significant amount
of technical debt.
filedesc.Builder to parse raw descriptors.
* The internal/encoding/defval package now handles enum values by name.
Change-Id: I3957bcc8588a70470fd6c7de1122216b80615ab7
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/182360
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/encoding/defval/default_test.go b/internal/encoding/defval/default_test.go
index a25a93f..d81150d 100644
--- a/internal/encoding/defval/default_test.go
+++ b/internal/encoding/defval/default_test.go
@@ -2,52 +2,103 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package defval
+package defval_test
import (
"math"
"reflect"
"testing"
+ "google.golang.org/protobuf/internal/encoding/defval"
+ fdesc "google.golang.org/protobuf/internal/filedesc"
pref "google.golang.org/protobuf/reflect/protoreflect"
)
func Test(t *testing.T) {
+ evs := fdesc.EnumValues{List: []fdesc.EnumValue{{}}}
+ evs.List[0].L0.ParentFile = fdesc.SurrogateProto2
+ evs.List[0].L0.FullName = "ALPHA"
+ evs.List[0].L1.Number = 1
+
V := pref.ValueOf
tests := []struct {
val pref.Value
+ enum pref.EnumValueDescriptor
+ enums pref.EnumValueDescriptors
kind pref.Kind
strPB string
strGo string
- }{
- {V(bool(true)), pref.BoolKind, "true", "1"},
- {V(int32(-0x1234)), pref.Int32Kind, "-4660", "-4660"},
- {V(float32(math.Pi)), pref.FloatKind, "3.1415927", "3.1415927"},
- {V(float64(math.Pi)), pref.DoubleKind, "3.141592653589793", "3.141592653589793"},
- {V(string("hello, \xde\xad\xbe\xef\n")), pref.StringKind, "hello, \xde\xad\xbe\xef\n", "hello, \xde\xad\xbe\xef\n"},
- {V([]byte("hello, \xde\xad\xbe\xef\n")), pref.BytesKind, "hello, \\336\\255\\276\\357\\n", "hello, \\336\\255\\276\\357\\n"},
- }
+ }{{
+ val: V(bool(true)),
+ enum: nil,
+ enums: nil,
+ kind: pref.BoolKind,
+ strPB: "true",
+ strGo: "1",
+ }, {
+ val: V(int32(-0x1234)),
+ enum: nil,
+ enums: nil,
+ kind: pref.Int32Kind,
+ strPB: "-4660",
+ strGo: "-4660",
+ }, {
+ val: V(float32(math.Pi)),
+ enum: nil,
+ enums: nil,
+ kind: pref.FloatKind,
+ strPB: "3.1415927",
+ strGo: "3.1415927",
+ }, {
+ val: V(float64(math.Pi)),
+ enum: nil,
+ enums: nil,
+ kind: pref.DoubleKind,
+ strPB: "3.141592653589793",
+ strGo: "3.141592653589793",
+ }, {
+ val: V(string("hello, \xde\xad\xbe\xef\n")),
+ enum: nil,
+ enums: nil,
+ kind: pref.StringKind,
+ strPB: "hello, \xde\xad\xbe\xef\n",
+ strGo: "hello, \xde\xad\xbe\xef\n",
+ }, {
+ val: V([]byte("hello, \xde\xad\xbe\xef\n")),
+ enum: nil,
+ enums: nil,
+ kind: pref.BytesKind,
+ strPB: "hello, \\336\\255\\276\\357\\n",
+ strGo: "hello, \\336\\255\\276\\357\\n",
+ }, {
+ val: V(pref.EnumNumber(1)),
+ enum: &evs.List[0],
+ enums: &evs,
+ kind: pref.EnumKind,
+ strPB: "ALPHA",
+ strGo: "1",
+ }}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
- gotStrPB, _ := Marshal(tt.val, tt.kind, Descriptor)
- if gotStrPB != tt.strPB {
- t.Errorf("Marshal(%v, %v, Descriptor) = %q, want %q", tt.val, tt.kind, gotStrPB, tt.strPB)
+ gotStr, _ := defval.Marshal(tt.val, tt.enum, tt.kind, defval.Descriptor)
+ if gotStr != tt.strPB {
+ t.Errorf("Marshal(%v, %v, Descriptor) = %q, want %q", tt.val, tt.kind, gotStr, tt.strPB)
}
- gotStrGo, _ := Marshal(tt.val, tt.kind, GoTag)
- if gotStrGo != tt.strGo {
- t.Errorf("Marshal(%v, %v, GoTag) = %q, want %q", tt.val, tt.kind, gotStrGo, tt.strGo)
+ gotStr, _ = defval.Marshal(tt.val, tt.enum, tt.kind, defval.GoTag)
+ if gotStr != tt.strGo {
+ t.Errorf("Marshal(%v, %v, GoTag) = %q, want %q", tt.val, tt.kind, gotStr, tt.strGo)
}
- gotValPB, _ := Unmarshal(tt.strPB, tt.kind, Descriptor)
- if !reflect.DeepEqual(gotValPB.Interface(), tt.val.Interface()) {
- t.Errorf("Unmarshal(%v, %v, Descriptor) = %q, want %q", tt.strPB, tt.kind, gotValPB, tt.val)
+ gotVal, gotEnum, _ := defval.Unmarshal(tt.strPB, tt.kind, tt.enums, defval.Descriptor)
+ if !reflect.DeepEqual(gotVal.Interface(), tt.val.Interface()) || gotEnum != tt.enum {
+ t.Errorf("Unmarshal(%v, %v, Descriptor) = (%q, %v), want (%q, %v)", tt.strPB, tt.kind, gotVal, gotEnum, tt.val, tt.enum)
}
- gotValGo, _ := Unmarshal(tt.strGo, tt.kind, GoTag)
- if !reflect.DeepEqual(gotValGo.Interface(), tt.val.Interface()) {
- t.Errorf("Unmarshal(%v, %v, GoTag) = %q, want %q", tt.strGo, tt.kind, gotValGo, tt.val)
+ gotVal, gotEnum, _ = defval.Unmarshal(tt.strGo, tt.kind, tt.enums, defval.GoTag)
+ if !reflect.DeepEqual(gotVal.Interface(), tt.val.Interface()) || gotEnum != tt.enum {
+ t.Errorf("Unmarshal(%v, %v, GoTag) = (%q, %v), want (%q, %v)", tt.strGo, tt.kind, gotVal, gotEnum, tt.val, tt.enum)
}
})
}