|
|
@@ -11,30 +11,75 @@ import (
|
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
|
)
|
|
|
|
|
|
+func newListConverter(t reflect.Type, fd pref.FieldDescriptor) Converter {
|
|
|
+ switch {
|
|
|
+ case t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Slice:
|
|
|
+ return &listPtrConverter{t, newSingularConverter(t.Elem().Elem(), fd)}
|
|
|
+ case t.Kind() == reflect.Slice:
|
|
|
+ return &listConverter{t, newSingularConverter(t.Elem(), fd)}
|
|
|
+ }
|
|
|
+ panic(fmt.Sprintf("invalid Go type %v for field %v", t, fd.FullName()))
|
|
|
+}
|
|
|
+
|
|
|
type listConverter struct {
|
|
|
goType reflect.Type
|
|
|
c Converter
|
|
|
}
|
|
|
|
|
|
-func newListConverter(t reflect.Type, fd pref.FieldDescriptor) Converter {
|
|
|
- if t.Kind() != reflect.Ptr || t.Elem().Kind() != reflect.Slice {
|
|
|
- panic(fmt.Sprintf("invalid Go type %v for field %v", t, fd.FullName()))
|
|
|
+func (c *listConverter) PBValueOf(v reflect.Value) pref.Value {
|
|
|
+ if v.Type() != c.goType {
|
|
|
+ panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
|
|
}
|
|
|
- return &listConverter{t, newSingularConverter(t.Elem().Elem(), fd)}
|
|
|
+ pv := reflect.New(c.goType)
|
|
|
+ pv.Elem().Set(v)
|
|
|
+ return pref.ValueOf(&listReflect{pv, c.c})
|
|
|
}
|
|
|
|
|
|
-func (c *listConverter) PBValueOf(v reflect.Value) pref.Value {
|
|
|
+func (c *listConverter) GoValueOf(v pref.Value) reflect.Value {
|
|
|
+ rv := v.List().(*listReflect).v
|
|
|
+ if rv.IsNil() {
|
|
|
+ return reflect.Zero(c.goType)
|
|
|
+ }
|
|
|
+ return rv.Elem()
|
|
|
+}
|
|
|
+
|
|
|
+func (c *listConverter) IsValidPB(v pref.Value) bool {
|
|
|
+ list, ok := v.Interface().(*listReflect)
|
|
|
+ if !ok {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return list.v.Type().Elem() == c.goType
|
|
|
+}
|
|
|
+
|
|
|
+func (c *listConverter) IsValidGo(v reflect.Value) bool {
|
|
|
+ return v.Type() == c.goType
|
|
|
+}
|
|
|
+
|
|
|
+func (c *listConverter) New() pref.Value {
|
|
|
+ return pref.ValueOf(&listReflect{reflect.New(c.goType), c.c})
|
|
|
+}
|
|
|
+
|
|
|
+func (c *listConverter) Zero() pref.Value {
|
|
|
+ return pref.ValueOf(&listReflect{reflect.Zero(reflect.PtrTo(c.goType)), c.c})
|
|
|
+}
|
|
|
+
|
|
|
+type listPtrConverter struct {
|
|
|
+ goType reflect.Type
|
|
|
+ c Converter
|
|
|
+}
|
|
|
+
|
|
|
+func (c *listPtrConverter) PBValueOf(v reflect.Value) pref.Value {
|
|
|
if v.Type() != c.goType {
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
|
|
}
|
|
|
return pref.ValueOf(&listReflect{v, c.c})
|
|
|
}
|
|
|
|
|
|
-func (c *listConverter) GoValueOf(v pref.Value) reflect.Value {
|
|
|
+func (c *listPtrConverter) GoValueOf(v pref.Value) reflect.Value {
|
|
|
return v.List().(*listReflect).v
|
|
|
}
|
|
|
|
|
|
-func (c *listConverter) IsValidPB(v pref.Value) bool {
|
|
|
+func (c *listPtrConverter) IsValidPB(v pref.Value) bool {
|
|
|
list, ok := v.Interface().(*listReflect)
|
|
|
if !ok {
|
|
|
return false
|
|
|
@@ -42,15 +87,15 @@ func (c *listConverter) IsValidPB(v pref.Value) bool {
|
|
|
return list.v.Type() == c.goType
|
|
|
}
|
|
|
|
|
|
-func (c *listConverter) IsValidGo(v reflect.Value) bool {
|
|
|
+func (c *listPtrConverter) IsValidGo(v reflect.Value) bool {
|
|
|
return v.Type() == c.goType
|
|
|
}
|
|
|
|
|
|
-func (c *listConverter) New() pref.Value {
|
|
|
+func (c *listPtrConverter) New() pref.Value {
|
|
|
return c.PBValueOf(reflect.New(c.goType.Elem()))
|
|
|
}
|
|
|
|
|
|
-func (c *listConverter) Zero() pref.Value {
|
|
|
+func (c *listPtrConverter) Zero() pref.Value {
|
|
|
return c.PBValueOf(reflect.Zero(c.goType))
|
|
|
}
|
|
|
|