-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
stringorslice.go
110 lines (95 loc) · 2.68 KB
/
stringorslice.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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package stringorslice
import (
"encoding/json"
"strings"
)
// StringOrSlice is a type that holds a []string, but marshals to a []string or a string.
type StringOrSlice struct {
values []string
forceEncodeAsArray bool
}
func (s *StringOrSlice) IsEmpty() bool {
return len(s.values) == 0
}
// Slice will build a value that marshals to a JSON array
func Slice(v []string) StringOrSlice {
return StringOrSlice{values: v, forceEncodeAsArray: true}
}
// Of will build a value that marshals to a JSON array if len(v) > 1,
// otherwise to a single string
func Of(v ...string) StringOrSlice {
if v == nil {
v = []string{}
}
return StringOrSlice{values: v}
}
// String will build a value that marshals to a single string
func String(v string) StringOrSlice {
return StringOrSlice{values: []string{v}, forceEncodeAsArray: false}
}
// UnmarshalJSON implements the json.Unmarshaller interface.
func (s *StringOrSlice) UnmarshalJSON(value []byte) error {
if value[0] == '[' {
s.forceEncodeAsArray = true
if err := json.Unmarshal(value, &s.values); err != nil {
return nil
}
return nil
}
s.forceEncodeAsArray = false
var stringValue string
if err := json.Unmarshal(value, &stringValue); err != nil {
return err
}
s.values = []string{stringValue}
return nil
}
// String returns the string value, or the Itoa of the int value.
func (s StringOrSlice) String() string {
return strings.Join(s.values, ",")
}
func (v *StringOrSlice) Value() []string {
return v.values
}
func (l StringOrSlice) Equal(r StringOrSlice) bool {
if len(l.values) != len(r.values) {
return false
}
for i := 0; i < len(l.values); i++ {
if l.values[i] != r.values[i] {
return false
}
}
return true
}
// MarshalJSON implements the json.Marshaller interface.
func (v StringOrSlice) MarshalJSON() ([]byte, error) {
encodeAsJSONArray := v.forceEncodeAsArray
if len(v.values) > 1 {
encodeAsJSONArray = true
}
values := v.values
if values == nil {
values = []string{}
}
if encodeAsJSONArray {
return json.Marshal(values)
} else if len(v.values) == 1 {
s := v.values[0]
return json.Marshal(&s)
} else {
return json.Marshal(values)
}
}