/
int_hash.go
193 lines (169 loc) · 3.86 KB
/
int_hash.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
package types
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/appscode/go/types"
)
/*
IntHash represents as int64 Generation and string Hash. It is json serialized into <int64>$<hash_string>.
*/
// +k8s:openapi-gen=true
type IntHash struct {
generation int64
hash string
}
func ParseIntHash(v interface{}) (*IntHash, error) {
switch m := v.(type) {
case nil:
return &IntHash{}, nil
case int:
return &IntHash{generation: int64(m)}, nil
case int64:
return &IntHash{generation: m}, nil
case *int64:
return &IntHash{generation: types.Int64(m)}, nil
case IntHash:
return &m, nil
case *IntHash:
return m, nil
case string:
return parseStringIntoIntHash(m)
case *string:
return parseStringIntoIntHash(types.String(m))
default:
return nil, fmt.Errorf("failed to parse type %s into IntHash", reflect.TypeOf(v).String())
}
}
func parseStringIntoIntHash(s string) (*IntHash, error) {
if s == "" {
return &IntHash{}, nil
}
idx := strings.IndexRune(s, '$')
switch {
case idx <= 0:
return nil, errors.New("missing generation")
case idx == len(s)-1:
return nil, errors.New("missing hash")
default:
i, err := strconv.ParseInt(s[:idx], 10, 64)
if err != nil {
return nil, err
}
h := s[idx+1:]
return &IntHash{generation: i, hash: h}, nil
}
}
func NewIntHash(i int64, h string) *IntHash { return &IntHash{generation: i, hash: h} }
func IntHashForGeneration(i int64) *IntHash { return &IntHash{generation: i} }
func IntHashForHash(h string) *IntHash { return &IntHash{hash: h} }
func (m IntHash) Generation() int64 {
return m.generation
}
func (m IntHash) Hash() string {
return m.hash
}
// IsZero returns true if the value is nil or time is zero.
func (m *IntHash) IsZero() bool {
if m == nil {
return true
}
return m.generation == 0 && m.hash == ""
}
func (m *IntHash) Equal(u *IntHash) bool {
if m == nil {
return u == nil
}
if u == nil { // t != nil
return false
}
if m == u {
return true
}
if m.generation == u.generation {
return m.hash == u.hash
}
return false
}
func (m *IntHash) MatchGeneration(u *IntHash) bool {
if m == nil {
return u == nil
}
if u == nil { // t != nil
return false
}
if m == u {
return true
}
return m.generation == u.generation
}
func (m *IntHash) DeepCopyInto(out *IntHash) {
*out = *m
}
func (m *IntHash) DeepCopy() *IntHash {
if m == nil {
return nil
}
out := new(IntHash)
m.DeepCopyInto(out)
return out
}
func (m IntHash) String() string {
return fmt.Sprintf(`%d$%s`, m.generation, m.hash)
}
func (m *IntHash) MarshalJSON() ([]byte, error) {
if m == nil {
return nil, nil
}
if m.hash == "" {
return json.Marshal(m.generation)
}
return json.Marshal(m.String())
}
func (m *IntHash) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("jsontypes.IntHash: UnmarshalJSON on nil pointer")
}
if data[0] == '"' {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
ih, err := ParseIntHash(s)
if err != nil {
return err
}
*m = *ih
return nil
} else if bytes.Equal(data, []byte("null")) {
return nil
}
var i int64
err := json.Unmarshal(data, &i)
if err != nil {
return err
}
m.generation = i
return nil
}
// OpenAPISchemaType is used by the kube-openapi generator when constructing
// the OpenAPI spec of this type.
//
// See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators
func (_ IntHash) OpenAPISchemaType() []string { return []string{"string"} }
// OpenAPISchemaFormat is used by the kube-openapi generator when constructing
// the OpenAPI spec of this type.
func (_ IntHash) OpenAPISchemaFormat() string { return "" }
// MarshalQueryParameter converts to a URL query parameter value
func (m IntHash) MarshalQueryParameter() (string, error) {
if m.IsZero() {
// Encode unset/nil objects as an empty string
return "", nil
}
return m.String(), nil
}