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