mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-07 12:56:47 +00:00
954bd92c19
A Converter converts between reflect.Values and protoreflect.Values. The existing usage of Converter is somewhat confusing: The internal/value package creates Converters for scalar types only, the internal/impl package creates Converters for legacy messages and enums, and the reflect/prototype package creates Converters for repeated fields. Change the Converter type to an interface. The constructor for Converter takes a FieldDescriptor and reflect.Type, and directly handles conversions for all field types: Scalars, lists, maps, and legacy types. Move Converter into the internal/impl package, since that package contains the necessary support for dealing with legacy messages and enums. Drop the internal/value package. Replace two uses of prototype.Extension with more focused implementations, since the implementation is trivial with the refactored Converter. Drop prototype.Extension for the moment since it is now unused. Change-Id: If0c570fefac002cc5925b3d56281b6eb17e90d5f Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/187857 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
// Copyright 2019 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package mapsort_test
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
|
|
"google.golang.org/protobuf/internal/mapsort"
|
|
testpb "google.golang.org/protobuf/internal/testprotos/test"
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
)
|
|
|
|
func TestRange(t *testing.T) {
|
|
m := (&testpb.TestAllTypes{
|
|
MapBoolBool: map[bool]bool{
|
|
false: false,
|
|
true: true,
|
|
},
|
|
MapInt32Int32: map[int32]int32{
|
|
0: 0,
|
|
1: 1,
|
|
2: 2,
|
|
},
|
|
MapUint64Uint64: map[uint64]uint64{
|
|
0: 0,
|
|
1: 1,
|
|
2: 2,
|
|
},
|
|
MapStringString: map[string]string{
|
|
"0": "0",
|
|
"1": "1",
|
|
"2": "2",
|
|
},
|
|
}).ProtoReflect()
|
|
m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
|
|
mapv := v.Map()
|
|
var got []pref.MapKey
|
|
mapsort.Range(mapv, fd.MapKey().Kind(), func(key pref.MapKey, _ pref.Value) bool {
|
|
got = append(got, key)
|
|
return true
|
|
})
|
|
for wanti, key := range got {
|
|
var goti int
|
|
switch x := mapv.Get(key).Interface().(type) {
|
|
case bool:
|
|
if x {
|
|
goti = 1
|
|
}
|
|
case int32:
|
|
goti = int(x)
|
|
case uint64:
|
|
goti = int(x)
|
|
case string:
|
|
goti, _ = strconv.Atoi(x)
|
|
default:
|
|
t.Fatalf("unhandled map value type %T", x)
|
|
}
|
|
if wanti != goti {
|
|
t.Errorf("out of order range over map field %v: %v", fd.FullName(), got)
|
|
break
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
}
|