forked from shogo82148/androidbinary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
327 lines (292 loc) · 6.68 KB
/
type.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package androidbinary
import (
"encoding/xml"
"fmt"
"reflect"
"strconv"
)
type injector interface {
inject(table *TableFile, config *ResTableConfig)
}
var injectorType = reflect.TypeOf((*injector)(nil)).Elem()
func inject(val reflect.Value, table *TableFile, config *ResTableConfig) {
if val.Kind() == reflect.Ptr {
if val.IsNil() {
return
}
val = val.Elem()
}
if val.CanInterface() && val.Type().Implements(injectorType) {
val.Interface().(injector).inject(table, config)
return
}
if val.CanAddr() {
pv := val.Addr()
if pv.CanInterface() && pv.Type().Implements(injectorType) {
pv.Interface().(injector).inject(table, config)
return
}
}
switch val.Kind() {
default:
// ignore other types
return
case reflect.Slice, reflect.Array:
l := val.Len()
for i := 0; i < l; i++ {
inject(val.Index(i), table, config)
}
return
case reflect.Struct:
l := val.NumField()
for i := 0; i < l; i++ {
inject(val.Field(i), table, config)
}
}
}
// Bool is a boolean value in XML file.
// It may be an immediate value or a reference.
type Bool struct {
value string
table *TableFile
config *ResTableConfig
}
// WithTableFile ties TableFile to the Bool.
func (v Bool) WithTableFile(table *TableFile) Bool {
return Bool{
value: v.value,
table: table,
config: v.config,
}
}
// WithResTableConfig ties ResTableConfig to the Bool.
func (v Bool) WithResTableConfig(config *ResTableConfig) Bool {
return Bool{
value: v.value,
table: v.table,
config: config,
}
}
func (v *Bool) inject(table *TableFile, config *ResTableConfig) {
v.table = table
v.config = config
}
// SetBool sets a boolean value.
func (v *Bool) SetBool(value bool) {
v.value = strconv.FormatBool(value)
}
// SetResID sets a boolean value with the resource id.
func (v *Bool) SetResID(resID ResID) {
v.value = resID.String()
}
// UnmarshalXMLAttr implements xml.UnmarshalerAttr.
func (v *Bool) UnmarshalXMLAttr(attr xml.Attr) error {
v.value = attr.Value
return nil
}
// MarshalXMLAttr implements xml.MarshalerAttr.
func (v Bool) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
if v.value == "" {
// return the zero value of bool
return xml.Attr{
Name: name,
Value: "false",
}, nil
}
return xml.Attr{
Name: name,
Value: v.value,
}, nil
}
// Bool returns the boolean value.
// It resolves the reference if needed.
func (v Bool) Bool() (bool, error) {
if v.value == "" {
return false, nil
}
if !IsResID(v.value) {
return strconv.ParseBool(v.value)
}
id, err := ParseResID(v.value)
if err != nil {
return false, err
}
value, err := v.table.GetResource(id, v.config)
if err != nil {
return false, err
}
ret, ok := value.(bool)
if !ok {
return false, fmt.Errorf("invalid type: %T", value)
}
return ret, nil
}
// MustBool is same as Bool, but it panics if it fails to parse the value.
func (v Bool) MustBool() bool {
ret, err := v.Bool()
if err != nil {
panic(err)
}
return ret
}
// Int32 is an integer value in XML file.
// It may be an immediate value or a reference.
type Int32 struct {
value string
table *TableFile
config *ResTableConfig
}
// WithTableFile ties TableFile to the Bool.
func (v Int32) WithTableFile(table *TableFile) Int32 {
return Int32{
value: v.value,
table: table,
config: v.config,
}
}
// WithResTableConfig ties ResTableConfig to the Bool.
func (v Int32) WithResTableConfig(config *ResTableConfig) Bool {
return Bool{
value: v.value,
table: v.table,
config: config,
}
}
func (v *Int32) inject(table *TableFile, config *ResTableConfig) {
v.table = table
v.config = config
}
// SetInt32 sets an integer value.
func (v *Int32) SetInt32(value int32) {
v.value = strconv.FormatInt(int64(value), 10)
}
// SetResID sets a boolean value with the resource id.
func (v *Int32) SetResID(resID ResID) {
v.value = resID.String()
}
// UnmarshalXMLAttr implements xml.UnmarshalerAttr.
func (v *Int32) UnmarshalXMLAttr(attr xml.Attr) error {
v.value = attr.Value
return nil
}
// MarshalXMLAttr implements xml.MarshalerAttr.
func (v Int32) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
if v.value == "" {
// return the zero value of int32
return xml.Attr{
Name: name,
Value: "0",
}, nil
}
return xml.Attr{
Name: name,
Value: v.value,
}, nil
}
// Int32 returns the integer value.
// It resolves the reference if needed.
func (v Int32) Int32() (int32, error) {
if v.value == "" {
return 0, nil
}
if !IsResID(v.value) {
v, err := strconv.ParseInt(v.value, 10, 32)
return int32(v), err
}
id, err := ParseResID(v.value)
if err != nil {
return 0, err
}
value, err := v.table.GetResource(id, v.config)
if err != nil {
return 0, err
}
ret, ok := value.(uint32)
if !ok {
return 0, fmt.Errorf("invalid type: %T", value)
}
return int32(ret), nil
}
// MustInt32 is same as Int32, but it panics if it fails to parse the value.
func (v Int32) MustInt32() int32 {
ret, err := v.Int32()
if err != nil {
panic(err)
}
return ret
}
// String is a boolean value in XML file.
// It may be an immediate value or a reference.
type String struct {
value string
table *TableFile
config *ResTableConfig
}
// WithTableFile ties TableFile to the Bool.
func (v String) WithTableFile(table *TableFile) String {
return String{
value: v.value,
table: table,
config: v.config,
}
}
// WithResTableConfig ties ResTableConfig to the Bool.
func (v String) WithResTableConfig(config *ResTableConfig) String {
return String{
value: v.value,
table: v.table,
config: config,
}
}
func (v *String) inject(table *TableFile, config *ResTableConfig) {
v.table = table
v.config = config
}
// SetString sets a string value.
func (v *String) SetString(value string) {
v.value = value
}
// SetResID sets a boolean value with the resource id.
func (v *String) SetResID(resID ResID) {
v.value = resID.String()
}
// UnmarshalXMLAttr implements xml.UnmarshalerAttr.
func (v *String) UnmarshalXMLAttr(attr xml.Attr) error {
v.value = attr.Value
return nil
}
// MarshalXMLAttr implements xml.MarshalerAttr.
func (v String) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return xml.Attr{
Name: name,
Value: v.value,
}, nil
}
// String returns the string value.
// It resolves the reference if needed.
func (v String) String() (string, error) {
if !IsResID(v.value) {
return v.value, nil
}
id, err := ParseResID(v.value)
if err != nil {
return "", err
}
value, err := v.table.GetResource(id, v.config)
if err != nil {
return "", err
}
ret, ok := value.(string)
if !ok {
return "", fmt.Errorf("invalid type: %T", value)
}
return ret, nil
}
// MustString is same as String, but it panics if it fails to parse the value.
func (v String) MustString() string {
ret, err := v.String()
if err != nil {
panic(err)
}
return ret
}