/
message.go
134 lines (120 loc) · 3.22 KB
/
message.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
package s3apt
import (
"errors"
"fmt"
"gopkg.in/oleiade/reflections.v1"
"os"
"reflect"
"strconv"
"time"
)
var messageCodes = map[int]string{
100: "Capabilities",
102: "Status",
200: "URI Start",
201: "URI Done",
400: "URI Failure",
401: "General Failure",
600: "URI Acquire",
601: "Configuration",
}
type Message struct {
Capability int
capabilityMessage string
FailIgnore bool `output:"Fail-Ignore"`
Filename string `output:"Filename"`
IndexFile bool `output:"Index-File"`
LastModified *time.Time `output:"Last-Modified"`
MD5Hash string `output:"MD5-Hash"`
MD5SumHash string `output:"MD5Sum-Hash"`
SHA256Hash string `output:"SHA256-Hash"`
SHA512Hash string `output:"SHA512-Hash"`
Message string `output:"Message"`
SingleInstance bool `output:"Single-Instance"`
Size int `output:"Size"`
Uri string `output:"URI"`
Version string `output:"Version"`
}
func (m *Message) Set(key string, value string) (err error) {
structTags, _ := reflections.Tags(m, "output")
for k, o := range structTags {
if o == key {
key = k
break
}
}
if ok, err := reflections.HasField(m, key); err != nil || !ok {
return fmt.Errorf("Invalid Key (%s) for Message structure", key)
}
kind, _ := reflections.GetFieldKind(m, key)
err = fmt.Errorf("Unexpected type %s for SetField", kind)
switch kind {
case reflect.String:
err = reflections.SetField(m, key, value)
case reflect.Bool:
var b bool
b, err = strconv.ParseBool(value)
err = reflections.SetField(m, key, b)
case reflect.Int:
var i int
i, err = strconv.Atoi(value)
err = reflections.SetField(m, key, i)
}
return err
}
func (m *Message) Output() (output string, err error) {
if m.Capability == 0 {
return "", errors.New("Capability not defined")
}
ok := false
if m.capabilityMessage, ok = messageCodes[m.Capability]; !ok {
return "", fmt.Errorf("Invalid message capability (%d)", m.Capability)
}
output += fmt.Sprintf("%d %s\n", m.Capability, m.capabilityMessage)
structTags, _ := reflections.Tags(m, "output")
for k, o := range structTags {
if o == "" {
continue
}
kind, _ := reflections.GetFieldKind(m, k)
switch kind {
case reflect.String:
v, err := reflections.GetField(m, k)
if err != nil {
return "", err
}
if v.(string) != "" {
output += fmt.Sprintf("%s: %s\n", o, v.(string))
}
case reflect.Bool:
v, err := reflections.GetField(m, k)
if err != nil {
return "", err
}
if v.(bool) {
output += fmt.Sprintf("%s: true\n", o)
}
case reflect.Int:
v, err := reflections.GetField(m, k)
if err != nil {
return "", err
}
if v.(int) != 0 {
output += fmt.Sprintf("%s: %d\n", o, v)
}
case reflect.Ptr:
ptrValue, _ := reflections.GetField(m, k)
switch v := ptrValue.(type) {
case *time.Time:
if v != nil {
output += fmt.Sprintf("%s: %s\n", o, v.Format(time.RFC1123))
}
default:
fmt.Fprintf(os.Stderr, "Unrecognised Messsage Field Ptr Kind for %s: %v\n", k, kind)
}
default:
fmt.Fprintf(os.Stderr, "Unrecognised Messsage Field Kind for %s: %v\n", k, kind)
}
}
return output, err
}