| 1 |
package yaml_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"log" |
| 9 |
"math" |
| 10 |
"net" |
| 11 |
"reflect" |
| 12 |
"strconv" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
"time" |
| 16 |
|
| 17 |
"github.com/goccy/go-yaml" |
| 18 |
"github.com/goccy/go-yaml/ast" |
| 19 |
"github.com/goccy/go-yaml/internal/errors" |
| 20 |
"github.com/goccy/go-yaml/parser" |
| 21 |
) |
| 22 |
|
| 23 |
type Child struct { |
| 24 |
B int |
| 25 |
C int `yaml:"-"` |
| 26 |
} |
| 27 |
|
| 28 |
func TestDecoder(t *testing.T) { |
| 29 |
tests := []struct { |
| 30 |
source string |
| 31 |
value interface{} |
| 32 |
}{ |
| 33 |
{ |
| 34 |
"null\n", |
| 35 |
(*struct{})(nil), |
| 36 |
}, |
| 37 |
{ |
| 38 |
"v: hi\n", |
| 39 |
map[string]string{"v": "hi"}, |
| 40 |
}, |
| 41 |
{ |
| 42 |
"v: \"true\"\n", |
| 43 |
map[string]string{"v": "true"}, |
| 44 |
}, |
| 45 |
{ |
| 46 |
"v: \"false\"\n", |
| 47 |
map[string]string{"v": "false"}, |
| 48 |
}, |
| 49 |
{ |
| 50 |
"v: true\n", |
| 51 |
map[string]interface{}{"v": true}, |
| 52 |
}, |
| 53 |
{ |
| 54 |
"v: true\n", |
| 55 |
map[string]string{"v": "true"}, |
| 56 |
}, |
| 57 |
{ |
| 58 |
"v: 10\n", |
| 59 |
map[string]string{"v": "10"}, |
| 60 |
}, |
| 61 |
{ |
| 62 |
"v: -10\n", |
| 63 |
map[string]string{"v": "-10"}, |
| 64 |
}, |
| 65 |
{ |
| 66 |
"v: 1.234\n", |
| 67 |
map[string]string{"v": "1.234"}, |
| 68 |
}, |
| 69 |
{ |
| 70 |
"v: \" foo\"\n", |
| 71 |
map[string]string{"v": " foo"}, |
| 72 |
}, |
| 73 |
{ |
| 74 |
"v: \"foo \"\n", |
| 75 |
map[string]string{"v": "foo "}, |
| 76 |
}, |
| 77 |
{ |
| 78 |
"v: \" foo \"\n", |
| 79 |
map[string]string{"v": " foo "}, |
| 80 |
}, |
| 81 |
{ |
| 82 |
"v: false\n", |
| 83 |
map[string]bool{"v": false}, |
| 84 |
}, |
| 85 |
{ |
| 86 |
"v: 10\n", |
| 87 |
map[string]int{"v": 10}, |
| 88 |
}, |
| 89 |
{ |
| 90 |
"v: 10", |
| 91 |
map[string]interface{}{"v": 10}, |
| 92 |
}, |
| 93 |
{ |
| 94 |
"v: 0b10", |
| 95 |
map[string]interface{}{"v": 2}, |
| 96 |
}, |
| 97 |
{ |
| 98 |
"v: -0b101010", |
| 99 |
map[string]interface{}{"v": -42}, |
| 100 |
}, |
| 101 |
{ |
| 102 |
"v: -0b1000000000000000000000000000000000000000000000000000000000000000", |
| 103 |
map[string]interface{}{"v": int64(-9223372036854775808)}, |
| 104 |
}, |
| 105 |
{ |
| 106 |
"v: 0xA", |
| 107 |
map[string]interface{}{"v": 10}, |
| 108 |
}, |
| 109 |
{ |
| 110 |
"v: .1", |
| 111 |
map[string]interface{}{"v": 0.1}, |
| 112 |
}, |
| 113 |
{ |
| 114 |
"v: -.1", |
| 115 |
map[string]interface{}{"v": -0.1}, |
| 116 |
}, |
| 117 |
{ |
| 118 |
"v: -10\n", |
| 119 |
map[string]int{"v": -10}, |
| 120 |
}, |
| 121 |
{ |
| 122 |
"v: 4294967296\n", |
| 123 |
map[string]int64{"v": int64(4294967296)}, |
| 124 |
}, |
| 125 |
{ |
| 126 |
"v: 0.1\n", |
| 127 |
map[string]interface{}{"v": 0.1}, |
| 128 |
}, |
| 129 |
{ |
| 130 |
"v: 0.99\n", |
| 131 |
map[string]float32{"v": 0.99}, |
| 132 |
}, |
| 133 |
{ |
| 134 |
"v: -0.1\n", |
| 135 |
map[string]float64{"v": -0.1}, |
| 136 |
}, |
| 137 |
{ |
| 138 |
"v: 6.8523e+5", |
| 139 |
map[string]interface{}{"v": 6.8523e+5}, |
| 140 |
}, |
| 141 |
{ |
| 142 |
"v: 685.230_15e+03", |
| 143 |
map[string]interface{}{"v": 685.23015e+03}, |
| 144 |
}, |
| 145 |
{ |
| 146 |
"v: 685_230.15", |
| 147 |
map[string]interface{}{"v": 685230.15}, |
| 148 |
}, |
| 149 |
{ |
| 150 |
"v: 685_230.15", |
| 151 |
map[string]float64{"v": 685230.15}, |
| 152 |
}, |
| 153 |
{ |
| 154 |
"v: 685230", |
| 155 |
map[string]interface{}{"v": 685230}, |
| 156 |
}, |
| 157 |
{ |
| 158 |
"v: +685_230", |
| 159 |
map[string]interface{}{"v": 685230}, |
| 160 |
}, |
| 161 |
{ |
| 162 |
"v: 02472256", |
| 163 |
map[string]interface{}{"v": 685230}, |
| 164 |
}, |
| 165 |
{ |
| 166 |
"v: 0x_0A_74_AE", |
| 167 |
map[string]interface{}{"v": 685230}, |
| 168 |
}, |
| 169 |
{ |
| 170 |
"v: 0b1010_0111_0100_1010_1110", |
| 171 |
map[string]interface{}{"v": 685230}, |
| 172 |
}, |
| 173 |
{ |
| 174 |
"v: +685_230", |
| 175 |
map[string]int{"v": 685230}, |
| 176 |
}, |
| 177 |
|
| 178 |
// Bools from spec |
| 179 |
{ |
| 180 |
"v: True", |
| 181 |
map[string]interface{}{"v": true}, |
| 182 |
}, |
| 183 |
{ |
| 184 |
"v: TRUE", |
| 185 |
map[string]interface{}{"v": true}, |
| 186 |
}, |
| 187 |
{ |
| 188 |
"v: False", |
| 189 |
map[string]interface{}{"v": false}, |
| 190 |
}, |
| 191 |
{ |
| 192 |
"v: FALSE", |
| 193 |
map[string]interface{}{"v": false}, |
| 194 |
}, |
| 195 |
{ |
| 196 |
"v: y", |
| 197 |
map[string]interface{}{"v": "y"}, // y or yes or Yes is string |
| 198 |
}, |
| 199 |
{ |
| 200 |
"v: NO", |
| 201 |
map[string]interface{}{"v": "NO"}, // no or No or NO is string |
| 202 |
}, |
| 203 |
{ |
| 204 |
"v: on", |
| 205 |
map[string]interface{}{"v": "on"}, // on is string |
| 206 |
}, |
| 207 |
|
| 208 |
// Some cross type conversions |
| 209 |
{ |
| 210 |
"v: 42", |
| 211 |
map[string]uint{"v": 42}, |
| 212 |
}, { |
| 213 |
"v: 4294967296", |
| 214 |
map[string]uint64{"v": uint64(4294967296)}, |
| 215 |
}, |
| 216 |
|
| 217 |
// int |
| 218 |
{ |
| 219 |
"v: 2147483647", |
| 220 |
map[string]int{"v": math.MaxInt32}, |
| 221 |
}, |
| 222 |
{ |
| 223 |
"v: -2147483648", |
| 224 |
map[string]int{"v": math.MinInt32}, |
| 225 |
}, |
| 226 |
|
| 227 |
// int64 |
| 228 |
{ |
| 229 |
"v: 9223372036854775807", |
| 230 |
map[string]int64{"v": math.MaxInt64}, |
| 231 |
}, |
| 232 |
{ |
| 233 |
"v: 0b111111111111111111111111111111111111111111111111111111111111111", |
| 234 |
map[string]int64{"v": math.MaxInt64}, |
| 235 |
}, |
| 236 |
{ |
| 237 |
"v: -9223372036854775808", |
| 238 |
map[string]int64{"v": math.MinInt64}, |
| 239 |
}, |
| 240 |
{ |
| 241 |
"v: -0b111111111111111111111111111111111111111111111111111111111111111", |
| 242 |
map[string]int64{"v": -math.MaxInt64}, |
| 243 |
}, |
| 244 |
|
| 245 |
// uint |
| 246 |
{ |
| 247 |
"v: 0", |
| 248 |
map[string]uint{"v": 0}, |
| 249 |
}, |
| 250 |
{ |
| 251 |
"v: 4294967295", |
| 252 |
map[string]uint{"v": math.MaxUint32}, |
| 253 |
}, |
| 254 |
{ |
| 255 |
"v: 1e3", |
| 256 |
map[string]uint{"v": 1000}, |
| 257 |
}, |
| 258 |
|
| 259 |
// uint64 |
| 260 |
{ |
| 261 |
"v: 0", |
| 262 |
map[string]uint{"v": 0}, |
| 263 |
}, |
| 264 |
{ |
| 265 |
"v: 18446744073709551615", |
| 266 |
map[string]uint64{"v": math.MaxUint64}, |
| 267 |
}, |
| 268 |
{ |
| 269 |
"v: 0b1111111111111111111111111111111111111111111111111111111111111111", |
| 270 |
map[string]uint64{"v": math.MaxUint64}, |
| 271 |
}, |
| 272 |
{ |
| 273 |
"v: 9223372036854775807", |
| 274 |
map[string]uint64{"v": math.MaxInt64}, |
| 275 |
}, |
| 276 |
{ |
| 277 |
"v: 1e3", |
| 278 |
map[string]uint64{"v": 1000}, |
| 279 |
}, |
| 280 |
|
| 281 |
// float32 |
| 282 |
{ |
| 283 |
"v: 3.40282346638528859811704183484516925440e+38", |
| 284 |
map[string]float32{"v": math.MaxFloat32}, |
| 285 |
}, |
| 286 |
{ |
| 287 |
"v: 1.401298464324817070923729583289916131280e-45", |
| 288 |
map[string]float32{"v": math.SmallestNonzeroFloat32}, |
| 289 |
}, |
| 290 |
{ |
| 291 |
"v: 18446744073709551615", |
| 292 |
map[string]float32{"v": float32(math.MaxUint64)}, |
| 293 |
}, |
| 294 |
{ |
| 295 |
"v: 18446744073709551616", |
| 296 |
map[string]float32{"v": float32(math.MaxUint64 + 1)}, |
| 297 |
}, |
| 298 |
{ |
| 299 |
"v: 1e-06", |
| 300 |
map[string]float32{"v": 1e-6}, |
| 301 |
}, |
| 302 |
|
| 303 |
// float64 |
| 304 |
{ |
| 305 |
"v: 1.797693134862315708145274237317043567981e+308", |
| 306 |
map[string]float64{"v": math.MaxFloat64}, |
| 307 |
}, |
| 308 |
{ |
| 309 |
"v: 4.940656458412465441765687928682213723651e-324", |
| 310 |
map[string]float64{"v": math.SmallestNonzeroFloat64}, |
| 311 |
}, |
| 312 |
{ |
| 313 |
"v: 18446744073709551615", |
| 314 |
map[string]float64{"v": float64(math.MaxUint64)}, |
| 315 |
}, |
| 316 |
{ |
| 317 |
"v: 18446744073709551616", |
| 318 |
map[string]float64{"v": float64(math.MaxUint64 + 1)}, |
| 319 |
}, |
| 320 |
{ |
| 321 |
"v: 1e-06", |
| 322 |
map[string]float64{"v": 1e-06}, |
| 323 |
}, |
| 324 |
|
| 325 |
// Timestamps |
| 326 |
{ |
| 327 |
// Date only. |
| 328 |
"v: 2015-01-01\n", |
| 329 |
map[string]time.Time{"v": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)}, |
| 330 |
}, |
| 331 |
{ |
| 332 |
// RFC3339 |
| 333 |
"v: 2015-02-24T18:19:39.12Z\n", |
| 334 |
map[string]time.Time{"v": time.Date(2015, 2, 24, 18, 19, 39, .12e9, time.UTC)}, |
| 335 |
}, |
| 336 |
{ |
| 337 |
// RFC3339 with short dates. |
| 338 |
"v: 2015-2-3T3:4:5Z", |
| 339 |
map[string]time.Time{"v": time.Date(2015, 2, 3, 3, 4, 5, 0, time.UTC)}, |
| 340 |
}, |
| 341 |
{ |
| 342 |
// ISO8601 lower case t |
| 343 |
"v: 2015-02-24t18:19:39Z\n", |
| 344 |
map[string]time.Time{"v": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)}, |
| 345 |
}, |
| 346 |
{ |
| 347 |
// space separate, no time zone |
| 348 |
"v: 2015-02-24 18:19:39\n", |
| 349 |
map[string]time.Time{"v": time.Date(2015, 2, 24, 18, 19, 39, 0, time.UTC)}, |
| 350 |
}, |
| 351 |
{ |
| 352 |
"v: 60s\n", |
| 353 |
map[string]time.Duration{"v": time.Minute}, |
| 354 |
}, |
| 355 |
{ |
| 356 |
"v: -0.5h\n", |
| 357 |
map[string]time.Duration{"v": -30 * time.Minute}, |
| 358 |
}, |
| 359 |
|
| 360 |
// Single Quoted values. |
| 361 |
{ |
| 362 |
`'1': '2'`, |
| 363 |
map[interface{}]interface{}{"1": `2`}, |
| 364 |
}, |
| 365 |
{ |
| 366 |
`'1': '"2"'`, |
| 367 |
map[interface{}]interface{}{"1": `"2"`}, |
| 368 |
}, |
| 369 |
{ |
| 370 |
`'1': ''''`, |
| 371 |
map[interface{}]interface{}{"1": `'`}, |
| 372 |
}, |
| 373 |
{ |
| 374 |
`'1': '''2'''`, |
| 375 |
map[interface{}]interface{}{"1": `'2'`}, |
| 376 |
}, |
| 377 |
{ |
| 378 |
`'1': 'B''z'`, |
| 379 |
map[interface{}]interface{}{"1": `B'z`}, |
| 380 |
}, |
| 381 |
{ |
| 382 |
`'1': '\'`, |
| 383 |
map[interface{}]interface{}{"1": `\`}, |
| 384 |
}, |
| 385 |
{ |
| 386 |
`'1': '\\'`, |
| 387 |
map[interface{}]interface{}{"1": `\\`}, |
| 388 |
}, |
| 389 |
{ |
| 390 |
`'1': '\"2\"'`, |
| 391 |
map[interface{}]interface{}{"1": `\"2\"`}, |
| 392 |
}, |
| 393 |
{ |
| 394 |
`'1': '\\"2\\"'`, |
| 395 |
map[interface{}]interface{}{"1": `\\"2\\"`}, |
| 396 |
}, |
| 397 |
{ |
| 398 |
"'1': ' 1\n 2\n 3'", |
| 399 |
map[interface{}]interface{}{"1": " 1 2 3"}, |
| 400 |
}, |
| 401 |
{ |
| 402 |
"'1': '\n 2\n 3'", |
| 403 |
map[interface{}]interface{}{"1": " 2 3"}, |
| 404 |
}, |
| 405 |
|
| 406 |
// Double Quoted values. |
| 407 |
{ |
| 408 |
`"1": "2"`, |
| 409 |
map[interface{}]interface{}{"1": `2`}, |
| 410 |
}, |
| 411 |
{ |
| 412 |
`"1": "\"2\""`, |
| 413 |
map[interface{}]interface{}{"1": `"2"`}, |
| 414 |
}, |
| 415 |
{ |
| 416 |
`"1": "\""`, |
| 417 |
map[interface{}]interface{}{"1": `"`}, |
| 418 |
}, |
| 419 |
{ |
| 420 |
`"1": "X\"z"`, |
| 421 |
map[interface{}]interface{}{"1": `X"z`}, |
| 422 |
}, |
| 423 |
{ |
| 424 |
`"1": "\\"`, |
| 425 |
map[interface{}]interface{}{"1": `\`}, |
| 426 |
}, |
| 427 |
{ |
| 428 |
`"1": "\\\\"`, |
| 429 |
map[interface{}]interface{}{"1": `\\`}, |
| 430 |
}, |
| 431 |
{ |
| 432 |
`"1": "\\\"2\\\""`, |
| 433 |
map[interface{}]interface{}{"1": `\"2\"`}, |
| 434 |
}, |
| 435 |
{ |
| 436 |
"'1': \" 1\n 2\n 3\"", |
| 437 |
map[interface{}]interface{}{"1": " 1 2 3"}, |
| 438 |
}, |
| 439 |
{ |
| 440 |
"'1': \"\n 2\n 3\"", |
| 441 |
map[interface{}]interface{}{"1": " 2 3"}, |
| 442 |
}, |
| 443 |
{ |
| 444 |
`"1": "a\x2Fb"`, |
| 445 |
map[interface{}]interface{}{"1": `a/b`}, |
| 446 |
}, |
| 447 |
{ |
| 448 |
`"1": "a\u002Fb"`, |
| 449 |
map[interface{}]interface{}{"1": `a/b`}, |
| 450 |
}, |
| 451 |
{ |
| 452 |
`"1": "a\x2Fb\u002Fc\U0000002Fd"`, |
| 453 |
map[interface{}]interface{}{"1": `a/b/c/d`}, |
| 454 |
}, |
| 455 |
{ |
| 456 |
"'1': \"2\\n3\"", |
| 457 |
map[interface{}]interface{}{"1": "2\n3"}, |
| 458 |
}, |
| 459 |
{ |
| 460 |
"'1': \"2\\r\\n3\"", |
| 461 |
map[interface{}]interface{}{"1": "2\r\n3"}, |
| 462 |
}, |
| 463 |
|
| 464 |
{ |
| 465 |
"a: -b_c", |
| 466 |
map[string]interface{}{"a": "-b_c"}, |
| 467 |
}, |
| 468 |
{ |
| 469 |
"a: +b_c", |
| 470 |
map[string]interface{}{"a": "+b_c"}, |
| 471 |
}, |
| 472 |
{ |
| 473 |
"a: 50cent_of_dollar", |
| 474 |
map[string]interface{}{"a": "50cent_of_dollar"}, |
| 475 |
}, |
| 476 |
|
| 477 |
// Nulls |
| 478 |
{ |
| 479 |
"v:", |
| 480 |
map[string]interface{}{"v": nil}, |
| 481 |
}, |
| 482 |
{ |
| 483 |
"v: ~", |
| 484 |
map[string]interface{}{"v": nil}, |
| 485 |
}, |
| 486 |
{ |
| 487 |
"~: null key", |
| 488 |
map[interface{}]string{nil: "null key"}, |
| 489 |
}, |
| 490 |
{ |
| 491 |
"v:", |
| 492 |
map[string]*bool{"v": nil}, |
| 493 |
}, |
| 494 |
{ |
| 495 |
"v: null", |
| 496 |
map[string]*string{"v": nil}, |
| 497 |
}, |
| 498 |
{ |
| 499 |
"v: null", |
| 500 |
map[string]string{"v": ""}, |
| 501 |
}, |
| 502 |
{ |
| 503 |
"v: null", |
| 504 |
map[string]interface{}{"v": nil}, |
| 505 |
}, |
| 506 |
{ |
| 507 |
"v: Null", |
| 508 |
map[string]interface{}{"v": nil}, |
| 509 |
}, |
| 510 |
{ |
| 511 |
"v: NULL", |
| 512 |
map[string]interface{}{"v": nil}, |
| 513 |
}, |
| 514 |
{ |
| 515 |
"v: ~", |
| 516 |
map[string]*string{"v": nil}, |
| 517 |
}, |
| 518 |
{ |
| 519 |
"v: ~", |
| 520 |
map[string]string{"v": ""}, |
| 521 |
}, |
| 522 |
|
| 523 |
{ |
| 524 |
"v: .inf\n", |
| 525 |
map[string]interface{}{"v": math.Inf(0)}, |
| 526 |
}, |
| 527 |
{ |
| 528 |
"v: .Inf\n", |
| 529 |
map[string]interface{}{"v": math.Inf(0)}, |
| 530 |
}, |
| 531 |
{ |
| 532 |
"v: .INF\n", |
| 533 |
map[string]interface{}{"v": math.Inf(0)}, |
| 534 |
}, |
| 535 |
{ |
| 536 |
"v: -.inf\n", |
| 537 |
map[string]interface{}{"v": math.Inf(-1)}, |
| 538 |
}, |
| 539 |
{ |
| 540 |
"v: -.Inf\n", |
| 541 |
map[string]interface{}{"v": math.Inf(-1)}, |
| 542 |
}, |
| 543 |
{ |
| 544 |
"v: -.INF\n", |
| 545 |
map[string]interface{}{"v": math.Inf(-1)}, |
| 546 |
}, |
| 547 |
{ |
| 548 |
"v: .nan\n", |
| 549 |
map[string]interface{}{"v": math.NaN()}, |
| 550 |
}, |
| 551 |
{ |
| 552 |
"v: .NaN\n", |
| 553 |
map[string]interface{}{"v": math.NaN()}, |
| 554 |
}, |
| 555 |
{ |
| 556 |
"v: .NAN\n", |
| 557 |
map[string]interface{}{"v": math.NaN()}, |
| 558 |
}, |
| 559 |
|
| 560 |
// Explicit tags. |
| 561 |
{ |
| 562 |
"v: !!float '1.1'", |
| 563 |
map[string]interface{}{"v": 1.1}, |
| 564 |
}, |
| 565 |
{ |
| 566 |
"v: !!float 0", |
| 567 |
map[string]interface{}{"v": float64(0)}, |
| 568 |
}, |
| 569 |
{ |
| 570 |
"v: !!float -1", |
| 571 |
map[string]interface{}{"v": float64(-1)}, |
| 572 |
}, |
| 573 |
{ |
| 574 |
"v: !!null ''", |
| 575 |
map[string]interface{}{"v": nil}, |
| 576 |
}, |
| 577 |
{ |
| 578 |
"v: !!timestamp \"2015-01-01\"", |
| 579 |
map[string]time.Time{"v": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)}, |
| 580 |
}, |
| 581 |
{ |
| 582 |
"v: !!timestamp 2015-01-01", |
| 583 |
map[string]time.Time{"v": time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)}, |
| 584 |
}, |
| 585 |
|
| 586 |
// Flow sequence |
| 587 |
{ |
| 588 |
"v: [A,B]", |
| 589 |
map[string]interface{}{"v": []interface{}{"A", "B"}}, |
| 590 |
}, |
| 591 |
{ |
| 592 |
"v: [A,B,C,]", |
| 593 |
map[string][]string{"v": {"A", "B", "C"}}, |
| 594 |
}, |
| 595 |
{ |
| 596 |
"v: [A,1,C]", |
| 597 |
map[string][]string{"v": {"A", "1", "C"}}, |
| 598 |
}, |
| 599 |
{ |
| 600 |
"v: [A,1,C]", |
| 601 |
map[string]interface{}{"v": []interface{}{"A", 1, "C"}}, |
| 602 |
}, |
| 603 |
|
| 604 |
// Block sequence |
| 605 |
{ |
| 606 |
"v:\n - A\n - B", |
| 607 |
map[string]interface{}{"v": []interface{}{"A", "B"}}, |
| 608 |
}, |
| 609 |
{ |
| 610 |
"v:\n - A\n - B\n - C", |
| 611 |
map[string][]string{"v": {"A", "B", "C"}}, |
| 612 |
}, |
| 613 |
{ |
| 614 |
"v:\n - A\n - 1\n - C", |
| 615 |
map[string][]string{"v": {"A", "1", "C"}}, |
| 616 |
}, |
| 617 |
{ |
| 618 |
"v:\n - A\n - 1\n - C", |
| 619 |
map[string]interface{}{"v": []interface{}{"A", 1, "C"}}, |
| 620 |
}, |
| 621 |
|
| 622 |
// Map inside interface with no type hints. |
| 623 |
{ |
| 624 |
"a: {b: c}", |
| 625 |
map[interface{}]interface{}{"a": map[interface{}]interface{}{"b": "c"}}, |
| 626 |
}, |
| 627 |
|
| 628 |
{ |
| 629 |
"v: \"\"\n", |
| 630 |
map[string]string{"v": ""}, |
| 631 |
}, |
| 632 |
{ |
| 633 |
"v:\n- A\n- B\n", |
| 634 |
map[string][]string{"v": {"A", "B"}}, |
| 635 |
}, |
| 636 |
{ |
| 637 |
"a: '-'\n", |
| 638 |
map[string]string{"a": "-"}, |
| 639 |
}, |
| 640 |
{ |
| 641 |
"123\n", |
| 642 |
123, |
| 643 |
}, |
| 644 |
{ |
| 645 |
"hello: world\n", |
| 646 |
map[string]string{"hello": "world"}, |
| 647 |
}, |
| 648 |
{ |
| 649 |
"hello: world\r\n", |
| 650 |
map[string]string{"hello": "world"}, |
| 651 |
}, |
| 652 |
{ |
| 653 |
"hello: world\rGo: Gopher", |
| 654 |
map[string]string{"hello": "world", "Go": "Gopher"}, |
| 655 |
}, |
| 656 |
|
| 657 |
// Structs and type conversions. |
| 658 |
{ |
| 659 |
"hello: world", |
| 660 |
struct{ Hello string }{"world"}, |
| 661 |
}, |
| 662 |
{ |
| 663 |
"a: {b: c}", |
| 664 |
struct{ A struct{ B string } }{struct{ B string }{"c"}}, |
| 665 |
}, |
| 666 |
{ |
| 667 |
"a: {b: c}", |
| 668 |
struct{ A map[string]string }{map[string]string{"b": "c"}}, |
| 669 |
}, |
| 670 |
{ |
| 671 |
"a:", |
| 672 |
struct{ A map[string]string }{}, |
| 673 |
}, |
| 674 |
{ |
| 675 |
"a: 1", |
| 676 |
struct{ A int }{1}, |
| 677 |
}, |
| 678 |
{ |
| 679 |
"a: 1", |
| 680 |
struct{ A float64 }{1}, |
| 681 |
}, |
| 682 |
{ |
| 683 |
"a: 1.0", |
| 684 |
struct{ A int }{1}, |
| 685 |
}, |
| 686 |
{ |
| 687 |
"a: 1.0", |
| 688 |
struct{ A uint }{1}, |
| 689 |
}, |
| 690 |
{ |
| 691 |
"a: [1, 2]", |
| 692 |
struct{ A []int }{[]int{1, 2}}, |
| 693 |
}, |
| 694 |
{ |
| 695 |
"a: [1, 2]", |
| 696 |
struct{ A [2]int }{[2]int{1, 2}}, |
| 697 |
}, |
| 698 |
{ |
| 699 |
"a: 1", |
| 700 |
struct{ B int }{0}, |
| 701 |
}, |
| 702 |
{ |
| 703 |
"a: 1", |
| 704 |
struct { |
| 705 |
B int `yaml:"a"` |
| 706 |
}{1}, |
| 707 |
}, |
| 708 |
|
| 709 |
{ |
| 710 |
"a: 1\n", |
| 711 |
yaml.MapItem{Key: "a", Value: 1}, |
| 712 |
}, |
| 713 |
{ |
| 714 |
"a: 1\nb: 2\nc: 3\n", |
| 715 |
yaml.MapSlice{ |
| 716 |
{Key: "a", Value: 1}, |
| 717 |
{Key: "b", Value: 2}, |
| 718 |
{Key: "c", Value: 3}, |
| 719 |
}, |
| 720 |
}, |
| 721 |
{ |
| 722 |
"v:\n- A\n- 1\n- B:\n - 2\n - 3\n", |
| 723 |
map[string]interface{}{ |
| 724 |
"v": []interface{}{ |
| 725 |
"A", |
| 726 |
1, |
| 727 |
map[string][]int{ |
| 728 |
"B": {2, 3}, |
| 729 |
}, |
| 730 |
}, |
| 731 |
}, |
| 732 |
}, |
| 733 |
{ |
| 734 |
"a:\n b: c\n", |
| 735 |
map[string]interface{}{ |
| 736 |
"a": map[string]string{ |
| 737 |
"b": "c", |
| 738 |
}, |
| 739 |
}, |
| 740 |
}, |
| 741 |
{ |
| 742 |
"a: {x: 1}\n", |
| 743 |
map[string]map[string]int{ |
| 744 |
"a": { |
| 745 |
"x": 1, |
| 746 |
}, |
| 747 |
}, |
| 748 |
}, |
| 749 |
{ |
| 750 |
"t2: 2018-01-09T10:40:47Z\nt4: 2098-01-09T10:40:47Z\n", |
| 751 |
map[string]string{ |
| 752 |
"t2": "2018-01-09T10:40:47Z", |
| 753 |
"t4": "2098-01-09T10:40:47Z", |
| 754 |
}, |
| 755 |
}, |
| 756 |
{ |
| 757 |
"a: [1, 2]\n", |
| 758 |
map[string][]int{ |
| 759 |
"a": {1, 2}, |
| 760 |
}, |
| 761 |
}, |
| 762 |
{ |
| 763 |
"a: {b: c, d: e}\n", |
| 764 |
map[string]interface{}{ |
| 765 |
"a": map[string]string{ |
| 766 |
"b": "c", |
| 767 |
"d": "e", |
| 768 |
}, |
| 769 |
}, |
| 770 |
}, |
| 771 |
{ |
| 772 |
"a: 3s\n", |
| 773 |
map[string]string{ |
| 774 |
"a": "3s", |
| 775 |
}, |
| 776 |
}, |
| 777 |
{ |
| 778 |
"a: <foo>\n", |
| 779 |
map[string]string{"a": "<foo>"}, |
| 780 |
}, |
| 781 |
{ |
| 782 |
"a: \"1:1\"\n", |
| 783 |
map[string]string{"a": "1:1"}, |
| 784 |
}, |
| 785 |
{ |
| 786 |
"a: 1.2.3.4\n", |
| 787 |
map[string]string{"a": "1.2.3.4"}, |
| 788 |
}, |
| 789 |
{ |
| 790 |
"a: 'b: c'\n", |
| 791 |
map[string]string{"a": "b: c"}, |
| 792 |
}, |
| 793 |
{ |
| 794 |
"a: 'Hello #comment'\n", |
| 795 |
map[string]string{"a": "Hello #comment"}, |
| 796 |
}, |
| 797 |
{ |
| 798 |
"a: 100.5\n", |
| 799 |
map[string]interface{}{ |
| 800 |
"a": 100.5, |
| 801 |
}, |
| 802 |
}, |
| 803 |
{ |
| 804 |
"a: \"\\0\"\n", |
| 805 |
map[string]string{"a": "\\0"}, |
| 806 |
}, |
| 807 |
{ |
| 808 |
"b: 2\na: 1\nd: 4\nc: 3\nsub:\n e: 5\n", |
| 809 |
map[string]interface{}{ |
| 810 |
"b": 2, |
| 811 |
"a": 1, |
| 812 |
"d": 4, |
| 813 |
"c": 3, |
| 814 |
"sub": map[string]int{ |
| 815 |
"e": 5, |
| 816 |
}, |
| 817 |
}, |
| 818 |
}, |
| 819 |
{ |
| 820 |
" a : b \n", |
| 821 |
map[string]string{"a": "b"}, |
| 822 |
}, |
| 823 |
{ |
| 824 |
"a: b # comment\nb: c\n", |
| 825 |
map[string]string{ |
| 826 |
"a": "b", |
| 827 |
"b": "c", |
| 828 |
}, |
| 829 |
}, |
| 830 |
{ |
| 831 |
"---\na: b\n", |
| 832 |
map[string]string{"a": "b"}, |
| 833 |
}, |
| 834 |
{ |
| 835 |
"a: b\n...\n", |
| 836 |
map[string]string{"a": "b"}, |
| 837 |
}, |
| 838 |
{ |
| 839 |
"%YAML 1.2\n---\n", |
| 840 |
(*struct{})(nil), |
| 841 |
}, |
| 842 |
{ |
| 843 |
"---\n", |
| 844 |
(*struct{})(nil), |
| 845 |
}, |
| 846 |
{ |
| 847 |
"...", |
| 848 |
(*struct{})(nil), |
| 849 |
}, |
| 850 |
{ |
| 851 |
"v: go test ./...", |
| 852 |
map[string]string{"v": "go test ./..."}, |
| 853 |
}, |
| 854 |
{ |
| 855 |
"v: echo ---", |
| 856 |
map[string]string{"v": "echo ---"}, |
| 857 |
}, |
| 858 |
{ |
| 859 |
"v: |\n hello\n ...\n world\n", |
| 860 |
map[string]string{"v": "hello\n...\nworld\n"}, |
| 861 |
}, |
| 862 |
{ |
| 863 |
"a: !!binary gIGC\n", |
| 864 |
map[string]string{"a": "\x80\x81\x82"}, |
| 865 |
}, |
| 866 |
{ |
| 867 |
"a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n", |
| 868 |
map[string]string{"a": strings.Repeat("\x90", 54)}, |
| 869 |
}, |
| 870 |
{ |
| 871 |
"v:\n- A\n- |-\n B\n C\n", |
| 872 |
map[string][]string{ |
| 873 |
"v": { |
| 874 |
"A", "B\nC", |
| 875 |
}, |
| 876 |
}, |
| 877 |
}, |
| 878 |
{ |
| 879 |
"v:\n- A\n- |-\n B\n C\n\n\n", |
| 880 |
map[string][]string{ |
| 881 |
"v": { |
| 882 |
"A", "B\nC", |
| 883 |
}, |
| 884 |
}, |
| 885 |
}, |
| 886 |
{ |
| 887 |
"v:\n- A\n- >-\n B\n C\n", |
| 888 |
map[string][]string{ |
| 889 |
"v": { |
| 890 |
"A", "B C", |
| 891 |
}, |
| 892 |
}, |
| 893 |
}, |
| 894 |
{ |
| 895 |
"v:\n- A\n- >-\n B\n C\n\n\n", |
| 896 |
map[string][]string{ |
| 897 |
"v": { |
| 898 |
"A", "B C", |
| 899 |
}, |
| 900 |
}, |
| 901 |
}, |
| 902 |
{ |
| 903 |
"a: b\nc: d\n", |
| 904 |
struct { |
| 905 |
A string |
| 906 |
C string `yaml:"c"` |
| 907 |
}{ |
| 908 |
"b", "d", |
| 909 |
}, |
| 910 |
}, |
| 911 |
{ |
| 912 |
"a: 1\nb: 2\n", |
| 913 |
struct { |
| 914 |
A int |
| 915 |
B int `yaml:"-"` |
| 916 |
}{ |
| 917 |
1, 0, |
| 918 |
}, |
| 919 |
}, |
| 920 |
{ |
| 921 |
"a: 1\nb: 2\n", |
| 922 |
struct { |
| 923 |
A int |
| 924 |
Child `yaml:",inline"` |
| 925 |
}{ |
| 926 |
1, |
| 927 |
Child{ |
| 928 |
B: 2, |
| 929 |
C: 0, |
| 930 |
}, |
| 931 |
}, |
| 932 |
}, |
| 933 |
|
| 934 |
// Anchors and aliases. |
| 935 |
{ |
| 936 |
"a: &x 1\nb: &y 2\nc: *x\nd: *y\n", |
| 937 |
struct{ A, B, C, D int }{1, 2, 1, 2}, |
| 938 |
}, |
| 939 |
{ |
| 940 |
"a: &a {c: 1}\nb: *a\n", |
| 941 |
struct { |
| 942 |
A, B struct { |
| 943 |
C int |
| 944 |
} |
| 945 |
}{struct{ C int }{1}, struct{ C int }{1}}, |
| 946 |
}, |
| 947 |
{ |
| 948 |
"a: &a [1, 2]\nb: *a\n", |
| 949 |
struct{ B []int }{[]int{1, 2}}, |
| 950 |
}, |
| 951 |
|
| 952 |
{ |
| 953 |
"tags:\n- hello-world\na: foo", |
| 954 |
struct { |
| 955 |
Tags []string |
| 956 |
A string |
| 957 |
}{Tags: []string{"hello-world"}, A: "foo"}, |
| 958 |
}, |
| 959 |
{ |
| 960 |
"", |
| 961 |
(*struct{})(nil), |
| 962 |
}, |
| 963 |
{ |
| 964 |
"{}", struct{}{}, |
| 965 |
}, |
| 966 |
{ |
| 967 |
"v: /a/{b}", |
| 968 |
map[string]string{"v": "/a/{b}"}, |
| 969 |
}, |
| 970 |
{ |
| 971 |
"v: 1[]{},!%?&*", |
| 972 |
map[string]string{"v": "1[]{},!%?&*"}, |
| 973 |
}, |
| 974 |
{ |
| 975 |
"v: user's item", |
| 976 |
map[string]string{"v": "user's item"}, |
| 977 |
}, |
| 978 |
{ |
| 979 |
"v: [1,[2,[3,[4,5],6],7],8]", |
| 980 |
map[string]interface{}{ |
| 981 |
"v": []interface{}{ |
| 982 |
1, |
| 983 |
[]interface{}{ |
| 984 |
2, |
| 985 |
[]interface{}{ |
| 986 |
3, |
| 987 |
[]int{4, 5}, |
| 988 |
6, |
| 989 |
}, |
| 990 |
7, |
| 991 |
}, |
| 992 |
8, |
| 993 |
}, |
| 994 |
}, |
| 995 |
}, |
| 996 |
{ |
| 997 |
"v: {a: {b: {c: {d: e},f: g},h: i},j: k}", |
| 998 |
map[string]interface{}{ |
| 999 |
"v": map[string]interface{}{ |
| 1000 |
"a": map[string]interface{}{ |
| 1001 |
"b": map[string]interface{}{ |
| 1002 |
"c": map[string]string{ |
| 1003 |
"d": "e", |
| 1004 |
}, |
| 1005 |
"f": "g", |
| 1006 |
}, |
| 1007 |
"h": "i", |
| 1008 |
}, |
| 1009 |
"j": "k", |
| 1010 |
}, |
| 1011 |
}, |
| 1012 |
}, |
| 1013 |
{ |
| 1014 |
`--- |
| 1015 |
- a: |
| 1016 |
b: |
| 1017 |
- c: d |
| 1018 |
`, |
| 1019 |
[]map[string]interface{}{ |
| 1020 |
{ |
| 1021 |
"a": map[string]interface{}{ |
| 1022 |
"b": nil, |
| 1023 |
}, |
| 1024 |
}, |
| 1025 |
{ |
| 1026 |
"c": "d", |
| 1027 |
}, |
| 1028 |
}, |
| 1029 |
}, |
| 1030 |
{ |
| 1031 |
`--- |
| 1032 |
a: |
| 1033 |
b: |
| 1034 |
c: d |
| 1035 |
`, |
| 1036 |
map[string]interface{}{ |
| 1037 |
"a": map[string]interface{}{ |
| 1038 |
"b": nil, |
| 1039 |
}, |
| 1040 |
"c": "d", |
| 1041 |
}, |
| 1042 |
}, |
| 1043 |
{ |
| 1044 |
`--- |
| 1045 |
a: |
| 1046 |
b: |
| 1047 |
c: |
| 1048 |
`, |
| 1049 |
map[string]interface{}{ |
| 1050 |
"a": nil, |
| 1051 |
"b": nil, |
| 1052 |
"c": nil, |
| 1053 |
}, |
| 1054 |
}, |
| 1055 |
{ |
| 1056 |
`--- |
| 1057 |
a: go test ./... |
| 1058 |
b: |
| 1059 |
c: |
| 1060 |
`, |
| 1061 |
map[string]interface{}{ |
| 1062 |
"a": "go test ./...", |
| 1063 |
"b": nil, |
| 1064 |
"c": nil, |
| 1065 |
}, |
| 1066 |
}, |
| 1067 |
{ |
| 1068 |
`--- |
| 1069 |
a: | |
| 1070 |
hello |
| 1071 |
... |
| 1072 |
world |
| 1073 |
b: |
| 1074 |
c: |
| 1075 |
`, |
| 1076 |
map[string]interface{}{ |
| 1077 |
"a": "hello\n...\nworld\n", |
| 1078 |
"b": nil, |
| 1079 |
"c": nil, |
| 1080 |
}, |
| 1081 |
}, |
| 1082 |
|
| 1083 |
// Multi bytes |
| 1084 |
{ |
| 1085 |
"v: あいうえお\nv2: かきくけこ", |
| 1086 |
map[string]string{"v": "あいうえお", "v2": "かきくけこ"}, |
| 1087 |
}, |
| 1088 |
} |
| 1089 |
for _, test := range tests { |
| 1090 |
t.Run(test.source, func(t *testing.T) { |
| 1091 |
buf := bytes.NewBufferString(test.source) |
| 1092 |
dec := yaml.NewDecoder(buf) |
| 1093 |
typ := reflect.ValueOf(test.value).Type() |
| 1094 |
value := reflect.New(typ) |
| 1095 |
if err := dec.Decode(value.Interface()); err != nil { |
| 1096 |
if err == io.EOF { |
| 1097 |
return |
| 1098 |
} |
| 1099 |
t.Fatalf("%s: %+v", test.source, err) |
| 1100 |
} |
| 1101 |
actual := fmt.Sprintf("%+v", value.Elem().Interface()) |
| 1102 |
expect := fmt.Sprintf("%+v", test.value) |
| 1103 |
if actual != expect { |
| 1104 |
t.Fatalf("failed to test [%s], actual=[%s], expect=[%s]", test.source, actual, expect) |
| 1105 |
} |
| 1106 |
}) |
| 1107 |
} |
| 1108 |
} |
| 1109 |
|
| 1110 |
func TestDecoder_ScientificNotation(t *testing.T) { |
| 1111 |
tests := []struct { |
| 1112 |
source string |
| 1113 |
value interface{} |
| 1114 |
}{ |
| 1115 |
{ |
| 1116 |
"v: 1e3", |
| 1117 |
map[string]uint{"v": 1000}, |
| 1118 |
}, |
| 1119 |
{ |
| 1120 |
"v: 1e-3", |
| 1121 |
map[string]uint{"v": 0}, |
| 1122 |
}, |
| 1123 |
{ |
| 1124 |
"v: 1e3", |
| 1125 |
map[string]int{"v": 1000}, |
| 1126 |
}, |
| 1127 |
{ |
| 1128 |
"v: 1e-3", |
| 1129 |
map[string]int{"v": 0}, |
| 1130 |
}, |
| 1131 |
{ |
| 1132 |
"v: 1e3", |
| 1133 |
map[string]float32{"v": 1000}, |
| 1134 |
}, |
| 1135 |
{ |
| 1136 |
"v: 1.0e3", |
| 1137 |
map[string]float64{"v": 1000}, |
| 1138 |
}, |
| 1139 |
{ |
| 1140 |
"v: 1e-3", |
| 1141 |
map[string]float64{"v": 0.001}, |
| 1142 |
}, |
| 1143 |
{ |
| 1144 |
"v: 1.0e-3", |
| 1145 |
map[string]float64{"v": 0.001}, |
| 1146 |
}, |
| 1147 |
{ |
| 1148 |
"v: 1.0e+3", |
| 1149 |
map[string]float64{"v": 1000}, |
| 1150 |
}, |
| 1151 |
{ |
| 1152 |
"v: 1.0e+3", |
| 1153 |
map[string]float64{"v": 1000}, |
| 1154 |
}, |
| 1155 |
} |
| 1156 |
for _, test := range tests { |
| 1157 |
t.Run(test.source, func(t *testing.T) { |
| 1158 |
buf := bytes.NewBufferString(test.source) |
| 1159 |
dec := yaml.NewDecoder(buf) |
| 1160 |
typ := reflect.ValueOf(test.value).Type() |
| 1161 |
value := reflect.New(typ) |
| 1162 |
if err := dec.Decode(value.Interface()); err != nil { |
| 1163 |
if err == io.EOF { |
| 1164 |
return |
| 1165 |
} |
| 1166 |
t.Fatalf("%s: %+v", test.source, err) |
| 1167 |
} |
| 1168 |
actual := fmt.Sprintf("%+v", value.Elem().Interface()) |
| 1169 |
expect := fmt.Sprintf("%+v", test.value) |
| 1170 |
if actual != expect { |
| 1171 |
t.Fatalf("failed to test [%s], actual=[%s], expect=[%s]", test.source, actual, expect) |
| 1172 |
} |
| 1173 |
}) |
| 1174 |
} |
| 1175 |
} |
| 1176 |
|
| 1177 |
func TestDecoder_TypeConversionError(t *testing.T) { |
| 1178 |
t.Run("type conversion for struct", func(t *testing.T) { |
| 1179 |
type T struct { |
| 1180 |
A int |
| 1181 |
B uint |
| 1182 |
C float32 |
| 1183 |
D bool |
| 1184 |
} |
| 1185 |
type U struct { |
| 1186 |
*T `yaml:",inline"` |
| 1187 |
} |
| 1188 |
t.Run("string to int", func(t *testing.T) { |
| 1189 |
var v T |
| 1190 |
err := yaml.Unmarshal([]byte(`a: str`), &v) |
| 1191 |
if err == nil { |
| 1192 |
t.Fatal("expected to error") |
| 1193 |
} |
| 1194 |
msg := "cannot unmarshal string into Go struct field T.A of type int" |
| 1195 |
if !strings.Contains(err.Error(), msg) { |
| 1196 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1197 |
} |
| 1198 |
}) |
| 1199 |
t.Run("string to uint", func(t *testing.T) { |
| 1200 |
var v T |
| 1201 |
err := yaml.Unmarshal([]byte(`b: str`), &v) |
| 1202 |
if err == nil { |
| 1203 |
t.Fatal("expected to error") |
| 1204 |
} |
| 1205 |
msg := "cannot unmarshal string into Go struct field T.B of type uint" |
| 1206 |
if !strings.Contains(err.Error(), msg) { |
| 1207 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1208 |
} |
| 1209 |
}) |
| 1210 |
t.Run("string to bool", func(t *testing.T) { |
| 1211 |
var v T |
| 1212 |
err := yaml.Unmarshal([]byte(`d: str`), &v) |
| 1213 |
if err == nil { |
| 1214 |
t.Fatal("expected to error") |
| 1215 |
} |
| 1216 |
msg := "cannot unmarshal string into Go struct field T.D of type bool" |
| 1217 |
if !strings.Contains(err.Error(), msg) { |
| 1218 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1219 |
} |
| 1220 |
}) |
| 1221 |
t.Run("string to int at inline", func(t *testing.T) { |
| 1222 |
var v U |
| 1223 |
err := yaml.Unmarshal([]byte(`a: str`), &v) |
| 1224 |
if err == nil { |
| 1225 |
t.Fatal("expected to error") |
| 1226 |
} |
| 1227 |
msg := "cannot unmarshal string into Go struct field U.T.A of type int" |
| 1228 |
if !strings.Contains(err.Error(), msg) { |
| 1229 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1230 |
} |
| 1231 |
}) |
| 1232 |
}) |
| 1233 |
t.Run("type conversion for array", func(t *testing.T) { |
| 1234 |
t.Run("string to int", func(t *testing.T) { |
| 1235 |
var v map[string][]int |
| 1236 |
err := yaml.Unmarshal([]byte(`v: [A,1,C]`), &v) |
| 1237 |
if err == nil { |
| 1238 |
t.Fatal("expected to error") |
| 1239 |
} |
| 1240 |
msg := "cannot unmarshal string into Go value of type int" |
| 1241 |
if !strings.Contains(err.Error(), msg) { |
| 1242 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1243 |
} |
| 1244 |
if len(v) == 0 || len(v["v"]) == 0 { |
| 1245 |
t.Fatal("failed to decode value") |
| 1246 |
} |
| 1247 |
if v["v"][0] != 1 { |
| 1248 |
t.Fatal("failed to decode value") |
| 1249 |
} |
| 1250 |
}) |
| 1251 |
t.Run("string to int", func(t *testing.T) { |
| 1252 |
var v map[string][]int |
| 1253 |
err := yaml.Unmarshal([]byte("v:\n - A\n - 1\n - C"), &v) |
| 1254 |
if err == nil { |
| 1255 |
t.Fatal("expected to error") |
| 1256 |
} |
| 1257 |
msg := "cannot unmarshal string into Go value of type int" |
| 1258 |
if !strings.Contains(err.Error(), msg) { |
| 1259 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1260 |
} |
| 1261 |
if len(v) == 0 || len(v["v"]) == 0 { |
| 1262 |
t.Fatal("failed to decode value") |
| 1263 |
} |
| 1264 |
if v["v"][0] != 1 { |
| 1265 |
t.Fatal("failed to decode value") |
| 1266 |
} |
| 1267 |
}) |
| 1268 |
}) |
| 1269 |
t.Run("overflow error", func(t *testing.T) { |
| 1270 |
t.Run("negative number to uint", func(t *testing.T) { |
| 1271 |
var v map[string]uint |
| 1272 |
err := yaml.Unmarshal([]byte("v: -42"), &v) |
| 1273 |
if err == nil { |
| 1274 |
t.Fatal("expected to error") |
| 1275 |
} |
| 1276 |
msg := "cannot unmarshal -42 into Go value of type uint ( overflow )" |
| 1277 |
if !strings.Contains(err.Error(), msg) { |
| 1278 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1279 |
} |
| 1280 |
if v["v"] != 0 { |
| 1281 |
t.Fatal("failed to decode value") |
| 1282 |
} |
| 1283 |
}) |
| 1284 |
t.Run("negative number to uint64", func(t *testing.T) { |
| 1285 |
var v map[string]uint64 |
| 1286 |
err := yaml.Unmarshal([]byte("v: -4294967296"), &v) |
| 1287 |
if err == nil { |
| 1288 |
t.Fatal("expected to error") |
| 1289 |
} |
| 1290 |
msg := "cannot unmarshal -4294967296 into Go value of type uint64 ( overflow )" |
| 1291 |
if !strings.Contains(err.Error(), msg) { |
| 1292 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1293 |
} |
| 1294 |
if v["v"] != 0 { |
| 1295 |
t.Fatal("failed to decode value") |
| 1296 |
} |
| 1297 |
}) |
| 1298 |
t.Run("larger number for int32", func(t *testing.T) { |
| 1299 |
var v map[string]int32 |
| 1300 |
err := yaml.Unmarshal([]byte("v: 4294967297"), &v) |
| 1301 |
if err == nil { |
| 1302 |
t.Fatal("expected to error") |
| 1303 |
} |
| 1304 |
msg := "cannot unmarshal 4294967297 into Go value of type int32 ( overflow )" |
| 1305 |
if !strings.Contains(err.Error(), msg) { |
| 1306 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1307 |
} |
| 1308 |
if v["v"] != 0 { |
| 1309 |
t.Fatal("failed to decode value") |
| 1310 |
} |
| 1311 |
}) |
| 1312 |
t.Run("larger number for int8", func(t *testing.T) { |
| 1313 |
var v map[string]int8 |
| 1314 |
err := yaml.Unmarshal([]byte("v: 128"), &v) |
| 1315 |
if err == nil { |
| 1316 |
t.Fatal("expected to error") |
| 1317 |
} |
| 1318 |
msg := "cannot unmarshal 128 into Go value of type int8 ( overflow )" |
| 1319 |
if !strings.Contains(err.Error(), msg) { |
| 1320 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1321 |
} |
| 1322 |
if v["v"] != 0 { |
| 1323 |
t.Fatal("failed to decode value") |
| 1324 |
} |
| 1325 |
}) |
| 1326 |
}) |
| 1327 |
t.Run("type conversion for time", func(t *testing.T) { |
| 1328 |
type T struct { |
| 1329 |
A time.Time |
| 1330 |
B time.Duration |
| 1331 |
} |
| 1332 |
t.Run("int to time", func(t *testing.T) { |
| 1333 |
var v T |
| 1334 |
err := yaml.Unmarshal([]byte(`a: 123`), &v) |
| 1335 |
if err == nil { |
| 1336 |
t.Fatal("expected to error") |
| 1337 |
} |
| 1338 |
msg := "cannot unmarshal uint64 into Go struct field T.A of type time.Time" |
| 1339 |
if !strings.Contains(err.Error(), msg) { |
| 1340 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1341 |
} |
| 1342 |
}) |
| 1343 |
t.Run("string to duration", func(t *testing.T) { |
| 1344 |
var v T |
| 1345 |
err := yaml.Unmarshal([]byte(`b: str`), &v) |
| 1346 |
if err == nil { |
| 1347 |
t.Fatal("expected to error") |
| 1348 |
} |
| 1349 |
msg := `time: invalid duration "str"` |
| 1350 |
if !strings.Contains(err.Error(), msg) { |
| 1351 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1352 |
} |
| 1353 |
}) |
| 1354 |
t.Run("int to duration", func(t *testing.T) { |
| 1355 |
var v T |
| 1356 |
err := yaml.Unmarshal([]byte(`b: 10`), &v) |
| 1357 |
if err == nil { |
| 1358 |
t.Fatal("expected to error") |
| 1359 |
} |
| 1360 |
msg := "cannot unmarshal uint64 into Go struct field T.B of type time.Duration" |
| 1361 |
if !strings.Contains(err.Error(), msg) { |
| 1362 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), msg) |
| 1363 |
} |
| 1364 |
}) |
| 1365 |
}) |
| 1366 |
} |
| 1367 |
|
| 1368 |
func TestDecoder_AnchorReferenceDirs(t *testing.T) { |
| 1369 |
buf := bytes.NewBufferString("a: *a\n") |
| 1370 |
dec := yaml.NewDecoder(buf, yaml.ReferenceDirs("testdata")) |
| 1371 |
var v struct { |
| 1372 |
A struct { |
| 1373 |
B int |
| 1374 |
C string |
| 1375 |
} |
| 1376 |
} |
| 1377 |
if err := dec.Decode(&v); err != nil { |
| 1378 |
t.Fatalf("%+v", err) |
| 1379 |
} |
| 1380 |
if v.A.B != 1 { |
| 1381 |
t.Fatal("failed to decode by reference dirs") |
| 1382 |
} |
| 1383 |
if v.A.C != "hello" { |
| 1384 |
t.Fatal("failed to decode by reference dirs") |
| 1385 |
} |
| 1386 |
} |
| 1387 |
|
| 1388 |
func TestDecoder_AnchorReferenceDirsRecursive(t *testing.T) { |
| 1389 |
buf := bytes.NewBufferString("a: *a\n") |
| 1390 |
dec := yaml.NewDecoder( |
| 1391 |
buf, |
| 1392 |
yaml.RecursiveDir(true), |
| 1393 |
yaml.ReferenceDirs("testdata"), |
| 1394 |
) |
| 1395 |
var v struct { |
| 1396 |
A struct { |
| 1397 |
B int |
| 1398 |
C string |
| 1399 |
} |
| 1400 |
} |
| 1401 |
if err := dec.Decode(&v); err != nil { |
| 1402 |
t.Fatalf("%+v", err) |
| 1403 |
} |
| 1404 |
if v.A.B != 1 { |
| 1405 |
t.Fatal("failed to decode by reference dirs") |
| 1406 |
} |
| 1407 |
if v.A.C != "hello" { |
| 1408 |
t.Fatal("failed to decode by reference dirs") |
| 1409 |
} |
| 1410 |
} |
| 1411 |
|
| 1412 |
func TestDecoder_AnchorFiles(t *testing.T) { |
| 1413 |
buf := bytes.NewBufferString("a: *a\n") |
| 1414 |
dec := yaml.NewDecoder(buf, yaml.ReferenceFiles("testdata/anchor.yml")) |
| 1415 |
var v struct { |
| 1416 |
A struct { |
| 1417 |
B int |
| 1418 |
C string |
| 1419 |
} |
| 1420 |
} |
| 1421 |
if err := dec.Decode(&v); err != nil { |
| 1422 |
t.Fatalf("%+v", err) |
| 1423 |
} |
| 1424 |
if v.A.B != 1 { |
| 1425 |
t.Fatal("failed to decode by reference dirs") |
| 1426 |
} |
| 1427 |
if v.A.C != "hello" { |
| 1428 |
t.Fatal("failed to decode by reference dirs") |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1432 |
func TestDecodeWithMergeKey(t *testing.T) { |
| 1433 |
yml := ` |
| 1434 |
a: &a |
| 1435 |
b: 1 |
| 1436 |
c: hello |
| 1437 |
items: |
| 1438 |
- <<: *a |
| 1439 |
- <<: *a |
| 1440 |
c: world |
| 1441 |
` |
| 1442 |
type Item struct { |
| 1443 |
B int |
| 1444 |
C string |
| 1445 |
} |
| 1446 |
type T struct { |
| 1447 |
Items []*Item |
| 1448 |
} |
| 1449 |
buf := bytes.NewBufferString(yml) |
| 1450 |
dec := yaml.NewDecoder(buf) |
| 1451 |
var v T |
| 1452 |
if err := dec.Decode(&v); err != nil { |
| 1453 |
t.Fatalf("%+v", err) |
| 1454 |
} |
| 1455 |
if len(v.Items) != 2 { |
| 1456 |
t.Fatal("failed to decode with merge key") |
| 1457 |
} |
| 1458 |
if v.Items[0].B != 1 || v.Items[0].C != "hello" { |
| 1459 |
t.Fatal("failed to decode with merge key") |
| 1460 |
} |
| 1461 |
if v.Items[1].B != 1 || v.Items[1].C != "world" { |
| 1462 |
t.Fatal("failed to decode with merge key") |
| 1463 |
} |
| 1464 |
t.Run("decode with interface{}", func(t *testing.T) { |
| 1465 |
buf := bytes.NewBufferString(yml) |
| 1466 |
dec := yaml.NewDecoder(buf) |
| 1467 |
var v interface{} |
| 1468 |
if err := dec.Decode(&v); err != nil { |
| 1469 |
t.Fatalf("%+v", err) |
| 1470 |
} |
| 1471 |
items, _ := v.(map[string]interface{})["items"].([]interface{}) |
| 1472 |
if len(items) != 2 { |
| 1473 |
t.Fatal("failed to decode with merge key") |
| 1474 |
} |
| 1475 |
b0 := items[0].(map[string]interface{})["b"] |
| 1476 |
if _, ok := b0.(uint64); !ok { |
| 1477 |
t.Fatal("failed to decode with merge key") |
| 1478 |
} |
| 1479 |
if b0.(uint64) != 1 { |
| 1480 |
t.Fatal("failed to decode with merge key") |
| 1481 |
} |
| 1482 |
c0 := items[0].(map[string]interface{})["c"] |
| 1483 |
if _, ok := c0.(string); !ok { |
| 1484 |
t.Fatal("failed to decode with merge key") |
| 1485 |
} |
| 1486 |
if c0.(string) != "hello" { |
| 1487 |
t.Fatal("failed to decode with merge key") |
| 1488 |
} |
| 1489 |
b1 := items[1].(map[string]interface{})["b"] |
| 1490 |
if _, ok := b1.(uint64); !ok { |
| 1491 |
t.Fatal("failed to decode with merge key") |
| 1492 |
} |
| 1493 |
if b1.(uint64) != 1 { |
| 1494 |
t.Fatal("failed to decode with merge key") |
| 1495 |
} |
| 1496 |
c1 := items[1].(map[string]interface{})["c"] |
| 1497 |
if _, ok := c1.(string); !ok { |
| 1498 |
t.Fatal("failed to decode with merge key") |
| 1499 |
} |
| 1500 |
if c1.(string) != "world" { |
| 1501 |
t.Fatal("failed to decode with merge key") |
| 1502 |
} |
| 1503 |
}) |
| 1504 |
t.Run("decode with map", func(t *testing.T) { |
| 1505 |
var v struct { |
| 1506 |
Items []map[string]interface{} |
| 1507 |
} |
| 1508 |
buf := bytes.NewBufferString(yml) |
| 1509 |
dec := yaml.NewDecoder(buf) |
| 1510 |
if err := dec.Decode(&v); err != nil { |
| 1511 |
t.Fatalf("%+v", err) |
| 1512 |
} |
| 1513 |
if len(v.Items) != 2 { |
| 1514 |
t.Fatal("failed to decode with merge key") |
| 1515 |
} |
| 1516 |
b0 := v.Items[0]["b"] |
| 1517 |
if _, ok := b0.(uint64); !ok { |
| 1518 |
t.Fatal("failed to decode with merge key") |
| 1519 |
} |
| 1520 |
if b0.(uint64) != 1 { |
| 1521 |
t.Fatal("failed to decode with merge key") |
| 1522 |
} |
| 1523 |
c0 := v.Items[0]["c"] |
| 1524 |
if _, ok := c0.(string); !ok { |
| 1525 |
t.Fatal("failed to decode with merge key") |
| 1526 |
} |
| 1527 |
if c0.(string) != "hello" { |
| 1528 |
t.Fatal("failed to decode with merge key") |
| 1529 |
} |
| 1530 |
b1 := v.Items[1]["b"] |
| 1531 |
if _, ok := b1.(uint64); !ok { |
| 1532 |
t.Fatal("failed to decode with merge key") |
| 1533 |
} |
| 1534 |
if b1.(uint64) != 1 { |
| 1535 |
t.Fatal("failed to decode with merge key") |
| 1536 |
} |
| 1537 |
c1 := v.Items[1]["c"] |
| 1538 |
if _, ok := c1.(string); !ok { |
| 1539 |
t.Fatal("failed to decode with merge key") |
| 1540 |
} |
| 1541 |
if c1.(string) != "world" { |
| 1542 |
t.Fatal("failed to decode with merge key") |
| 1543 |
} |
| 1544 |
}) |
| 1545 |
} |
| 1546 |
|
| 1547 |
func TestDecoder_Inline(t *testing.T) { |
| 1548 |
type Base struct { |
| 1549 |
A int |
| 1550 |
B string |
| 1551 |
} |
| 1552 |
yml := `--- |
| 1553 |
a: 1 |
| 1554 |
b: hello |
| 1555 |
c: true |
| 1556 |
` |
| 1557 |
var v struct { |
| 1558 |
*Base `yaml:",inline"` |
| 1559 |
C bool |
| 1560 |
} |
| 1561 |
if err := yaml.NewDecoder(strings.NewReader(yml)).Decode(&v); err != nil { |
| 1562 |
t.Fatalf("%+v", err) |
| 1563 |
} |
| 1564 |
if v.A != 1 { |
| 1565 |
t.Fatal("failed to decode with inline key") |
| 1566 |
} |
| 1567 |
if v.B != "hello" { |
| 1568 |
t.Fatal("failed to decode with inline key") |
| 1569 |
} |
| 1570 |
if !v.C { |
| 1571 |
t.Fatal("failed to decode with inline key") |
| 1572 |
} |
| 1573 |
|
| 1574 |
t.Run("multiple inline with strict", func(t *testing.T) { |
| 1575 |
type Base struct { |
| 1576 |
A int |
| 1577 |
B string |
| 1578 |
} |
| 1579 |
type Base2 struct { |
| 1580 |
Base *Base `yaml:",inline"` |
| 1581 |
} |
| 1582 |
yml := `--- |
| 1583 |
a: 1 |
| 1584 |
b: hello |
| 1585 |
` |
| 1586 |
var v struct { |
| 1587 |
Base2 *Base2 `yaml:",inline"` |
| 1588 |
} |
| 1589 |
if err := yaml.NewDecoder(strings.NewReader(yml), yaml.Strict()).Decode(&v); err != nil { |
| 1590 |
t.Fatalf("%+v", err) |
| 1591 |
} |
| 1592 |
if v.Base2.Base.A != 1 { |
| 1593 |
t.Fatal("failed to decode with inline key") |
| 1594 |
} |
| 1595 |
if v.Base2.Base.B != "hello" { |
| 1596 |
t.Fatal("failed to decode with inline key") |
| 1597 |
} |
| 1598 |
}) |
| 1599 |
} |
| 1600 |
|
| 1601 |
func TestDecoder_InlineAndConflictKey(t *testing.T) { |
| 1602 |
type Base struct { |
| 1603 |
A int |
| 1604 |
B string |
| 1605 |
} |
| 1606 |
yml := `--- |
| 1607 |
a: 1 |
| 1608 |
b: hello |
| 1609 |
c: true |
| 1610 |
` |
| 1611 |
var v struct { |
| 1612 |
*Base `yaml:",inline"` |
| 1613 |
A int |
| 1614 |
C bool |
| 1615 |
} |
| 1616 |
if err := yaml.NewDecoder(strings.NewReader(yml)).Decode(&v); err != nil { |
| 1617 |
t.Fatalf("%+v", err) |
| 1618 |
} |
| 1619 |
if v.A != 1 { |
| 1620 |
t.Fatal("failed to decode with inline key") |
| 1621 |
} |
| 1622 |
if v.B != "hello" { |
| 1623 |
t.Fatal("failed to decode with inline key") |
| 1624 |
} |
| 1625 |
if !v.C { |
| 1626 |
t.Fatal("failed to decode with inline key") |
| 1627 |
} |
| 1628 |
if v.Base.A != 0 { |
| 1629 |
t.Fatal("failed to decode with inline key") |
| 1630 |
} |
| 1631 |
} |
| 1632 |
|
| 1633 |
func TestDecoder_InlineAndWrongTypeStrict(t *testing.T) { |
| 1634 |
type Base struct { |
| 1635 |
A int |
| 1636 |
B string |
| 1637 |
} |
| 1638 |
yml := `--- |
| 1639 |
a: notanint |
| 1640 |
b: hello |
| 1641 |
c: true |
| 1642 |
` |
| 1643 |
var v struct { |
| 1644 |
*Base `yaml:",inline"` |
| 1645 |
C bool |
| 1646 |
} |
| 1647 |
err := yaml.NewDecoder(strings.NewReader(yml), yaml.Strict()).Decode(&v) |
| 1648 |
if err == nil { |
| 1649 |
t.Fatalf("expected error") |
| 1650 |
} |
| 1651 |
|
| 1652 |
//TODO: properly check if errors are colored/have source |
| 1653 |
t.Logf("%s", err) |
| 1654 |
t.Logf("%s", yaml.FormatError(err, true, false)) |
| 1655 |
t.Logf("%s", yaml.FormatError(err, false, true)) |
| 1656 |
t.Logf("%s", yaml.FormatError(err, true, true)) |
| 1657 |
} |
| 1658 |
|
| 1659 |
func TestDecoder_InvalidCases(t *testing.T) { |
| 1660 |
const src = `--- |
| 1661 |
a: |
| 1662 |
- b |
| 1663 |
c: d |
| 1664 |
` |
| 1665 |
var v struct { |
| 1666 |
A []string |
| 1667 |
} |
| 1668 |
err := yaml.NewDecoder(strings.NewReader(src)).Decode(&v) |
| 1669 |
if err == nil { |
| 1670 |
t.Fatalf("expected error") |
| 1671 |
} |
| 1672 |
|
| 1673 |
if err.Error() != yaml.FormatError(err, false, true) { |
| 1674 |
t.Logf("err.Error() = %s", err.Error()) |
| 1675 |
t.Logf("yaml.FormatError(err, false, true) = %s", yaml.FormatError(err, false, true)) |
| 1676 |
t.Fatal(`err.Error() should match yaml.FormatError(err, false, true)`) |
| 1677 |
} |
| 1678 |
|
| 1679 |
//TODO: properly check if errors are colored/have source |
| 1680 |
t.Logf("%s", err) |
| 1681 |
t.Logf("%s", yaml.FormatError(err, true, false)) |
| 1682 |
t.Logf("%s", yaml.FormatError(err, false, true)) |
| 1683 |
t.Logf("%s", yaml.FormatError(err, true, true)) |
| 1684 |
} |
| 1685 |
|
| 1686 |
func TestDecoder_JSONTags(t *testing.T) { |
| 1687 |
var v struct { |
| 1688 |
A string `json:"a_json"` // no YAML tag |
| 1689 |
B string `json:"b_json" yaml:"b_yaml"` // both tags |
| 1690 |
} |
| 1691 |
|
| 1692 |
const src = `--- |
| 1693 |
a_json: a_json_value |
| 1694 |
b_json: b_json_value |
| 1695 |
b_yaml: b_yaml_value |
| 1696 |
` |
| 1697 |
if err := yaml.NewDecoder(strings.NewReader(src)).Decode(&v); err != nil { |
| 1698 |
t.Fatalf(`parsing should succeed: %s`, err) |
| 1699 |
} |
| 1700 |
|
| 1701 |
if v.A != "a_json_value" { |
| 1702 |
t.Fatalf("v.A should be `a_json_value`, got `%s`", v.A) |
| 1703 |
} |
| 1704 |
|
| 1705 |
if v.B != "b_yaml_value" { |
| 1706 |
t.Fatalf("v.B should be `b_yaml_value`, got `%s`", v.B) |
| 1707 |
} |
| 1708 |
} |
| 1709 |
|
| 1710 |
func TestDecoder_DisallowUnknownField(t *testing.T) { |
| 1711 |
t.Run("different level keys with same name", func(t *testing.T) { |
| 1712 |
var v struct { |
| 1713 |
C Child `yaml:"c"` |
| 1714 |
} |
| 1715 |
yml := `--- |
| 1716 |
b: 1 |
| 1717 |
c: |
| 1718 |
b: 1 |
| 1719 |
` |
| 1720 |
|
| 1721 |
err := yaml.NewDecoder(strings.NewReader(yml), yaml.DisallowUnknownField()).Decode(&v) |
| 1722 |
if err == nil { |
| 1723 |
t.Fatalf("error expected") |
| 1724 |
} |
| 1725 |
}) |
| 1726 |
t.Run("inline", func(t *testing.T) { |
| 1727 |
var v struct { |
| 1728 |
*Child `yaml:",inline"` |
| 1729 |
A string `yaml:"a"` |
| 1730 |
} |
| 1731 |
yml := `--- |
| 1732 |
a: a |
| 1733 |
b: 1 |
| 1734 |
` |
| 1735 |
|
| 1736 |
if err := yaml.NewDecoder(strings.NewReader(yml), yaml.DisallowUnknownField()).Decode(&v); err != nil { |
| 1737 |
t.Fatalf(`parsing should succeed: %s`, err) |
| 1738 |
} |
| 1739 |
if v.A != "a" { |
| 1740 |
t.Fatalf("v.A should be `a`, got `%s`", v.A) |
| 1741 |
} |
| 1742 |
if v.B != 1 { |
| 1743 |
t.Fatalf("v.B should be 1, got %d", v.B) |
| 1744 |
} |
| 1745 |
if v.C != 0 { |
| 1746 |
t.Fatalf("v.C should be 0, got %d", v.C) |
| 1747 |
} |
| 1748 |
}) |
| 1749 |
t.Run("list", func(t *testing.T) { |
| 1750 |
type C struct { |
| 1751 |
Child `yaml:",inline"` |
| 1752 |
} |
| 1753 |
|
| 1754 |
var v struct { |
| 1755 |
Children []C `yaml:"children"` |
| 1756 |
} |
| 1757 |
|
| 1758 |
yml := `--- |
| 1759 |
children: |
| 1760 |
- b: 1 |
| 1761 |
- b: 2 |
| 1762 |
` |
| 1763 |
|
| 1764 |
if err := yaml.NewDecoder(strings.NewReader(yml), yaml.DisallowUnknownField()).Decode(&v); err != nil { |
| 1765 |
t.Fatalf(`parsing should succeed: %s`, err) |
| 1766 |
} |
| 1767 |
|
| 1768 |
if len(v.Children) != 2 { |
| 1769 |
t.Fatalf(`len(v.Children) should be 2, got %d`, len(v.Children)) |
| 1770 |
} |
| 1771 |
|
| 1772 |
if v.Children[0].B != 1 { |
| 1773 |
t.Fatalf(`v.Children[0].B should be 1, got %d`, v.Children[0].B) |
| 1774 |
} |
| 1775 |
|
| 1776 |
if v.Children[1].B != 2 { |
| 1777 |
t.Fatalf(`v.Children[1].B should be 2, got %d`, v.Children[1].B) |
| 1778 |
} |
| 1779 |
}) |
| 1780 |
} |
| 1781 |
|
| 1782 |
func TestDecoder_DisallowDuplicateKey(t *testing.T) { |
| 1783 |
yml := ` |
| 1784 |
a: b |
| 1785 |
a: c |
| 1786 |
` |
| 1787 |
expected := ` |
| 1788 |
[3:1] duplicate key "a" |
| 1789 |
2 | a: b |
| 1790 |
> 3 | a: c |
| 1791 |
^ |
| 1792 |
` |
| 1793 |
t.Run("map", func(t *testing.T) { |
| 1794 |
var v map[string]string |
| 1795 |
err := yaml.NewDecoder(strings.NewReader(yml), yaml.DisallowDuplicateKey()).Decode(&v) |
| 1796 |
if err == nil { |
| 1797 |
t.Fatal("decoding should fail") |
| 1798 |
} |
| 1799 |
actual := "\n" + err.Error() |
| 1800 |
if expected != actual { |
| 1801 |
t.Fatalf("expected:[%s] actual:[%s]", expected, actual) |
| 1802 |
} |
| 1803 |
}) |
| 1804 |
t.Run("struct", func(t *testing.T) { |
| 1805 |
var v struct { |
| 1806 |
A string |
| 1807 |
} |
| 1808 |
err := yaml.NewDecoder(strings.NewReader(yml), yaml.DisallowDuplicateKey()).Decode(&v) |
| 1809 |
if err == nil { |
| 1810 |
t.Fatal("decoding should fail") |
| 1811 |
} |
| 1812 |
actual := "\n" + err.Error() |
| 1813 |
if expected != actual { |
| 1814 |
t.Fatalf("expected:[%s] actual:[%s]", expected, actual) |
| 1815 |
} |
| 1816 |
}) |
| 1817 |
} |
| 1818 |
|
| 1819 |
func TestDecoder_DefaultValues(t *testing.T) { |
| 1820 |
v := struct { |
| 1821 |
A string `yaml:"a"` |
| 1822 |
B string `yaml:"b"` |
| 1823 |
c string // private |
| 1824 |
D struct { |
| 1825 |
E string `yaml:"e"` |
| 1826 |
F struct { |
| 1827 |
G string `yaml:"g"` |
| 1828 |
} `yaml:"f"` |
| 1829 |
H struct { |
| 1830 |
I string `yaml:"i"` |
| 1831 |
} `yaml:",inline"` |
| 1832 |
} `yaml:"d"` |
| 1833 |
J struct { |
| 1834 |
K string `yaml:"k"` |
| 1835 |
L struct { |
| 1836 |
M string `yaml:"m"` |
| 1837 |
} `yaml:"l"` |
| 1838 |
N struct { |
| 1839 |
O string `yaml:"o"` |
| 1840 |
} `yaml:",inline"` |
| 1841 |
} `yaml:",inline"` |
| 1842 |
P struct { |
| 1843 |
Q string `yaml:"q"` |
| 1844 |
R struct { |
| 1845 |
S string `yaml:"s"` |
| 1846 |
} `yaml:"r"` |
| 1847 |
T struct { |
| 1848 |
U string `yaml:"u"` |
| 1849 |
} `yaml:",inline"` |
| 1850 |
} `yaml:"p"` |
| 1851 |
V struct { |
| 1852 |
W string `yaml:"w"` |
| 1853 |
X struct { |
| 1854 |
Y string `yaml:"y"` |
| 1855 |
} `yaml:"x"` |
| 1856 |
Z struct { |
| 1857 |
Ä string `yaml:"ä"` |
| 1858 |
} `yaml:",inline"` |
| 1859 |
} `yaml:",inline"` |
| 1860 |
}{ |
| 1861 |
B: "defaultBValue", |
| 1862 |
c: "defaultCValue", |
| 1863 |
} |
| 1864 |
|
| 1865 |
v.D.E = "defaultEValue" |
| 1866 |
v.D.F.G = "defaultGValue" |
| 1867 |
v.D.H.I = "defaultIValue" |
| 1868 |
v.J.K = "defaultKValue" |
| 1869 |
v.J.L.M = "defaultMValue" |
| 1870 |
v.J.N.O = "defaultOValue" |
| 1871 |
v.P.R.S = "defaultSValue" |
| 1872 |
v.P.T.U = "defaultUValue" |
| 1873 |
v.V.X.Y = "defaultYValue" |
| 1874 |
v.V.Z.Ä = "defaultÄValue" |
| 1875 |
|
| 1876 |
const src = `--- |
| 1877 |
a: a_value |
| 1878 |
p: |
| 1879 |
q: q_value |
| 1880 |
w: w_value |
| 1881 |
` |
| 1882 |
if err := yaml.NewDecoder(strings.NewReader(src)).Decode(&v); err != nil { |
| 1883 |
t.Fatalf(`parsing should succeed: %s`, err) |
| 1884 |
} |
| 1885 |
if v.A != "a_value" { |
| 1886 |
t.Fatalf("v.A should be `a_value`, got `%s`", v.A) |
| 1887 |
} |
| 1888 |
|
| 1889 |
if v.B != "defaultBValue" { |
| 1890 |
t.Fatalf("v.B should be `defaultValue`, got `%s`", v.B) |
| 1891 |
} |
| 1892 |
|
| 1893 |
if v.c != "defaultCValue" { |
| 1894 |
t.Fatalf("v.c should be `defaultCValue`, got `%s`", v.c) |
| 1895 |
} |
| 1896 |
|
| 1897 |
if v.D.E != "defaultEValue" { |
| 1898 |
t.Fatalf("v.D.E should be `defaultEValue`, got `%s`", v.D.E) |
| 1899 |
} |
| 1900 |
|
| 1901 |
if v.D.F.G != "defaultGValue" { |
| 1902 |
t.Fatalf("v.D.F.G should be `defaultGValue`, got `%s`", v.D.F.G) |
| 1903 |
} |
| 1904 |
|
| 1905 |
if v.D.H.I != "defaultIValue" { |
| 1906 |
t.Fatalf("v.D.H.I should be `defaultIValue`, got `%s`", v.D.H.I) |
| 1907 |
} |
| 1908 |
|
| 1909 |
if v.J.K != "defaultKValue" { |
| 1910 |
t.Fatalf("v.J.K should be `defaultKValue`, got `%s`", v.J.K) |
| 1911 |
} |
| 1912 |
|
| 1913 |
if v.J.L.M != "defaultMValue" { |
| 1914 |
t.Fatalf("v.J.L.M should be `defaultMValue`, got `%s`", v.J.L.M) |
| 1915 |
} |
| 1916 |
|
| 1917 |
if v.J.N.O != "defaultOValue" { |
| 1918 |
t.Fatalf("v.J.N.O should be `defaultOValue`, got `%s`", v.J.N.O) |
| 1919 |
} |
| 1920 |
|
| 1921 |
if v.P.Q != "q_value" { |
| 1922 |
t.Fatalf("v.P.Q should be `q_value`, got `%s`", v.P.Q) |
| 1923 |
} |
| 1924 |
|
| 1925 |
if v.P.R.S != "defaultSValue" { |
| 1926 |
t.Fatalf("v.P.R.S should be `defaultSValue`, got `%s`", v.P.R.S) |
| 1927 |
} |
| 1928 |
|
| 1929 |
if v.P.T.U != "defaultUValue" { |
| 1930 |
t.Fatalf("v.P.T.U should be `defaultUValue`, got `%s`", v.P.T.U) |
| 1931 |
} |
| 1932 |
|
| 1933 |
if v.V.W != "w_value" { |
| 1934 |
t.Fatalf("v.V.W should be `w_value`, got `%s`", v.V.W) |
| 1935 |
} |
| 1936 |
|
| 1937 |
if v.V.X.Y != "defaultYValue" { |
| 1938 |
t.Fatalf("v.V.X.Y should be `defaultYValue`, got `%s`", v.V.X.Y) |
| 1939 |
} |
| 1940 |
|
| 1941 |
if v.V.Z.Ä != "defaultÄValue" { |
| 1942 |
t.Fatalf("v.V.Z.Ä should be `defaultÄValue`, got `%s`", v.V.Z.Ä) |
| 1943 |
} |
| 1944 |
} |
| 1945 |
|
| 1946 |
func Example_YAMLTags() { |
| 1947 |
yml := `--- |
| 1948 |
foo: 1 |
| 1949 |
bar: c |
| 1950 |
A: 2 |
| 1951 |
B: d |
| 1952 |
` |
| 1953 |
var v struct { |
| 1954 |
A int `yaml:"foo" json:"A"` |
| 1955 |
B string `yaml:"bar" json:"B"` |
| 1956 |
} |
| 1957 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 1958 |
log.Fatal(err) |
| 1959 |
} |
| 1960 |
fmt.Println(v.A) |
| 1961 |
fmt.Println(v.B) |
| 1962 |
// OUTPUT: |
| 1963 |
// 1 |
| 1964 |
// c |
| 1965 |
} |
| 1966 |
|
| 1967 |
type useJSONUnmarshalerTest struct { |
| 1968 |
s string |
| 1969 |
} |
| 1970 |
|
| 1971 |
func (t *useJSONUnmarshalerTest) UnmarshalJSON(b []byte) error { |
| 1972 |
s, err := strconv.Unquote(string(b)) |
| 1973 |
if err != nil { |
| 1974 |
return err |
| 1975 |
} |
| 1976 |
t.s = s |
| 1977 |
return nil |
| 1978 |
} |
| 1979 |
|
| 1980 |
func TestDecoder_UseJSONUnmarshaler(t *testing.T) { |
| 1981 |
var v useJSONUnmarshalerTest |
| 1982 |
if err := yaml.UnmarshalWithOptions([]byte(`"a"`), &v, yaml.UseJSONUnmarshaler()); err != nil { |
| 1983 |
t.Fatal(err) |
| 1984 |
} |
| 1985 |
if v.s != "a" { |
| 1986 |
t.Fatalf("unexpected decoded value: %s", v.s) |
| 1987 |
} |
| 1988 |
} |
| 1989 |
|
| 1990 |
func TestDecoder_CustomUnmarshaler(t *testing.T) { |
| 1991 |
t.Run("override struct type", func(t *testing.T) { |
| 1992 |
type T struct { |
| 1993 |
Foo string `yaml:"foo"` |
| 1994 |
} |
| 1995 |
src := []byte(`foo: "bar"`) |
| 1996 |
var v T |
| 1997 |
if err := yaml.UnmarshalWithOptions(src, &v, yaml.CustomUnmarshaler[T](func(dst *T, b []byte) error { |
| 1998 |
if !bytes.Equal(src, b) { |
| 1999 |
t.Fatalf("failed to get decode target buffer. expected %q but got %q", src, b) |
| 2000 |
} |
| 2001 |
var v T |
| 2002 |
if err := yaml.Unmarshal(b, &v); err != nil { |
| 2003 |
return err |
| 2004 |
} |
| 2005 |
if v.Foo != "bar" { |
| 2006 |
t.Fatal("failed to decode") |
| 2007 |
} |
| 2008 |
dst.Foo = "bazbaz" // assign another value to target |
| 2009 |
return nil |
| 2010 |
})); err != nil { |
| 2011 |
t.Fatal(err) |
| 2012 |
} |
| 2013 |
if v.Foo != "bazbaz" { |
| 2014 |
t.Fatalf("failed to switch to custom unmarshaler. got: %v", v.Foo) |
| 2015 |
} |
| 2016 |
}) |
| 2017 |
t.Run("override bytes type", func(t *testing.T) { |
| 2018 |
type T struct { |
| 2019 |
Foo []byte `yaml:"foo"` |
| 2020 |
} |
| 2021 |
src := []byte(`foo: "bar"`) |
| 2022 |
var v T |
| 2023 |
if err := yaml.UnmarshalWithOptions(src, &v, yaml.CustomUnmarshaler[[]byte](func(dst *[]byte, b []byte) error { |
| 2024 |
if !bytes.Equal(b, []byte(`"bar"`)) { |
| 2025 |
t.Fatalf("failed to get target buffer: %q", b) |
| 2026 |
} |
| 2027 |
*dst = []byte("bazbaz") |
| 2028 |
return nil |
| 2029 |
})); err != nil { |
| 2030 |
t.Fatal(err) |
| 2031 |
} |
| 2032 |
if !bytes.Equal(v.Foo, []byte("bazbaz")) { |
| 2033 |
t.Fatalf("failed to switch to custom unmarshaler. got: %q", v.Foo) |
| 2034 |
} |
| 2035 |
}) |
| 2036 |
} |
| 2037 |
|
| 2038 |
type unmarshalContext struct { |
| 2039 |
v int |
| 2040 |
} |
| 2041 |
|
| 2042 |
func (c *unmarshalContext) UnmarshalYAML(ctx context.Context, b []byte) error { |
| 2043 |
v, ok := ctx.Value("k").(int) |
| 2044 |
if !ok { |
| 2045 |
return fmt.Errorf("cannot get valid context") |
| 2046 |
} |
| 2047 |
if v != 1 { |
| 2048 |
return fmt.Errorf("cannot get valid context") |
| 2049 |
} |
| 2050 |
if string(b) != "1" { |
| 2051 |
return fmt.Errorf("cannot get valid bytes") |
| 2052 |
} |
| 2053 |
c.v = v |
| 2054 |
return nil |
| 2055 |
} |
| 2056 |
|
| 2057 |
func Test_UnmarshalerContext(t *testing.T) { |
| 2058 |
ctx := context.WithValue(context.Background(), "k", 1) |
| 2059 |
var v unmarshalContext |
| 2060 |
if err := yaml.UnmarshalContext(ctx, []byte(`1`), &v); err != nil { |
| 2061 |
t.Fatalf("%+v", err) |
| 2062 |
} |
| 2063 |
if v.v != 1 { |
| 2064 |
t.Fatal("cannot call UnmarshalYAML") |
| 2065 |
} |
| 2066 |
} |
| 2067 |
|
| 2068 |
func TestDecoder_DecodeFromNode(t *testing.T) { |
| 2069 |
t.Run("has reference", func(t *testing.T) { |
| 2070 |
str := ` |
| 2071 |
anchor: &map |
| 2072 |
text: hello |
| 2073 |
map: *map` |
| 2074 |
var buf bytes.Buffer |
| 2075 |
dec := yaml.NewDecoder(&buf) |
| 2076 |
f, err := parser.ParseBytes([]byte(str), 0) |
| 2077 |
if err != nil { |
| 2078 |
t.Fatalf("failed to parse: %s", err) |
| 2079 |
} |
| 2080 |
type T struct { |
| 2081 |
Map map[string]string |
| 2082 |
} |
| 2083 |
var v T |
| 2084 |
if err := dec.DecodeFromNode(f.Docs[0].Body, &v); err != nil { |
| 2085 |
t.Fatalf("failed to decode: %s", err) |
| 2086 |
} |
| 2087 |
actual := fmt.Sprintf("%+v", v) |
| 2088 |
expect := fmt.Sprintf("%+v", T{map[string]string{"text": "hello"}}) |
| 2089 |
if actual != expect { |
| 2090 |
t.Fatalf("actual=[%s], expect=[%s]", actual, expect) |
| 2091 |
} |
| 2092 |
}) |
| 2093 |
t.Run("with reference option", func(t *testing.T) { |
| 2094 |
anchor := strings.NewReader(` |
| 2095 |
map: &map |
| 2096 |
text: hello`) |
| 2097 |
var buf bytes.Buffer |
| 2098 |
dec := yaml.NewDecoder(&buf, yaml.ReferenceReaders(anchor)) |
| 2099 |
f, err := parser.ParseBytes([]byte("map: *map"), 0) |
| 2100 |
if err != nil { |
| 2101 |
t.Fatalf("failed to parse: %s", err) |
| 2102 |
} |
| 2103 |
type T struct { |
| 2104 |
Map map[string]string |
| 2105 |
} |
| 2106 |
var v T |
| 2107 |
if err := dec.DecodeFromNode(f.Docs[0].Body, &v); err != nil { |
| 2108 |
t.Fatalf("failed to decode: %s", err) |
| 2109 |
} |
| 2110 |
actual := fmt.Sprintf("%+v", v) |
| 2111 |
expect := fmt.Sprintf("%+v", T{map[string]string{"text": "hello"}}) |
| 2112 |
if actual != expect { |
| 2113 |
t.Fatalf("actual=[%s], expect=[%s]", actual, expect) |
| 2114 |
} |
| 2115 |
}) |
| 2116 |
t.Run("value is not pointer", func(t *testing.T) { |
| 2117 |
var buf bytes.Buffer |
| 2118 |
var v bool |
| 2119 |
err := yaml.NewDecoder(&buf).DecodeFromNode(nil, v) |
| 2120 |
if !errors.Is(err, yaml.ErrDecodeRequiredPointerType) { |
| 2121 |
t.Fatalf("unexpected error: %s", err) |
| 2122 |
} |
| 2123 |
}) |
| 2124 |
} |
| 2125 |
|
| 2126 |
func Example_JSONTags() { |
| 2127 |
yml := `--- |
| 2128 |
foo: 1 |
| 2129 |
bar: c |
| 2130 |
` |
| 2131 |
var v struct { |
| 2132 |
A int `json:"foo"` |
| 2133 |
B string `json:"bar"` |
| 2134 |
} |
| 2135 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 2136 |
log.Fatal(err) |
| 2137 |
} |
| 2138 |
fmt.Println(v.A) |
| 2139 |
fmt.Println(v.B) |
| 2140 |
// OUTPUT: |
| 2141 |
// 1 |
| 2142 |
// c |
| 2143 |
} |
| 2144 |
|
| 2145 |
func Example_DisallowUnknownField() { |
| 2146 |
var v struct { |
| 2147 |
A string `yaml:"simple"` |
| 2148 |
C string `yaml:"complicated"` |
| 2149 |
} |
| 2150 |
|
| 2151 |
const src = `--- |
| 2152 |
simple: string |
| 2153 |
unknown: string |
| 2154 |
` |
| 2155 |
err := yaml.NewDecoder(strings.NewReader(src), yaml.DisallowUnknownField()).Decode(&v) |
| 2156 |
fmt.Printf("%v\n", err) |
| 2157 |
|
| 2158 |
// OUTPUT: |
| 2159 |
// [3:1] unknown field "unknown" |
| 2160 |
// 1 | --- |
| 2161 |
// 2 | simple: string |
| 2162 |
// > 3 | unknown: string |
| 2163 |
// ^ |
| 2164 |
} |
| 2165 |
|
| 2166 |
func Example_Unmarshal_Node() { |
| 2167 |
f, err := parser.ParseBytes([]byte("text: node example"), 0) |
| 2168 |
if err != nil { |
| 2169 |
panic(err) |
| 2170 |
} |
| 2171 |
var v struct { |
| 2172 |
Text string `yaml:"text"` |
| 2173 |
} |
| 2174 |
if err := yaml.NodeToValue(f.Docs[0].Body, &v); err != nil { |
| 2175 |
panic(err) |
| 2176 |
} |
| 2177 |
fmt.Println(v.Text) |
| 2178 |
// OUTPUT: |
| 2179 |
// node example |
| 2180 |
} |
| 2181 |
|
| 2182 |
type unmarshalableYAMLStringValue string |
| 2183 |
|
| 2184 |
func (v *unmarshalableYAMLStringValue) UnmarshalYAML(b []byte) error { |
| 2185 |
var s string |
| 2186 |
if err := yaml.Unmarshal(b, &s); err != nil { |
| 2187 |
return err |
| 2188 |
} |
| 2189 |
*v = unmarshalableYAMLStringValue(s) |
| 2190 |
return nil |
| 2191 |
} |
| 2192 |
|
| 2193 |
type unmarshalableTextStringValue string |
| 2194 |
|
| 2195 |
func (v *unmarshalableTextStringValue) UnmarshalText(b []byte) error { |
| 2196 |
*v = unmarshalableTextStringValue(string(b)) |
| 2197 |
return nil |
| 2198 |
} |
| 2199 |
|
| 2200 |
type unmarshalableStringContainer struct { |
| 2201 |
A unmarshalableYAMLStringValue `yaml:"a"` |
| 2202 |
B unmarshalableTextStringValue `yaml:"b"` |
| 2203 |
} |
| 2204 |
|
| 2205 |
func TestUnmarshalableString(t *testing.T) { |
| 2206 |
t.Run("empty string", func(t *testing.T) { |
| 2207 |
t.Parallel() |
| 2208 |
yml := ` |
| 2209 |
a: "" |
| 2210 |
b: "" |
| 2211 |
` |
| 2212 |
var container unmarshalableStringContainer |
| 2213 |
if err := yaml.Unmarshal([]byte(yml), &container); err != nil { |
| 2214 |
t.Fatalf("failed to unmarshal %v", err) |
| 2215 |
} |
| 2216 |
if container.A != "" { |
| 2217 |
t.Fatalf("expected empty string, but %q is set", container.A) |
| 2218 |
} |
| 2219 |
if container.B != "" { |
| 2220 |
t.Fatalf("expected empty string, but %q is set", container.B) |
| 2221 |
} |
| 2222 |
}) |
| 2223 |
t.Run("filled string", func(t *testing.T) { |
| 2224 |
t.Parallel() |
| 2225 |
yml := ` |
| 2226 |
a: "aaa" |
| 2227 |
b: "bbb" |
| 2228 |
` |
| 2229 |
var container unmarshalableStringContainer |
| 2230 |
if err := yaml.Unmarshal([]byte(yml), &container); err != nil { |
| 2231 |
t.Fatalf("failed to unmarshal %v", err) |
| 2232 |
} |
| 2233 |
if container.A != "aaa" { |
| 2234 |
t.Fatalf("expected \"aaa\", but %q is set", container.A) |
| 2235 |
} |
| 2236 |
if container.B != "bbb" { |
| 2237 |
t.Fatalf("expected \"bbb\", but %q is set", container.B) |
| 2238 |
} |
| 2239 |
}) |
| 2240 |
t.Run("single-quoted string", func(t *testing.T) { |
| 2241 |
t.Parallel() |
| 2242 |
yml := ` |
| 2243 |
a: 'aaa' |
| 2244 |
b: 'bbb' |
| 2245 |
` |
| 2246 |
var container unmarshalableStringContainer |
| 2247 |
if err := yaml.Unmarshal([]byte(yml), &container); err != nil { |
| 2248 |
t.Fatalf("failed to unmarshal %v", err) |
| 2249 |
} |
| 2250 |
if container.A != "aaa" { |
| 2251 |
t.Fatalf("expected \"aaa\", but %q is set", container.A) |
| 2252 |
} |
| 2253 |
if container.B != "bbb" { |
| 2254 |
t.Fatalf("expected \"aaa\", but %q is set", container.B) |
| 2255 |
} |
| 2256 |
}) |
| 2257 |
t.Run("literal", func(t *testing.T) { |
| 2258 |
t.Parallel() |
| 2259 |
yml := ` |
| 2260 |
a: | |
| 2261 |
a |
| 2262 |
b |
| 2263 |
c |
| 2264 |
b: | |
| 2265 |
a |
| 2266 |
b |
| 2267 |
c |
| 2268 |
` |
| 2269 |
var container unmarshalableStringContainer |
| 2270 |
if err := yaml.Unmarshal([]byte(yml), &container); err != nil { |
| 2271 |
t.Fatalf("failed to unmarshal %v", err) |
| 2272 |
} |
| 2273 |
if container.A != "a\nb\nc\n" { |
| 2274 |
t.Fatalf("expected \"a\nb\nc\n\", but %q is set", container.A) |
| 2275 |
} |
| 2276 |
if container.B != "a\nb\nc\n" { |
| 2277 |
t.Fatalf("expected \"a\nb\nc\n\", but %q is set", container.B) |
| 2278 |
} |
| 2279 |
}) |
| 2280 |
t.Run("anchor/alias", func(t *testing.T) { |
| 2281 |
yml := ` |
| 2282 |
a: &x 1 |
| 2283 |
b: *x |
| 2284 |
c: &y hello |
| 2285 |
d: *y |
| 2286 |
` |
| 2287 |
var v struct { |
| 2288 |
A, B, C, D unmarshalableTextStringValue |
| 2289 |
} |
| 2290 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 2291 |
t.Fatal(err) |
| 2292 |
} |
| 2293 |
if v.A != "1" { |
| 2294 |
t.Fatal("failed to unmarshal") |
| 2295 |
} |
| 2296 |
if v.B != "1" { |
| 2297 |
t.Fatal("failed to unmarshal") |
| 2298 |
} |
| 2299 |
if v.C != "hello" { |
| 2300 |
t.Fatal("failed to unmarshal") |
| 2301 |
} |
| 2302 |
if v.D != "hello" { |
| 2303 |
t.Fatal("failed to unmarshal") |
| 2304 |
} |
| 2305 |
}) |
| 2306 |
t.Run("net.IP", func(t *testing.T) { |
| 2307 |
yml := ` |
| 2308 |
a: &a 127.0.0.1 |
| 2309 |
b: *a |
| 2310 |
` |
| 2311 |
var v struct { |
| 2312 |
A, B net.IP |
| 2313 |
} |
| 2314 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 2315 |
t.Fatal(err) |
| 2316 |
} |
| 2317 |
if v.A.String() != net.IPv4(127, 0, 0, 1).String() { |
| 2318 |
t.Fatal("failed to unmarshal") |
| 2319 |
} |
| 2320 |
if v.B.String() != net.IPv4(127, 0, 0, 1).String() { |
| 2321 |
t.Fatal("failed to unmarshal") |
| 2322 |
} |
| 2323 |
}) |
| 2324 |
t.Run("quoted map keys", func(t *testing.T) { |
| 2325 |
t.Parallel() |
| 2326 |
yml := ` |
| 2327 |
a: |
| 2328 |
"b" : 2 |
| 2329 |
'c': true |
| 2330 |
` |
| 2331 |
var v struct { |
| 2332 |
A struct { |
| 2333 |
B int |
| 2334 |
C bool |
| 2335 |
} |
| 2336 |
} |
| 2337 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 2338 |
t.Fatalf("failed to unmarshal %v", err) |
| 2339 |
} |
| 2340 |
if v.A.B != 2 { |
| 2341 |
t.Fatalf("expected a.b to equal 2 but was %d", v.A.B) |
| 2342 |
} |
| 2343 |
if !v.A.C { |
| 2344 |
t.Fatal("expected a.c to be true but was false") |
| 2345 |
} |
| 2346 |
}) |
| 2347 |
} |
| 2348 |
|
| 2349 |
type unmarshalablePtrStringContainer struct { |
| 2350 |
V *string `yaml:"value"` |
| 2351 |
} |
| 2352 |
|
| 2353 |
func TestUnmarshalablePtrString(t *testing.T) { |
| 2354 |
t.Run("empty string", func(t *testing.T) { |
| 2355 |
t.Parallel() |
| 2356 |
var container unmarshalablePtrStringContainer |
| 2357 |
if err := yaml.Unmarshal([]byte(`value: ""`), &container); err != nil { |
| 2358 |
t.Fatalf("failed to unmarshal %v", err) |
| 2359 |
} |
| 2360 |
if container.V == nil || *container.V != "" { |
| 2361 |
t.Fatalf("expected empty string, but %q is set", *container.V) |
| 2362 |
} |
| 2363 |
}) |
| 2364 |
|
| 2365 |
t.Run("null", func(t *testing.T) { |
| 2366 |
t.Parallel() |
| 2367 |
var container unmarshalablePtrStringContainer |
| 2368 |
if err := yaml.Unmarshal([]byte(`value: null`), &container); err != nil { |
| 2369 |
t.Fatalf("failed to unmarshal %v", err) |
| 2370 |
} |
| 2371 |
if container.V != (*string)(nil) { |
| 2372 |
t.Fatalf("expected nil, but %q is set", *container.V) |
| 2373 |
} |
| 2374 |
}) |
| 2375 |
} |
| 2376 |
|
| 2377 |
type unmarshalableIntValue int |
| 2378 |
|
| 2379 |
func (v *unmarshalableIntValue) UnmarshalYAML(raw []byte) error { |
| 2380 |
i, err := strconv.Atoi(string(raw)) |
| 2381 |
if err != nil { |
| 2382 |
return err |
| 2383 |
} |
| 2384 |
*v = unmarshalableIntValue(i) |
| 2385 |
return nil |
| 2386 |
} |
| 2387 |
|
| 2388 |
type unmarshalableIntContainer struct { |
| 2389 |
V unmarshalableIntValue `yaml:"value"` |
| 2390 |
} |
| 2391 |
|
| 2392 |
func TestUnmarshalableInt(t *testing.T) { |
| 2393 |
t.Run("empty int", func(t *testing.T) { |
| 2394 |
t.Parallel() |
| 2395 |
var container unmarshalableIntContainer |
| 2396 |
if err := yaml.Unmarshal([]byte(``), &container); err != nil { |
| 2397 |
t.Fatalf("failed to unmarshal %v", err) |
| 2398 |
} |
| 2399 |
if container.V != 0 { |
| 2400 |
t.Fatalf("expected empty int, but %d is set", container.V) |
| 2401 |
} |
| 2402 |
}) |
| 2403 |
t.Run("filled int", func(t *testing.T) { |
| 2404 |
t.Parallel() |
| 2405 |
var container unmarshalableIntContainer |
| 2406 |
if err := yaml.Unmarshal([]byte(`value: 9`), &container); err != nil { |
| 2407 |
t.Fatalf("failed to unmarshal %v", err) |
| 2408 |
} |
| 2409 |
if container.V != 9 { |
| 2410 |
t.Fatalf("expected 9, but %d is set", container.V) |
| 2411 |
} |
| 2412 |
}) |
| 2413 |
t.Run("filled number", func(t *testing.T) { |
| 2414 |
t.Parallel() |
| 2415 |
var container unmarshalableIntContainer |
| 2416 |
if err := yaml.Unmarshal([]byte(`value: 9`), &container); err != nil { |
| 2417 |
t.Fatalf("failed to unmarshal %v", err) |
| 2418 |
} |
| 2419 |
if container.V != 9 { |
| 2420 |
t.Fatalf("expected 9, but %d is set", container.V) |
| 2421 |
} |
| 2422 |
}) |
| 2423 |
} |
| 2424 |
|
| 2425 |
type unmarshalablePtrIntContainer struct { |
| 2426 |
V *int `yaml:"value"` |
| 2427 |
} |
| 2428 |
|
| 2429 |
func TestUnmarshalablePtrInt(t *testing.T) { |
| 2430 |
t.Run("empty int", func(t *testing.T) { |
| 2431 |
t.Parallel() |
| 2432 |
var container unmarshalablePtrIntContainer |
| 2433 |
if err := yaml.Unmarshal([]byte(`value: 0`), &container); err != nil { |
| 2434 |
t.Fatalf("failed to unmarshal %v", err) |
| 2435 |
} |
| 2436 |
if container.V == nil || *container.V != 0 { |
| 2437 |
t.Fatalf("expected 0, but %q is set", *container.V) |
| 2438 |
} |
| 2439 |
}) |
| 2440 |
|
| 2441 |
t.Run("null", func(t *testing.T) { |
| 2442 |
t.Parallel() |
| 2443 |
var container unmarshalablePtrIntContainer |
| 2444 |
if err := yaml.Unmarshal([]byte(`value: null`), &container); err != nil { |
| 2445 |
t.Fatalf("failed to unmarshal %v", err) |
| 2446 |
} |
| 2447 |
if container.V != (*int)(nil) { |
| 2448 |
t.Fatalf("expected nil, but %q is set", *container.V) |
| 2449 |
} |
| 2450 |
}) |
| 2451 |
} |
| 2452 |
|
| 2453 |
type literalContainer struct { |
| 2454 |
v string |
| 2455 |
} |
| 2456 |
|
| 2457 |
func (c *literalContainer) UnmarshalYAML(v []byte) error { |
| 2458 |
var lit string |
| 2459 |
if err := yaml.Unmarshal(v, &lit); err != nil { |
| 2460 |
return err |
| 2461 |
} |
| 2462 |
c.v = lit |
| 2463 |
return nil |
| 2464 |
} |
| 2465 |
|
| 2466 |
func TestDecode_Literal(t *testing.T) { |
| 2467 |
yml := `--- |
| 2468 |
value: | |
| 2469 |
{ |
| 2470 |
"key": "value" |
| 2471 |
} |
| 2472 |
` |
| 2473 |
var v map[string]*literalContainer |
| 2474 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 2475 |
t.Fatalf("failed to unmarshal %+v", err) |
| 2476 |
} |
| 2477 |
if v["value"] == nil { |
| 2478 |
t.Fatal("failed to unmarshal literal with bytes unmarshaler") |
| 2479 |
} |
| 2480 |
if v["value"].v == "" { |
| 2481 |
t.Fatal("failed to unmarshal literal with bytes unmarshaler") |
| 2482 |
} |
| 2483 |
} |
| 2484 |
|
| 2485 |
func TestDecoder_UseOrderedMap(t *testing.T) { |
| 2486 |
yml := ` |
| 2487 |
a: b |
| 2488 |
c: d |
| 2489 |
e: |
| 2490 |
f: g |
| 2491 |
h: i |
| 2492 |
j: k |
| 2493 |
` |
| 2494 |
var v interface{} |
| 2495 |
if err := yaml.NewDecoder(strings.NewReader(yml), yaml.UseOrderedMap()).Decode(&v); err != nil { |
| 2496 |
t.Fatalf("%+v", err) |
| 2497 |
} |
| 2498 |
if _, ok := v.(yaml.MapSlice); !ok { |
| 2499 |
t.Fatalf("failed to convert to ordered map: %T", v) |
| 2500 |
} |
| 2501 |
bytes, err := yaml.Marshal(v) |
| 2502 |
if err != nil { |
| 2503 |
t.Fatalf("%+v", err) |
| 2504 |
} |
| 2505 |
if string(yml) != "\n"+string(bytes) { |
| 2506 |
t.Fatalf("expected:[%s] actual:[%s]", string(yml), "\n"+string(bytes)) |
| 2507 |
} |
| 2508 |
} |
| 2509 |
|
| 2510 |
func TestDecoder_Stream(t *testing.T) { |
| 2511 |
yml := ` |
| 2512 |
--- |
| 2513 |
a: b |
| 2514 |
c: d |
| 2515 |
--- |
| 2516 |
e: f |
| 2517 |
g: h |
| 2518 |
--- |
| 2519 |
i: j |
| 2520 |
k: l |
| 2521 |
` |
| 2522 |
dec := yaml.NewDecoder(strings.NewReader(yml)) |
| 2523 |
values := []map[string]string{} |
| 2524 |
for { |
| 2525 |
var v map[string]string |
| 2526 |
if err := dec.Decode(&v); err != nil { |
| 2527 |
if err == io.EOF { |
| 2528 |
break |
| 2529 |
} |
| 2530 |
t.Fatalf("%+v", err) |
| 2531 |
} |
| 2532 |
values = append(values, v) |
| 2533 |
} |
| 2534 |
if len(values) != 3 { |
| 2535 |
t.Fatal("failed to stream decoding") |
| 2536 |
} |
| 2537 |
if values[0]["a"] != "b" { |
| 2538 |
t.Fatal("failed to stream decoding") |
| 2539 |
} |
| 2540 |
if values[1]["e"] != "f" { |
| 2541 |
t.Fatal("failed to stream decoding") |
| 2542 |
} |
| 2543 |
if values[2]["i"] != "j" { |
| 2544 |
t.Fatal("failed to stream decoding") |
| 2545 |
} |
| 2546 |
} |
| 2547 |
|
| 2548 |
type unmarshalYAMLWithAliasString string |
| 2549 |
|
| 2550 |
func (v *unmarshalYAMLWithAliasString) UnmarshalYAML(b []byte) error { |
| 2551 |
var s string |
| 2552 |
if err := yaml.Unmarshal(b, &s); err != nil { |
| 2553 |
return err |
| 2554 |
} |
| 2555 |
*v = unmarshalYAMLWithAliasString(s) |
| 2556 |
return nil |
| 2557 |
} |
| 2558 |
|
| 2559 |
type unmarshalYAMLWithAliasMap map[string]interface{} |
| 2560 |
|
| 2561 |
func (v *unmarshalYAMLWithAliasMap) UnmarshalYAML(b []byte) error { |
| 2562 |
var m map[string]interface{} |
| 2563 |
if err := yaml.Unmarshal(b, &m); err != nil { |
| 2564 |
return err |
| 2565 |
} |
| 2566 |
*v = unmarshalYAMLWithAliasMap(m) |
| 2567 |
return nil |
| 2568 |
} |
| 2569 |
|
| 2570 |
func TestDecoder_UnmarshalYAMLWithAlias(t *testing.T) { |
| 2571 |
type value struct { |
| 2572 |
String unmarshalYAMLWithAliasString |
| 2573 |
Map unmarshalYAMLWithAliasMap |
| 2574 |
} |
| 2575 |
tests := []struct { |
| 2576 |
name string |
| 2577 |
yaml string |
| 2578 |
expectedValue value |
| 2579 |
err string |
| 2580 |
}{ |
| 2581 |
{ |
| 2582 |
name: "ok", |
| 2583 |
yaml: ` |
| 2584 |
anchors: |
| 2585 |
w: &w "\"hello\" \"world\"" |
| 2586 |
map: &x |
| 2587 |
a: b |
| 2588 |
c: d |
| 2589 |
d: *w |
| 2590 |
string: *w |
| 2591 |
map: |
| 2592 |
<<: *x |
| 2593 |
e: f |
| 2594 |
`, |
| 2595 |
expectedValue: value{ |
| 2596 |
String: unmarshalYAMLWithAliasString(`"hello" "world"`), |
| 2597 |
Map: unmarshalYAMLWithAliasMap(map[string]interface{}{ |
| 2598 |
"a": "b", |
| 2599 |
"c": "d", |
| 2600 |
"d": `"hello" "world"`, |
| 2601 |
"e": "f", |
| 2602 |
}), |
| 2603 |
}, |
| 2604 |
}, |
| 2605 |
{ |
| 2606 |
name: "unknown alias", |
| 2607 |
yaml: ` |
| 2608 |
anchors: |
| 2609 |
w: &w "\"hello\" \"world\"" |
| 2610 |
map: &x |
| 2611 |
a: b |
| 2612 |
c: d |
| 2613 |
d: *w |
| 2614 |
string: *y |
| 2615 |
map: |
| 2616 |
<<: *z |
| 2617 |
e: f |
| 2618 |
`, |
| 2619 |
err: "cannot find anchor by alias name y", |
| 2620 |
}, |
| 2621 |
} |
| 2622 |
|
| 2623 |
for _, test := range tests { |
| 2624 |
t.Run(test.name, func(t *testing.T) { |
| 2625 |
var v value |
| 2626 |
err := yaml.Unmarshal([]byte(test.yaml), &v) |
| 2627 |
|
| 2628 |
if test.err != "" { |
| 2629 |
if err == nil { |
| 2630 |
t.Fatal("expected to error") |
| 2631 |
} |
| 2632 |
if !strings.Contains(err.Error(), test.err) { |
| 2633 |
t.Fatalf("expected error message: %s to contain: %s", err.Error(), test.err) |
| 2634 |
} |
| 2635 |
} else { |
| 2636 |
if err != nil { |
| 2637 |
t.Fatalf("%+v", err) |
| 2638 |
} |
| 2639 |
if !reflect.DeepEqual(test.expectedValue, v) { |
| 2640 |
t.Fatalf("non matching values:\nexpected[%s]\ngot [%s]", test.expectedValue, v) |
| 2641 |
} |
| 2642 |
} |
| 2643 |
}) |
| 2644 |
} |
| 2645 |
} |
| 2646 |
|
| 2647 |
type unmarshalString string |
| 2648 |
|
| 2649 |
func (u *unmarshalString) UnmarshalYAML(b []byte) error { |
| 2650 |
*u = unmarshalString(string(b)) |
| 2651 |
return nil |
| 2652 |
} |
| 2653 |
|
| 2654 |
type unmarshalList struct { |
| 2655 |
v []map[string]unmarshalString |
| 2656 |
} |
| 2657 |
|
| 2658 |
func (u *unmarshalList) UnmarshalYAML(b []byte) error { |
| 2659 |
expected := ` |
| 2660 |
- b: c |
| 2661 |
d: | |
| 2662 |
hello |
| 2663 |
|
| 2664 |
hello |
| 2665 |
f: g |
| 2666 |
- h: i` |
| 2667 |
actual := "\n" + string(b) |
| 2668 |
if expected != actual { |
| 2669 |
return fmt.Errorf("unexpected bytes: expected [%q] but got [%q]", expected, actual) |
| 2670 |
} |
| 2671 |
var v []map[string]unmarshalString |
| 2672 |
if err := yaml.Unmarshal(b, &v); err != nil { |
| 2673 |
return err |
| 2674 |
} |
| 2675 |
u.v = v |
| 2676 |
return nil |
| 2677 |
} |
| 2678 |
|
| 2679 |
func TestDecoder_UnmarshalBytesWithSeparatedList(t *testing.T) { |
| 2680 |
yml := ` |
| 2681 |
a: |
| 2682 |
- b: c |
| 2683 |
d: | |
| 2684 |
hello |
| 2685 |
|
| 2686 |
hello |
| 2687 |
f: g |
| 2688 |
- h: i |
| 2689 |
` |
| 2690 |
var v struct { |
| 2691 |
A unmarshalList |
| 2692 |
} |
| 2693 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 2694 |
t.Fatal(err) |
| 2695 |
} |
| 2696 |
if len(v.A.v) != 2 { |
| 2697 |
t.Fatalf("failed to unmarshal %+v", v) |
| 2698 |
} |
| 2699 |
if len(v.A.v[0]) != 3 { |
| 2700 |
t.Fatalf("failed to unmarshal %+v", v.A.v[0]) |
| 2701 |
} |
| 2702 |
if len(v.A.v[1]) != 1 { |
| 2703 |
t.Fatalf("failed to unmarshal %+v", v.A.v[1]) |
| 2704 |
} |
| 2705 |
} |
| 2706 |
|
| 2707 |
func TestDecoder_LiteralWithNewLine(t *testing.T) { |
| 2708 |
type A struct { |
| 2709 |
Node string `yaml:"b"` |
| 2710 |
LastNode string `yaml:"last"` |
| 2711 |
} |
| 2712 |
tests := []A{ |
| 2713 |
{ |
| 2714 |
Node: "hello\nworld", |
| 2715 |
}, |
| 2716 |
{ |
| 2717 |
Node: "hello\nworld\n", |
| 2718 |
}, |
| 2719 |
{ |
| 2720 |
Node: "hello\nworld\n\n", |
| 2721 |
}, |
| 2722 |
{ |
| 2723 |
LastNode: "hello\nworld", |
| 2724 |
}, |
| 2725 |
{ |
| 2726 |
LastNode: "hello\nworld\n", |
| 2727 |
}, |
| 2728 |
{ |
| 2729 |
LastNode: "hello\nworld\n\n", |
| 2730 |
}, |
| 2731 |
} |
| 2732 |
// struct(want) -> Marshal -> Unmarchal -> struct(got) |
| 2733 |
for _, want := range tests { |
| 2734 |
bytes, _ := yaml.Marshal(want) |
| 2735 |
got := A{} |
| 2736 |
if err := yaml.Unmarshal(bytes, &got); err != nil { |
| 2737 |
t.Fatal(err) |
| 2738 |
} |
| 2739 |
if want.Node != got.Node { |
| 2740 |
t.Fatalf("expected:%q but got %q", want.Node, got.Node) |
| 2741 |
} |
| 2742 |
if want.LastNode != got.LastNode { |
| 2743 |
t.Fatalf("expected:%q but got %q", want.LastNode, got.LastNode) |
| 2744 |
} |
| 2745 |
} |
| 2746 |
} |
| 2747 |
|
| 2748 |
func TestDecoder_TabCharacterAtRight(t *testing.T) { |
| 2749 |
yml := ` |
| 2750 |
- a: [2 , 2] |
| 2751 |
b: [2 , 2] |
| 2752 |
c: [2 , 2]` |
| 2753 |
var v []map[string][]int |
| 2754 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 2755 |
t.Fatal(err) |
| 2756 |
} |
| 2757 |
if len(v) != 1 { |
| 2758 |
t.Fatalf("failed to unmarshal %+v", v) |
| 2759 |
} |
| 2760 |
if len(v[0]) != 3 { |
| 2761 |
t.Fatalf("failed to unmarshal %+v", v) |
| 2762 |
} |
| 2763 |
} |
| 2764 |
|
| 2765 |
func TestDecoder_Canonical(t *testing.T) { |
| 2766 |
yml := ` |
| 2767 |
!!map { |
| 2768 |
? !!str "explicit":!!str "entry", |
| 2769 |
? !!str "implicit" : !!str "entry", |
| 2770 |
? !!null "" : !!null "", |
| 2771 |
} |
| 2772 |
` |
| 2773 |
var v interface{} |
| 2774 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 2775 |
t.Fatalf("%+v", err) |
| 2776 |
} |
| 2777 |
m, ok := v.(map[string]interface{}) |
| 2778 |
if !ok { |
| 2779 |
t.Fatalf("failed to decode canonical yaml: %+v", v) |
| 2780 |
} |
| 2781 |
if m["explicit"] != "entry" { |
| 2782 |
t.Fatalf("failed to decode canonical yaml: %+v", m) |
| 2783 |
} |
| 2784 |
if m["implicit"] != "entry" { |
| 2785 |
t.Fatalf("failed to decode canonical yaml: %+v", m) |
| 2786 |
} |
| 2787 |
if m["null"] != nil { |
| 2788 |
t.Fatalf("failed to decode canonical yaml: %+v", m) |
| 2789 |
} |
| 2790 |
} |
| 2791 |
|
| 2792 |
func TestDecoder_DecodeFromFile(t *testing.T) { |
| 2793 |
yml := ` |
| 2794 |
a: b |
| 2795 |
c: d |
| 2796 |
` |
| 2797 |
file, err := parser.ParseBytes([]byte(yml), 0) |
| 2798 |
if err != nil { |
| 2799 |
t.Fatal(err) |
| 2800 |
} |
| 2801 |
var v map[string]string |
| 2802 |
if err := yaml.NewDecoder(file).Decode(&v); err != nil { |
| 2803 |
t.Fatal(err) |
| 2804 |
} |
| 2805 |
if len(v) != 2 { |
| 2806 |
t.Fatal("failed to decode from ast.File") |
| 2807 |
} |
| 2808 |
if v["a"] != "b" { |
| 2809 |
t.Fatal("failed to decode from ast.File") |
| 2810 |
} |
| 2811 |
if v["c"] != "d" { |
| 2812 |
t.Fatal("failed to decode from ast.File") |
| 2813 |
} |
| 2814 |
} |
| 2815 |
|
| 2816 |
func TestDecoder_DecodeWithNode(t *testing.T) { |
| 2817 |
t.Run("abstract node", func(t *testing.T) { |
| 2818 |
type T struct { |
| 2819 |
Text ast.Node `yaml:"text"` |
| 2820 |
} |
| 2821 |
var v T |
| 2822 |
if err := yaml.Unmarshal([]byte(`text: hello`), &v); err != nil { |
| 2823 |
t.Fatalf("%+v", err) |
| 2824 |
} |
| 2825 |
expected := "hello" |
| 2826 |
got := v.Text.String() |
| 2827 |
if expected != got { |
| 2828 |
t.Fatalf("failed to decode to ast.Node: expected %s but got %s", expected, got) |
| 2829 |
} |
| 2830 |
}) |
| 2831 |
t.Run("concrete node", func(t *testing.T) { |
| 2832 |
type T struct { |
| 2833 |
Text *ast.StringNode `yaml:"text"` |
| 2834 |
} |
| 2835 |
var v T |
| 2836 |
if err := yaml.Unmarshal([]byte(`text: hello`), &v); err != nil { |
| 2837 |
t.Fatalf("%+v", err) |
| 2838 |
} |
| 2839 |
expected := "hello" |
| 2840 |
got := v.Text.String() |
| 2841 |
if expected != got { |
| 2842 |
t.Fatalf("failed to decode to ast.Node: expected %s but got %s", expected, got) |
| 2843 |
} |
| 2844 |
}) |
| 2845 |
} |
| 2846 |
|
| 2847 |
func TestRoundtripAnchorAlias(t *testing.T) { |
| 2848 |
t.Run("irreversible", func(t *testing.T) { |
| 2849 |
type foo struct { |
| 2850 |
K1 string |
| 2851 |
K2 string |
| 2852 |
} |
| 2853 |
|
| 2854 |
type bar struct { |
| 2855 |
K1 string |
| 2856 |
K3 string |
| 2857 |
} |
| 2858 |
|
| 2859 |
type doc struct { |
| 2860 |
Foo foo |
| 2861 |
Bar bar |
| 2862 |
} |
| 2863 |
yml := ` |
| 2864 |
foo: |
| 2865 |
<<: &test-anchor |
| 2866 |
k1: "One" |
| 2867 |
k2: "Two" |
| 2868 |
|
| 2869 |
bar: |
| 2870 |
<<: *test-anchor |
| 2871 |
k3: "Three" |
| 2872 |
` |
| 2873 |
var v doc |
| 2874 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 2875 |
t.Fatalf("%+v", err) |
| 2876 |
} |
| 2877 |
bytes, err := yaml.Marshal(v) |
| 2878 |
if err != nil { |
| 2879 |
t.Fatalf("%+v", err) |
| 2880 |
} |
| 2881 |
expected := ` |
| 2882 |
foo: |
| 2883 |
k1: One |
| 2884 |
k2: Two |
| 2885 |
bar: |
| 2886 |
k1: One |
| 2887 |
k3: Three |
| 2888 |
` |
| 2889 |
got := "\n" + string(bytes) |
| 2890 |
if expected != got { |
| 2891 |
t.Fatalf("expected:[%s] but got [%s]", expected, got) |
| 2892 |
} |
| 2893 |
}) |
| 2894 |
t.Run("reversible", func(t *testing.T) { |
| 2895 |
type TestAnchor struct { |
| 2896 |
K1 string |
| 2897 |
} |
| 2898 |
type foo struct { |
| 2899 |
*TestAnchor `yaml:",inline,alias"` |
| 2900 |
K2 string |
| 2901 |
} |
| 2902 |
type bar struct { |
| 2903 |
*TestAnchor `yaml:",inline,alias"` |
| 2904 |
K3 string |
| 2905 |
} |
| 2906 |
type doc struct { |
| 2907 |
TestAnchor *TestAnchor `yaml:"test-anchor,anchor"` |
| 2908 |
Foo foo |
| 2909 |
Bar bar |
| 2910 |
} |
| 2911 |
yml := ` |
| 2912 |
test-anchor: &test-anchor |
| 2913 |
k1: One |
| 2914 |
foo: |
| 2915 |
<<: *test-anchor |
| 2916 |
k2: Two |
| 2917 |
bar: |
| 2918 |
<<: *test-anchor |
| 2919 |
k3: Three |
| 2920 |
` |
| 2921 |
var v doc |
| 2922 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 2923 |
t.Fatalf("%+v", err) |
| 2924 |
} |
| 2925 |
bytes, err := yaml.Marshal(v) |
| 2926 |
if err != nil { |
| 2927 |
t.Fatalf("%+v", err) |
| 2928 |
} |
| 2929 |
got := "\n" + string(bytes) |
| 2930 |
if yml != got { |
| 2931 |
t.Fatalf("expected:[%s] but got [%s]", yml, got) |
| 2932 |
} |
| 2933 |
}) |
| 2934 |
} |
| 2935 |
|
| 2936 |
func TestUnmarshalMapSliceParallel(t *testing.T) { |
| 2937 |
content := ` |
| 2938 |
steps: |
| 2939 |
req0: |
| 2940 |
desc: Get /users/1 |
| 2941 |
req: |
| 2942 |
/users/1: |
| 2943 |
get: nil |
| 2944 |
test: | |
| 2945 |
current.res.status == 200 |
| 2946 |
req1: |
| 2947 |
desc: Get /private |
| 2948 |
req: |
| 2949 |
/private: |
| 2950 |
get: nil |
| 2951 |
test: | |
| 2952 |
current.res.status == 403 |
| 2953 |
req2: |
| 2954 |
desc: Get /users |
| 2955 |
req: |
| 2956 |
/users: |
| 2957 |
get: nil |
| 2958 |
test: | |
| 2959 |
current.res.status == 200 |
| 2960 |
` |
| 2961 |
type mappedSteps struct { |
| 2962 |
Steps yaml.MapSlice `yaml:"steps,omitempty"` |
| 2963 |
} |
| 2964 |
for i := 0; i < 100; i++ { |
| 2965 |
t.Run(fmt.Sprintf("i=%d", i), func(t *testing.T) { |
| 2966 |
t.Parallel() |
| 2967 |
for i := 0; i < 10; i++ { |
| 2968 |
m := mappedSteps{ |
| 2969 |
Steps: yaml.MapSlice{}, |
| 2970 |
} |
| 2971 |
if err := yaml.Unmarshal([]byte(content), &m); err != nil { |
| 2972 |
t.Fatal(err) |
| 2973 |
} |
| 2974 |
for _, s := range m.Steps { |
| 2975 |
_, ok := s.Value.(map[string]interface{}) |
| 2976 |
if !ok { |
| 2977 |
t.Fatal("unexpected error") |
| 2978 |
} |
| 2979 |
} |
| 2980 |
} |
| 2981 |
}) |
| 2982 |
} |
| 2983 |
} |
| 2984 |
|
| 2985 |
func TestSameNameInineStruct(t *testing.T) { |
| 2986 |
type X struct { |
| 2987 |
X float64 `yaml:"x"` |
| 2988 |
} |
| 2989 |
|
| 2990 |
type T struct { |
| 2991 |
X `yaml:",inline"` |
| 2992 |
} |
| 2993 |
|
| 2994 |
var v T |
| 2995 |
if err := yaml.Unmarshal([]byte(`x: 0.7`), &v); err != nil { |
| 2996 |
t.Fatal(err) |
| 2997 |
} |
| 2998 |
if fmt.Sprint(v.X.X) != "0.7" { |
| 2999 |
t.Fatalf("failed to decode") |
| 3000 |
} |
| 3001 |
} |
| 3002 |
|
| 3003 |
type unmarshableMapKey struct { |
| 3004 |
Key string |
| 3005 |
} |
| 3006 |
|
| 3007 |
func (mk *unmarshableMapKey) UnmarshalYAML(b []byte) error { |
| 3008 |
mk.Key = string(b) |
| 3009 |
return nil |
| 3010 |
} |
| 3011 |
|
| 3012 |
func TestMapKeyCustomUnmarshaler(t *testing.T) { |
| 3013 |
var m map[unmarshableMapKey]string |
| 3014 |
if err := yaml.Unmarshal([]byte(`key: value`), &m); err != nil { |
| 3015 |
t.Fatalf("failed to unmarshal %v", err) |
| 3016 |
} |
| 3017 |
if len(m) != 1 { |
| 3018 |
t.Fatalf("expected 1 element in map, but got %d", len(m)) |
| 3019 |
} |
| 3020 |
val, ok := m[unmarshableMapKey{Key: "key"}] |
| 3021 |
if !ok { |
| 3022 |
t.Fatal("expected to have element 'key' in map") |
| 3023 |
} |
| 3024 |
if val != "value" { |
| 3025 |
t.Fatalf("expected to have value \"value\", but got %q", val) |
| 3026 |
} |
| 3027 |
} |
| 3028 |
|
| 3029 |
func TestDecoderPreservesDefaultValues(t *testing.T) { |
| 3030 |
type nested struct { |
| 3031 |
Val string `yaml:"val"` |
| 3032 |
} |
| 3033 |
|
| 3034 |
type test struct { |
| 3035 |
First string `yaml:"first"` |
| 3036 |
Default nested `yaml:"nested"` |
| 3037 |
} |
| 3038 |
|
| 3039 |
yml := ` |
| 3040 |
first: "Test" |
| 3041 |
nested: |
| 3042 |
# Just some comment here |
| 3043 |
# val: "default" |
| 3044 |
` |
| 3045 |
v := test{Default: nested{Val: "default"}} |
| 3046 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 3047 |
t.Fatal(err) |
| 3048 |
} |
| 3049 |
if v.Default.Val != "default" { |
| 3050 |
t.Fatal("decoder doesn't preserve struct defaults") |
| 3051 |
} |
| 3052 |
} |