-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdecode.go
More file actions
90 lines (77 loc) · 2.05 KB
/
decode.go
File metadata and controls
90 lines (77 loc) · 2.05 KB
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
package rtgo
import "fmt"
import "reflect"
import "strconv"
import "strings"
import "time"
func UnmarshalShort(dataStr string, v interface{}) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return fmt.Errorf("cannot unmarshal into type %v, not a pointer", reflect.TypeOf(v))
}
el := rv.Elem()
if el.Kind() != reflect.Struct {
return fmt.Errorf("cannot unmarshal into non-struct pointer")
}
elType := el.Type()
// map from names to values that we should assign
values := make(map[string]reflect.Value)
for i := 0; i < el.NumField(); i++ {
structField := elType.Field(i)
name := structField.Tag.Get("rt")
if name == "" {
name = structField.Name
}
values[name] = el.Field(i)
}
// convert data string to string key-value map
data := make(map[string]string)
var lastKey string
for _, line := range strings.Split(dataStr, "\n") {
if len(line) == 0 {
continue
} else if line[0] == ' ' && lastKey != "" {
// multiline values are prefixed with a space
data[lastKey] += "\n" + strings.TrimSpace(line)
}
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 {
return fmt.Errorf("data has line without colon: %s", line)
}
lastKey = parts[0]
if len(parts[1]) > 0 && parts[1][0] == ' ' {
data[lastKey] = parts[1][1:]
} else {
data[lastKey] = parts[1]
}
}
// convert values to struct field
for key, value := range data {
if value == "" || value == "Not set" {
continue
}
rv, ok := values[key]
if !ok {
continue
}
rvType := rv.Type()
if rvType.PkgPath() == "time" && rvType.Name() == "Time" {
t, err := time.Parse("Mon Jan 2 15:04:05 2006", value)
if err == nil {
rv.Set(reflect.ValueOf(t))
} else {
return fmt.Errorf("failed to decode %s as time: %v", value, err)
}
} else if rv.Kind() == reflect.String {
rv.SetString(value)
} else if rv.Kind() == reflect.Int {
x, err := strconv.Atoi(value)
if err == nil {
rv.SetInt(int64(x))
} else {
return fmt.Errorf("failed to decode %s as int: %v", value, err)
}
}
}
return nil
}