This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
values.go
194 lines (153 loc) · 4.63 KB
/
values.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
package table
import (
"fmt"
"io"
"reflect"
"strconv"
"strings"
"time"
semver "github.com/cppforlife/go-semi-semantic/version"
"github.com/dustin/go-humanize"
"gopkg.in/yaml.v2"
boshuifmt "github.com/cloudfoundry/bosh-cli/ui/fmt"
)
func NewValueString(s string) ValueString { return ValueString{S: s} }
func (t ValueString) String() string { return t.S }
func (t ValueString) Value() Value { return t }
func (t ValueString) Compare(other Value) int {
otherS := other.(ValueString).S
switch {
case t.S == otherS:
return 0
case t.S < otherS:
return -1
default:
return 1
}
}
func NewValueStrings(s []string) ValueStrings { return ValueStrings{S: s} }
func (t ValueStrings) String() string { return strings.Join(t.S, "\n") }
func (t ValueStrings) Value() Value { return t }
func (t ValueStrings) Compare(other Value) int {
otherS := other.(ValueStrings).S
switch {
case len(t.S) == len(otherS):
return 0
case len(t.S) < len(otherS):
return -1
default:
return 1
}
}
func NewValueInt(i int) ValueInt { return ValueInt{I: i} }
func (t ValueInt) String() string { return strconv.Itoa(t.I) }
func (t ValueInt) Value() Value { return t }
func (t ValueInt) Compare(other Value) int {
otherI := other.(ValueInt).I
switch {
case t.I == otherI:
return 0
case t.I < otherI:
return -1
default:
return 1
}
}
func NewValueBytes(i uint64) ValueBytes { return ValueBytes{I: i} }
func NewValueMegaBytes(i uint64) ValueBytes { return ValueBytes{I: i * 1024 * 1024} }
func (t ValueBytes) String() string { return humanize.Bytes(t.I) }
func (t ValueBytes) Value() Value { return t }
func (t ValueBytes) Compare(other Value) int {
otherI := other.(ValueBytes).I
switch {
case t.I == otherI:
return 0
case t.I < otherI:
return -1
default:
return 1
}
}
func NewValueTime(t time.Time) ValueTime { return ValueTime{T: t} }
func (t ValueTime) String() string { return t.T.Format(boshuifmt.TimeFullFmt) }
func (t ValueTime) Value() Value { return t }
func (t ValueTime) Compare(other Value) int {
otherT := other.(ValueTime).T
switch {
case t.T.Equal(otherT):
return 0
case t.T.Before(otherT):
return -1
default:
return 1
}
}
func NewValueBool(b bool) ValueBool { return ValueBool{B: b} }
func (t ValueBool) String() string { return fmt.Sprintf("%t", t.B) }
func (t ValueBool) Value() Value { return t }
func (t ValueBool) Compare(other Value) int {
otherB := other.(ValueBool).B
switch {
case t.B == otherB:
return 0
case t.B == false && otherB == true:
return -1
default:
return 1
}
}
func NewValueVersion(v semver.Version) ValueVersion { return ValueVersion{V: v} }
func (t ValueVersion) String() string { return t.V.String() }
func (t ValueVersion) Value() Value { return t }
func (t ValueVersion) Compare(other Value) int {
return t.V.Compare(other.(ValueVersion).V)
}
func NewValueError(e error) ValueError { return ValueError{E: e} }
func (t ValueError) String() string {
if t.E != nil {
return t.E.Error()
}
return ""
}
func NewValueInterface(i interface{}) ValueInterface { return ValueInterface{I: i} }
func (t ValueInterface) String() string {
if t.I == nil {
return ""
}
val := reflect.ValueOf(t.I)
if val.Kind() == reflect.Map && val.Len() == 0 {
return ""
}
bytes, err := yaml.Marshal(t.I)
if err != nil {
return fmt.Sprintf("<serialization error> : %#v", t.I)
}
return strings.TrimSpace(string(bytes))
}
func (t ValueInterface) Value() Value { return t }
func (t ValueInterface) Compare(other Value) int { panic("Never called") }
func (t ValueError) Value() Value { return t }
func (t ValueError) Compare(other Value) int { panic("Never called") }
func (t ValueNone) String() string { return "" }
func (t ValueNone) Value() Value { return t }
func (t ValueNone) Compare(other Value) int { panic("Never called") }
func NewValueFmt(v Value, error bool) ValueFmt { return ValueFmt{V: v, Error: error} }
func (t ValueFmt) String() string { return t.V.String() }
func (t ValueFmt) Value() Value { return t.V }
func (t ValueFmt) Compare(other Value) int { panic("Never called") }
func (t ValueFmt) Fprintf(w io.Writer, pattern string, rest ...interface{}) (int, error) {
if t.Func == nil {
return fmt.Fprintf(w, pattern, rest...)
}
return fmt.Fprintf(w, "%s", t.Func(pattern, rest...))
}
func NewValueSuffix(v Value, s string) ValueSuffix { return ValueSuffix{V: v, Suffix: s} }
func (t ValueSuffix) String() string {
str := t.V.String()
if len(str) > 0 {
return str + t.Suffix
}
return ""
}
func (t ValueSuffix) Value() Value { return t.V }
func (t ValueSuffix) Compare(other Value) int { panic("Never called") }