Skip to content

Commit

Permalink
Get wirepath tests working again
Browse files Browse the repository at this point in the history
  • Loading branch information
reddaly authored and gonzojive committed Oct 4, 2021
1 parent 1aa58ce commit 777a2fd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 142 deletions.
1 change: 1 addition & 0 deletions wirepath/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ go_test(
srcs = ["wirepath_test.go"],
embed = [":go_default_library"],
deps = [
"//internal/protoreflectcmp:go_default_library",
"//proto/wirepath:go_default_library",
"//proto/wirepath/testproto:go_default_library",
"@com_github_google_go_cmp//cmp:go_default_library",
Expand Down
152 changes: 10 additions & 142 deletions wirepath/wirepath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
package wirepath

import (
"reflect"
"regexp"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/xtoproto/internal/protoreflectcmp"
"github.com/google/xtoproto/proto/wirepath"
"github.com/google/xtoproto/proto/wirepath/testproto"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestGetValue(t *testing.T) {
want: int32(42),
},
{
name: "child of repeated field",
name: "child of repeated field out of bounds",
path: MustParse(`8(proto_imports)[2]/5(proto_tag)`).Proto(),
within: &testproto.Example{
Children: []*testproto.Example{
Expand All @@ -119,33 +119,29 @@ func TestGetValue(t *testing.T) {
wantErr: true, // out of bounds
},
{
name: "child of repeated field",
name: "child of repeated field ",
path: MustParse(`8(proto_imports)/5(proto_tag)`).Proto(),
within: &testproto.Example{
Children: []*testproto.Example{
{},
{ProtoTag: 42},
},
},
wantErr: true, // cannot specify a field (proto_tag) of a list.
wantErr: true, // cannot specify a child path elemnt of a list without a slot selector.
},
{
name: "child of repeated field",
name: "repeated field should return a list",
path: MustParse(`8(proto_imports)`).Proto(),
within: &testproto.Example{
Children: []*testproto.Example{
{},
{ProtoTag: 42},
},
},
want: func() protoreflect.List {
l := newEZlist(&testproto.Example{})
l.Append(protoreflect.ValueOfMessage((&testproto.Example{}).ProtoReflect()))
l.Append(protoreflect.ValueOfMessage((&testproto.Example{
ProtoTag: 42,
}).ProtoReflect()))
return l
}(),
want: []*testproto.Example{
{},
{ProtoTag: 42},
},
},
}
for _, tt := range tests {
Expand All @@ -162,8 +158,7 @@ func TestGetValue(t *testing.T) {

got := gotValue.Interface()
w, g := &tt.want, &got
if diff := cmp.Diff(w, g, protocmp.Transform(), cmpProtoReflectOpt); diff != "" {
//if diff := cmp.Diff(tt.want, got.Interface(), protocmp.Transform(), reflectListTransform, transform2); diff != "" {
if diff := cmp.Diff(w, g, protocmp.Transform(), protoreflectcmp.Transform()); diff != "" {
t.Errorf("GetValue() got unexpected diff (-want, +got):\n%s", diff)
}
})
Expand Down Expand Up @@ -485,130 +480,3 @@ func TestParseProtobufStringLiteral(t *testing.T) {
})
}
}

var (
cmpProtoReflectOpt = cmp.FilterValues(func(a, b interface{}) bool {
asymmetric := func(a, b interface{}) bool {
list, ok := a.(protoreflect.List)
if !ok {
return false
}
if b == nil || reflect.TypeOf(b).Kind() != reflect.Slice {
return false
}
// Now check that elements of a and b are both

return bIsSlice
}
return asymmetric(a, b) || asymmetric(b, a)
}, reflectListTransform)

reflectListTransform = cmp.Transformer("listToSlice", func(x interface{}) interface{} {
list := x.(protoreflect.List)
if !list.IsValid() {
return nil
}
switch list.NewElement().Interface().(type) {
case protoreflect.ProtoMessage:
out := make([]proto.Message, list.Len())
for i := 0; i < list.Len(); i++ {
out[i] = list.Get(i).Message().Interface()
}
return out
default:
return list
}
// elemType := reflect.TypeOf(list.NewElement().Interface())
// out := reflect.MakeSlice(reflect.SliceOf(elemType), list.Len(), list.Len())
// for i := 0; i < list.Len(); i++ {
// out.Index(i).Set(reflect.ValueOf(list.Get(i).Interface()))
// }
// return out.Interface()
})
)

var transform3 = cmp.Transformer("listToSlice", func(list protoreflect.List) []protoreflect.Value {
out := make([]protoreflect.Value, list.Len())
for i := 0; i < list.Len(); i++ {
out[i] = list.Get(i)
}
return out
})

type ezList struct {
slice []protoreflect.Value
newElem func() protoreflect.Value
}

func newEZlist(prototype proto.Message) *ezList {
return &ezList{
nil,
func() protoreflect.Value {
return protoreflect.ValueOfMessage(proto.Clone(prototype).ProtoReflect())
},
}
}

// Len reports the number of entries in the List.
// Get, Set, and Truncate panic with out of bound indexes.
func (l *ezList) Len() int {
return len(l.slice)
}

// Get retrieves the value at the given index.
// It never returns an invalid value.
func (l *ezList) Get(i int) protoreflect.Value {
return l.slice[i]
}

// Set stores a value for the given index.
// When setting a composite type, it is unspecified whether the set
// value aliases the source's memory in any way.
//
// Set is a mutating operation and unsafe for concurrent use.
func (l *ezList) Set(i int, v protoreflect.Value) {
l.slice[i] = v
}

// Append appends the provided value to the end of the list.
// When appending a composite type, it is unspecified whether the appended
// value aliases the source's memory in any way.
//
// Append is a mutating operation and unsafe for concurrent use.
func (l *ezList) Append(v protoreflect.Value) {
l.slice = append(l.slice, v)
}

// AppendMutable appends a new, empty, mutable message value to the end
// of the list and returns it.
// It panics if the list does not contain a message type.
func (l *ezList) AppendMutable() protoreflect.Value {
e := l.newElem()
l.Append(e)
return e
}

// Truncate truncates the list to a smaller length.
//
// Truncate is a mutating operation and unsafe for concurrent use.
func (l *ezList) Truncate(newLen int) {
l.slice = l.slice[0:newLen]
}

// NewElement returns a new value for a list element.
// For enums, this returns the first enum value.
// For other scalars, this returns the zero value.
// For messages, this returns a new, empty, mutable value.
func (l *ezList) NewElement() protoreflect.Value {
return l.newElem()
}

// IsValid reports whether the list is valid.
//
// An invalid list is an empty, read-only value.
//
// Validity is not part of the protobuf data model, and may not
// be preserved in marshaling or other operations.
func (l *ezList) IsValid() bool {
return true
}

0 comments on commit 777a2fd

Please sign in to comment.