all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/impl/convert.go b/internal/impl/convert.go
index 9016fa4..86da513 100644
--- a/internal/impl/convert.go
+++ b/internal/impl/convert.go
@@ -57,48 +57,67 @@
byteType = reflect.TypeOf(byte(0))
)
+var (
+ boolZero = pref.ValueOf(bool(false))
+ int32Zero = pref.ValueOf(int32(0))
+ int64Zero = pref.ValueOf(int64(0))
+ uint32Zero = pref.ValueOf(uint32(0))
+ uint64Zero = pref.ValueOf(uint64(0))
+ float32Zero = pref.ValueOf(float32(0))
+ float64Zero = pref.ValueOf(float64(0))
+ stringZero = pref.ValueOf(string(""))
+ bytesZero = pref.ValueOf([]byte(nil))
+)
+
type scalarConverter struct {
goType, pbType reflect.Type
def pref.Value
}
func newSingularConverter(t reflect.Type, fd pref.FieldDescriptor) Converter {
+ defVal := func(fd pref.FieldDescriptor, zero pref.Value) pref.Value {
+ if fd.Cardinality() == pref.Repeated {
+ // Default isn't defined for repeated fields.
+ return zero
+ }
+ return fd.Default()
+ }
switch fd.Kind() {
case pref.BoolKind:
if t.Kind() == reflect.Bool {
- return &scalarConverter{t, boolType, fd.Default()}
+ return &scalarConverter{t, boolType, defVal(fd, boolZero)}
}
case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
if t.Kind() == reflect.Int32 {
- return &scalarConverter{t, int32Type, fd.Default()}
+ return &scalarConverter{t, int32Type, defVal(fd, int32Zero)}
}
case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
if t.Kind() == reflect.Int64 {
- return &scalarConverter{t, int64Type, fd.Default()}
+ return &scalarConverter{t, int64Type, defVal(fd, int64Zero)}
}
case pref.Uint32Kind, pref.Fixed32Kind:
if t.Kind() == reflect.Uint32 {
- return &scalarConverter{t, uint32Type, fd.Default()}
+ return &scalarConverter{t, uint32Type, defVal(fd, uint32Zero)}
}
case pref.Uint64Kind, pref.Fixed64Kind:
if t.Kind() == reflect.Uint64 {
- return &scalarConverter{t, uint64Type, fd.Default()}
+ return &scalarConverter{t, uint64Type, defVal(fd, uint64Zero)}
}
case pref.FloatKind:
if t.Kind() == reflect.Float32 {
- return &scalarConverter{t, float32Type, fd.Default()}
+ return &scalarConverter{t, float32Type, defVal(fd, float32Zero)}
}
case pref.DoubleKind:
if t.Kind() == reflect.Float64 {
- return &scalarConverter{t, float64Type, fd.Default()}
+ return &scalarConverter{t, float64Type, defVal(fd, float64Zero)}
}
case pref.StringKind:
if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
- return &scalarConverter{t, stringType, fd.Default()}
+ return &scalarConverter{t, stringType, defVal(fd, stringZero)}
}
case pref.BytesKind:
if t.Kind() == reflect.String || (t.Kind() == reflect.Slice && t.Elem() == byteType) {
- return &scalarConverter{t, bytesType, fd.Default()}
+ return &scalarConverter{t, bytesType, defVal(fd, bytesZero)}
}
case pref.EnumKind:
// Handle enums, which must be a named int32 type.
@@ -133,6 +152,9 @@
}
func (c *scalarConverter) New() pref.Value {
+ if c.pbType == bytesType {
+ return pref.ValueOf(append(([]byte)(nil), c.def.Bytes()...))
+ }
return c.def
}
@@ -142,7 +164,13 @@
}
func newEnumConverter(goType reflect.Type, fd pref.FieldDescriptor) Converter {
- return &enumConverter{goType, fd.Default()}
+ var def pref.Value
+ if fd.Cardinality() == pref.Repeated {
+ def = pref.ValueOf(fd.Enum().Values().Get(0).Number())
+ } else {
+ def = fd.Default()
+ }
+ return &enumConverter{goType, def}
}
func (c *enumConverter) PBValueOf(v reflect.Value) pref.Value {