| 1 |
// Copyright 2015 go-swagger maintainers |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package strfmt |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding" |
| 19 |
"encoding/json" |
| 20 |
"reflect" |
| 21 |
"strings" |
| 22 |
"sync" |
| 23 |
|
| 24 |
"k8s.io/kube-openapi/pkg/validation/errors" |
| 25 |
) |
| 26 |
|
| 27 |
// Default is the default formats registry |
| 28 |
var Default = NewSeededFormats(nil, nil) |
| 29 |
|
| 30 |
// Validator represents a validator for a string format. |
| 31 |
type Validator func(string) bool |
| 32 |
|
| 33 |
// Format represents a string format. |
| 34 |
// |
| 35 |
// All implementations of Format provide a string representation and text |
| 36 |
// marshaling/unmarshaling interface to be used by encoders (e.g. encoding/json). |
| 37 |
type Format interface { |
| 38 |
String() string |
| 39 |
encoding.TextMarshaler |
| 40 |
encoding.TextUnmarshaler |
| 41 |
} |
| 42 |
|
| 43 |
// Registry is a registry of string formats, with a validation method. |
| 44 |
type Registry interface { |
| 45 |
Add(string, Format, Validator) bool |
| 46 |
DelByName(string) bool |
| 47 |
GetType(string) (reflect.Type, bool) |
| 48 |
ContainsName(string) bool |
| 49 |
Validates(string, string) bool |
| 50 |
Parse(string, string) (interface{}, error) |
| 51 |
} |
| 52 |
|
| 53 |
type knownFormat struct { |
| 54 |
Name string |
| 55 |
OrigName string |
| 56 |
Type reflect.Type |
| 57 |
Validator Validator |
| 58 |
} |
| 59 |
|
| 60 |
// NameNormalizer is a function that normalizes a format name. |
| 61 |
type NameNormalizer func(string) string |
| 62 |
|
| 63 |
// DefaultNameNormalizer removes all dashes |
| 64 |
func DefaultNameNormalizer(name string) string { |
| 65 |
return strings.Replace(name, "-", "", -1) |
| 66 |
} |
| 67 |
|
| 68 |
type defaultFormats struct { |
| 69 |
sync.Mutex |
| 70 |
data []knownFormat |
| 71 |
normalizeName NameNormalizer |
| 72 |
} |
| 73 |
|
| 74 |
// NewFormats creates a new formats registry seeded with the values from the default |
| 75 |
func NewFormats() Registry { |
| 76 |
return NewSeededFormats(Default.(*defaultFormats).data, nil) |
| 77 |
} |
| 78 |
|
| 79 |
// NewSeededFormats creates a new formats registry |
| 80 |
func NewSeededFormats(seeds []knownFormat, normalizer NameNormalizer) Registry { |
| 81 |
if normalizer == nil { |
| 82 |
normalizer = DefaultNameNormalizer |
| 83 |
} |
| 84 |
// copy here, don't modify original |
| 85 |
d := append([]knownFormat(nil), seeds...) |
| 86 |
return &defaultFormats{ |
| 87 |
data: d, |
| 88 |
normalizeName: normalizer, |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// Add adds a new format, return true if this was a new item instead of a replacement |
| 93 |
func (f *defaultFormats) Add(name string, strfmt Format, validator Validator) bool { |
| 94 |
f.Lock() |
| 95 |
defer f.Unlock() |
| 96 |
|
| 97 |
nme := f.normalizeName(name) |
| 98 |
|
| 99 |
tpe := reflect.TypeOf(strfmt) |
| 100 |
if tpe.Kind() == reflect.Ptr { |
| 101 |
tpe = tpe.Elem() |
| 102 |
} |
| 103 |
|
| 104 |
for i := range f.data { |
| 105 |
v := &f.data[i] |
| 106 |
if v.Name == nme { |
| 107 |
v.Type = tpe |
| 108 |
v.Validator = validator |
| 109 |
return false |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// turns out it's new after all |
| 114 |
f.data = append(f.data, knownFormat{Name: nme, OrigName: name, Type: tpe, Validator: validator}) |
| 115 |
return true |
| 116 |
} |
| 117 |
|
| 118 |
// GetType gets the type for the specified name |
| 119 |
func (f *defaultFormats) GetType(name string) (reflect.Type, bool) { |
| 120 |
f.Lock() |
| 121 |
defer f.Unlock() |
| 122 |
nme := f.normalizeName(name) |
| 123 |
for _, v := range f.data { |
| 124 |
if v.Name == nme { |
| 125 |
return v.Type, true |
| 126 |
} |
| 127 |
} |
| 128 |
return nil, false |
| 129 |
} |
| 130 |
|
| 131 |
// DelByName removes the format by the specified name, returns true when an item was actually removed |
| 132 |
func (f *defaultFormats) DelByName(name string) bool { |
| 133 |
f.Lock() |
| 134 |
defer f.Unlock() |
| 135 |
|
| 136 |
nme := f.normalizeName(name) |
| 137 |
|
| 138 |
for i, v := range f.data { |
| 139 |
if v.Name == nme { |
| 140 |
f.data[i] = knownFormat{} // release |
| 141 |
f.data = append(f.data[:i], f.data[i+1:]...) |
| 142 |
return true |
| 143 |
} |
| 144 |
} |
| 145 |
return false |
| 146 |
} |
| 147 |
|
| 148 |
// DelByFormat removes the specified format, returns true when an item was actually removed |
| 149 |
func (f *defaultFormats) DelByFormat(strfmt Format) bool { |
| 150 |
f.Lock() |
| 151 |
defer f.Unlock() |
| 152 |
|
| 153 |
tpe := reflect.TypeOf(strfmt) |
| 154 |
if tpe.Kind() == reflect.Ptr { |
| 155 |
tpe = tpe.Elem() |
| 156 |
} |
| 157 |
|
| 158 |
for i, v := range f.data { |
| 159 |
if v.Type == tpe { |
| 160 |
f.data[i] = knownFormat{} // release |
| 161 |
f.data = append(f.data[:i], f.data[i+1:]...) |
| 162 |
return true |
| 163 |
} |
| 164 |
} |
| 165 |
return false |
| 166 |
} |
| 167 |
|
| 168 |
// ContainsName returns true if this registry contains the specified name |
| 169 |
func (f *defaultFormats) ContainsName(name string) bool { |
| 170 |
f.Lock() |
| 171 |
defer f.Unlock() |
| 172 |
nme := f.normalizeName(name) |
| 173 |
for _, v := range f.data { |
| 174 |
if v.Name == nme { |
| 175 |
return true |
| 176 |
} |
| 177 |
} |
| 178 |
return false |
| 179 |
} |
| 180 |
|
| 181 |
// ContainsFormat returns true if this registry contains the specified format |
| 182 |
func (f *defaultFormats) ContainsFormat(strfmt Format) bool { |
| 183 |
f.Lock() |
| 184 |
defer f.Unlock() |
| 185 |
tpe := reflect.TypeOf(strfmt) |
| 186 |
if tpe.Kind() == reflect.Ptr { |
| 187 |
tpe = tpe.Elem() |
| 188 |
} |
| 189 |
|
| 190 |
for _, v := range f.data { |
| 191 |
if v.Type == tpe { |
| 192 |
return true |
| 193 |
} |
| 194 |
} |
| 195 |
return false |
| 196 |
} |
| 197 |
|
| 198 |
// Validates passed data against format. |
| 199 |
// |
| 200 |
// Note that the format name is automatically normalized, e.g. one may |
| 201 |
// use "date-time" to use the "datetime" format validator. |
| 202 |
func (f *defaultFormats) Validates(name, data string) bool { |
| 203 |
f.Lock() |
| 204 |
defer f.Unlock() |
| 205 |
nme := f.normalizeName(name) |
| 206 |
for _, v := range f.data { |
| 207 |
if v.Name == nme { |
| 208 |
return v.Validator(data) |
| 209 |
} |
| 210 |
} |
| 211 |
return false |
| 212 |
} |
| 213 |
|
| 214 |
// Parse a string into the appropriate format representation type. |
| 215 |
// |
| 216 |
// E.g. parsing a string a "date" will return a Date type. |
| 217 |
func (f *defaultFormats) Parse(name, data string) (interface{}, error) { |
| 218 |
f.Lock() |
| 219 |
defer f.Unlock() |
| 220 |
nme := f.normalizeName(name) |
| 221 |
for _, v := range f.data { |
| 222 |
if v.Name == nme { |
| 223 |
nw := reflect.New(v.Type).Interface() |
| 224 |
if dec, ok := nw.(encoding.TextUnmarshaler); ok { |
| 225 |
if err := dec.UnmarshalText([]byte(data)); err != nil { |
| 226 |
return nil, err |
| 227 |
} |
| 228 |
return nw, nil |
| 229 |
} |
| 230 |
return nil, errors.InvalidTypeName(name) |
| 231 |
} |
| 232 |
} |
| 233 |
return nil, errors.InvalidTypeName(name) |
| 234 |
} |
| 235 |
|
| 236 |
// unmarshalJSON provides a generic implementation of json.Unmarshaler interface's UnmarshalJSON function for basic string formats. |
| 237 |
func unmarshalJSON[T ~string](r *T, data []byte) error { |
| 238 |
if string(data) == jsonNull { |
| 239 |
return nil |
| 240 |
} |
| 241 |
var ustr string |
| 242 |
if err := json.Unmarshal(data, &ustr); err != nil { |
| 243 |
return err |
| 244 |
} |
| 245 |
*r = T(ustr) |
| 246 |
return nil |
| 247 |
} |
| 248 |
|
| 249 |
// deepCopy provides a generic implementation of DeepCopy for basic string formats. |
| 250 |
func deepCopy[T ~string](r *T) *T { |
| 251 |
if r == nil { |
| 252 |
return nil |
| 253 |
} |
| 254 |
out := new(T) |
| 255 |
*out = *r |
| 256 |
return out |
| 257 |
} |