-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
68 lines (58 loc) · 1.83 KB
/
slice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package flag_types
import (
"encoding/json"
"reflect"
"github.com/iancoleman/strcase"
"github.com/spf13/pflag"
"google.golang.org/protobuf/proto"
preflect "google.golang.org/protobuf/reflect/protoreflect"
)
type SliceVar struct {
FdPath []preflect.FieldDescriptor
DVals []interface{}
Fd preflect.FieldDescriptor
Msg proto.Message
Tp reflect.Type
}
var _ pflag.Value = (*SliceVar)(nil)
func (v *SliceVar) String() string {
currentSlice, _ := getCurrentRawValue(v.Msg, v.FdPath)
data, err := json.Marshal(currentSlice)
if err != nil {
panic(err)
}
return string(data)
}
func (v *SliceVar) Set(raw string) error {
current, isCurrentSet := getCurrentRawValue(v.Msg, v.FdPath)
if !isCurrentSet {
current = reflect.New(reflect.TypeOf(v.DVals[len(v.DVals)-1]).Elem()).Elem().Interface()
}
elemType := reflect.TypeOf(current).Elem()
val, err := makeProtoValue(raw, v.Fd, elemType)
if err != nil {
return err
}
var newSliceElement reflect.Value
switch v.Fd.Kind() {
case preflect.EnumKind:
enumType := reflect.New(elemType).Interface().(preflect.Enum).Type()
newSliceElement = reflect.ValueOf(enumType.New(val.Enum()))
case preflect.MessageKind, preflect.GroupKind:
newSliceElement = reflect.ValueOf(val.Message().Interface())
default:
newSliceElement = reflect.ValueOf(val.Interface())
}
newSlice := reflect.Append(reflect.ValueOf(current), newSliceElement)
pathDefaults, pathFds := stripPathToLast(v.DVals, v.FdPath)
currentMsg := getFieldHolderAndEnsurePath(v.Msg, pathDefaults, pathFds)
// A bit tricky, but since slices cannot be in oneofs, we can just simply
// use reflection and spare effort to convert into preflect.List
reflect.ValueOf(currentMsg.Interface()).Elem().
FieldByName(strcase.ToCamel(string(v.Fd.Name()))).
Set(newSlice)
return nil
}
func (v *SliceVar) Type() string {
return "slice"
}