-
Notifications
You must be signed in to change notification settings - Fork 35
/
format.go
196 lines (174 loc) · 4.14 KB
/
format.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
package models
//go:generate enumer -type=Format -autotrimprefix -json
import (
"fmt"
"strconv"
"time"
"github.com/sleepinggenius2/gosmi/types"
)
type Format byte
const (
FormatNone Format = 0
FormatEnumName Format = 1 << iota
FormatEnumValue
FormatBits
FormatString
FormatUnits
FormatDurationShort
FormatAll Format = 0xff & ^FormatUnits
)
func ResolveFormat(formats []Format, defaultFormat ...Format) (format Format) {
if len(formats) == 0 {
if len(defaultFormat) == 0 {
return FormatAll
}
return defaultFormat[0]
}
for _, f := range formats {
format |= f
}
return
}
type Value struct {
Format Format
Formatted string
Raw interface{}
}
func (v Value) Bytes() []byte {
if b, ok := v.Raw.([]byte); ok {
return b
}
if s, ok := v.Raw.(string); ok {
return []byte(s)
}
return []byte{}
}
func (v Value) Duration() time.Duration {
if d, ok := v.Raw.(time.Duration); ok {
return d
}
return time.Duration(0)
}
func (v Value) Int64() int64 {
if i, ok := v.Raw.(int64); ok {
return i
}
return 0
}
func (v Value) Uint64() uint64 {
if i, ok := v.Raw.(int64); ok {
return uint64(i)
}
return 0
}
func (v Value) String() string {
if v.Format != FormatNone {
return v.Formatted
}
if v.Raw == nil {
return ""
}
switch r := v.Raw.(type) {
case string:
return r
case []byte:
return string(r)
}
return fmt.Sprintf("%v", v.Raw)
}
func ToInt64(value interface{}) (val int64, err error) {
switch value := value.(type) {
case int64:
val = value
case uint64:
val = int64(value)
case int:
val = int64(value)
case int8:
val = int64(value)
case int16:
val = int64(value)
case int32:
val = int64(value)
case uint:
val = int64(value)
case uint8:
val = int64(value)
case uint16:
val = int64(value)
case uint32:
val = int64(value)
case types.SmiSubId:
val = int64(value)
case string:
return strconv.ParseInt(value, 10, 64)
default:
err = fmt.Errorf("Value has invalid type: %T", value)
}
return
}
type ValueFormatter func(interface{}) Value
func (n Node) FormatValue(value interface{}, flags ...Format) Value {
return n.Type.FormatValue(value, flags...)
}
func (n Node) GetValueFormatter(flags ...Format) ValueFormatter {
return n.Type.GetValueFormatter(flags...)
}
func (n ScalarNode) FormatValue(value interface{}, flags ...Format) Value {
return n.Type.FormatValue(value, flags...)
}
func (n ScalarNode) GetValueFormatter(flags ...Format) ValueFormatter {
return n.Type.GetValueFormatter(flags...)
}
func (n ColumnNode) FormatValue(value interface{}, flags ...Format) Value {
return n.Type.FormatValue(value, flags...)
}
func (n ColumnNode) GetValueFormatter(flags ...Format) ValueFormatter {
return n.Type.GetValueFormatter(flags...)
}
func (t Type) FormatValue(value interface{}, flags ...Format) Value {
formatFlags := ResolveFormat(flags)
switch t.BaseType {
case types.BaseTypeOctetString:
switch t.Name {
case "IpAddress", "InetAddress", "IpV4orV6Addr":
return GetInetAddressFormatted(value, formatFlags)
}
return GetOctetStringFormatted(value, formatFlags, t.Format)
case types.BaseTypeBits:
if t.Enum == nil {
return GetBitsFormatted(value, formatFlags)
}
return GetEnumBitsFormatted(value, formatFlags, t.Enum)
case types.BaseTypeEnum:
return GetEnumFormatted(value, formatFlags, t.Enum)
}
switch t.Name {
case "TimeTicks", "TimeInterval", "TimeStamp":
return GetDurationFormatted(value, formatFlags)
}
return GetIntFormatted(value, formatFlags, t.Format)
}
func (t Type) GetValueFormatter(flags ...Format) ValueFormatter {
formatFlags := ResolveFormat(flags)
switch t.BaseType {
case types.BaseTypeOctetString:
switch t.Name {
case "IpAddress", "InetAddress", "IpV4orV6Addr":
return GetInetAddressFormatter(formatFlags)
}
return GetOctetStringFormatter(formatFlags, t.Format)
case types.BaseTypeBits:
if t.Enum == nil {
return GetBitsFormatter(formatFlags)
}
return GetEnumBitsFormatter(formatFlags, t.Enum)
case types.BaseTypeEnum:
return GetEnumFormatter(formatFlags, t.Enum)
}
switch t.Name {
case "TimeTicks", "TimeInterval", "TimeStamp":
return GetDurationFormatter(formatFlags)
}
return GetIntFormatter(formatFlags, t.Format)
}