internal: ignore unexported fields when comparing struct fields (#2853)

This commit is contained in:
Menghan Li
2019-06-05 09:54:21 -07:00
committed by Doug Fawley
parent 2df9cb80d4
commit 914c27f822

View File

@ -21,25 +21,37 @@ import (
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
"unicode"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
basepb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/core/base" basepb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/core/base"
) )
const ignorePrefix = "XXX_"
func ignore(name string) bool {
if !unicode.IsUpper([]rune(name)[0]) {
return true
}
return strings.HasPrefix(name, ignorePrefix)
}
// A reflection based test to make sure internal.Locality contains all the // A reflection based test to make sure internal.Locality contains all the
// fields (expect for XXX_) from the proto message. // fields (expect for XXX_) from the proto message.
func TestLocalityMatchProtoMessage(t *testing.T) { func TestLocalityMatchProtoMessage(t *testing.T) {
want1 := make(map[string]string) want1 := make(map[string]string)
for ty, i := reflect.TypeOf(Locality{}), 0; i < ty.NumField(); i++ { for ty, i := reflect.TypeOf(Locality{}), 0; i < ty.NumField(); i++ {
f := ty.Field(i) f := ty.Field(i)
if ignore(f.Name) {
continue
}
want1[f.Name] = f.Type.Name() want1[f.Name] = f.Type.Name()
} }
const ignorePrefix = "XXX_"
want2 := make(map[string]string) want2 := make(map[string]string)
for ty, i := reflect.TypeOf(basepb.Locality{}), 0; i < ty.NumField(); i++ { for ty, i := reflect.TypeOf(basepb.Locality{}), 0; i < ty.NumField(); i++ {
f := ty.Field(i) f := ty.Field(i)
if strings.HasPrefix(f.Name, ignorePrefix) { if ignore(f.Name) {
continue continue
} }
want2[f.Name] = f.Type.Name() want2[f.Name] = f.Type.Name()