forked from jopbrown/gtk-sugar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guify_args.go
197 lines (175 loc) · 4.47 KB
/
guify_args.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package sugar
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/pkg/errors"
)
const (
STRING_NIL = "NULL"
STRING_WHITE_SPACE_CHARS = " \t\n\r"
)
func formatCommand(cmd string, args ...interface{}) string {
var sb strings.Builder
sb.WriteString(cmd)
writeArgs(&sb, args)
return sb.String()
}
func formatArgs(args ...interface{}) string {
var sb strings.Builder
writeArgs(&sb, args)
return sb.String()
}
func writeArgs(sb *strings.Builder, args []interface{}) {
for _, arg := range args {
sb.WriteByte(' ')
switch v := arg.(type) {
case nil:
sb.WriteString(STRING_NIL)
case string:
if len(v) == 0 {
sb.WriteString(STRING_NIL)
} else {
if strings.IndexAny(v, STRING_WHITE_SPACE_CHARS) > 0 {
v = strconv.Quote(v)
}
sb.WriteString(v)
}
case bool:
if v {
sb.WriteString(STRING_BOOL_TRUE)
} else {
sb.WriteString(STRING_BOOL_FALSE)
}
default:
sb.WriteString(fmt.Sprintf("%v", v))
}
}
}
type Args []interface{}
func (args Args) String() string {
return formatArgs(args)
}
type Varargs []interface{}
func (args Varargs) String() string {
var sb strings.Builder
for _, arg := range args {
sb.WriteByte(' ')
switch v := arg.(type) {
case nil:
sb.WriteString(STRING_NIL)
case string:
sb.WriteString("s:")
sb.WriteString(v)
case int, int8, int16, int32, uint, uint8, uint16, uint32:
sb.WriteString("i:")
sb.WriteString(fmt.Sprintf("%v", v))
case int64, uint64:
sb.WriteString("l:")
sb.WriteString(fmt.Sprintf("%v", v))
case float32:
sb.WriteString("f:")
sb.WriteString(fmt.Sprintf("%v", v))
case float64:
sb.WriteString("d:")
sb.WriteString(fmt.Sprintf("%v", v))
case bool:
sb.WriteString("b:")
if v {
sb.WriteString(STRING_BOOL_TRUE)
} else {
sb.WriteString(STRING_BOOL_FALSE)
}
case CandyWrapper:
sb.WriteString("w:")
sb.WriteString(v.ID())
default:
sb.WriteString("p:")
sb.WriteString(fmt.Sprintf("%v", v))
}
}
return sb.String()
}
type Base64Packer reflect.Value
func NewBase64Packer(s interface{}) *Base64Packer {
v := reflect.ValueOf(s).Elem()
if v.Kind() != reflect.Struct {
panic(errors.Errorf("invalid value to base64 packer: not struct but %v", v.Type()))
}
packer := Base64Packer(v)
return &packer
}
func (packer *Base64Packer) Format() string {
t := (*reflect.Value)(packer).Type()
var sb strings.Builder
n := t.NumField()
for i := 0; i < n; i++ {
switch t.Field(i).Type.Kind() {
case reflect.String:
sb.WriteString("%s")
case reflect.Int64, reflect.Uint64:
sb.WriteString("%l")
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
sb.WriteString("%i")
case reflect.Float32:
sb.WriteString("%f")
case reflect.Float64:
sb.WriteString("%d")
case reflect.Bool:
sb.WriteString("%b")
}
}
return sb.String()
}
func (packer *Base64Packer) Args() Args {
v := (*reflect.Value)(packer)
n := v.NumField()
args := make(Args, 0, n)
for i := 0; i < n; i++ {
field := v.Field(i)
switch field.Kind() {
case reflect.String, reflect.Int64, reflect.Uint64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Float32, reflect.Float64, reflect.Bool:
args = append(args, field.Interface())
}
}
return args
}
func (packer *Base64Packer) Unmarshal(fields RespFields) error {
v := (*reflect.Value)(packer)
n := v.NumField()
for i := 0; i < n; i++ {
f := v.Field(i)
if i >= len(fields) {
return errors.Errorf("number of RespFields(%v) is less than struct", fields)
}
resp := fields[i]
switch f.Kind() {
case reflect.String:
v, err := resp.Unquote()
if nil != err {
return errors.WithStack(err)
}
f.SetString(v)
case reflect.Int64, reflect.Uint64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
v, err := resp.Int64()
if nil != err {
return errors.WithStack(err)
}
f.SetInt(v)
case reflect.Float32, reflect.Float64:
v, err := resp.Float64()
if nil != err {
return errors.WithStack(err)
}
f.SetFloat(v)
case reflect.Bool:
v, err := resp.Bool()
if nil != err {
return errors.WithStack(err)
}
f.SetBool(v)
}
}
return nil
}