-
Notifications
You must be signed in to change notification settings - Fork 3
/
options.go
157 lines (127 loc) · 4.37 KB
/
options.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package qbcli
import (
"fmt"
"reflect"
"strconv"
"github.com/QuickBase/quickbase-cli/qbclient"
"github.com/cpliakas/cliutil"
"github.com/spf13/viper"
)
// QueryOption implements Option for string options that contain queries.
type QueryOption struct {
tag map[string]string
}
// NewQueryOption is a cliutil.OptionTypeFunc that returns a *cliutil.QueryOption.
func NewQueryOption(tag map[string]string) cliutil.OptionType { return &QueryOption{tag} }
// Set implements cliutil.OptionType.Set.
func (opt *QueryOption) Set(f *cliutil.Flagger) error {
f.String(opt.tag["option"], opt.tag["short"], opt.tag["default"], opt.tag["usage"])
return nil
}
// Read implements cliutil.OptionType.Read.
func (opt *QueryOption) Read(cfg *viper.Viper, field reflect.Value) error {
s := ParseQuery(cfg.GetString(opt.tag["option"]))
field.SetString(s)
return nil
}
// SortOption implements Option for string options that contain queries.
type SortOption struct {
tag map[string]string
}
// NewSortOption is a cliutil.OptionTypeFunc that returns a *cliutil.SortOption.
func NewSortOption(tag map[string]string) cliutil.OptionType { return &SortOption{tag} }
// Set implements cliutil.OptionType.Set.
func (opt *SortOption) Set(f *cliutil.Flagger) error {
f.String(opt.tag["option"], opt.tag["short"], opt.tag["default"], opt.tag["usage"])
return nil
}
// Read implements cliutil.OptionType.Read.
func (opt *SortOption) Read(cfg *viper.Viper, field reflect.Value) error {
s := cfg.GetString(opt.tag["option"])
if s == "" {
return nil
}
v, err := ParseSortBy(s)
if err != nil {
return err
}
field.Set(reflect.ValueOf(v))
return nil
}
// GroupOption implements Option for string options that contain queries.
type GroupOption struct {
tag map[string]string
}
// NewGroupOption is a cliutil.OptionTypeFunc that returns a *cliutil.GroupOption.
func NewGroupOption(tag map[string]string) cliutil.OptionType { return &GroupOption{tag} }
// Set implements cliutil.OptionType.Set.
func (opt *GroupOption) Set(f *cliutil.Flagger) error {
f.String(opt.tag["option"], opt.tag["short"], opt.tag["default"], opt.tag["usage"])
return nil
}
// Read implements cliutil.OptionType.Read.
func (opt *GroupOption) Read(cfg *viper.Viper, field reflect.Value) error {
s := cfg.GetString(opt.tag["option"])
if s == "" {
return nil
}
v, err := ParseGroupBy(s)
if err != nil {
return err
}
field.Set(reflect.ValueOf(v))
return nil
}
// RecordOption implements Option for string options that contain record datas.
type RecordOption struct {
tag map[string]string
}
// NewRecordOption is a cliutil.OptionTypeFunc that returns a *cliutil.RecordOption.
func NewRecordOption(tag map[string]string) cliutil.OptionType { return &RecordOption{tag} }
// Set implements cliutil.OptionType.Set.
func (opt *RecordOption) Set(f *cliutil.Flagger) error {
f.String(opt.tag["option"], opt.tag["short"], opt.tag["default"], opt.tag["usage"])
return nil
}
// Read implements cliutil.OptionType.Read.
func (opt *RecordOption) Read(cfg *viper.Viper, field reflect.Value) error {
// Get the table's field type map.
// TODO Do we need to remove the hard-coding to "to"?
m, err := GetFieldTypeMap(cfg.GetString("to"))
if err != nil {
return err
}
// Parse the key/value pairs into *qbclient.Value objects, and build the
// record being inserted into the table.
record := make(map[int]*qbclient.InsertRecordsInputData)
data := cliutil.ParseKeyValue(cfg.GetString(opt.tag["option"]))
for k, v := range data {
// Make sure k is an integer.
fid, err := strconv.Atoi(k)
if err != nil {
return fmt.Errorf("key %s: %w", k, err)
}
// Get the field type from the table's field type map.
ft, ok := m[fid]
if !ok {
return fmt.Errorf("field %v not defined in table", fid)
}
// Create a *qbclient.Value from the string value and field type.
val, err := qbclient.NewValueFromString(v, ft)
if err != nil {
return fmt.Errorf("value invalid for field %v: %w", fid, err)
}
// Add the value to the record .
record[fid] = &qbclient.InsertRecordsInputData{Value: val}
}
// Set the field's value.
r := []map[int]*qbclient.InsertRecordsInputData{record}
field.Set(reflect.ValueOf(r))
return nil
}
func init() {
cliutil.RegisterOptionTypeFunc("query", NewQueryOption)
cliutil.RegisterOptionTypeFunc("record", NewRecordOption)
cliutil.RegisterOptionTypeFunc("sort", NewSortOption)
cliutil.RegisterOptionTypeFunc("group", NewGroupOption)
}