| 1 |
/* |
| 2 |
Copyright 2025 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package kyaml |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"strconv" |
| 22 |
"strings" |
| 23 |
"testing" |
| 24 |
"time" |
| 25 |
|
| 26 |
"github.com/google/go-cmp/cmp" |
| 27 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 28 |
yamlv3 "go.yaml.in/yaml/v3" |
| 29 |
"sigs.k8s.io/randfill" |
| 30 |
"sigs.k8s.io/yaml" |
| 31 |
) |
| 32 |
|
| 33 |
// Return the input string with the longest common leading whitespace removed |
| 34 |
// from each line. |
| 35 |
func dedent(in string) string { |
| 36 |
lines := strings.Split(in, "\n") |
| 37 |
pfx := "" |
| 38 |
started := false |
| 39 |
for _, line := range lines { |
| 40 |
if len(line) == 0 { |
| 41 |
continue |
| 42 |
} |
| 43 |
if !started { |
| 44 |
pfx = leadingWhitespace(line) |
| 45 |
started = true |
| 46 |
continue |
| 47 |
} |
| 48 |
if strings.HasPrefix(line, pfx) { |
| 49 |
continue |
| 50 |
} |
| 51 |
pfx = commonPrefix(pfx, line) |
| 52 |
} |
| 53 |
if pfx == "" { |
| 54 |
return in |
| 55 |
} |
| 56 |
for i, line := range lines { |
| 57 |
lines[i] = strings.TrimPrefix(line, pfx) |
| 58 |
} |
| 59 |
return strings.Join(lines, "\n") |
| 60 |
} |
| 61 |
|
| 62 |
func leadingWhitespace(s string) string { |
| 63 |
if len(s) == 0 { |
| 64 |
return "" |
| 65 |
} |
| 66 |
for i, r := range s { |
| 67 |
if r != ' ' && r != '\t' { |
| 68 |
return s[:i] |
| 69 |
} |
| 70 |
} |
| 71 |
return s // all whitespace |
| 72 |
} |
| 73 |
|
| 74 |
func commonPrefix(a, b string) string { |
| 75 |
minLen := len(a) |
| 76 |
if len(b) < minLen { |
| 77 |
minLen = len(b) |
| 78 |
} |
| 79 |
for i := 0; i < minLen; i++ { |
| 80 |
if a[i] != b[i] { |
| 81 |
return a[:i] |
| 82 |
} |
| 83 |
} |
| 84 |
return a[:minLen] |
| 85 |
} |
| 86 |
|
| 87 |
// The following types are intended to provide robust coverage for testing. |
| 88 |
|
| 89 |
type AllTypesStruct struct { |
| 90 |
SimpleStruct1 `json:","` // embedded unnamed |
| 91 |
SimpleStruct2 `json:"named,"` // embedded named |
| 92 |
|
| 93 |
// Omitted fields (`json:"-"`) are handled in a different test |
| 94 |
NamedDash SimpleStruct4 `json:"-,"` |
| 95 |
|
| 96 |
Plain PlainStruct `json:"plain"` |
| 97 |
OmitEmpty OmitEmptyStruct `json:"omitEmpty"` |
| 98 |
OmitZero OmitZeroStruct `json:"omitZero"` |
| 99 |
OmitEmptyAndZero OmitEmptyAndZeroStruct `json:"omitEmptyAndZero"` |
| 100 |
} |
| 101 |
|
| 102 |
type SimpleStruct struct { |
| 103 |
String string `json:"string"` |
| 104 |
Bool bool `json:"bool"` |
| 105 |
Int int `json:"int"` |
| 106 |
} |
| 107 |
type SimpleStruct1 SimpleStruct |
| 108 |
type SimpleStruct2 SimpleStruct |
| 109 |
type SimpleStruct3 SimpleStruct |
| 110 |
type SimpleStruct4 SimpleStruct |
| 111 |
|
| 112 |
type PlainStruct struct { |
| 113 |
// Basic types |
| 114 |
String string `json:"string"` |
| 115 |
Bool bool `json:"bool"` |
| 116 |
Int int `json:"int"` |
| 117 |
Int8 int8 `json:"int8"` |
| 118 |
Int16 int16 `json:"int16"` |
| 119 |
Int32 int32 `json:"int32"` |
| 120 |
Int64 int64 `json:"int64"` |
| 121 |
Uint uint `json:"uint"` |
| 122 |
Uint8 uint8 `json:"uint8"` |
| 123 |
Uint16 uint16 `json:"uint16"` |
| 124 |
Uint32 uint32 `json:"uint32"` |
| 125 |
Uint64 uint64 `json:"uint64"` |
| 126 |
Float32 float32 `json:"float32"` |
| 127 |
Float64 float64 `json:"float64"` |
| 128 |
Time time.Time `json:"time"` |
| 129 |
Bytes []byte `json:"bytes"` |
| 130 |
|
| 131 |
// Pointers to basic types |
| 132 |
StringPtr *string `json:"stringPtr"` |
| 133 |
BoolPtr *bool `json:"boolPtr"` |
| 134 |
IntPtr *int `json:"intPtr"` |
| 135 |
Int8Ptr *int8 `json:"int8Ptr"` |
| 136 |
Int16Ptr *int16 `json:"int16Ptr"` |
| 137 |
Int32Ptr *int32 `json:"int32Ptr"` |
| 138 |
Int64Ptr *int64 `json:"int64Ptr"` |
| 139 |
UintPtr *uint `json:"uintPtr"` |
| 140 |
Uint8Ptr *uint8 `json:"uint8Ptr"` |
| 141 |
Uint16Ptr *uint16 `json:"uint16Ptr"` |
| 142 |
Uint32Ptr *uint32 `json:"uint32Ptr"` |
| 143 |
Uint64Ptr *uint64 `json:"uint64Ptr"` |
| 144 |
Float32Ptr *float32 `json:"float32Ptr"` |
| 145 |
Float64Ptr *float64 `json:"float64Ptr"` |
| 146 |
TimePtr *time.Time `json:"timePtr,omitempty"` // Time implements json.Marshaler, but on a value receiver |
| 147 |
|
| 148 |
// Slices of basic types |
| 149 |
StringSlice []string `json:"stringSlice"` |
| 150 |
BoolSlice []bool `json:"boolSlice"` |
| 151 |
IntSlice []int `json:"intSlice"` |
| 152 |
Int8Slice []int8 `json:"int8Slice"` |
| 153 |
Int16Slice []int16 `json:"int16Slice"` |
| 154 |
Int32Slice []int32 `json:"int32Slice"` |
| 155 |
Int64Slice []int64 `json:"int64Slice"` |
| 156 |
UintSlice []uint `json:"uintSlice"` |
| 157 |
Uint8Slice []uint8 `json:"uint8Slice"` |
| 158 |
Uint16Slice []uint16 `json:"uint16Slice"` |
| 159 |
Uint32Slice []uint32 `json:"uint32Slice"` |
| 160 |
Uint64Slice []uint64 `json:"uint64Slice"` |
| 161 |
Float32Slice []float32 `json:"float32Slice"` |
| 162 |
Float64Slice []float64 `json:"float64Slice"` |
| 163 |
TimeSlice []time.Time `json:"timeSlice"` |
| 164 |
|
| 165 |
// Maps of string to basic types |
| 166 |
StringMap map[string]string `json:"stringMap"` |
| 167 |
BoolMap map[string]bool `json:"boolMap"` |
| 168 |
IntMap map[string]int `json:"intMap"` |
| 169 |
Int8Map map[string]int8 `json:"int8Map"` |
| 170 |
Int16Map map[string]int16 `json:"int16Map"` |
| 171 |
Int32Map map[string]int32 `json:"int32Map"` |
| 172 |
Int64Map map[string]int64 `json:"int64Map"` |
| 173 |
UintMap map[string]uint `json:"uintMap"` |
| 174 |
Uint8Map map[string]uint8 `json:"uint8Map"` |
| 175 |
Uint16Map map[string]uint16 `json:"uint16Map"` |
| 176 |
Uint32Map map[string]uint32 `json:"uint32Map"` |
| 177 |
Uint64Map map[string]uint64 `json:"uint64Map"` |
| 178 |
Float32Map map[string]float32 `json:"float32Map"` |
| 179 |
Float64Map map[string]float64 `json:"float64Map"` |
| 180 |
TimeMap map[string]time.Time `json:"timeMap"` |
| 181 |
|
| 182 |
// Slice of slices |
| 183 |
StringSliceSlice [][]string `json:"stringSliceSlice"` |
| 184 |
BoolSliceSlice [][]bool `json:"boolSliceSlice"` |
| 185 |
IntSliceSlice [][]int `json:"intSliceSlice"` |
| 186 |
Int8SliceSlice [][]int8 `json:"int8SliceSlice"` |
| 187 |
Int16SliceSlice [][]int16 `json:"int16SliceSlice"` |
| 188 |
Int32SliceSlice [][]int32 `json:"int32SliceSlice"` |
| 189 |
Int64SliceSlice [][]int64 `json:"int64SliceSlice"` |
| 190 |
UintSliceSlice [][]uint `json:"uintSliceSlice"` |
| 191 |
Uint8SliceSlice [][]uint8 `json:"uint8SliceSlice"` |
| 192 |
Uint16SliceSlice [][]uint16 `json:"uint16SliceSlice"` |
| 193 |
Uint32SliceSlice [][]uint32 `json:"uint32SliceSlice"` |
| 194 |
Uint64SliceSlice [][]uint64 `json:"uint64SliceSlice"` |
| 195 |
Float32SliceSlice [][]float32 `json:"float32SliceSlice"` |
| 196 |
Float64SliceSlice [][]float64 `json:"float64SliceSlice"` |
| 197 |
TimeSliceSlice [][]time.Time `json:"timeSliceSlice"` |
| 198 |
|
| 199 |
// Slice of maps |
| 200 |
StringSliceMap []map[string]string `json:"stringSliceMap"` |
| 201 |
BoolSliceMap []map[string]bool `json:"boolSliceMap"` |
| 202 |
IntSliceMap []map[string]int `json:"intSliceMap"` |
| 203 |
Int8SliceMap []map[string]int8 `json:"int8SliceMap"` |
| 204 |
Int16SliceMap []map[string]int16 `json:"int16SliceMap"` |
| 205 |
Int32SliceMap []map[string]int32 `json:"int32SliceMap"` |
| 206 |
Int64SliceMap []map[string]int64 `json:"int64SliceMap"` |
| 207 |
UintSliceMap []map[string]uint `json:"uintSliceMap"` |
| 208 |
Uint8SliceMap []map[string]uint8 `json:"uint8SliceMap"` |
| 209 |
Uint16SliceMap []map[string]uint16 `json:"uint16SliceMap"` |
| 210 |
Uint32SliceMap []map[string]uint32 `json:"uint32SliceMap"` |
| 211 |
Uint64SliceMap []map[string]uint64 `json:"uint64SliceMap"` |
| 212 |
Float32SliceMap []map[string]float32 `json:"float32SliceMap"` |
| 213 |
Float64SliceMap []map[string]float64 `json:"float64SliceMap"` |
| 214 |
TimeSliceMap []map[string]time.Time `json:"timeSliceMap"` |
| 215 |
|
| 216 |
// Map of string to slices |
| 217 |
StringMapSlice map[string][]string `json:"stringMapSlice"` |
| 218 |
BoolMapSlice map[string][]bool `json:"boolMapSlice"` |
| 219 |
IntMapSlice map[string][]int `json:"intMapSlice"` |
| 220 |
Int8MapSlice map[string][]int8 `json:"int8MapSlice"` |
| 221 |
Int16MapSlice map[string][]int16 `json:"int16MapSlice"` |
| 222 |
Int32MapSlice map[string][]int32 `json:"int32MapSlice"` |
| 223 |
Int64MapSlice map[string][]int64 `json:"int64MapSlice"` |
| 224 |
UintMapSlice map[string][]uint `json:"uintMapSlice"` |
| 225 |
Uint8MapSlice map[string][]uint8 `json:"uint8MapSlice"` |
| 226 |
Uint16MapSlice map[string][]uint16 `json:"uint16MapSlice"` |
| 227 |
Uint32MapSlice map[string][]uint32 `json:"uint32MapSlice"` |
| 228 |
Uint64MapSlice map[string][]uint64 `json:"uint64MapSlice"` |
| 229 |
Float32MapSlice map[string][]float32 `json:"float32MapSlice"` |
| 230 |
Float64MapSlice map[string][]float64 `json:"float64MapSlice"` |
| 231 |
TimeMapSlice map[string][]time.Time `json:"timeMapSlice"` |
| 232 |
|
| 233 |
// Map of string to maps |
| 234 |
StringMapMap map[string]map[string]string `json:"stringMapMap"` |
| 235 |
BoolMapMap map[string]map[string]bool `json:"boolMapMap"` |
| 236 |
IntMapMap map[string]map[string]int `json:"intMapMap"` |
| 237 |
Int8MapMap map[string]map[string]int8 `json:"int8MapMap"` |
| 238 |
Int16MapMap map[string]map[string]int16 `json:"int16MapMap"` |
| 239 |
Int32MapMap map[string]map[string]int32 `json:"int32MapMap"` |
| 240 |
Int64MapMap map[string]map[string]int64 `json:"int64MapMap"` |
| 241 |
UintMapMap map[string]map[string]uint `json:"uintMapMap"` |
| 242 |
Uint8MapMap map[string]map[string]uint8 `json:"uint8MapMap"` |
| 243 |
Uint16MapMap map[string]map[string]uint16 `json:"uint16MapMap"` |
| 244 |
Uint32MapMap map[string]map[string]uint32 `json:"uint32MapMap"` |
| 245 |
Uint64MapMap map[string]map[string]uint64 `json:"uint64MapMap"` |
| 246 |
Float32MapMap map[string]map[string]float32 `json:"float32MapMap"` |
| 247 |
Float64MapMap map[string]map[string]float64 `json:"float64MapMap"` |
| 248 |
TimeMapMap map[string]map[string]time.Time `json:"timeMapMap"` |
| 249 |
|
| 250 |
// Recursive types |
| 251 |
Self *PlainStruct `json:"self"` |
| 252 |
SelfSlice []*PlainStruct `json:"selfSlice"` |
| 253 |
SelfMap map[string]*PlainStruct `json:"selfMap"` |
| 254 |
SelfSliceSlice [][](*PlainStruct) `json:"selfSliceSlice"` |
| 255 |
SelfSliceMap []map[string]*PlainStruct `json:"selfSliceMap"` |
| 256 |
SelfMapSlice map[string][]*PlainStruct `json:"selfMapSlice"` |
| 257 |
SelfMapMap map[string]map[string]*PlainStruct `json:"selfMapMap"` |
| 258 |
} |
| 259 |
|
| 260 |
type OmitEmptyStruct struct { |
| 261 |
// Basic types |
| 262 |
String string `json:"string,omitempty"` |
| 263 |
Bool bool `json:"bool,omitempty"` |
| 264 |
Int int `json:"int,omitempty"` |
| 265 |
Int8 int8 `json:"int8,omitempty"` |
| 266 |
Int16 int16 `json:"int16,omitempty"` |
| 267 |
Int32 int32 `json:"int32,omitempty"` |
| 268 |
Int64 int64 `json:"int64,omitempty"` |
| 269 |
Uint uint `json:"uint,omitempty"` |
| 270 |
Uint8 uint8 `json:"uint8,omitempty"` |
| 271 |
Uint16 uint16 `json:"uint16,omitempty"` |
| 272 |
Uint32 uint32 `json:"uint32,omitempty"` |
| 273 |
Uint64 uint64 `json:"uint64,omitempty"` |
| 274 |
Float32 float32 `json:"float32,omitempty"` |
| 275 |
Float64 float64 `json:"float64,omitempty"` |
| 276 |
Time time.Time `json:"time,omitempty"` |
| 277 |
Bytes []byte `json:"bytes,omitempty"` |
| 278 |
|
| 279 |
// Pointers to basic types |
| 280 |
StringPtr *string `json:"stringPtr,omitempty"` |
| 281 |
BoolPtr *bool `json:"boolPtr,omitempty"` |
| 282 |
IntPtr *int `json:"intPtr,omitempty"` |
| 283 |
Int8Ptr *int8 `json:"int8Ptr,omitempty"` |
| 284 |
Int16Ptr *int16 `json:"int16Ptr,omitempty"` |
| 285 |
Int32Ptr *int32 `json:"int32Ptr,omitempty"` |
| 286 |
Int64Ptr *int64 `json:"int64Ptr,omitempty"` |
| 287 |
UintPtr *uint `json:"uintPtr,omitempty"` |
| 288 |
Uint8Ptr *uint8 `json:"uint8Ptr,omitempty"` |
| 289 |
Uint16Ptr *uint16 `json:"uint16Ptr,omitempty"` |
| 290 |
Uint32Ptr *uint32 `json:"uint32Ptr,omitempty"` |
| 291 |
Uint64Ptr *uint64 `json:"uint64Ptr,omitempty"` |
| 292 |
Float32Ptr *float32 `json:"float32Ptr,omitempty"` |
| 293 |
Float64Ptr *float64 `json:"float64Ptr,omitempty"` |
| 294 |
TimePtr *time.Time `json:"timePtr,omitempty"` |
| 295 |
|
| 296 |
// Slices of basic types |
| 297 |
StringSlice []string `json:"stringSlice,omitempty"` |
| 298 |
BoolSlice []bool `json:"boolSlice,omitempty"` |
| 299 |
IntSlice []int `json:"intSlice,omitempty"` |
| 300 |
Int8Slice []int8 `json:"int8Slice,omitempty"` |
| 301 |
Int16Slice []int16 `json:"int16Slice,omitempty"` |
| 302 |
Int32Slice []int32 `json:"int32Slice,omitempty"` |
| 303 |
Int64Slice []int64 `json:"int64Slice,omitempty"` |
| 304 |
UintSlice []uint `json:"uintSlice,omitempty"` |
| 305 |
Uint8Slice []uint8 `json:"uint8Slice,omitempty"` |
| 306 |
Uint16Slice []uint16 `json:"uint16Slice,omitempty"` |
| 307 |
Uint32Slice []uint32 `json:"uint32Slice,omitempty"` |
| 308 |
Uint64Slice []uint64 `json:"uint64Slice,omitempty"` |
| 309 |
Float32Slice []float32 `json:"float32Slice,omitempty"` |
| 310 |
Float64Slice []float64 `json:"float64Slice,omitempty"` |
| 311 |
TimeSlice []time.Time `json:"timeSlice,omitempty"` |
| 312 |
|
| 313 |
// Maps of string to basic types |
| 314 |
StringMap map[string]string `json:"stringMap,omitempty"` |
| 315 |
BoolMap map[string]bool `json:"boolMap,omitempty"` |
| 316 |
IntMap map[string]int `json:"intMap,omitempty"` |
| 317 |
Int8Map map[string]int8 `json:"int8Map,omitempty"` |
| 318 |
Int16Map map[string]int16 `json:"int16Map,omitempty"` |
| 319 |
Int32Map map[string]int32 `json:"int32Map,omitempty"` |
| 320 |
Int64Map map[string]int64 `json:"int64Map,omitempty"` |
| 321 |
UintMap map[string]uint `json:"uintMap,omitempty"` |
| 322 |
Uint8Map map[string]uint8 `json:"uint8Map,omitempty"` |
| 323 |
Uint16Map map[string]uint16 `json:"uint16Map,omitempty"` |
| 324 |
Uint32Map map[string]uint32 `json:"uint32Map,omitempty"` |
| 325 |
Uint64Map map[string]uint64 `json:"uint64Map,omitempty"` |
| 326 |
Float32Map map[string]float32 `json:"float32Map,omitempty"` |
| 327 |
Float64Map map[string]float64 `json:"float64Map,omitempty"` |
| 328 |
TimeMap map[string]time.Time `json:"timeMap,omitempty"` |
| 329 |
|
| 330 |
// Slice of slices |
| 331 |
StringSliceSlice [][]string `json:"stringSliceSlice,omitempty"` |
| 332 |
BoolSliceSlice [][]bool `json:"boolSliceSlice,omitempty"` |
| 333 |
IntSliceSlice [][]int `json:"intSliceSlice,omitempty"` |
| 334 |
Int8SliceSlice [][]int8 `json:"int8SliceSlice,omitempty"` |
| 335 |
Int16SliceSlice [][]int16 `json:"int16SliceSlice,omitempty"` |
| 336 |
Int32SliceSlice [][]int32 `json:"int32SliceSlice,omitempty"` |
| 337 |
Int64SliceSlice [][]int64 `json:"int64SliceSlice,omitempty"` |
| 338 |
UintSliceSlice [][]uint `json:"uintSliceSlice,omitempty"` |
| 339 |
Uint8SliceSlice [][]uint8 `json:"uint8SliceSlice,omitempty"` |
| 340 |
Uint16SliceSlice [][]uint16 `json:"uint16SliceSlice,omitempty"` |
| 341 |
Uint32SliceSlice [][]uint32 `json:"uint32SliceSlice,omitempty"` |
| 342 |
Uint64SliceSlice [][]uint64 `json:"uint64SliceSlice,omitempty"` |
| 343 |
Float32SliceSlice [][]float32 `json:"float32SliceSlice,omitempty"` |
| 344 |
Float64SliceSlice [][]float64 `json:"float64SliceSlice,omitempty"` |
| 345 |
TimeSliceSlice [][]time.Time `json:"timeSliceSlice,omitempty"` |
| 346 |
|
| 347 |
// Slice of maps |
| 348 |
StringSliceMap []map[string]string `json:"stringSliceMap,omitempty"` |
| 349 |
BoolSliceMap []map[string]bool `json:"boolSliceMap,omitempty"` |
| 350 |
IntSliceMap []map[string]int `json:"intSliceMap,omitempty"` |
| 351 |
Int8SliceMap []map[string]int8 `json:"int8SliceMap,omitempty"` |
| 352 |
Int16SliceMap []map[string]int16 `json:"int16SliceMap,omitempty"` |
| 353 |
Int32SliceMap []map[string]int32 `json:"int32SliceMap,omitempty"` |
| 354 |
Int64SliceMap []map[string]int64 `json:"int64SliceMap,omitempty"` |
| 355 |
UintSliceMap []map[string]uint `json:"uintSliceMap,omitempty"` |
| 356 |
Uint8SliceMap []map[string]uint8 `json:"uint8SliceMap,omitempty"` |
| 357 |
Uint16SliceMap []map[string]uint16 `json:"uint16SliceMap,omitempty"` |
| 358 |
Uint32SliceMap []map[string]uint32 `json:"uint32SliceMap,omitempty"` |
| 359 |
Uint64SliceMap []map[string]uint64 `json:"uint64SliceMap,omitempty"` |
| 360 |
Float32SliceMap []map[string]float32 `json:"float32SliceMap,omitempty"` |
| 361 |
Float64SliceMap []map[string]float64 `json:"float64SliceMap,omitempty"` |
| 362 |
TimeSliceMap []map[string]time.Time `json:"timeSliceMap,omitempty"` |
| 363 |
|
| 364 |
// Map of string to slices |
| 365 |
StringMapSlice map[string][]string `json:"stringMapSlice,omitempty"` |
| 366 |
BoolMapSlice map[string][]bool `json:"boolMapSlice,omitempty"` |
| 367 |
IntMapSlice map[string][]int `json:"intMapSlice,omitempty"` |
| 368 |
Int8MapSlice map[string][]int8 `json:"int8MapSlice,omitempty"` |
| 369 |
Int16MapSlice map[string][]int16 `json:"int16MapSlice,omitempty"` |
| 370 |
Int32MapSlice map[string][]int32 `json:"int32MapSlice,omitempty"` |
| 371 |
Int64MapSlice map[string][]int64 `json:"int64MapSlice,omitempty"` |
| 372 |
UintMapSlice map[string][]uint `json:"uintMapSlice,omitempty"` |
| 373 |
Uint8MapSlice map[string][]uint8 `json:"uint8MapSlice,omitempty"` |
| 374 |
Uint16MapSlice map[string][]uint16 `json:"uint16MapSlice,omitempty"` |
| 375 |
Uint32MapSlice map[string][]uint32 `json:"uint32MapSlice,omitempty"` |
| 376 |
Uint64MapSlice map[string][]uint64 `json:"uint64MapSlice,omitempty"` |
| 377 |
Float32MapSlice map[string][]float32 `json:"float32MapSlice,omitempty"` |
| 378 |
Float64MapSlice map[string][]float64 `json:"float64MapSlice,omitempty"` |
| 379 |
TimeMapSlice map[string][]time.Time `json:"timeMapSlice,omitempty"` |
| 380 |
|
| 381 |
// Map of string to maps |
| 382 |
StringMapMap map[string]map[string]string `json:"stringMapMap,omitempty"` |
| 383 |
BoolMapMap map[string]map[string]bool `json:"boolMapMap,omitempty"` |
| 384 |
IntMapMap map[string]map[string]int `json:"intMapMap,omitempty"` |
| 385 |
Int8MapMap map[string]map[string]int8 `json:"int8MapMap,omitempty"` |
| 386 |
Int16MapMap map[string]map[string]int16 `json:"int16MapMap,omitempty"` |
| 387 |
Int32MapMap map[string]map[string]int32 `json:"int32MapMap,omitempty"` |
| 388 |
Int64MapMap map[string]map[string]int64 `json:"int64MapMap,omitempty"` |
| 389 |
UintMapMap map[string]map[string]uint `json:"uintMapMap,omitempty"` |
| 390 |
Uint8MapMap map[string]map[string]uint8 `json:"uint8MapMap,omitempty"` |
| 391 |
Uint16MapMap map[string]map[string]uint16 `json:"uint16MapMap,omitempty"` |
| 392 |
Uint32MapMap map[string]map[string]uint32 `json:"uint32MapMap,omitempty"` |
| 393 |
Uint64MapMap map[string]map[string]uint64 `json:"uint64MapMap,omitempty"` |
| 394 |
Float32MapMap map[string]map[string]float32 `json:"float32MapMap,omitempty"` |
| 395 |
Float64MapMap map[string]map[string]float64 `json:"float64MapMap,omitempty"` |
| 396 |
TimeMapMap map[string]map[string]time.Time `json:"timeMapMap,omitempty"` |
| 397 |
|
| 398 |
// Recursive types |
| 399 |
Self *OmitEmptyStruct `json:"self,omitempty"` |
| 400 |
SelfSlice []*OmitEmptyStruct `json:"selfSlice,omitempty"` |
| 401 |
SelfMap map[string]*OmitEmptyStruct `json:"selfMap,omitempty"` |
| 402 |
SelfSliceSlice [][](*OmitEmptyStruct) `json:"selfSliceSlice,omitempty"` |
| 403 |
SelfSliceMap []map[string]*OmitEmptyStruct `json:"selfSliceMap,omitempty"` |
| 404 |
SelfMapSlice map[string][]*OmitEmptyStruct `json:"selfMapSlice,omitempty"` |
| 405 |
SelfMapMap map[string]map[string]*OmitEmptyStruct `json:"selfMapMap,omitempty"` |
| 406 |
} |
| 407 |
|
| 408 |
type OmitZeroStruct struct { |
| 409 |
// Basic types |
| 410 |
String string `json:"string,omitzero"` |
| 411 |
Bool bool `json:"bool,omitzero"` |
| 412 |
Int int `json:"int,omitzero"` |
| 413 |
Int8 int8 `json:"int8,omitzero"` |
| 414 |
Int16 int16 `json:"int16,omitzero"` |
| 415 |
Int32 int32 `json:"int32,omitzero"` |
| 416 |
Int64 int64 `json:"int64,omitzero"` |
| 417 |
Uint uint `json:"uint,omitzero"` |
| 418 |
Uint8 uint8 `json:"uint8,omitzero"` |
| 419 |
Uint16 uint16 `json:"uint16,omitzero"` |
| 420 |
Uint32 uint32 `json:"uint32,omitzero"` |
| 421 |
Uint64 uint64 `json:"uint64,omitzero"` |
| 422 |
Float32 float32 `json:"float32,omitzero"` |
| 423 |
Float64 float64 `json:"float64,omitzero"` |
| 424 |
Time time.Time `json:"time,omitzero"` |
| 425 |
Bytes []byte `json:"bytes,omitzero"` |
| 426 |
|
| 427 |
// Pointers to basic types |
| 428 |
StringPtr *string `json:"stringPtr,omitzero"` |
| 429 |
BoolPtr *bool `json:"boolPtr,omitzero"` |
| 430 |
IntPtr *int `json:"intPtr,omitzero"` |
| 431 |
Int8Ptr *int8 `json:"int8Ptr,omitzero"` |
| 432 |
Int16Ptr *int16 `json:"int16Ptr,omitzero"` |
| 433 |
Int32Ptr *int32 `json:"int32Ptr,omitzero"` |
| 434 |
Int64Ptr *int64 `json:"int64Ptr,omitzero"` |
| 435 |
UintPtr *uint `json:"uintPtr,omitzero"` |
| 436 |
Uint8Ptr *uint8 `json:"uint8Ptr,omitzero"` |
| 437 |
Uint16Ptr *uint16 `json:"uint16Ptr,omitzero"` |
| 438 |
Uint32Ptr *uint32 `json:"uint32Ptr,omitzero"` |
| 439 |
Uint64Ptr *uint64 `json:"uint64Ptr,omitzero"` |
| 440 |
Float32Ptr *float32 `json:"float32Ptr,omitzero"` |
| 441 |
Float64Ptr *float64 `json:"float64Ptr,omitzero"` |
| 442 |
TimePtr *time.Time `json:"timePtr,omitzero"` |
| 443 |
|
| 444 |
// Slices of basic types |
| 445 |
StringSlice []string `json:"stringSlice,omitzero"` |
| 446 |
BoolSlice []bool `json:"boolSlice,omitzero"` |
| 447 |
IntSlice []int `json:"intSlice,omitzero"` |
| 448 |
Int8Slice []int8 `json:"int8Slice,omitzero"` |
| 449 |
Int16Slice []int16 `json:"int16Slice,omitzero"` |
| 450 |
Int32Slice []int32 `json:"int32Slice,omitzero"` |
| 451 |
Int64Slice []int64 `json:"int64Slice,omitzero"` |
| 452 |
UintSlice []uint `json:"uintSlice,omitzero"` |
| 453 |
Uint8Slice []uint8 `json:"uint8Slice,omitzero"` |
| 454 |
Uint16Slice []uint16 `json:"uint16Slice,omitzero"` |
| 455 |
Uint32Slice []uint32 `json:"uint32Slice,omitzero"` |
| 456 |
Uint64Slice []uint64 `json:"uint64Slice,omitzero"` |
| 457 |
Float32Slice []float32 `json:"float32Slice,omitzero"` |
| 458 |
Float64Slice []float64 `json:"float64Slice,omitzero"` |
| 459 |
TimeSlice []time.Time `json:"timeSlice,omitzero"` |
| 460 |
|
| 461 |
// Maps of string to basic types |
| 462 |
StringMap map[string]string `json:"stringMap,omitzero"` |
| 463 |
BoolMap map[string]bool `json:"boolMap,omitzero"` |
| 464 |
IntMap map[string]int `json:"intMap,omitzero"` |
| 465 |
Int8Map map[string]int8 `json:"int8Map,omitzero"` |
| 466 |
Int16Map map[string]int16 `json:"int16Map,omitzero"` |
| 467 |
Int32Map map[string]int32 `json:"int32Map,omitzero"` |
| 468 |
Int64Map map[string]int64 `json:"int64Map,omitzero"` |
| 469 |
UintMap map[string]uint `json:"uintMap,omitzero"` |
| 470 |
Uint8Map map[string]uint8 `json:"uint8Map,omitzero"` |
| 471 |
Uint16Map map[string]uint16 `json:"uint16Map,omitzero"` |
| 472 |
Uint32Map map[string]uint32 `json:"uint32Map,omitzero"` |
| 473 |
Uint64Map map[string]uint64 `json:"uint64Map,omitzero"` |
| 474 |
Float32Map map[string]float32 `json:"float32Map,omitzero"` |
| 475 |
Float64Map map[string]float64 `json:"float64Map,omitzero"` |
| 476 |
TimeMap map[string]time.Time `json:"timeMap,omitzero"` |
| 477 |
|
| 478 |
// Slice of slices |
| 479 |
StringSliceSlice [][]string `json:"stringSliceSlice,omitzero"` |
| 480 |
BoolSliceSlice [][]bool `json:"boolSliceSlice,omitzero"` |
| 481 |
IntSliceSlice [][]int `json:"intSliceSlice,omitzero"` |
| 482 |
Int8SliceSlice [][]int8 `json:"int8SliceSlice,omitzero"` |
| 483 |
Int16SliceSlice [][]int16 `json:"int16SliceSlice,omitzero"` |
| 484 |
Int32SliceSlice [][]int32 `json:"int32SliceSlice,omitzero"` |
| 485 |
Int64SliceSlice [][]int64 `json:"int64SliceSlice,omitzero"` |
| 486 |
UintSliceSlice [][]uint `json:"uintSliceSlice,omitzero"` |
| 487 |
Uint8SliceSlice [][]uint8 `json:"uint8SliceSlice,omitzero"` |
| 488 |
Uint16SliceSlice [][]uint16 `json:"uint16SliceSlice,omitzero"` |
| 489 |
Uint32SliceSlice [][]uint32 `json:"uint32SliceSlice,omitzero"` |
| 490 |
Uint64SliceSlice [][]uint64 `json:"uint64SliceSlice,omitzero"` |
| 491 |
Float32SliceSlice [][]float32 `json:"float32SliceSlice,omitzero"` |
| 492 |
Float64SliceSlice [][]float64 `json:"float64SliceSlice,omitzero"` |
| 493 |
TimeSliceSlice [][]time.Time `json:"timeSliceSlice,omitzero"` |
| 494 |
|
| 495 |
// Slice of maps |
| 496 |
StringSliceMap []map[string]string `json:"stringSliceMap,omitzero"` |
| 497 |
BoolSliceMap []map[string]bool `json:"boolSliceMap,omitzero"` |
| 498 |
IntSliceMap []map[string]int `json:"intSliceMap,omitzero"` |
| 499 |
Int8SliceMap []map[string]int8 `json:"int8SliceMap,omitzero"` |
| 500 |
Int16SliceMap []map[string]int16 `json:"int16SliceMap,omitzero"` |
| 501 |
Int32SliceMap []map[string]int32 `json:"int32SliceMap,omitzero"` |
| 502 |
Int64SliceMap []map[string]int64 `json:"int64SliceMap,omitzero"` |
| 503 |
UintSliceMap []map[string]uint `json:"uintSliceMap,omitzero"` |
| 504 |
Uint8SliceMap []map[string]uint8 `json:"uint8SliceMap,omitzero"` |
| 505 |
Uint16SliceMap []map[string]uint16 `json:"uint16SliceMap,omitzero"` |
| 506 |
Uint32SliceMap []map[string]uint32 `json:"uint32SliceMap,omitzero"` |
| 507 |
Uint64SliceMap []map[string]uint64 `json:"uint64SliceMap,omitzero"` |
| 508 |
Float32SliceMap []map[string]float32 `json:"float32SliceMap,omitzero"` |
| 509 |
Float64SliceMap []map[string]float64 `json:"float64SliceMap,omitzero"` |
| 510 |
TimeSliceMap []map[string]time.Time `json:"timeSliceMap,omitzero"` |
| 511 |
|
| 512 |
// Map of string to slices |
| 513 |
StringMapSlice map[string][]string `json:"stringMapSlice,omitzero"` |
| 514 |
BoolMapSlice map[string][]bool `json:"boolMapSlice,omitzero"` |
| 515 |
IntMapSlice map[string][]int `json:"intMapSlice,omitzero"` |
| 516 |
Int8MapSlice map[string][]int8 `json:"int8MapSlice,omitzero"` |
| 517 |
Int16MapSlice map[string][]int16 `json:"int16MapSlice,omitzero"` |
| 518 |
Int32MapSlice map[string][]int32 `json:"int32MapSlice,omitzero"` |
| 519 |
Int64MapSlice map[string][]int64 `json:"int64MapSlice,omitzero"` |
| 520 |
UintMapSlice map[string][]uint `json:"uintMapSlice,omitzero"` |
| 521 |
Uint8MapSlice map[string][]uint8 `json:"uint8MapSlice,omitzero"` |
| 522 |
Uint16MapSlice map[string][]uint16 `json:"uint16MapSlice,omitzero"` |
| 523 |
Uint32MapSlice map[string][]uint32 `json:"uint32MapSlice,omitzero"` |
| 524 |
Uint64MapSlice map[string][]uint64 `json:"uint64MapSlice,omitzero"` |
| 525 |
Float32MapSlice map[string][]float32 `json:"float32MapSlice,omitzero"` |
| 526 |
Float64MapSlice map[string][]float64 `json:"float64MapSlice,omitzero"` |
| 527 |
TimeMapSlice map[string][]time.Time `json:"timeMapSlice,omitzero"` |
| 528 |
|
| 529 |
// Map of string to maps |
| 530 |
StringMapMap map[string]map[string]string `json:"stringMapMap,omitzero"` |
| 531 |
BoolMapMap map[string]map[string]bool `json:"boolMapMap,omitzero"` |
| 532 |
IntMapMap map[string]map[string]int `json:"intMapMap,omitzero"` |
| 533 |
Int8MapMap map[string]map[string]int8 `json:"int8MapMap,omitzero"` |
| 534 |
Int16MapMap map[string]map[string]int16 `json:"int16MapMap,omitzero"` |
| 535 |
Int32MapMap map[string]map[string]int32 `json:"int32MapMap,omitzero"` |
| 536 |
Int64MapMap map[string]map[string]int64 `json:"int64MapMap,omitzero"` |
| 537 |
UintMapMap map[string]map[string]uint `json:"uintMapMap,omitzero"` |
| 538 |
Uint8MapMap map[string]map[string]uint8 `json:"uint8MapMap,omitzero"` |
| 539 |
Uint16MapMap map[string]map[string]uint16 `json:"uint16MapMap,omitzero"` |
| 540 |
Uint32MapMap map[string]map[string]uint32 `json:"uint32MapMap,omitzero"` |
| 541 |
Uint64MapMap map[string]map[string]uint64 `json:"uint64MapMap,omitzero"` |
| 542 |
Float32MapMap map[string]map[string]float32 `json:"float32MapMap,omitzero"` |
| 543 |
Float64MapMap map[string]map[string]float64 `json:"float64MapMap,omitzero"` |
| 544 |
TimeMapMap map[string]map[string]time.Time `json:"timeMapMap,omitzero"` |
| 545 |
|
| 546 |
// Recursive types |
| 547 |
Self *OmitZeroStruct `json:"self,omitzero"` |
| 548 |
SelfSlice []*OmitZeroStruct `json:"selfSlice,omitzero"` |
| 549 |
SelfMap map[string]*OmitZeroStruct `json:"selfMap,omitzero"` |
| 550 |
SelfSliceSlice [][](*OmitZeroStruct) `json:"selfSliceSlice,omitzero"` |
| 551 |
SelfSliceMap []map[string]*OmitZeroStruct `json:"selfSliceMap,omitzero"` |
| 552 |
SelfMapSlice map[string][]*OmitZeroStruct `json:"selfMapSlice,omitzero"` |
| 553 |
SelfMapMap map[string]map[string]*OmitZeroStruct `json:"selfMapMap,omitzero"` |
| 554 |
} |
| 555 |
|
| 556 |
type OmitEmptyAndZeroStruct struct { |
| 557 |
// Basic types |
| 558 |
String string `json:"string,omitempty,omitzero"` |
| 559 |
Bool bool `json:"bool,omitempty,omitzero"` |
| 560 |
Int int `json:"int,omitempty,omitzero"` |
| 561 |
Int8 int8 `json:"int8,omitempty,omitzero"` |
| 562 |
Int16 int16 `json:"int16,omitempty,omitzero"` |
| 563 |
Int32 int32 `json:"int32,omitempty,omitzero"` |
| 564 |
Int64 int64 `json:"int64,omitempty,omitzero"` |
| 565 |
Uint uint `json:"uint,omitempty,omitzero"` |
| 566 |
Uint8 uint8 `json:"uint8,omitempty,omitzero"` |
| 567 |
Uint16 uint16 `json:"uint16,omitempty,omitzero"` |
| 568 |
Uint32 uint32 `json:"uint32,omitempty,omitzero"` |
| 569 |
Uint64 uint64 `json:"uint64,omitempty,omitzero"` |
| 570 |
Float32 float32 `json:"float32,omitempty,omitzero"` |
| 571 |
Float64 float64 `json:"float64,omitempty,omitzero"` |
| 572 |
Time time.Time `json:"time,omitempty,omitzero"` |
| 573 |
Bytes []byte `json:"bytes,omitempty,omitzero"` |
| 574 |
|
| 575 |
// Pointers to basic types |
| 576 |
StringPtr *string `json:"stringPtr,omitempty,omitzero"` |
| 577 |
BoolPtr *bool `json:"boolPtr,omitempty,omitzero"` |
| 578 |
IntPtr *int `json:"intPtr,omitempty,omitzero"` |
| 579 |
Int8Ptr *int8 `json:"int8Ptr,omitempty,omitzero"` |
| 580 |
Int16Ptr *int16 `json:"int16Ptr,omitempty,omitzero"` |
| 581 |
Int32Ptr *int32 `json:"int32Ptr,omitempty,omitzero"` |
| 582 |
Int64Ptr *int64 `json:"int64Ptr,omitempty,omitzero"` |
| 583 |
UintPtr *uint `json:"uintPtr,omitempty,omitzero"` |
| 584 |
Uint8Ptr *uint8 `json:"uint8Ptr,omitempty,omitzero"` |
| 585 |
Uint16Ptr *uint16 `json:"uint16Ptr,omitempty,omitzero"` |
| 586 |
Uint32Ptr *uint32 `json:"uint32Ptr,omitempty,omitzero"` |
| 587 |
Uint64Ptr *uint64 `json:"uint64Ptr,omitempty,omitzero"` |
| 588 |
Float32Ptr *float32 `json:"float32Ptr,omitempty,omitzero"` |
| 589 |
Float64Ptr *float64 `json:"float64Ptr,omitempty,omitzero"` |
| 590 |
TimePtr *time.Time `json:"timePtr,omitempty,omitzero"` |
| 591 |
|
| 592 |
// Slices of basic types |
| 593 |
StringSlice []string `json:"stringSlice,omitempty,omitzero"` |
| 594 |
BoolSlice []bool `json:"boolSlice,omitempty,omitzero"` |
| 595 |
IntSlice []int `json:"intSlice,omitempty,omitzero"` |
| 596 |
Int8Slice []int8 `json:"int8Slice,omitempty,omitzero"` |
| 597 |
Int16Slice []int16 `json:"int16Slice,omitempty,omitzero"` |
| 598 |
Int32Slice []int32 `json:"int32Slice,omitempty,omitzero"` |
| 599 |
Int64Slice []int64 `json:"int64Slice,omitempty,omitzero"` |
| 600 |
UintSlice []uint `json:"uintSlice,omitempty,omitzero"` |
| 601 |
Uint8Slice []uint8 `json:"uint8Slice,omitempty,omitzero"` |
| 602 |
Uint16Slice []uint16 `json:"uint16Slice,omitempty,omitzero"` |
| 603 |
Uint32Slice []uint32 `json:"uint32Slice,omitempty,omitzero"` |
| 604 |
Uint64Slice []uint64 `json:"uint64Slice,omitempty,omitzero"` |
| 605 |
Float32Slice []float32 `json:"float32Slice,omitempty,omitzero"` |
| 606 |
Float64Slice []float64 `json:"float64Slice,omitempty,omitzero"` |
| 607 |
TimeSlice []time.Time `json:"timeSlice,omitempty,omitzero"` |
| 608 |
|
| 609 |
// Maps of string to basic types |
| 610 |
StringMap map[string]string `json:"stringMap,omitempty,omitzero"` |
| 611 |
BoolMap map[string]bool `json:"boolMap,omitempty,omitzero"` |
| 612 |
IntMap map[string]int `json:"intMap,omitempty,omitzero"` |
| 613 |
Int8Map map[string]int8 `json:"int8Map,omitempty,omitzero"` |
| 614 |
Int16Map map[string]int16 `json:"int16Map,omitempty,omitzero"` |
| 615 |
Int32Map map[string]int32 `json:"int32Map,omitempty,omitzero"` |
| 616 |
Int64Map map[string]int64 `json:"int64Map,omitempty,omitzero"` |
| 617 |
UintMap map[string]uint `json:"uintMap,omitempty,omitzero"` |
| 618 |
Uint8Map map[string]uint8 `json:"uint8Map,omitempty,omitzero"` |
| 619 |
Uint16Map map[string]uint16 `json:"uint16Map,omitempty,omitzero"` |
| 620 |
Uint32Map map[string]uint32 `json:"uint32Map,omitempty,omitzero"` |
| 621 |
Uint64Map map[string]uint64 `json:"uint64Map,omitempty,omitzero"` |
| 622 |
Float32Map map[string]float32 `json:"float32Map,omitempty,omitzero"` |
| 623 |
Float64Map map[string]float64 `json:"float64Map,omitempty,omitzero"` |
| 624 |
TimeMap map[string]time.Time `json:"timeMap,omitempty,omitzero"` |
| 625 |
|
| 626 |
// Slice of slices |
| 627 |
StringSliceSlice [][]string `json:"stringSliceSlice,omitempty,omitzero"` |
| 628 |
BoolSliceSlice [][]bool `json:"boolSliceSlice,omitempty,omitzero"` |
| 629 |
IntSliceSlice [][]int `json:"intSliceSlice,omitempty,omitzero"` |
| 630 |
Int8SliceSlice [][]int8 `json:"int8SliceSlice,omitempty,omitzero"` |
| 631 |
Int16SliceSlice [][]int16 `json:"int16SliceSlice,omitempty,omitzero"` |
| 632 |
Int32SliceSlice [][]int32 `json:"int32SliceSlice,omitempty,omitzero"` |
| 633 |
Int64SliceSlice [][]int64 `json:"int64SliceSlice,omitempty,omitzero"` |
| 634 |
UintSliceSlice [][]uint `json:"uintSliceSlice,omitempty,omitzero"` |
| 635 |
Uint8SliceSlice [][]uint8 `json:"uint8SliceSlice,omitempty,omitzero"` |
| 636 |
Uint16SliceSlice [][]uint16 `json:"uint16SliceSlice,omitempty,omitzero"` |
| 637 |
Uint32SliceSlice [][]uint32 `json:"uint32SliceSlice,omitempty,omitzero"` |
| 638 |
Uint64SliceSlice [][]uint64 `json:"uint64SliceSlice,omitempty,omitzero"` |
| 639 |
Float32SliceSlice [][]float32 `json:"float32SliceSlice,omitempty,omitzero"` |
| 640 |
Float64SliceSlice [][]float64 `json:"float64SliceSlice,omitempty,omitzero"` |
| 641 |
TimeSliceSlice [][]time.Time `json:"timeSliceSlice,omitempty,omitzero"` |
| 642 |
|
| 643 |
// Slice of maps |
| 644 |
StringSliceMap []map[string]string `json:"stringSliceMap,omitempty,omitzero"` |
| 645 |
BoolSliceMap []map[string]bool `json:"boolSliceMap,omitempty,omitzero"` |
| 646 |
IntSliceMap []map[string]int `json:"intSliceMap,omitempty,omitzero"` |
| 647 |
Int8SliceMap []map[string]int8 `json:"int8SliceMap,omitempty,omitzero"` |
| 648 |
Int16SliceMap []map[string]int16 `json:"int16SliceMap,omitempty,omitzero"` |
| 649 |
Int32SliceMap []map[string]int32 `json:"int32SliceMap,omitempty,omitzero"` |
| 650 |
Int64SliceMap []map[string]int64 `json:"int64SliceMap,omitempty,omitzero"` |
| 651 |
UintSliceMap []map[string]uint `json:"uintSliceMap,omitempty,omitzero"` |
| 652 |
Uint8SliceMap []map[string]uint8 `json:"uint8SliceMap,omitempty,omitzero"` |
| 653 |
Uint16SliceMap []map[string]uint16 `json:"uint16SliceMap,omitempty,omitzero"` |
| 654 |
Uint32SliceMap []map[string]uint32 `json:"uint32SliceMap,omitempty,omitzero"` |
| 655 |
Uint64SliceMap []map[string]uint64 `json:"uint64SliceMap,omitempty,omitzero"` |
| 656 |
Float32SliceMap []map[string]float32 `json:"float32SliceMap,omitempty,omitzero"` |
| 657 |
Float64SliceMap []map[string]float64 `json:"float64SliceMap,omitempty,omitzero"` |
| 658 |
TimeSliceMap []map[string]time.Time `json:"timeSliceMap,omitempty,omitzero"` |
| 659 |
|
| 660 |
// Map of string to slices |
| 661 |
StringMapSlice map[string][]string `json:"stringMapSlice,omitempty,omitzero"` |
| 662 |
BoolMapSlice map[string][]bool `json:"boolMapSlice,omitempty,omitzero"` |
| 663 |
IntMapSlice map[string][]int `json:"intMapSlice,omitempty,omitzero"` |
| 664 |
Int8MapSlice map[string][]int8 `json:"int8MapSlice,omitempty,omitzero"` |
| 665 |
Int16MapSlice map[string][]int16 `json:"int16MapSlice,omitempty,omitzero"` |
| 666 |
Int32MapSlice map[string][]int32 `json:"int32MapSlice,omitempty,omitzero"` |
| 667 |
Int64MapSlice map[string][]int64 `json:"int64MapSlice,omitempty,omitzero"` |
| 668 |
UintMapSlice map[string][]uint `json:"uintMapSlice,omitempty,omitzero"` |
| 669 |
Uint8MapSlice map[string][]uint8 `json:"uint8MapSlice,omitempty,omitzero"` |
| 670 |
Uint16MapSlice map[string][]uint16 `json:"uint16MapSlice,omitempty,omitzero"` |
| 671 |
Uint32MapSlice map[string][]uint32 `json:"uint32MapSlice,omitempty,omitzero"` |
| 672 |
Uint64MapSlice map[string][]uint64 `json:"uint64MapSlice,omitempty,omitzero"` |
| 673 |
Float32MapSlice map[string][]float32 `json:"float32MapSlice,omitempty,omitzero"` |
| 674 |
Float64MapSlice map[string][]float64 `json:"float64MapSlice,omitempty,omitzero"` |
| 675 |
TimeMapSlice map[string][]time.Time `json:"timeMapSlice,omitempty,omitzero"` |
| 676 |
|
| 677 |
// Map of string to maps |
| 678 |
StringMapMap map[string]map[string]string `json:"stringMapMap,omitempty,omitzero"` |
| 679 |
BoolMapMap map[string]map[string]bool `json:"boolMapMap,omitempty,omitzero"` |
| 680 |
IntMapMap map[string]map[string]int `json:"intMapMap,omitempty,omitzero"` |
| 681 |
Int8MapMap map[string]map[string]int8 `json:"int8MapMap,omitempty,omitzero"` |
| 682 |
Int16MapMap map[string]map[string]int16 `json:"int16MapMap,omitempty,omitzero"` |
| 683 |
Int32MapMap map[string]map[string]int32 `json:"int32MapMap,omitempty,omitzero"` |
| 684 |
Int64MapMap map[string]map[string]int64 `json:"int64MapMap,omitempty,omitzero"` |
| 685 |
UintMapMap map[string]map[string]uint `json:"uintMapMap,omitempty,omitzero"` |
| 686 |
Uint8MapMap map[string]map[string]uint8 `json:"uint8MapMap,omitempty,omitzero"` |
| 687 |
Uint16MapMap map[string]map[string]uint16 `json:"uint16MapMap,omitempty,omitzero"` |
| 688 |
Uint32MapMap map[string]map[string]uint32 `json:"uint32MapMap,omitempty,omitzero"` |
| 689 |
Uint64MapMap map[string]map[string]uint64 `json:"uint64MapMap,omitempty,omitzero"` |
| 690 |
Float32MapMap map[string]map[string]float32 `json:"float32MapMap,omitempty,omitzero"` |
| 691 |
Float64MapMap map[string]map[string]float64 `json:"float64MapMap,omitempty,omitzero"` |
| 692 |
TimeMapMap map[string]map[string]time.Time `json:"timeMapMap,omitempty,omitzero"` |
| 693 |
|
| 694 |
// Recursive types |
| 695 |
Self *OmitEmptyAndZeroStruct `json:"self,omitempty,omitzero"` |
| 696 |
SelfSlice []*OmitEmptyAndZeroStruct `json:"selfSlice,omitempty,omitzero"` |
| 697 |
SelfMap map[string]*OmitEmptyAndZeroStruct `json:"selfMap,omitempty,omitzero"` |
| 698 |
SelfSliceSlice [][](*OmitEmptyAndZeroStruct) `json:"selfSliceSlice,omitempty,omitzero"` |
| 699 |
SelfSliceMap []map[string]*OmitEmptyAndZeroStruct `json:"selfSliceMap,omitempty,omitzero"` |
| 700 |
SelfMapSlice map[string][]*OmitEmptyAndZeroStruct `json:"selfMapSlice,omitempty,omitzero"` |
| 701 |
SelfMapMap map[string]map[string]*OmitEmptyAndZeroStruct `json:"selfMapMap,omitempty,omitzero"` |
| 702 |
} |
| 703 |
|
| 704 |
func TestKYAMLEncoderFuzzRoundTrip(t *testing.T) { |
| 705 |
for i := 0; i < 1000; i++ { |
| 706 |
t.Run("i="+strconv.Itoa(i), func(t *testing.T) { |
| 707 |
t.Parallel() |
| 708 |
|
| 709 |
// Create and fill an instance. |
| 710 |
original := &AllTypesStruct{} |
| 711 |
f := randfill.New().NilChance(0.5).NumElements(1, 3).MaxDepth(3) |
| 712 |
f.Fill(original) |
| 713 |
|
| 714 |
// Run one test. |
| 715 |
run := func(t *testing.T, ky *Encoder) { |
| 716 |
t.Helper() |
| 717 |
|
| 718 |
// Marshal to KYAML. |
| 719 |
yb, err := ky.Marshal(original) |
| 720 |
if err != nil { |
| 721 |
t.Fatalf("iteration %d: failed to render KYAML: %v", i, err) |
| 722 |
} |
| 723 |
|
| 724 |
// Parse back from KYAML with the standard parser. |
| 725 |
parsed := &AllTypesStruct{} |
| 726 |
if err := yaml.Unmarshal(yb, parsed); err != nil { |
| 727 |
t.Fatalf("iteration %d: failed to parse KYAML: %v\nKYAML:\n%s", i, err, string(yb)) |
| 728 |
} |
| 729 |
|
| 730 |
// Compare. |
| 731 |
if diff := cmp.Diff(original, parsed, cmpopts.EquateEmpty()); diff != "" { |
| 732 |
t.Fatalf("iteration %d: objects differ after round trip (-original +parsed):\n%s\nKYAML:\n%s", i, diff, string(yb)) |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
t.Run("regular", func(t *testing.T) { |
| 737 |
run(t, &Encoder{}) |
| 738 |
}) |
| 739 |
t.Run("compact", func(t *testing.T) { |
| 740 |
run(t, &Encoder{Compact: true}) |
| 741 |
}) |
| 742 |
}) |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
func TestKYAMLOmittedField(t *testing.T) { |
| 747 |
type Struct struct { |
| 748 |
String string `json:"string"` |
| 749 |
Bool bool `json:"bool"` |
| 750 |
Int int `json:"int"` |
| 751 |
Plain PlainStruct `json:"-"` |
| 752 |
} |
| 753 |
|
| 754 |
for i := 0; i < 1000; i++ { |
| 755 |
// Create and fill an instance. |
| 756 |
original := &Struct{} |
| 757 |
f := randfill.New().NilChance(0.5).NumElements(1, 3).MaxDepth(3) |
| 758 |
f.Fill(original) |
| 759 |
|
| 760 |
// Marshal to KYAML. |
| 761 |
ky := &Encoder{} |
| 762 |
yb, err := ky.Marshal(original) |
| 763 |
if err != nil { |
| 764 |
t.Fatalf("iteration %d: failed to render KYAML: %v", i, err) |
| 765 |
} |
| 766 |
|
| 767 |
// Parse back from KYAML with the standard parser. |
| 768 |
parsed := &Struct{} |
| 769 |
if err := yaml.Unmarshal(yb, parsed); err != nil { |
| 770 |
t.Fatalf("iteration %d: failed to parse KYAML: %v\nKYAML:\n%s", i, err, string(yb)) |
| 771 |
} |
| 772 |
|
| 773 |
// Wipe the state that should not have been produced. |
| 774 |
original.Plain = PlainStruct{} |
| 775 |
|
| 776 |
// Compare. |
| 777 |
if diff := cmp.Diff(original, parsed, cmpopts.EquateEmpty()); diff != "" { |
| 778 |
t.Fatalf("iteration %d: objects differ after round trip (-original +parsed):\n%s\nKYAML:\n%s", i, diff, string(yb)) |
| 779 |
} |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
type SelfMarshalStruct struct { |
| 784 |
String string `json:"string,omitempty"` |
| 785 |
} |
| 786 |
|
| 787 |
func (s SelfMarshalStruct) MarshalJSON() ([]byte, error) { |
| 788 |
return []byte(`{"key":"value"}`), nil |
| 789 |
} |
| 790 |
|
| 791 |
func TestKYAMLSelfMarshal(t *testing.T) { |
| 792 |
original := &SelfMarshalStruct{String: "string"} |
| 793 |
ky := &Encoder{} |
| 794 |
yb, err := ky.Marshal(original) |
| 795 |
if err != nil { |
| 796 |
t.Fatalf("failed to render KYAML: %v", err) |
| 797 |
} |
| 798 |
expected := "{\n key: \"value\",\n}" |
| 799 |
if s := string(yb); s != expected { |
| 800 |
t.Fatalf("wrong result:\nexpected: %q\n got: %q", expected, s) |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
// TestKYAMLOutput checks that the KYAML output is correct for various Go |
| 805 |
// inputs. |
| 806 |
func TestKYAMLOutput(t *testing.T) { |
| 807 |
type testCase struct { |
| 808 |
name string |
| 809 |
input any |
| 810 |
expectRegular string |
| 811 |
expectCompact string |
| 812 |
} |
| 813 |
|
| 814 |
tests := []testCase{{ |
| 815 |
name: "positive int", |
| 816 |
input: int(123), |
| 817 |
expectRegular: `123`, |
| 818 |
expectCompact: `123`, |
| 819 |
}, { |
| 820 |
name: "negative int", |
| 821 |
input: int(-123), |
| 822 |
expectRegular: `-123`, |
| 823 |
expectCompact: `-123`, |
| 824 |
}, { |
| 825 |
name: "zero int", |
| 826 |
input: int(0), |
| 827 |
expectRegular: `0`, |
| 828 |
expectCompact: `0`, |
| 829 |
}, { |
| 830 |
name: "positive int8", |
| 831 |
input: int8(123), |
| 832 |
expectRegular: `123`, |
| 833 |
expectCompact: `123`, |
| 834 |
}, { |
| 835 |
name: "negative int8", |
| 836 |
input: int8(-123), |
| 837 |
expectRegular: `-123`, |
| 838 |
expectCompact: `-123`, |
| 839 |
}, { |
| 840 |
name: "zero int8", |
| 841 |
input: int8(0), |
| 842 |
expectRegular: `0`, |
| 843 |
expectCompact: `0`, |
| 844 |
}, { |
| 845 |
name: "positive int16", |
| 846 |
input: int16(1234), |
| 847 |
expectRegular: `1234`, |
| 848 |
expectCompact: `1234`, |
| 849 |
}, { |
| 850 |
name: "negative int16", |
| 851 |
input: int16(-1234), |
| 852 |
expectRegular: `-1234`, |
| 853 |
expectCompact: `-1234`, |
| 854 |
}, { |
| 855 |
name: "zero int16", |
| 856 |
input: int16(0), |
| 857 |
expectRegular: `0`, |
| 858 |
expectCompact: `0`, |
| 859 |
}, { |
| 860 |
name: "positive int32", |
| 861 |
input: int32(123456), |
| 862 |
expectRegular: `123456`, |
| 863 |
expectCompact: `123456`, |
| 864 |
}, { |
| 865 |
name: "negative int32", |
| 866 |
input: int32(-123456), |
| 867 |
expectRegular: `-123456`, |
| 868 |
expectCompact: `-123456`, |
| 869 |
}, { |
| 870 |
name: "zero int32", |
| 871 |
input: int32(0), |
| 872 |
expectRegular: `0`, |
| 873 |
expectCompact: `0`, |
| 874 |
}, { |
| 875 |
name: "positive int64", |
| 876 |
input: int64(12345678900), |
| 877 |
expectRegular: `12345678900`, |
| 878 |
expectCompact: `12345678900`, |
| 879 |
}, { |
| 880 |
name: "negative int64", |
| 881 |
input: int64(-12345678900), |
| 882 |
expectRegular: `-12345678900`, |
| 883 |
expectCompact: `-12345678900`, |
| 884 |
}, { |
| 885 |
name: "zero int64", |
| 886 |
input: int64(0), |
| 887 |
expectRegular: `0`, |
| 888 |
expectCompact: `0`, |
| 889 |
}, { |
| 890 |
name: "positive uint", |
| 891 |
input: uint(123), |
| 892 |
expectRegular: `123`, |
| 893 |
expectCompact: `123`, |
| 894 |
}, { |
| 895 |
name: "zero uint", |
| 896 |
input: uint(0), |
| 897 |
expectRegular: `0`, |
| 898 |
expectCompact: `0`, |
| 899 |
}, { |
| 900 |
name: "positive uint8", |
| 901 |
input: uint8(123), |
| 902 |
expectRegular: `123`, |
| 903 |
expectCompact: `123`, |
| 904 |
}, { |
| 905 |
name: "zero uint8", |
| 906 |
input: uint8(0), |
| 907 |
expectRegular: `0`, |
| 908 |
expectCompact: `0`, |
| 909 |
}, { |
| 910 |
name: "positive uint16", |
| 911 |
input: uint16(1234), |
| 912 |
expectRegular: `1234`, |
| 913 |
expectCompact: `1234`, |
| 914 |
}, { |
| 915 |
name: "zero uint16", |
| 916 |
input: uint16(0), |
| 917 |
expectRegular: `0`, |
| 918 |
expectCompact: `0`, |
| 919 |
}, { |
| 920 |
name: "positive uint32", |
| 921 |
input: uint32(123456), |
| 922 |
expectRegular: `123456`, |
| 923 |
expectCompact: `123456`, |
| 924 |
}, { |
| 925 |
name: "zero uint32", |
| 926 |
input: uint32(0), |
| 927 |
expectRegular: `0`, |
| 928 |
expectCompact: `0`, |
| 929 |
}, { |
| 930 |
name: "positive uint64", |
| 931 |
input: uint64(12345678900), |
| 932 |
expectRegular: `12345678900`, |
| 933 |
expectCompact: `12345678900`, |
| 934 |
}, { |
| 935 |
name: "zero uint64", |
| 936 |
input: uint64(0), |
| 937 |
expectRegular: `0`, |
| 938 |
expectCompact: `0`, |
| 939 |
}, { |
| 940 |
name: "positive float32", |
| 941 |
input: float32(3.5), |
| 942 |
expectRegular: `3.5`, |
| 943 |
expectCompact: `3.5`, |
| 944 |
}, { |
| 945 |
name: "negative float32", |
| 946 |
input: float32(-3.5), |
| 947 |
expectRegular: `-3.5`, |
| 948 |
expectCompact: `-3.5`, |
| 949 |
}, { |
| 950 |
name: "zero float32", |
| 951 |
input: float32(0), |
| 952 |
expectRegular: `0`, |
| 953 |
expectCompact: `0`, |
| 954 |
}, { |
| 955 |
name: "positive float64", |
| 956 |
input: float64(3.5), |
| 957 |
expectRegular: `3.5`, |
| 958 |
expectCompact: `3.5`, |
| 959 |
}, { |
| 960 |
name: "negative float64", |
| 961 |
input: float64(-3.5), |
| 962 |
expectRegular: `-3.5`, |
| 963 |
expectCompact: `-3.5`, |
| 964 |
}, { |
| 965 |
name: "zero float64", |
| 966 |
input: float64(0), |
| 967 |
expectRegular: `0`, |
| 968 |
expectCompact: `0`, |
| 969 |
}, { |
| 970 |
name: "true bool", |
| 971 |
input: bool(true), |
| 972 |
expectRegular: `true`, |
| 973 |
expectCompact: `true`, |
| 974 |
}, { |
| 975 |
name: "false bool", |
| 976 |
input: bool(false), |
| 977 |
expectRegular: `false`, |
| 978 |
expectCompact: `false`, |
| 979 |
}, { |
| 980 |
name: "empty string", |
| 981 |
input: "", |
| 982 |
expectRegular: `""`, |
| 983 |
expectCompact: `""`, |
| 984 |
}, { |
| 985 |
name: "regular string", |
| 986 |
input: "abc", |
| 987 |
expectRegular: `"abc"`, |
| 988 |
expectCompact: `"abc"`, |
| 989 |
}, { |
| 990 |
name: "multi-line string", |
| 991 |
input: "This\n is a\n multi-line\n string", |
| 992 |
expectRegular: "\"\\\n This\\n\\\n \\ is a\\n\\\n \\ multi-line\\n\\\n \\ string\\\n \"", |
| 993 |
expectCompact: `"This\n is a\n multi-line\n string"`, |
| 994 |
}, { |
| 995 |
name: "no-init struct", |
| 996 |
input: struct { |
| 997 |
I int |
| 998 |
S string |
| 999 |
}{}, |
| 1000 |
expectRegular: ` |
| 1001 |
{ |
| 1002 |
I: 0, |
| 1003 |
S: "", |
| 1004 |
}`, |
| 1005 |
expectCompact: `{I: 0, S: ""}`, |
| 1006 |
}, { |
| 1007 |
name: "init struct", |
| 1008 |
input: struct { |
| 1009 |
I int |
| 1010 |
S string |
| 1011 |
}{1, "one"}, |
| 1012 |
expectRegular: ` |
| 1013 |
{ |
| 1014 |
I: 1, |
| 1015 |
S: "one", |
| 1016 |
}`, |
| 1017 |
expectCompact: `{I: 1, S: "one"}`, |
| 1018 |
}, { |
| 1019 |
name: "empty struct", |
| 1020 |
input: struct{}{}, |
| 1021 |
expectRegular: `{}`, |
| 1022 |
expectCompact: `{}`, |
| 1023 |
}, { |
| 1024 |
name: "omitempty struct", |
| 1025 |
input: struct { |
| 1026 |
I int `json:",omitempty"` |
| 1027 |
S string `json:",omitempty"` |
| 1028 |
B bool `json:",omitempty"` |
| 1029 |
P *int `json:",omitempty"` |
| 1030 |
}{}, |
| 1031 |
expectRegular: `{}`, |
| 1032 |
expectCompact: `{}`, |
| 1033 |
}, { |
| 1034 |
name: "omitempty struct nil slice", |
| 1035 |
input: struct { |
| 1036 |
S []int `json:",omitempty"` |
| 1037 |
}{}, |
| 1038 |
expectRegular: `{}`, |
| 1039 |
expectCompact: `{}`, |
| 1040 |
}, { |
| 1041 |
name: "omitempty struct empty slice", |
| 1042 |
input: struct { |
| 1043 |
S []int `json:",omitempty"` |
| 1044 |
}{S: []int{}}, |
| 1045 |
expectRegular: `{}`, |
| 1046 |
expectCompact: `{}`, |
| 1047 |
}, { |
| 1048 |
name: "omitempty struct nil map", |
| 1049 |
input: struct { |
| 1050 |
M map[int]int `json:",omitempty"` |
| 1051 |
}{}, |
| 1052 |
expectRegular: `{}`, |
| 1053 |
expectCompact: `{}`, |
| 1054 |
}, { |
| 1055 |
name: "omitempty struct empty map", |
| 1056 |
input: struct { |
| 1057 |
M map[int]int `json:",omitempty"` |
| 1058 |
}{map[int]int{}}, |
| 1059 |
expectRegular: `{}`, |
| 1060 |
expectCompact: `{}`, |
| 1061 |
}, { |
| 1062 |
name: "non-empty slice", |
| 1063 |
input: []int{1, 2, 3}, |
| 1064 |
expectRegular: ` |
| 1065 |
[ |
| 1066 |
1, |
| 1067 |
2, |
| 1068 |
3, |
| 1069 |
]`, |
| 1070 |
expectCompact: `[1, 2, 3]`, |
| 1071 |
}, { |
| 1072 |
name: "empty slice", |
| 1073 |
input: []int{}, |
| 1074 |
expectRegular: `[]`, |
| 1075 |
expectCompact: `[]`, |
| 1076 |
}, { |
| 1077 |
name: "nil slice", |
| 1078 |
input: []int(nil), |
| 1079 |
expectRegular: `null`, |
| 1080 |
expectCompact: `null`, |
| 1081 |
}, { |
| 1082 |
name: "empty array", |
| 1083 |
input: [3]int{}, |
| 1084 |
expectRegular: ` |
| 1085 |
[ |
| 1086 |
0, |
| 1087 |
0, |
| 1088 |
0, |
| 1089 |
]`, |
| 1090 |
expectCompact: `[0, 0, 0]`, |
| 1091 |
}, { |
| 1092 |
name: "non-empty array", |
| 1093 |
input: [3]int{1, 2, 3}, |
| 1094 |
expectRegular: ` |
| 1095 |
[ |
| 1096 |
1, |
| 1097 |
2, |
| 1098 |
3, |
| 1099 |
]`, |
| 1100 |
expectCompact: `[1, 2, 3]`, |
| 1101 |
}, { |
| 1102 |
name: "zero-len array", |
| 1103 |
input: [0]int{}, |
| 1104 |
expectRegular: `[]`, |
| 1105 |
expectCompact: `[]`, |
| 1106 |
}, { |
| 1107 |
name: "non-empty map[string]", |
| 1108 |
input: map[string]int{"a": 1, "b": 2, "c": 3}, |
| 1109 |
expectRegular: ` |
| 1110 |
{ |
| 1111 |
a: 1, |
| 1112 |
b: 2, |
| 1113 |
c: 3, |
| 1114 |
}`, |
| 1115 |
expectCompact: `{a: 1, b: 2, c: 3}`, |
| 1116 |
}, { |
| 1117 |
name: "non-empty map[int]", |
| 1118 |
input: map[int]int{1: 1, 2: 2, 3: 3}, |
| 1119 |
expectRegular: ` |
| 1120 |
{ |
| 1121 |
"1": 1, |
| 1122 |
"2": 2, |
| 1123 |
"3": 3, |
| 1124 |
}`, |
| 1125 |
expectCompact: `{"1": 1, "2": 2, "3": 3}`, |
| 1126 |
}, { |
| 1127 |
name: "empty map", |
| 1128 |
input: map[string]int{}, |
| 1129 |
expectRegular: `{}`, |
| 1130 |
expectCompact: `{}`, |
| 1131 |
}, { |
| 1132 |
name: "nil map", |
| 1133 |
input: map[int]int(nil), |
| 1134 |
expectRegular: `null`, |
| 1135 |
expectCompact: `null`, |
| 1136 |
}, { |
| 1137 |
name: "string map with nulls", |
| 1138 |
input: map[string]int{"NULL": 1, "Null": 2, "null": 3, "unambiguous": 4, "~": 5}, |
| 1139 |
expectRegular: ` |
| 1140 |
{ |
| 1141 |
"NULL": 1, |
| 1142 |
"Null": 2, |
| 1143 |
"null": 3, |
| 1144 |
unambiguous: 4, |
| 1145 |
"~": 5, |
| 1146 |
}`, |
| 1147 |
expectCompact: `{"NULL": 1, "Null": 2, "null": 3, unambiguous: 4, "~": 5}`, |
| 1148 |
}, { |
| 1149 |
name: "string map with trues", |
| 1150 |
input: map[string]int{"ON": 1, "On": 2, "TRUE": 3, "True": 4, "YES": 5, "Yes": 6, "on": 7, "true": 8, "yes": 9}, |
| 1151 |
expectRegular: ` |
| 1152 |
{ |
| 1153 |
"ON": 1, |
| 1154 |
"On": 2, |
| 1155 |
"TRUE": 3, |
| 1156 |
"True": 4, |
| 1157 |
"YES": 5, |
| 1158 |
"Yes": 6, |
| 1159 |
"on": 7, |
| 1160 |
"true": 8, |
| 1161 |
"yes": 9, |
| 1162 |
}`, |
| 1163 |
expectCompact: `{"ON": 1, "On": 2, "TRUE": 3, "True": 4, "YES": 5, "Yes": 6, "on": 7, "true": 8, "yes": 9}`, |
| 1164 |
}, { |
| 1165 |
name: "string map with falses", |
| 1166 |
input: map[string]int{"FALSE": 1, "False": 2, "NO": 3, "No": 4, "OFF": 5, "Off": 6, "false": 7, "no": 8, "off": 9}, |
| 1167 |
expectRegular: ` |
| 1168 |
{ |
| 1169 |
"FALSE": 1, |
| 1170 |
"False": 2, |
| 1171 |
"NO": 3, |
| 1172 |
"No": 4, |
| 1173 |
"OFF": 5, |
| 1174 |
"Off": 6, |
| 1175 |
"false": 7, |
| 1176 |
"no": 8, |
| 1177 |
"off": 9, |
| 1178 |
}`, |
| 1179 |
expectCompact: `{"FALSE": 1, "False": 2, "NO": 3, "No": 4, "OFF": 5, "Off": 6, "false": 7, "no": 8, "off": 9}`, |
| 1180 |
}, { |
| 1181 |
name: "string map with ints", |
| 1182 |
input: map[string]int{"-1": 1, "1": 2, "_1": 3, "__1__2__": 4}, |
| 1183 |
expectRegular: ` |
| 1184 |
{ |
| 1185 |
"-1": 1, |
| 1186 |
"1": 2, |
| 1187 |
"_1": 3, |
| 1188 |
"__1__2__": 4, |
| 1189 |
}`, |
| 1190 |
expectCompact: `{"-1": 1, "1": 2, "_1": 3, "__1__2__": 4}`, |
| 1191 |
}, { |
| 1192 |
name: "string map with floats", |
| 1193 |
input: map[string]int{"+.inf": 1, "-.inf": 2, ".inf": 3, ".nan": 4, "3.14": 5}, |
| 1194 |
expectRegular: ` |
| 1195 |
{ |
| 1196 |
"+.inf": 1, |
| 1197 |
"-.inf": 2, |
| 1198 |
".inf": 3, |
| 1199 |
".nan": 4, |
| 1200 |
"3.14": 5, |
| 1201 |
}`, |
| 1202 |
expectCompact: `{"+.inf": 1, "-.inf": 2, ".inf": 3, ".nan": 4, "3.14": 5}`, |
| 1203 |
}, { |
| 1204 |
name: "string map with unquoted keys", |
| 1205 |
input: map[string]int{"_": 1, "_with_underscore_": 2, "safe": 3, "with-dash": 4, "with.dot": 5, "with/slash": 6}, |
| 1206 |
expectRegular: ` |
| 1207 |
{ |
| 1208 |
_: 1, |
| 1209 |
_with_underscore_: 2, |
| 1210 |
safe: 3, |
| 1211 |
with-dash: 4, |
| 1212 |
with.dot: 5, |
| 1213 |
with/slash: 6, |
| 1214 |
}`, |
| 1215 |
expectCompact: `{_: 1, _with_underscore_: 2, safe: 3, with-dash: 4, with.dot: 5, with/slash: 6}`, |
| 1216 |
}, { |
| 1217 |
name: "string map with quoted keys", |
| 1218 |
input: map[string]int{"not safe": 1, "with\\backslash": 2}, |
| 1219 |
expectRegular: ` |
| 1220 |
{ |
| 1221 |
"not safe": 1, |
| 1222 |
"with\\backslash": 2, |
| 1223 |
}`, |
| 1224 |
expectCompact: `{"not safe": 1, "with\\backslash": 2}`, |
| 1225 |
}, { |
| 1226 |
name: "string map with dash keys", |
| 1227 |
input: map[string]int{"-": 1, "-leading-dash": 2, "trailing-dash-": 3}, |
| 1228 |
expectRegular: ` |
| 1229 |
{ |
| 1230 |
"-": 1, |
| 1231 |
"-leading-dash": 2, |
| 1232 |
"trailing-dash-": 3, |
| 1233 |
}`, |
| 1234 |
expectCompact: `{"-": 1, "-leading-dash": 2, "trailing-dash-": 3}`, |
| 1235 |
}, { |
| 1236 |
name: "string map with dot keys", |
| 1237 |
input: map[string]int{".": 1, ".leading.dot": 2, "trailing.dot.": 3}, |
| 1238 |
expectRegular: ` |
| 1239 |
{ |
| 1240 |
".": 1, |
| 1241 |
".leading.dot": 2, |
| 1242 |
"trailing.dot.": 3, |
| 1243 |
}`, |
| 1244 |
expectCompact: `{".": 1, ".leading.dot": 2, "trailing.dot.": 3}`, |
| 1245 |
}, { |
| 1246 |
name: "string map with slash keys", |
| 1247 |
input: map[string]int{"/": 1, "/leading/slash": 2, "trailing/slash/": 3}, |
| 1248 |
expectRegular: ` |
| 1249 |
{ |
| 1250 |
"/": 1, |
| 1251 |
"/leading/slash": 2, |
| 1252 |
"trailing/slash/": 3, |
| 1253 |
}`, |
| 1254 |
expectCompact: `{"/": 1, "/leading/slash": 2, "trailing/slash/": 3}`, |
| 1255 |
}, { |
| 1256 |
name: "string map with dates", |
| 1257 |
input: map[string]int{"11:00": 1, "2006": 2, "2006-1-2": 3, "2006-1-2T15:4:5.999999999-08:00": 4}, |
| 1258 |
expectRegular: ` |
| 1259 |
{ |
| 1260 |
"11:00": 1, |
| 1261 |
"2006": 2, |
| 1262 |
"2006-1-2": 3, |
| 1263 |
"2006-1-2T15:4:5.999999999-08:00": 4, |
| 1264 |
}`, |
| 1265 |
expectCompact: `{"11:00": 1, "2006": 2, "2006-1-2": 3, "2006-1-2T15:4:5.999999999-08:00": 4}`, |
| 1266 |
}, { |
| 1267 |
name: "multi-line-string-key map", |
| 1268 |
input: map[string]int{ |
| 1269 |
"1\n 2\n \n3": 123, |
| 1270 |
"4\n 5\n \n6": 456, |
| 1271 |
}, |
| 1272 |
expectRegular: ` |
| 1273 |
{ |
| 1274 |
"1\n 2\n \n3": 123, |
| 1275 |
"4\n 5\n \n6": 456, |
| 1276 |
}`, |
| 1277 |
expectCompact: `{"1\n 2\n \n3": 123, "4\n 5\n \n6": 456}`, |
| 1278 |
}, { |
| 1279 |
name: "non-nil pointer", |
| 1280 |
input: new(int), |
| 1281 |
expectRegular: `0`, |
| 1282 |
expectCompact: `0`, |
| 1283 |
}, { |
| 1284 |
name: "nil pointer", |
| 1285 |
input: (*int)(nil), |
| 1286 |
expectRegular: `null`, |
| 1287 |
expectCompact: `null`, |
| 1288 |
}, { |
| 1289 |
name: "slice of struct", |
| 1290 |
input: []struct{ I int }{{1}, {2}, {3}}, |
| 1291 |
expectRegular: ` |
| 1292 |
[{ |
| 1293 |
I: 1, |
| 1294 |
}, { |
| 1295 |
I: 2, |
| 1296 |
}, { |
| 1297 |
I: 3, |
| 1298 |
}]`, |
| 1299 |
expectCompact: `[{I: 1}, {I: 2}, {I: 3}]`, |
| 1300 |
}, { |
| 1301 |
name: "slice of slice", |
| 1302 |
input: [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, |
| 1303 |
expectRegular: ` |
| 1304 |
[[ |
| 1305 |
1, |
| 1306 |
2, |
| 1307 |
3, |
| 1308 |
], [ |
| 1309 |
4, |
| 1310 |
5, |
| 1311 |
6, |
| 1312 |
], [ |
| 1313 |
7, |
| 1314 |
8, |
| 1315 |
9, |
| 1316 |
]]`, |
| 1317 |
expectCompact: `[[1, 2, 3], [4, 5, 6], [7, 8, 9]]`, |
| 1318 |
}, { |
| 1319 |
name: "slice of map", |
| 1320 |
input: []map[string]int{{"a": 1, "b": 2}, {"c": 3, "d": 4}, {"e": 5, "f": 6}}, |
| 1321 |
expectRegular: ` |
| 1322 |
[{ |
| 1323 |
a: 1, |
| 1324 |
b: 2, |
| 1325 |
}, { |
| 1326 |
c: 3, |
| 1327 |
d: 4, |
| 1328 |
}, { |
| 1329 |
e: 5, |
| 1330 |
"f": 6, |
| 1331 |
}]`, |
| 1332 |
expectCompact: `[{a: 1, b: 2}, {c: 3, d: 4}, {e: 5, "f": 6}]`, |
| 1333 |
// TODO: figure out how to make a reflect.Value where Kind() == |
| 1334 |
// Interface. As far as I can tell, ValueOf() returns either the |
| 1335 |
// underlying type's Kind or Invalid. |
| 1336 |
}} |
| 1337 |
|
| 1338 |
mkdoc := func(s string) string { |
| 1339 |
return "---\n" + s + "\n" |
| 1340 |
} |
| 1341 |
|
| 1342 |
test := func(t *testing.T, ky *Encoder, input any, expect string) { |
| 1343 |
t.Helper() |
| 1344 |
|
| 1345 |
yb, err := ky.Marshal(input) |
| 1346 |
if err != nil { |
| 1347 |
t.Fatalf("failed to marshal KYAML: %v", err) |
| 1348 |
} |
| 1349 |
want := dedent(strings.TrimPrefix(expect, "\n")) |
| 1350 |
got := dedent(strings.TrimPrefix(string(yb), "\n")) |
| 1351 |
if got != want { |
| 1352 |
t.Errorf("Marshal() got wrong result:\nwanted: %q\n got: %q", want, got) |
| 1353 |
} |
| 1354 |
|
| 1355 |
buf := bytes.Buffer{} |
| 1356 |
if err := ky.FromObject(input, &buf); err != nil { |
| 1357 |
t.Fatalf("failed to render KYAML from object: %v", err) |
| 1358 |
} |
| 1359 |
if want, got := mkdoc(want), buf.String(); got != want { |
| 1360 |
t.Errorf("FromObject() got wrong result:\nwanted: %q\n got: %q", want, got) |
| 1361 |
} |
| 1362 |
} |
| 1363 |
|
| 1364 |
for _, tt := range tests { |
| 1365 |
t.Run(tt.name, func(t *testing.T) { |
| 1366 |
t.Run("regular", func(t *testing.T) { |
| 1367 |
ky := &Encoder{} |
| 1368 |
test(t, ky, tt.input, tt.expectRegular) |
| 1369 |
}) |
| 1370 |
t.Run("compact", func(t *testing.T) { |
| 1371 |
ky := &Encoder{Compact: true} |
| 1372 |
test(t, ky, tt.input, tt.expectCompact) |
| 1373 |
}) |
| 1374 |
}) |
| 1375 |
} |
| 1376 |
} |
| 1377 |
|
| 1378 |
// TestKYAMLFromYAML checks that the KYAML output is correct for various YAML |
| 1379 |
// inputs. It takes YAML inputs in various non-KYAML forms (often, but not |
| 1380 |
// exclusively "traditional" YAML), including comments, and re-renders them as |
| 1381 |
// KYAML. |
| 1382 |
func TestKYAMLFromYAML(t *testing.T) { |
| 1383 |
type testCase struct { |
| 1384 |
name string |
| 1385 |
input string |
| 1386 |
expectRegular string |
| 1387 |
expectCompact string |
| 1388 |
} |
| 1389 |
|
| 1390 |
tests := []testCase{ |
| 1391 |
{ // Without comments |
| 1392 |
name: "empty YAML", |
| 1393 |
input: ``, |
| 1394 |
expectRegular: ``, |
| 1395 |
expectCompact: ``, |
| 1396 |
}, { |
| 1397 |
name: "int", |
| 1398 |
input: `42`, |
| 1399 |
expectRegular: ` |
| 1400 |
--- |
| 1401 |
42 |
| 1402 |
`, |
| 1403 |
expectCompact: "---\n42\n", |
| 1404 |
}, { |
| 1405 |
name: "bool", |
| 1406 |
input: `true`, |
| 1407 |
expectRegular: ` |
| 1408 |
--- |
| 1409 |
true |
| 1410 |
`, |
| 1411 |
expectCompact: "---\ntrue\n", |
| 1412 |
}, { |
| 1413 |
name: "naked string", |
| 1414 |
input: `a string`, |
| 1415 |
expectRegular: ` |
| 1416 |
--- |
| 1417 |
"a string" |
| 1418 |
`, |
| 1419 |
expectCompact: "---\n\"a string\"\n", |
| 1420 |
}, { |
| 1421 |
name: "quoted string", |
| 1422 |
input: `a string`, |
| 1423 |
expectRegular: ` |
| 1424 |
--- |
| 1425 |
"a string" |
| 1426 |
`, |
| 1427 |
expectCompact: "---\n\"a string\"\n", |
| 1428 |
}, { |
| 1429 |
name: "empty list", |
| 1430 |
input: ` |
| 1431 |
[ |
| 1432 |
] |
| 1433 |
`, |
| 1434 |
expectRegular: ` |
| 1435 |
--- |
| 1436 |
[] |
| 1437 |
`, |
| 1438 |
expectCompact: "---\n[]\n", |
| 1439 |
}, { |
| 1440 |
name: "list of int", |
| 1441 |
input: ` |
| 1442 |
--- |
| 1443 |
- 1 |
| 1444 |
- 2 |
| 1445 |
- 3 |
| 1446 |
`, |
| 1447 |
expectRegular: ` |
| 1448 |
--- |
| 1449 |
[ |
| 1450 |
1, |
| 1451 |
2, |
| 1452 |
3, |
| 1453 |
] |
| 1454 |
`, |
| 1455 |
expectCompact: "---\n[1, 2, 3]\n", |
| 1456 |
}, { |
| 1457 |
name: "list of bool", |
| 1458 |
input: ` |
| 1459 |
--- |
| 1460 |
- true |
| 1461 |
- false |
| 1462 |
- true |
| 1463 |
`, |
| 1464 |
expectRegular: ` |
| 1465 |
--- |
| 1466 |
[ |
| 1467 |
true, |
| 1468 |
false, |
| 1469 |
true, |
| 1470 |
] |
| 1471 |
`, |
| 1472 |
expectCompact: "---\n[true, false, true]\n", |
| 1473 |
}, { |
| 1474 |
name: "list of string", |
| 1475 |
input: ` |
| 1476 |
--- |
| 1477 |
- naked |
| 1478 |
- "dquoted" |
| 1479 |
- 'squoted' |
| 1480 |
`, |
| 1481 |
expectRegular: ` |
| 1482 |
--- |
| 1483 |
[ |
| 1484 |
"naked", |
| 1485 |
"dquoted", |
| 1486 |
"squoted", |
| 1487 |
] |
| 1488 |
`, |
| 1489 |
expectCompact: "---\n[\"naked\", \"dquoted\", \"squoted\"]\n", |
| 1490 |
}, { |
| 1491 |
name: "empty mapping", |
| 1492 |
input: ` |
| 1493 |
{ |
| 1494 |
} |
| 1495 |
`, |
| 1496 |
expectRegular: ` |
| 1497 |
--- |
| 1498 |
{} |
| 1499 |
`, |
| 1500 |
expectCompact: "---\n{}\n", |
| 1501 |
}, { |
| 1502 |
name: "mapping of ints", |
| 1503 |
input: ` |
| 1504 |
a: 1 |
| 1505 |
b: 2 |
| 1506 |
c: 3 |
| 1507 |
`, |
| 1508 |
expectRegular: ` |
| 1509 |
--- |
| 1510 |
{ |
| 1511 |
a: 1, |
| 1512 |
b: 2, |
| 1513 |
c: 3, |
| 1514 |
} |
| 1515 |
`, |
| 1516 |
expectCompact: "---\n{a: 1, b: 2, c: 3}\n", |
| 1517 |
}, { |
| 1518 |
name: "mapping of bool", |
| 1519 |
input: ` |
| 1520 |
a: true |
| 1521 |
b: false |
| 1522 |
c: true |
| 1523 |
`, |
| 1524 |
expectRegular: ` |
| 1525 |
--- |
| 1526 |
{ |
| 1527 |
a: true, |
| 1528 |
b: false, |
| 1529 |
c: true, |
| 1530 |
} |
| 1531 |
`, |
| 1532 |
expectCompact: "---\n{a: true, b: false, c: true}\n", |
| 1533 |
}, { |
| 1534 |
name: "mapping of string", |
| 1535 |
input: ` |
| 1536 |
a: naked |
| 1537 |
b: "dquoted" |
| 1538 |
c: 'squoted' |
| 1539 |
`, |
| 1540 |
expectRegular: ` |
| 1541 |
--- |
| 1542 |
{ |
| 1543 |
a: "naked", |
| 1544 |
b: "dquoted", |
| 1545 |
c: "squoted", |
| 1546 |
} |
| 1547 |
`, |
| 1548 |
expectCompact: "---\n{a: \"naked\", b: \"dquoted\", c: \"squoted\"}\n", |
| 1549 |
}, { |
| 1550 |
// Note: there's no escaping within single-quotes. |
| 1551 |
name: "mapping of strings with quotes", |
| 1552 |
input: ` |
| 1553 |
a: naked "with" 'quotes' embedded |
| 1554 |
b: "dquoted \"with\" 'quotes' embedded" |
| 1555 |
c: 'squoted "with" quotes embedded' |
| 1556 |
`, |
| 1557 |
expectRegular: ` |
| 1558 |
--- |
| 1559 |
{ |
| 1560 |
a: "naked \"with\" 'quotes' embedded", |
| 1561 |
b: "dquoted \"with\" 'quotes' embedded", |
| 1562 |
c: "squoted \"with\" quotes embedded", |
| 1563 |
} |
| 1564 |
`, |
| 1565 |
expectCompact: "---\n{a: \"naked \\\"with\\\" 'quotes' embedded\", b: \"dquoted \\\"with\\\" 'quotes' embedded\", c: \"squoted \\\"with\\\" quotes embedded\"}\n", |
| 1566 |
}, { |
| 1567 |
name: "list of list", |
| 1568 |
input: ` |
| 1569 |
- |
| 1570 |
- 1 |
| 1571 |
- 2 |
| 1572 |
- 3 |
| 1573 |
- |
| 1574 |
- true |
| 1575 |
- false |
| 1576 |
- true |
| 1577 |
- |
| 1578 |
- naked |
| 1579 |
- "dquoted" |
| 1580 |
- 'squoted' |
| 1581 |
`, |
| 1582 |
expectRegular: ` |
| 1583 |
--- |
| 1584 |
[[ |
| 1585 |
1, |
| 1586 |
2, |
| 1587 |
3, |
| 1588 |
], [ |
| 1589 |
true, |
| 1590 |
false, |
| 1591 |
true, |
| 1592 |
], [ |
| 1593 |
"naked", |
| 1594 |
"dquoted", |
| 1595 |
"squoted", |
| 1596 |
]] |
| 1597 |
`, |
| 1598 |
expectCompact: "---\n[[1, 2, 3], [true, false, true], [\"naked\", \"dquoted\", \"squoted\"]]\n", |
| 1599 |
}, { |
| 1600 |
name: "list of mapping", |
| 1601 |
input: ` |
| 1602 |
- a: 1 |
| 1603 |
b: 2 |
| 1604 |
c: 3 |
| 1605 |
- a: true |
| 1606 |
b: false |
| 1607 |
c: true |
| 1608 |
- a: naked |
| 1609 |
b: "dquoted" |
| 1610 |
c: 'squoted' |
| 1611 |
`, |
| 1612 |
expectRegular: ` |
| 1613 |
--- |
| 1614 |
[{ |
| 1615 |
a: 1, |
| 1616 |
b: 2, |
| 1617 |
c: 3, |
| 1618 |
}, { |
| 1619 |
a: true, |
| 1620 |
b: false, |
| 1621 |
c: true, |
| 1622 |
}, { |
| 1623 |
a: "naked", |
| 1624 |
b: "dquoted", |
| 1625 |
c: "squoted", |
| 1626 |
}] |
| 1627 |
`, |
| 1628 |
expectCompact: "---\n[{a: 1, b: 2, c: 3}, {a: true, b: false, c: true}, {a: \"naked\", b: \"dquoted\", c: \"squoted\"}]\n", |
| 1629 |
}, { |
| 1630 |
name: "mapping of list", |
| 1631 |
input: ` |
| 1632 |
a: |
| 1633 |
- 1 |
| 1634 |
- 2 |
| 1635 |
- 3 |
| 1636 |
b: |
| 1637 |
- true |
| 1638 |
- false |
| 1639 |
- true |
| 1640 |
c: |
| 1641 |
- naked |
| 1642 |
- "dquoted" |
| 1643 |
- 'squoted' |
| 1644 |
`, |
| 1645 |
expectRegular: ` |
| 1646 |
--- |
| 1647 |
{ |
| 1648 |
a: [ |
| 1649 |
1, |
| 1650 |
2, |
| 1651 |
3, |
| 1652 |
], |
| 1653 |
b: [ |
| 1654 |
true, |
| 1655 |
false, |
| 1656 |
true, |
| 1657 |
], |
| 1658 |
c: [ |
| 1659 |
"naked", |
| 1660 |
"dquoted", |
| 1661 |
"squoted", |
| 1662 |
], |
| 1663 |
} |
| 1664 |
`, |
| 1665 |
expectCompact: "---\n{a: [1, 2, 3], b: [true, false, true], c: [\"naked\", \"dquoted\", \"squoted\"]}\n", |
| 1666 |
}, { |
| 1667 |
name: "mapping with null-string keys", |
| 1668 |
input: ` |
| 1669 |
null: 123 |
| 1670 |
Null: 123 |
| 1671 |
NULL: 123 |
| 1672 |
~: 123 |
| 1673 |
`, |
| 1674 |
expectRegular: ` |
| 1675 |
--- |
| 1676 |
{ |
| 1677 |
"null": 123, |
| 1678 |
"Null": 123, |
| 1679 |
"NULL": 123, |
| 1680 |
"~": 123, |
| 1681 |
} |
| 1682 |
`, |
| 1683 |
expectCompact: "---\n{\"null\": 123, \"Null\": 123, \"NULL\": 123, \"~\": 123}\n", |
| 1684 |
}, { |
| 1685 |
name: "mapping with true-string keys", |
| 1686 |
input: ` |
| 1687 |
true: 123 |
| 1688 |
True: 123 |
| 1689 |
TRUE: 123 |
| 1690 |
on: 123 |
| 1691 |
On: 123 |
| 1692 |
ON: 123 |
| 1693 |
yes: 123 |
| 1694 |
Yes: 123 |
| 1695 |
YES: 123 |
| 1696 |
`, |
| 1697 |
expectRegular: ` |
| 1698 |
--- |
| 1699 |
{ |
| 1700 |
"true": 123, |
| 1701 |
"True": 123, |
| 1702 |
"TRUE": 123, |
| 1703 |
"on": 123, |
| 1704 |
"On": 123, |
| 1705 |
"ON": 123, |
| 1706 |
"yes": 123, |
| 1707 |
"Yes": 123, |
| 1708 |
"YES": 123, |
| 1709 |
} |
| 1710 |
`, |
| 1711 |
expectCompact: "---\n{\"true\": 123, \"True\": 123, \"TRUE\": 123, \"on\": 123, \"On\": 123, \"ON\": 123, \"yes\": 123, \"Yes\": 123, \"YES\": 123}\n", |
| 1712 |
}, { |
| 1713 |
name: "mapping with false-string keys", |
| 1714 |
input: ` |
| 1715 |
false: 123 |
| 1716 |
False: 123 |
| 1717 |
FALSE: 123 |
| 1718 |
off: 123 |
| 1719 |
Off: 123 |
| 1720 |
OFF: 123 |
| 1721 |
no: 123 |
| 1722 |
No: 123 |
| 1723 |
NO: 123 |
| 1724 |
`, |
| 1725 |
expectRegular: ` |
| 1726 |
--- |
| 1727 |
{ |
| 1728 |
"false": 123, |
| 1729 |
"False": 123, |
| 1730 |
"FALSE": 123, |
| 1731 |
"off": 123, |
| 1732 |
"Off": 123, |
| 1733 |
"OFF": 123, |
| 1734 |
"no": 123, |
| 1735 |
"No": 123, |
| 1736 |
"NO": 123, |
| 1737 |
} |
| 1738 |
`, |
| 1739 |
expectCompact: "---\n{\"false\": 123, \"False\": 123, \"FALSE\": 123, \"off\": 123, \"Off\": 123, \"OFF\": 123, \"no\": 123, \"No\": 123, \"NO\": 123}\n", |
| 1740 |
}, { |
| 1741 |
name: "mapping with int-string keys", |
| 1742 |
input: ` |
| 1743 |
1: 123 |
| 1744 |
-1: 123 |
| 1745 |
+1: 123 |
| 1746 |
_1: 123 |
| 1747 |
-_1: 123 |
| 1748 |
+_1: 123 |
| 1749 |
__1__2__: 123 |
| 1750 |
_-_1__2__: 123 |
| 1751 |
_+_1__2__: 123 |
| 1752 |
`, |
| 1753 |
expectRegular: ` |
| 1754 |
--- |
| 1755 |
{ |
| 1756 |
"1": 123, |
| 1757 |
"-1": 123, |
| 1758 |
"+1": 123, |
| 1759 |
"_1": 123, |
| 1760 |
"-_1": 123, |
| 1761 |
"+_1": 123, |
| 1762 |
"__1__2__": 123, |
| 1763 |
"_-_1__2__": 123, |
| 1764 |
"_+_1__2__": 123, |
| 1765 |
} |
| 1766 |
`, |
| 1767 |
expectCompact: "---\n{\"1\": 123, \"-1\": 123, \"+1\": 123, \"_1\": 123, \"-_1\": 123, \"+_1\": 123, \"__1__2__\": 123, \"_-_1__2__\": 123, \"_+_1__2__\": 123}\n", |
| 1768 |
}, { |
| 1769 |
name: "mapping with float-string keys", |
| 1770 |
input: ` |
| 1771 |
3.14: 123 |
| 1772 |
-3.14: 123 |
| 1773 |
+3.14: 123 |
| 1774 |
.inf: 123 |
| 1775 |
-.inf: 123 |
| 1776 |
+.inf: 123 |
| 1777 |
.nan: 123 |
| 1778 |
`, |
| 1779 |
expectRegular: ` |
| 1780 |
--- |
| 1781 |
{ |
| 1782 |
"3.14": 123, |
| 1783 |
"-3.14": 123, |
| 1784 |
"+3.14": 123, |
| 1785 |
".inf": 123, |
| 1786 |
"-.inf": 123, |
| 1787 |
"+.inf": 123, |
| 1788 |
".nan": 123, |
| 1789 |
} |
| 1790 |
`, |
| 1791 |
expectCompact: "---\n{\"3.14\": 123, \"-3.14\": 123, \"+3.14\": 123, \".inf\": 123, \"-.inf\": 123, \"+.inf\": 123, \".nan\": 123}\n", |
| 1792 |
}, { |
| 1793 |
name: "mapping with naked-string keys", |
| 1794 |
input: ` |
| 1795 |
safe: 123 |
| 1796 |
_: 123 |
| 1797 |
_with_underscore_: 123 |
| 1798 |
with-dash: 123 |
| 1799 |
with.dot: 123 |
| 1800 |
with/slash: 123 |
| 1801 |
`, |
| 1802 |
expectRegular: ` |
| 1803 |
--- |
| 1804 |
{ |
| 1805 |
safe: 123, |
| 1806 |
_: 123, |
| 1807 |
_with_underscore_: 123, |
| 1808 |
with-dash: 123, |
| 1809 |
with.dot: 123, |
| 1810 |
with/slash: 123, |
| 1811 |
} |
| 1812 |
`, |
| 1813 |
expectCompact: "---\n{safe: 123, _: 123, _with_underscore_: 123, with-dash: 123, with.dot: 123, with/slash: 123}\n", |
| 1814 |
}, { |
| 1815 |
name: "mapping with unsafe-string keys", |
| 1816 |
input: ` |
| 1817 |
not safe: 123 |
| 1818 |
with\backslash: 123 |
| 1819 |
`, |
| 1820 |
expectRegular: ` |
| 1821 |
--- |
| 1822 |
{ |
| 1823 |
"not safe": 123, |
| 1824 |
"with\\backslash": 123, |
| 1825 |
} |
| 1826 |
`, |
| 1827 |
expectCompact: "---\n{\"not safe\": 123, \"with\\\\backslash\": 123}\n", |
| 1828 |
}, { |
| 1829 |
name: "mapping with dash-string keys", |
| 1830 |
input: ` |
| 1831 |
-: 123 |
| 1832 |
-leading-dash: 123 |
| 1833 |
trailing-dash-: 123 |
| 1834 |
`, |
| 1835 |
expectRegular: ` |
| 1836 |
--- |
| 1837 |
{ |
| 1838 |
"-": 123, |
| 1839 |
"-leading-dash": 123, |
| 1840 |
"trailing-dash-": 123, |
| 1841 |
} |
| 1842 |
`, |
| 1843 |
expectCompact: "---\n{\"-\": 123, \"-leading-dash\": 123, \"trailing-dash-\": 123}\n", |
| 1844 |
}, { |
| 1845 |
name: "mapping with dot-string keys", |
| 1846 |
input: ` |
| 1847 |
.: 123 |
| 1848 |
.leading.dot: 123 |
| 1849 |
trailing.dot.: 123 |
| 1850 |
`, |
| 1851 |
expectRegular: ` |
| 1852 |
--- |
| 1853 |
{ |
| 1854 |
".": 123, |
| 1855 |
".leading.dot": 123, |
| 1856 |
"trailing.dot.": 123, |
| 1857 |
} |
| 1858 |
`, |
| 1859 |
expectCompact: "---\n{\".\": 123, \".leading.dot\": 123, \"trailing.dot.\": 123}\n", |
| 1860 |
}, { |
| 1861 |
name: "mapping with slash-string keys", |
| 1862 |
input: ` |
| 1863 |
/: 123 |
| 1864 |
/leading/slash: 123 |
| 1865 |
trailing/slash/: 123 |
| 1866 |
`, |
| 1867 |
expectRegular: ` |
| 1868 |
--- |
| 1869 |
{ |
| 1870 |
"/": 123, |
| 1871 |
"/leading/slash": 123, |
| 1872 |
"trailing/slash/": 123, |
| 1873 |
} |
| 1874 |
`, |
| 1875 |
expectCompact: "---\n{\"/\": 123, \"/leading/slash\": 123, \"trailing/slash/\": 123}\n", |
| 1876 |
}, { |
| 1877 |
name: "mapping with date-string keys", |
| 1878 |
input: ` |
| 1879 |
2006: 123 |
| 1880 |
2006-1-2: 123 |
| 1881 |
2006-1-2T15:4:5.999999999-08:00: 123 |
| 1882 |
11:00: 123 |
| 1883 |
`, |
| 1884 |
expectRegular: ` |
| 1885 |
--- |
| 1886 |
{ |
| 1887 |
"2006": 123, |
| 1888 |
"2006-1-2": 123, |
| 1889 |
"2006-1-2T15:4:5.999999999-08:00": 123, |
| 1890 |
"11:00": 123, |
| 1891 |
} |
| 1892 |
`, |
| 1893 |
expectCompact: "---\n{\"2006\": 123, \"2006-1-2\": 123, \"2006-1-2T15:4:5.999999999-08:00\": 123, \"11:00\": 123}\n", |
| 1894 |
}, { |
| 1895 |
// This case covers: multi-line, with and without leading spaces |
| 1896 |
// and blank lines. It's hard to see whitespace, so subsequent |
| 1897 |
// cases cover the same things in a more obvious way. |
| 1898 |
name: "multi-line strings", |
| 1899 |
input: ` |
| 1900 |
simple: | |
| 1901 |
This is a multi-line string. |
| 1902 |
It has multiple lines. |
| 1903 |
leading_spaces: | |
| 1904 |
This is a multi-line string. |
| 1905 |
It can |
| 1906 |
retain space indentation. |
| 1907 |
leading_tabs: | |
| 1908 |
This is a multi-line string. |
| 1909 |
It can |
| 1910 |
retain tab indentation. |
| 1911 |
blank_lines: | |
| 1912 |
This is a multi-line string. |
| 1913 |
|
| 1914 |
It can retain blank lines. |
| 1915 |
`, |
| 1916 |
expectRegular: ` |
| 1917 |
--- |
| 1918 |
{ |
| 1919 |
simple: "\ |
| 1920 |
This is a multi-line string.\n\ |
| 1921 |
It has multiple lines.\n\ |
| 1922 |
", |
| 1923 |
leading_spaces: "\ |
| 1924 |
This is a multi-line string.\n\ |
| 1925 |
\ It can\n\ |
| 1926 |
\ retain space indentation.\n\ |
| 1927 |
", |
| 1928 |
leading_tabs: "\ |
| 1929 |
This is a multi-line string.\n\ |
| 1930 |
\ It can\n\ |
| 1931 |
\ retain tab indentation.\n\ |
| 1932 |
", |
| 1933 |
blank_lines: "\ |
| 1934 |
This is a multi-line string.\n\ |
| 1935 |
\n\ |
| 1936 |
It can retain blank lines.\n\ |
| 1937 |
", |
| 1938 |
} |
| 1939 |
`, |
| 1940 |
expectCompact: "---\n{simple: \"This is a multi-line string.\\nIt has multiple lines.\\n\", leading_spaces: \"This is a multi-line string.\\n It can\\n retain space indentation.\\n\", leading_tabs: \"This is a multi-line string.\\n\\tIt can\\n\\t\\tretain tab indentation.\\n\", blank_lines: \"This is a multi-line string.\\n\\nIt can retain blank lines.\\n\"}\n", |
| 1941 |
}, { |
| 1942 |
// This case covers: multi-line, with and without leading spaces |
| 1943 |
// and blank lines. |
| 1944 |
name: "multi-line strings dquoted", |
| 1945 |
input: ` |
| 1946 |
simple: "This is a multi-line string.\nIt has multiple lines.\n" |
| 1947 |
leading_spaces: "This is a multi-line string.\n It can\n retain space indentation.\n" |
| 1948 |
leading_tabs: "This is a multi-line string.\n\tIt can\n\t\tretain tab indentation.\n" |
| 1949 |
blank_lines: "This is a multi-line string.\n\nIt can retain blank lines.\n" |
| 1950 |
`, |
| 1951 |
expectRegular: ` |
| 1952 |
--- |
| 1953 |
{ |
| 1954 |
simple: "\ |
| 1955 |
This is a multi-line string.\n\ |
| 1956 |
It has multiple lines.\n\ |
| 1957 |
", |
| 1958 |
leading_spaces: "\ |
| 1959 |
This is a multi-line string.\n\ |
| 1960 |
\ It can\n\ |
| 1961 |
\ retain space indentation.\n\ |
| 1962 |
", |
| 1963 |
leading_tabs: "\ |
| 1964 |
This is a multi-line string.\n\ |
| 1965 |
\ It can\n\ |
| 1966 |
\ retain tab indentation.\n\ |
| 1967 |
", |
| 1968 |
blank_lines: "\ |
| 1969 |
This is a multi-line string.\n\ |
| 1970 |
\n\ |
| 1971 |
It can retain blank lines.\n\ |
| 1972 |
", |
| 1973 |
} |
| 1974 |
`, |
| 1975 |
expectCompact: "---\n{simple: \"This is a multi-line string.\\nIt has multiple lines.\\n\", leading_spaces: \"This is a multi-line string.\\n It can\\n retain space indentation.\\n\", leading_tabs: \"This is a multi-line string.\\n\\tIt can\\n\\t\\tretain tab indentation.\\n\", blank_lines: \"This is a multi-line string.\\n\\nIt can retain blank lines.\\n\"}\n", |
| 1976 |
}, |
| 1977 |
{ // With comments |
| 1978 |
name: "scalar with comments", |
| 1979 |
input: ` |
| 1980 |
# This is a head comment. |
| 1981 |
# It can be multi-line. |
| 1982 |
42 # This is a line comment |
| 1983 |
# This is a foot comment. |
| 1984 |
# It can also be multi-line. |
| 1985 |
`, |
| 1986 |
expectRegular: ` |
| 1987 |
--- |
| 1988 |
# This is a head comment. |
| 1989 |
# It can be multi-line. |
| 1990 |
42 # This is a line comment |
| 1991 |
# This is a foot comment. |
| 1992 |
# It can also be multi-line. |
| 1993 |
`, |
| 1994 |
expectCompact: "---\n42\n", |
| 1995 |
}, { |
| 1996 |
name: "multi-line string with comments", |
| 1997 |
input: ` |
| 1998 |
# This is a head comment. |
| 1999 |
# It can be multi-line. |
| 2000 |
foo: | # This is a line comment |
| 2001 |
this is a |
| 2002 |
multi-line |
| 2003 |
comment |
| 2004 |
# This is a foot comment. |
| 2005 |
# It can also be multi-line. |
| 2006 |
`, |
| 2007 |
expectRegular: ` |
| 2008 |
--- |
| 2009 |
{ |
| 2010 |
# This is a head comment. |
| 2011 |
# It can be multi-line. |
| 2012 |
foo: "\ |
| 2013 |
this is a\n\ |
| 2014 |
multi-line\n\ |
| 2015 |
comment\n\ |
| 2016 |
", # This is a line comment |
| 2017 |
# This is a foot comment. |
| 2018 |
# It can also be multi-line. |
| 2019 |
} |
| 2020 |
`, |
| 2021 |
expectCompact: "---\n{foo: \"this is a\\nmulti-line\\ncomment\\n\"}\n", |
| 2022 |
}, { |
| 2023 |
name: "block sequence with comments", |
| 2024 |
input: ` |
| 2025 |
# This seems like a head comment. |
| 2026 |
# But it will be attributed to the first item. |
| 2027 |
- 1 # line1 |
| 2028 |
- 2 # line2 |
| 2029 |
- 3 # line3 |
| 2030 |
# This is a foot comment. |
| 2031 |
# It can also be multi-line. |
| 2032 |
`, |
| 2033 |
expectRegular: ` |
| 2034 |
--- |
| 2035 |
[ |
| 2036 |
# This seems like a head comment. |
| 2037 |
# But it will be attributed to the first item. |
| 2038 |
1, # line1 |
| 2039 |
2, # line2 |
| 2040 |
3, # line3 |
| 2041 |
# This is a foot comment. |
| 2042 |
# It can also be multi-line. |
| 2043 |
] |
| 2044 |
`, |
| 2045 |
expectCompact: "---\n[1, 2, 3]\n", |
| 2046 |
}, { |
| 2047 |
name: "short flow sequence with line-comment", |
| 2048 |
input: ` |
| 2049 |
# This is a head comment. |
| 2050 |
# It can be multi-line. |
| 2051 |
[ 1, 2, 3 ] # This is a line comment. |
| 2052 |
# This is a foot comment. |
| 2053 |
# It can also be multi-line. |
| 2054 |
`, |
| 2055 |
expectRegular: ` |
| 2056 |
--- |
| 2057 |
# This is a head comment. |
| 2058 |
# It can be multi-line. |
| 2059 |
[ |
| 2060 |
1, |
| 2061 |
2, |
| 2062 |
3, |
| 2063 |
] # This is a line comment. |
| 2064 |
# This is a foot comment. |
| 2065 |
# It can also be multi-line. |
| 2066 |
`, |
| 2067 |
expectCompact: "---\n[1, 2, 3]\n", |
| 2068 |
}, { |
| 2069 |
name: "flow sequence with comments", |
| 2070 |
input: ` |
| 2071 |
# This is a head comment. |
| 2072 |
# It can be multi-line. |
| 2073 |
[ # This will be lost. |
| 2074 |
1, # line1 |
| 2075 |
2, # line2 |
| 2076 |
3, # line3 |
| 2077 |
] # This is a line comment. |
| 2078 |
# This is a foot comment. |
| 2079 |
# It can also be multi-line. |
| 2080 |
`, |
| 2081 |
expectRegular: ` |
| 2082 |
--- |
| 2083 |
# This is a head comment. |
| 2084 |
# It can be multi-line. |
| 2085 |
[ |
| 2086 |
1, # line1 |
| 2087 |
2, # line2 |
| 2088 |
3, # line3 |
| 2089 |
] # This is a line comment. |
| 2090 |
# This is a foot comment. |
| 2091 |
# It can also be multi-line. |
| 2092 |
`, |
| 2093 |
expectCompact: "---\n[1, 2, 3]\n", |
| 2094 |
}, {}, { |
| 2095 |
name: "block mapping with comments", |
| 2096 |
input: ` |
| 2097 |
# This is a head comment. |
| 2098 |
# It can be multi-line. |
| 2099 |
foo: 1 # line1 |
| 2100 |
bar: 2 # line2 |
| 2101 |
# This is a foot comment. |
| 2102 |
# It can also be multi-line. |
| 2103 |
`, |
| 2104 |
expectRegular: ` |
| 2105 |
--- |
| 2106 |
{ |
| 2107 |
# This is a head comment. |
| 2108 |
# It can be multi-line. |
| 2109 |
foo: 1, # line1 |
| 2110 |
bar: 2, # line2 |
| 2111 |
# This is a foot comment. |
| 2112 |
# It can also be multi-line. |
| 2113 |
} |
| 2114 |
`, |
| 2115 |
expectCompact: "---\n{foo: 1, bar: 2}\n", |
| 2116 |
}, { |
| 2117 |
name: "short flow mapping with line-comment", |
| 2118 |
input: ` |
| 2119 |
# This is a head comment. |
| 2120 |
# It can be multi-line. |
| 2121 |
{ foo: 1, bar: 2 } # This is a line comment. |
| 2122 |
# This is a foot comment. |
| 2123 |
# It can also be multi-line. |
| 2124 |
`, |
| 2125 |
expectRegular: ` |
| 2126 |
--- |
| 2127 |
# This is a head comment. |
| 2128 |
# It can be multi-line. |
| 2129 |
{ |
| 2130 |
foo: 1, |
| 2131 |
bar: 2, |
| 2132 |
} # This is a line comment. |
| 2133 |
# This is a foot comment. |
| 2134 |
# It can also be multi-line. |
| 2135 |
`, |
| 2136 |
expectCompact: "---\n{foo: 1, bar: 2}\n", |
| 2137 |
}, { |
| 2138 |
name: "flow mapping with comments", |
| 2139 |
input: ` |
| 2140 |
# This is a head comment. |
| 2141 |
# It can be multi-line. |
| 2142 |
{ # This will be lost. |
| 2143 |
foo: 1, # line1 |
| 2144 |
bar: 2, # line2 |
| 2145 |
} # This is a line comment. |
| 2146 |
# This is a foot comment. |
| 2147 |
# It can also be multi-line. |
| 2148 |
`, |
| 2149 |
expectRegular: ` |
| 2150 |
--- |
| 2151 |
# This is a head comment. |
| 2152 |
# It can be multi-line. |
| 2153 |
{ |
| 2154 |
foo: 1, # line1 |
| 2155 |
bar: 2, # line2 |
| 2156 |
} # This is a line comment. |
| 2157 |
# This is a foot comment. |
| 2158 |
# It can also be multi-line. |
| 2159 |
`, |
| 2160 |
expectCompact: "---\n{foo: 1, bar: 2}\n", |
| 2161 |
}, { |
| 2162 |
name: "list of list with comments", |
| 2163 |
input: ` |
| 2164 |
# This is a head comment. |
| 2165 |
# It can be multi-line. |
| 2166 |
[ # This will be lost. |
| 2167 |
[1], |
| 2168 |
[2], |
| 2169 |
# head3 |
| 2170 |
[3], |
| 2171 |
[4], |
| 2172 |
# foot4 |
| 2173 |
[5], |
| 2174 |
# foot5 |
| 2175 |
# head6 |
| 2176 |
[6], |
| 2177 |
[7], # line7 |
| 2178 |
[8], |
| 2179 |
[9], # line9 |
| 2180 |
] # This is a line comment. |
| 2181 |
# This is a foot comment. |
| 2182 |
# It can also be multi-line. |
| 2183 |
`, |
| 2184 |
expectRegular: ` |
| 2185 |
--- |
| 2186 |
# This is a head comment. |
| 2187 |
# It can be multi-line. |
| 2188 |
[ |
| 2189 |
[ |
| 2190 |
1, |
| 2191 |
], |
| 2192 |
[ |
| 2193 |
2, |
| 2194 |
], |
| 2195 |
# head3 |
| 2196 |
[ |
| 2197 |
3, |
| 2198 |
], |
| 2199 |
[ |
| 2200 |
4, |
| 2201 |
], |
| 2202 |
# foot4 |
| 2203 |
[ |
| 2204 |
5, |
| 2205 |
], |
| 2206 |
# foot5 |
| 2207 |
# head6 |
| 2208 |
[ |
| 2209 |
6, |
| 2210 |
], |
| 2211 |
[ |
| 2212 |
7, |
| 2213 |
], # line7 |
| 2214 |
[ |
| 2215 |
8, |
| 2216 |
], |
| 2217 |
[ |
| 2218 |
9, |
| 2219 |
], # line9 |
| 2220 |
] # This is a line comment. |
| 2221 |
# This is a foot comment. |
| 2222 |
# It can also be multi-line. |
| 2223 |
`, |
| 2224 |
expectCompact: "---\n[[1], [2], [3], [4], [5], [6], [7], [8], [9]]\n", |
| 2225 |
}, { |
| 2226 |
name: "list of map with comments", |
| 2227 |
input: ` |
| 2228 |
# This is a head comment. |
| 2229 |
# It can be multi-line. |
| 2230 |
[ # This will be lost. |
| 2231 |
{fld: 1}, |
| 2232 |
{fld: 2}, |
| 2233 |
# head3 |
| 2234 |
{fld: 3}, |
| 2235 |
{fld: 4}, |
| 2236 |
# foot4 |
| 2237 |
{fld: 5}, |
| 2238 |
# foot5 |
| 2239 |
# head6 |
| 2240 |
{fld: 6}, |
| 2241 |
{fld: 7}, # line7 |
| 2242 |
{fld: 8}, |
| 2243 |
{fld: 9}, # line9 |
| 2244 |
] # This is a line comment. |
| 2245 |
# This is a foot comment. |
| 2246 |
# It can also be multi-line. |
| 2247 |
`, |
| 2248 |
expectRegular: ` |
| 2249 |
--- |
| 2250 |
# This is a head comment. |
| 2251 |
# It can be multi-line. |
| 2252 |
[ |
| 2253 |
{ |
| 2254 |
fld: 1, |
| 2255 |
}, |
| 2256 |
{ |
| 2257 |
fld: 2, |
| 2258 |
}, |
| 2259 |
# head3 |
| 2260 |
{ |
| 2261 |
fld: 3, |
| 2262 |
}, |
| 2263 |
{ |
| 2264 |
fld: 4, |
| 2265 |
}, |
| 2266 |
# foot4 |
| 2267 |
{ |
| 2268 |
fld: 5, |
| 2269 |
}, |
| 2270 |
# foot5 |
| 2271 |
# head6 |
| 2272 |
{ |
| 2273 |
fld: 6, |
| 2274 |
}, |
| 2275 |
{ |
| 2276 |
fld: 7, |
| 2277 |
}, # line7 |
| 2278 |
{ |
| 2279 |
fld: 8, |
| 2280 |
}, |
| 2281 |
{ |
| 2282 |
fld: 9, |
| 2283 |
}, # line9 |
| 2284 |
] # This is a line comment. |
| 2285 |
# This is a foot comment. |
| 2286 |
# It can also be multi-line. |
| 2287 |
`, |
| 2288 |
expectCompact: "---\n[{fld: 1}, {fld: 2}, {fld: 3}, {fld: 4}, {fld: 5}, {fld: 6}, {fld: 7}, {fld: 8}, {fld: 9}]\n", |
| 2289 |
}, { |
| 2290 |
name: "list of mixed types", |
| 2291 |
input: ` |
| 2292 |
[ |
| 2293 |
"a string", |
| 2294 |
12345, |
| 2295 |
true, |
| 2296 |
{fld: 1}, |
| 2297 |
[a, b, c], |
| 2298 |
] |
| 2299 |
`, |
| 2300 |
expectRegular: ` |
| 2301 |
--- |
| 2302 |
[ |
| 2303 |
"a string", |
| 2304 |
12345, |
| 2305 |
true, |
| 2306 |
{ |
| 2307 |
fld: 1, |
| 2308 |
}, |
| 2309 |
[ |
| 2310 |
"a", |
| 2311 |
"b", |
| 2312 |
"c", |
| 2313 |
], |
| 2314 |
] |
| 2315 |
`, |
| 2316 |
expectCompact: "---\n[\"a string\", 12345, true, {fld: 1}, [\"a\", \"b\", \"c\"]]\n", |
| 2317 |
}, { |
| 2318 |
name: "doc comments", |
| 2319 |
input: ` |
| 2320 |
# This seems like a doc comment. |
| 2321 |
# But it will be attributed to the content. |
| 2322 |
--- |
| 2323 |
# This is a head comment. |
| 2324 |
42 # This is a line comment |
| 2325 |
# This is a foot comment. |
| 2326 |
# It can also be multi-line. |
| 2327 |
`, |
| 2328 |
expectRegular: ` |
| 2329 |
--- |
| 2330 |
# This seems like a doc comment. |
| 2331 |
# But it will be attributed to the content. |
| 2332 |
# This is a head comment. |
| 2333 |
42 # This is a line comment |
| 2334 |
# This is a foot comment. |
| 2335 |
# It can also be multi-line. |
| 2336 |
`, |
| 2337 |
expectCompact: "---\n42\n", |
| 2338 |
}, { |
| 2339 |
name: "YAML alias", |
| 2340 |
input: ` |
| 2341 |
list: |
| 2342 |
- &alias 42 |
| 2343 |
- 43 |
| 2344 |
- *alias |
| 2345 |
`, |
| 2346 |
expectRegular: ` |
| 2347 |
--- |
| 2348 |
{ |
| 2349 |
list: [ |
| 2350 |
42, |
| 2351 |
43, |
| 2352 |
42, |
| 2353 |
], |
| 2354 |
} |
| 2355 |
`, |
| 2356 |
expectCompact: "---\n{list: [42, 43, 42]}\n", |
| 2357 |
}, |
| 2358 |
} |
| 2359 |
|
| 2360 |
test := func(t *testing.T, ky *Encoder, input string, expect string) { |
| 2361 |
t.Helper() |
| 2362 |
|
| 2363 |
input = dedent(strings.TrimLeft(input, "\n")) |
| 2364 |
expect = dedent(strings.TrimPrefix(expect, "\n")) |
| 2365 |
|
| 2366 |
buf := bytes.Buffer{} |
| 2367 |
if err := ky.FromYAML(strings.NewReader(input), &buf); err != nil { |
| 2368 |
t.Fatalf("failed to render KYAML from YAML: %v", err) |
| 2369 |
} |
| 2370 |
if want, got := expect, buf.String(); got != want { |
| 2371 |
t.Errorf("FromYAML() got wrong result:\nwanted:\n```\n%s\n```\ngot:\n```\n%s\n```\n\nor:\n\nwanted: %q\n got: %q\n", want, got, want, got) |
| 2372 |
} |
| 2373 |
} |
| 2374 |
|
| 2375 |
for _, tt := range tests { |
| 2376 |
t.Run(tt.name, func(t *testing.T) { |
| 2377 |
t.Run("regular", func(t *testing.T) { |
| 2378 |
ky := &Encoder{} |
| 2379 |
test(t, ky, tt.input, tt.expectRegular) |
| 2380 |
}) |
| 2381 |
t.Run("compact", func(t *testing.T) { |
| 2382 |
ky := &Encoder{Compact: true} |
| 2383 |
test(t, ky, tt.input, tt.expectCompact) |
| 2384 |
}) |
| 2385 |
}) |
| 2386 |
} |
| 2387 |
} |
| 2388 |
|
| 2389 |
func TestIsTypeAmbiguous(t *testing.T) { |
| 2390 |
type testCase struct { |
| 2391 |
name string |
| 2392 |
input string |
| 2393 |
expected bool |
| 2394 |
} |
| 2395 |
|
| 2396 |
tests := []testCase{ |
| 2397 |
// Regular strings that should not need quotes |
| 2398 |
{"regular string", "regular", false}, |
| 2399 |
{"alphanumeric", "abc123", false}, |
| 2400 |
{"underscore", "hello_world", false}, |
| 2401 |
{"hyphen", "hello-world", false}, |
| 2402 |
|
| 2403 |
// Boolean-like strings |
| 2404 |
{"yes", "yes", true}, |
| 2405 |
{"YES", "YES", true}, |
| 2406 |
{"y", "y", true}, |
| 2407 |
{"Y", "Y", true}, |
| 2408 |
{"no", "no", true}, |
| 2409 |
{"NO", "NO", true}, |
| 2410 |
{"n", "n", true}, |
| 2411 |
{"N", "N", true}, |
| 2412 |
{"on", "on", true}, |
| 2413 |
{"ON", "ON", true}, |
| 2414 |
{"off", "off", true}, |
| 2415 |
{"OFF", "OFF", true}, |
| 2416 |
|
| 2417 |
// Numbers should stay strings |
| 2418 |
{"decimal", "1234", true}, |
| 2419 |
{"underscores", "_1_2_3_4_", true}, |
| 2420 |
{"leading zero", "0123", true}, |
| 2421 |
{"plus sign", "+123", true}, |
| 2422 |
{"negative sign", "-123", true}, |
| 2423 |
{"large decimal", "123456789012345678901234567890", true}, |
| 2424 |
{"octal 0", "0777", true}, |
| 2425 |
{"octal 0o", "0o777", true}, |
| 2426 |
{"hex lowercase", "0xff", true}, |
| 2427 |
{"hex uppercase", "0xFF", true}, |
| 2428 |
|
| 2429 |
// Infinity and NaN |
| 2430 |
{"infinity", ".inf", true}, |
| 2431 |
{"negative infinity", "-.inf", true}, |
| 2432 |
{"positive infinity", "+.inf", true}, |
| 2433 |
{"not a number", ".nan", true}, |
| 2434 |
{"uppercase infinity", ".INF", true}, |
| 2435 |
{"uppercase nan", ".NAN", true}, |
| 2436 |
|
| 2437 |
// Scientific notation |
| 2438 |
{"scientific", "1e10", true}, |
| 2439 |
{"scientific uppercase", "1E10", true}, |
| 2440 |
|
| 2441 |
// Timestamp-like strings |
| 2442 |
{"year", "2006", true}, |
| 2443 |
{"date", "2006-1-2", true}, |
| 2444 |
{"RCF3339Nano with short date", "2006-1-2T15:4:5.999999999-08:00", true}, |
| 2445 |
{"RCF3339Nano with lowercase t", "2006-1-2t15:4:5.999999999-08:00", true}, |
| 2446 |
{"space separated", "2006-1-2 14:4:5.999999999", true}, |
| 2447 |
|
| 2448 |
// Sexagesimal strings |
| 2449 |
{"small sexagesimal int", "1:00", true}, |
| 2450 |
{"large sexagesimal int 59", "12345:59", true}, |
| 2451 |
{"invalid sexagesimal int", "1:60", false}, |
| 2452 |
{"multi-part sexagesimal", "1:2:3:4:5:00:00:01", true}, |
| 2453 |
{"small sexagesimal float 0", "1:00.0", true}, |
| 2454 |
{"small sexagesimal float 59", "12345:59.59", true}, |
| 2455 |
{"small non-sexagesimal int", "12345:60.00", false}, |
| 2456 |
{"multi-part sexagesimal", "1:2:3:4:5:00:00:01.02", true}, |
| 2457 |
|
| 2458 |
// Null-like strings |
| 2459 |
{"null lowercase", "null", true}, |
| 2460 |
{"null mixed", "Null", true}, |
| 2461 |
{"null uppercase", "NULL", true}, |
| 2462 |
{"tilde", "~", true}, |
| 2463 |
} |
| 2464 |
|
| 2465 |
for _, tt := range tests { |
| 2466 |
t.Run(tt.name, func(t *testing.T) { |
| 2467 |
result := isTypeAmbiguous(tt.input) |
| 2468 |
if result != tt.expected { |
| 2469 |
t.Errorf("isTypeAmbiguous(%q) = %v, want %v", tt.input, result, tt.expected) |
| 2470 |
} |
| 2471 |
}) |
| 2472 |
} |
| 2473 |
} |
| 2474 |
|
| 2475 |
func TestRenderStringEscapes(t *testing.T) { |
| 2476 |
simpleCases := []struct { |
| 2477 |
name string |
| 2478 |
input rune |
| 2479 |
expectRegular string |
| 2480 |
expectCompact string |
| 2481 |
}{{ |
| 2482 |
name: "backslash", |
| 2483 |
input: '\\', |
| 2484 |
expectRegular: `"\\"`, |
| 2485 |
expectCompact: `"\\"`, |
| 2486 |
}, { |
| 2487 |
name: "dquote", |
| 2488 |
input: '"', |
| 2489 |
expectRegular: `"\""`, |
| 2490 |
expectCompact: `"\""`, |
| 2491 |
}, { |
| 2492 |
name: "bell", |
| 2493 |
input: '\a', |
| 2494 |
expectRegular: `"\a"`, |
| 2495 |
expectCompact: `"\a"`, |
| 2496 |
}, { |
| 2497 |
name: "backspace", |
| 2498 |
input: '\b', |
| 2499 |
expectRegular: `"\b"`, |
| 2500 |
expectCompact: `"\b"`, |
| 2501 |
}, { |
| 2502 |
name: "ff", |
| 2503 |
input: '\f', |
| 2504 |
expectRegular: `"\f"`, |
| 2505 |
expectCompact: `"\f"`, |
| 2506 |
}, { |
| 2507 |
name: "nl", |
| 2508 |
input: '\n', |
| 2509 |
expectRegular: "\"\\\n \\n\\\n\"", |
| 2510 |
expectCompact: "\"\\n\"", |
| 2511 |
}, { |
| 2512 |
name: "cr", |
| 2513 |
input: '\r', |
| 2514 |
expectRegular: `"\r"`, |
| 2515 |
expectCompact: `"\r"`, |
| 2516 |
}, { |
| 2517 |
name: "tab", |
| 2518 |
input: '\t', |
| 2519 |
expectRegular: "\"\t\"", |
| 2520 |
expectCompact: "\"\\t\"", |
| 2521 |
}, { |
| 2522 |
name: "vtab", |
| 2523 |
input: '\v', |
| 2524 |
expectRegular: `"\v"`, |
| 2525 |
expectCompact: `"\v"`, |
| 2526 |
}, { |
| 2527 |
name: "null", |
| 2528 |
input: '\x00', |
| 2529 |
expectRegular: `"\0"`, |
| 2530 |
expectCompact: `"\0"`, |
| 2531 |
}, { |
| 2532 |
name: "esc", |
| 2533 |
input: '\x1b', |
| 2534 |
expectRegular: `"\e"`, |
| 2535 |
expectCompact: `"\e"`, |
| 2536 |
}, { |
| 2537 |
name: "nextline", |
| 2538 |
input: '\u0085', |
| 2539 |
expectRegular: `"\N"`, |
| 2540 |
expectCompact: `"\N"`, |
| 2541 |
}, { |
| 2542 |
name: "nbsp", |
| 2543 |
input: '\u00a0', |
| 2544 |
expectRegular: `"\_"`, |
| 2545 |
expectCompact: `"\_"`, |
| 2546 |
}, { |
| 2547 |
name: "linesep", |
| 2548 |
input: '\u2028', |
| 2549 |
expectRegular: `"\L"`, |
| 2550 |
expectCompact: `"\L"`, |
| 2551 |
}, { |
| 2552 |
name: "parasep", |
| 2553 |
input: '\u2029', |
| 2554 |
expectRegular: `"\P"`, |
| 2555 |
expectCompact: `"\P"`, |
| 2556 |
}, { |
| 2557 |
name: "x01", |
| 2558 |
input: '\x01', |
| 2559 |
expectRegular: `"\x01"`, |
| 2560 |
expectCompact: `"\x01"`, |
| 2561 |
}, { |
| 2562 |
name: "uffff", |
| 2563 |
input: '\uffff', |
| 2564 |
expectRegular: `"\uffff"`, |
| 2565 |
expectCompact: `"\uffff"`, |
| 2566 |
}, { |
| 2567 |
name: "U0010ffff", |
| 2568 |
input: '\U0010ffff', |
| 2569 |
expectRegular: `"\U0010ffff"`, |
| 2570 |
expectCompact: `"\U0010ffff"`, |
| 2571 |
}} |
| 2572 |
|
| 2573 |
test := func(t *testing.T, input string, flags flagMask, expect string) { |
| 2574 |
t.Helper() |
| 2575 |
ky := &Encoder{} |
| 2576 |
buf := &bytes.Buffer{} |
| 2577 |
err := ky.renderString(input, 0, flags, buf) |
| 2578 |
if err != nil { |
| 2579 |
t.Fatalf("renderString(%q) returned error: %v", input, err) |
| 2580 |
} |
| 2581 |
if result := buf.String(); result != expect { |
| 2582 |
t.Errorf("renderString(%q): want %q, got %q", input, expect, result) |
| 2583 |
} |
| 2584 |
} |
| 2585 |
|
| 2586 |
for _, tt := range simpleCases { |
| 2587 |
t.Run(tt.name, func(t *testing.T) { |
| 2588 |
t.Run("regular", func(t *testing.T) { |
| 2589 |
test(t, string(tt.input), flagsNone, tt.expectRegular) |
| 2590 |
}) |
| 2591 |
t.Run("compact", func(t *testing.T) { |
| 2592 |
test(t, string(tt.input), flagCompact, tt.expectCompact) |
| 2593 |
}) |
| 2594 |
}) |
| 2595 |
} |
| 2596 |
} |
| 2597 |
|
| 2598 |
func TestUnexpectedErrors(t *testing.T) { |
| 2599 |
// This test is to ensure that we don't panic when we encounter unexpected |
| 2600 |
// errors, such as nil pointers or other runtime errors. |
| 2601 |
ky := &Encoder{} |
| 2602 |
|
| 2603 |
t.Run("renderNode(nil)", func(t *testing.T) { |
| 2604 |
err := ky.renderNode(nil, 0, 0, nil) |
| 2605 |
if err != nil { |
| 2606 |
t.Errorf("unexpected error: %v", err) |
| 2607 |
} |
| 2608 |
}) |
| 2609 |
|
| 2610 |
t.Run("renderNode(<unknown kind>)", func(t *testing.T) { |
| 2611 |
n := yamlv3.Node{ |
| 2612 |
Kind: 0, |
| 2613 |
} |
| 2614 |
err := ky.renderNode(&n, 0, 0, nil) |
| 2615 |
if err == nil { |
| 2616 |
t.Errorf("expected error") |
| 2617 |
} |
| 2618 |
}) |
| 2619 |
|
| 2620 |
t.Run("YAML Node without tag", func(t *testing.T) { |
| 2621 |
n := yamlv3.Node{ |
| 2622 |
Kind: yamlv3.DocumentNode, |
| 2623 |
Content: []*yamlv3.Node{ |
| 2624 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "test", Tag: ""}, |
| 2625 |
}, |
| 2626 |
} |
| 2627 |
err := ky.renderDocument(&n, 0, 0, nil) |
| 2628 |
if err == nil { |
| 2629 |
t.Errorf("expected error") |
| 2630 |
} |
| 2631 |
}) |
| 2632 |
|
| 2633 |
t.Run("renderDocument", func(t *testing.T) { |
| 2634 |
buf := bytes.Buffer{} |
| 2635 |
t.Run("should pass", func(t *testing.T) { |
| 2636 |
n := yamlv3.Node{ |
| 2637 |
Kind: yamlv3.DocumentNode, |
| 2638 |
Content: []*yamlv3.Node{ |
| 2639 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "test", Tag: "!!str"}, |
| 2640 |
}, |
| 2641 |
} |
| 2642 |
err := ky.renderDocument(&n, 0, 0, &buf) |
| 2643 |
if err != nil { |
| 2644 |
t.Errorf("unexpected error: %v", err) |
| 2645 |
} |
| 2646 |
}) |
| 2647 |
t.Run("0 contents", func(t *testing.T) { |
| 2648 |
n := yamlv3.Node{ |
| 2649 |
Kind: yamlv3.DocumentNode, |
| 2650 |
Content: nil, |
| 2651 |
} |
| 2652 |
err := ky.renderDocument(&n, 0, 0, &buf) |
| 2653 |
if err == nil { |
| 2654 |
t.Errorf("expected error") |
| 2655 |
} |
| 2656 |
}) |
| 2657 |
t.Run("2 contents", func(t *testing.T) { |
| 2658 |
n := yamlv3.Node{ |
| 2659 |
Kind: yamlv3.DocumentNode, |
| 2660 |
Content: []*yamlv3.Node{ |
| 2661 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "test1", Tag: "!!str"}, |
| 2662 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "test2", Tag: "!!str"}, |
| 2663 |
}, |
| 2664 |
} |
| 2665 |
err := ky.renderDocument(&n, 0, 0, &buf) |
| 2666 |
if err == nil { |
| 2667 |
t.Errorf("expected error") |
| 2668 |
} |
| 2669 |
}) |
| 2670 |
t.Run("non-zero indent", func(t *testing.T) { |
| 2671 |
n := yamlv3.Node{ |
| 2672 |
Kind: yamlv3.DocumentNode, |
| 2673 |
Content: []*yamlv3.Node{ |
| 2674 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "test", Tag: "!!str"}, |
| 2675 |
}, |
| 2676 |
} |
| 2677 |
err := ky.renderDocument(&n, 1, 0, &buf) |
| 2678 |
if err == nil { |
| 2679 |
t.Errorf("expected error") |
| 2680 |
} |
| 2681 |
}) |
| 2682 |
}) |
| 2683 |
|
| 2684 |
t.Run("renderScalar", func(t *testing.T) { |
| 2685 |
buf := bytes.Buffer{} |
| 2686 |
t.Run("should pass", func(t *testing.T) { |
| 2687 |
n := yamlv3.Node{ |
| 2688 |
Kind: yamlv3.ScalarNode, |
| 2689 |
Value: "test", |
| 2690 |
Tag: "!!str", |
| 2691 |
} |
| 2692 |
err := ky.renderScalar(&n, 0, 0, &buf) |
| 2693 |
if err != nil { |
| 2694 |
t.Errorf("unexpected error: %v", err) |
| 2695 |
} |
| 2696 |
}) |
| 2697 |
t.Run("unknown tag", func(t *testing.T) { |
| 2698 |
n := yamlv3.Node{ |
| 2699 |
Kind: yamlv3.ScalarNode, |
| 2700 |
Value: "test", |
| 2701 |
Tag: "", |
| 2702 |
} |
| 2703 |
err := ky.renderScalar(&n, 0, 0, &buf) |
| 2704 |
if err == nil { |
| 2705 |
t.Errorf("expected error") |
| 2706 |
} |
| 2707 |
}) |
| 2708 |
}) |
| 2709 |
|
| 2710 |
t.Run("renderMapping", func(t *testing.T) { |
| 2711 |
buf := bytes.Buffer{} |
| 2712 |
t.Run("should pass 1", func(t *testing.T) { |
| 2713 |
n := yamlv3.Node{ |
| 2714 |
Kind: yamlv3.MappingNode, |
| 2715 |
Content: []*yamlv3.Node{ |
| 2716 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "key", Tag: "!!str"}, |
| 2717 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "val", Tag: "!!str"}, |
| 2718 |
}, |
| 2719 |
} |
| 2720 |
err := ky.renderMapping(&n, 0, 0, &buf) |
| 2721 |
if err != nil { |
| 2722 |
t.Errorf("unexpected error: %v", err) |
| 2723 |
} |
| 2724 |
}) |
| 2725 |
t.Run("should pass 2", func(t *testing.T) { |
| 2726 |
n := yamlv3.Node{ |
| 2727 |
Kind: yamlv3.MappingNode, |
| 2728 |
Content: []*yamlv3.Node{ |
| 2729 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "key", Tag: "!!str", LineComment: "line comment"}, |
| 2730 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "val", Tag: "!!str"}, |
| 2731 |
}, |
| 2732 |
} |
| 2733 |
err := ky.renderMapping(&n, 0, 0, &buf) |
| 2734 |
if err != nil { |
| 2735 |
t.Errorf("unexpected error: %v", err) |
| 2736 |
} |
| 2737 |
}) |
| 2738 |
t.Run("should pass 3", func(t *testing.T) { |
| 2739 |
n := yamlv3.Node{ |
| 2740 |
Kind: yamlv3.MappingNode, |
| 2741 |
Content: []*yamlv3.Node{ |
| 2742 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "key", Tag: "!!str"}, |
| 2743 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "val", Tag: "!!str", LineComment: "line comment"}, |
| 2744 |
}, |
| 2745 |
} |
| 2746 |
err := ky.renderMapping(&n, 0, 0, &buf) |
| 2747 |
if err != nil { |
| 2748 |
t.Errorf("unexpected error: %v", err) |
| 2749 |
} |
| 2750 |
}) |
| 2751 |
t.Run("line-comments on key and val", func(t *testing.T) { |
| 2752 |
n := yamlv3.Node{ |
| 2753 |
Kind: yamlv3.MappingNode, |
| 2754 |
Content: []*yamlv3.Node{ |
| 2755 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "key", Tag: "!!str", LineComment: "line comment"}, |
| 2756 |
&yamlv3.Node{Kind: yamlv3.ScalarNode, Value: "val", Tag: "!!str", LineComment: "line comment"}, |
| 2757 |
}, |
| 2758 |
} |
| 2759 |
err := ky.renderMapping(&n, 0, 0, &buf) |
| 2760 |
if err == nil { |
| 2761 |
t.Errorf("expected error") |
| 2762 |
} |
| 2763 |
}) |
| 2764 |
}) |
| 2765 |
} |
| 2766 |
|
| 2767 |
func TestNodeKindString(t *testing.T) { |
| 2768 |
tests := []struct { |
| 2769 |
kind yamlv3.Kind |
| 2770 |
expected string |
| 2771 |
}{ |
| 2772 |
{yamlv3.DocumentNode, "document"}, |
| 2773 |
{yamlv3.ScalarNode, "scalar"}, |
| 2774 |
{yamlv3.MappingNode, "mapping"}, |
| 2775 |
{yamlv3.SequenceNode, "sequence"}, |
| 2776 |
{yamlv3.AliasNode, "alias"}, |
| 2777 |
{0, "unknown"}, |
| 2778 |
} |
| 2779 |
|
| 2780 |
ky := &Encoder{} |
| 2781 |
for _, tt := range tests { |
| 2782 |
t.Run(tt.expected, func(t *testing.T) { |
| 2783 |
if got := ky.nodeKindString(tt.kind); got != tt.expected { |
| 2784 |
t.Errorf("nodeKindString(%d) = %q, want %q", tt.kind, got, tt.expected) |
| 2785 |
} |
| 2786 |
}) |
| 2787 |
} |
| 2788 |
} |
| 2789 |
|
| 2790 |
func TestIsCuddledKind(t *testing.T) { |
| 2791 |
tests := []struct { |
| 2792 |
node yamlv3.Node |
| 2793 |
expected bool |
| 2794 |
}{ |
| 2795 |
{yamlv3.Node{Kind: yamlv3.SequenceNode}, true}, |
| 2796 |
{yamlv3.Node{Kind: yamlv3.MappingNode}, true}, |
| 2797 |
{yamlv3.Node{Kind: yamlv3.DocumentNode}, false}, |
| 2798 |
{yamlv3.Node{Kind: yamlv3.ScalarNode}, false}, |
| 2799 |
{yamlv3.Node{Kind: yamlv3.AliasNode, Alias: &yamlv3.Node{Kind: yamlv3.SequenceNode}}, true}, |
| 2800 |
{yamlv3.Node{Kind: yamlv3.AliasNode, Alias: &yamlv3.Node{Kind: yamlv3.ScalarNode}}, false}, |
| 2801 |
{yamlv3.Node{Kind: 0}, false}, |
| 2802 |
} |
| 2803 |
|
| 2804 |
for _, tt := range tests { |
| 2805 |
if got := isCuddledKind(&tt.node); got != tt.expected { |
| 2806 |
t.Errorf("isCuddledKind(%v) = %v, want %v", tt.node, got, tt.expected) |
| 2807 |
} |
| 2808 |
} |
| 2809 |
} |