| 1 |
package yaml_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"reflect" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/goccy/go-yaml" |
| 11 |
"github.com/goccy/go-yaml/parser" |
| 12 |
) |
| 13 |
|
| 14 |
func builder() *yaml.PathBuilder { return &yaml.PathBuilder{} } |
| 15 |
|
| 16 |
func TestPathBuilder(t *testing.T) { |
| 17 |
tests := []struct { |
| 18 |
expected string |
| 19 |
path *yaml.Path |
| 20 |
}{ |
| 21 |
{ |
| 22 |
expected: `$.a.b[0]`, |
| 23 |
path: builder().Root().Child("a").Child("b").Index(0).Build(), |
| 24 |
}, |
| 25 |
{ |
| 26 |
expected: `$.'a.b'.'c*d'`, |
| 27 |
path: builder().Root().Child("a.b").Child("c*d").Build(), |
| 28 |
}, |
| 29 |
{ |
| 30 |
expected: `$.'a.b-*'.c`, |
| 31 |
path: builder().Root().Child("a.b-*").Child("c").Build(), |
| 32 |
}, |
| 33 |
{ |
| 34 |
expected: `$.'a'.b`, |
| 35 |
path: builder().Root().Child("'a'").Child("b").Build(), |
| 36 |
}, |
| 37 |
{ |
| 38 |
expected: `$.'a.b'.c`, |
| 39 |
path: builder().Root().Child("'a.b'").Child("c").Build(), |
| 40 |
}, |
| 41 |
} |
| 42 |
for _, test := range tests { |
| 43 |
t.Run(test.expected, func(t *testing.T) { |
| 44 |
expected := test.expected |
| 45 |
got := test.path.String() |
| 46 |
if expected != got { |
| 47 |
t.Fatalf("failed to build path. expected:[%q] but got:[%q]", expected, got) |
| 48 |
} |
| 49 |
}) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestPath(t *testing.T) { |
| 54 |
yml := ` |
| 55 |
store: |
| 56 |
book: |
| 57 |
- author: john |
| 58 |
price: 10 |
| 59 |
- author: ken |
| 60 |
price: 12 |
| 61 |
bicycle: |
| 62 |
color: red |
| 63 |
price: 19.95 |
| 64 |
bicycle*unicycle: |
| 65 |
price: 20.25 |
| 66 |
` |
| 67 |
tests := []struct { |
| 68 |
name string |
| 69 |
path *yaml.Path |
| 70 |
expected interface{} |
| 71 |
}{ |
| 72 |
{ |
| 73 |
name: "$.store.book[0].author", |
| 74 |
path: builder().Root().Child("store").Child("book").Index(0).Child("author").Build(), |
| 75 |
expected: "john", |
| 76 |
}, |
| 77 |
{ |
| 78 |
name: "$.store.book[1].price", |
| 79 |
path: builder().Root().Child("store").Child("book").Index(1).Child("price").Build(), |
| 80 |
expected: uint64(12), |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "$.store.book[*].author", |
| 84 |
path: builder().Root().Child("store").Child("book").IndexAll().Child("author").Build(), |
| 85 |
expected: []interface{}{"john", "ken"}, |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "$.store.book[0]", |
| 89 |
path: builder().Root().Child("store").Child("book").Index(0).Build(), |
| 90 |
expected: map[string]interface{}{"author": "john", "price": uint64(10)}, |
| 91 |
}, |
| 92 |
{ |
| 93 |
name: "$..author", |
| 94 |
path: builder().Root().Recursive("author").Build(), |
| 95 |
expected: []interface{}{"john", "ken"}, |
| 96 |
}, |
| 97 |
{ |
| 98 |
name: "$.store.bicycle.price", |
| 99 |
path: builder().Root().Child("store").Child("bicycle").Child("price").Build(), |
| 100 |
expected: float64(19.95), |
| 101 |
}, |
| 102 |
{ |
| 103 |
name: `$.store.'bicycle*unicycle'.price`, |
| 104 |
path: builder().Root().Child("store").Child(`bicycle*unicycle`).Child("price").Build(), |
| 105 |
expected: float64(20.25), |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "$", |
| 109 |
path: builder().Root().Build(), |
| 110 |
expected: map[string]interface{}{ |
| 111 |
"store": map[string]interface{}{ |
| 112 |
"book": []interface{}{ |
| 113 |
map[string]interface{}{ |
| 114 |
"author": "john", |
| 115 |
"price": uint64(10), |
| 116 |
}, |
| 117 |
map[string]interface{}{ |
| 118 |
"author": "ken", |
| 119 |
"price": uint64(12), |
| 120 |
}, |
| 121 |
}, |
| 122 |
"bicycle": map[string]interface{}{ |
| 123 |
"color": "red", |
| 124 |
"price": 19.95, |
| 125 |
}, |
| 126 |
"bicycle*unicycle": map[string]interface{}{ |
| 127 |
"price": 20.25, |
| 128 |
}, |
| 129 |
}, |
| 130 |
}, |
| 131 |
}, |
| 132 |
} |
| 133 |
t.Run("PathString", func(t *testing.T) { |
| 134 |
for _, test := range tests { |
| 135 |
t.Run(test.name, func(t *testing.T) { |
| 136 |
path, err := yaml.PathString(test.name) |
| 137 |
if err != nil { |
| 138 |
t.Fatalf("%+v", err) |
| 139 |
} |
| 140 |
if test.name != path.String() { |
| 141 |
t.Fatalf("expected %s but actual %s", test.name, path.String()) |
| 142 |
} |
| 143 |
}) |
| 144 |
} |
| 145 |
}) |
| 146 |
t.Run("string", func(t *testing.T) { |
| 147 |
for _, test := range tests { |
| 148 |
t.Run(test.name, func(t *testing.T) { |
| 149 |
if test.name != test.path.String() { |
| 150 |
t.Fatalf("expected %s but actual %s", test.name, test.path.String()) |
| 151 |
} |
| 152 |
}) |
| 153 |
} |
| 154 |
}) |
| 155 |
t.Run("read", func(t *testing.T) { |
| 156 |
for _, test := range tests { |
| 157 |
t.Run(test.name, func(t *testing.T) { |
| 158 |
var v interface{} |
| 159 |
if err := test.path.Read(strings.NewReader(yml), &v); err != nil { |
| 160 |
t.Fatalf("%+v", err) |
| 161 |
} |
| 162 |
if !reflect.DeepEqual(test.expected, v) { |
| 163 |
t.Fatalf("expected %v(%T). but actual %v(%T)", test.expected, test.expected, v, v) |
| 164 |
} |
| 165 |
}) |
| 166 |
} |
| 167 |
}) |
| 168 |
t.Run("filter", func(t *testing.T) { |
| 169 |
var target interface{} |
| 170 |
if err := yaml.Unmarshal([]byte(yml), &target); err != nil { |
| 171 |
t.Fatalf("failed to unmarshal: %+v", err) |
| 172 |
} |
| 173 |
for _, test := range tests { |
| 174 |
t.Run(test.name, func(t *testing.T) { |
| 175 |
var v interface{} |
| 176 |
if err := test.path.Filter(target, &v); err != nil { |
| 177 |
t.Fatalf("%+v", err) |
| 178 |
} |
| 179 |
if !reflect.DeepEqual(test.expected, v) { |
| 180 |
t.Fatalf("expected %v(%T). but actual %v(%T)", test.expected, test.expected, v, v) |
| 181 |
} |
| 182 |
}) |
| 183 |
} |
| 184 |
}) |
| 185 |
} |
| 186 |
|
| 187 |
func TestPath_ReservedKeyword(t *testing.T) { |
| 188 |
tests := []struct { |
| 189 |
name string |
| 190 |
path string |
| 191 |
src string |
| 192 |
expected interface{} |
| 193 |
failure bool |
| 194 |
}{ |
| 195 |
{ |
| 196 |
name: "quoted path", |
| 197 |
path: `$.'a.b.c'.foo`, |
| 198 |
src: ` |
| 199 |
a.b.c: |
| 200 |
foo: bar |
| 201 |
`, |
| 202 |
expected: "bar", |
| 203 |
}, |
| 204 |
{ |
| 205 |
name: "contains quote key", |
| 206 |
path: `$.a'b`, |
| 207 |
src: `a'b: 10`, |
| 208 |
expected: uint64(10), |
| 209 |
}, |
| 210 |
{ |
| 211 |
name: "escaped quote", |
| 212 |
path: `$.'alice\'s age'`, |
| 213 |
src: `alice's age: 10`, |
| 214 |
expected: uint64(10), |
| 215 |
}, |
| 216 |
{ |
| 217 |
name: "directly use white space", |
| 218 |
path: `$.a b`, |
| 219 |
src: `a b: 10`, |
| 220 |
expected: uint64(10), |
| 221 |
}, |
| 222 |
{ |
| 223 |
name: "empty quoted key", |
| 224 |
path: `$.''`, |
| 225 |
src: `a: 10`, |
| 226 |
failure: true, |
| 227 |
}, |
| 228 |
{ |
| 229 |
name: "unterminated quote", |
| 230 |
path: `$.'abcd`, |
| 231 |
src: `abcd: 10`, |
| 232 |
failure: true, |
| 233 |
}, |
| 234 |
} |
| 235 |
for _, test := range tests { |
| 236 |
t.Run(test.name, func(t *testing.T) { |
| 237 |
path, err := yaml.PathString(test.path) |
| 238 |
if test.failure { |
| 239 |
if err == nil { |
| 240 |
t.Fatal("expected error") |
| 241 |
} |
| 242 |
return |
| 243 |
} else { |
| 244 |
if err != nil { |
| 245 |
t.Fatalf("%+v", err) |
| 246 |
} |
| 247 |
} |
| 248 |
file, err := parser.ParseBytes([]byte(test.src), 0) |
| 249 |
if err != nil { |
| 250 |
t.Fatal(err) |
| 251 |
} |
| 252 |
var v interface{} |
| 253 |
if err := path.Read(file, &v); err != nil { |
| 254 |
t.Fatalf("%+v", err) |
| 255 |
} |
| 256 |
if v != test.expected { |
| 257 |
t.Fatalf("failed to get value. expected:[%v] but got:[%v]", test.expected, v) |
| 258 |
} |
| 259 |
}) |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
func TestPath_Invalid(t *testing.T) { |
| 264 |
tests := []struct { |
| 265 |
path string |
| 266 |
src string |
| 267 |
}{ |
| 268 |
{ |
| 269 |
path: "$.wrong", |
| 270 |
src: "foo: bar", |
| 271 |
}, |
| 272 |
} |
| 273 |
for _, test := range tests { |
| 274 |
path, err := yaml.PathString(test.path) |
| 275 |
if err != nil { |
| 276 |
t.Fatal(err) |
| 277 |
} |
| 278 |
t.Run("path.Read", func(t *testing.T) { |
| 279 |
file, err := parser.ParseBytes([]byte(test.src), 0) |
| 280 |
if err != nil { |
| 281 |
t.Fatal(err) |
| 282 |
} |
| 283 |
var v interface{} |
| 284 |
err = path.Read(file, &v) |
| 285 |
if err == nil { |
| 286 |
t.Fatal("expected error") |
| 287 |
} |
| 288 |
if !yaml.IsNotFoundNodeError(err) { |
| 289 |
t.Fatalf("unexpected error %s", err) |
| 290 |
} |
| 291 |
}) |
| 292 |
t.Run("path.ReadNode", func(t *testing.T) { |
| 293 |
file, err := parser.ParseBytes([]byte(test.src), 0) |
| 294 |
if err != nil { |
| 295 |
t.Fatal(err) |
| 296 |
} |
| 297 |
_, err = path.ReadNode(file) |
| 298 |
if err == nil { |
| 299 |
t.Fatal("expected error") |
| 300 |
} |
| 301 |
if !yaml.IsNotFoundNodeError(err) { |
| 302 |
t.Fatalf("unexpected error %s", err) |
| 303 |
} |
| 304 |
}) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
func TestPath_Merge(t *testing.T) { |
| 309 |
tests := []struct { |
| 310 |
path string |
| 311 |
dst string |
| 312 |
src string |
| 313 |
expected string |
| 314 |
}{ |
| 315 |
{ |
| 316 |
path: "$.c", |
| 317 |
dst: ` |
| 318 |
a: 1 |
| 319 |
b: 2 |
| 320 |
c: |
| 321 |
d: 3 |
| 322 |
e: 4 |
| 323 |
`, |
| 324 |
src: ` |
| 325 |
f: 5 |
| 326 |
g: 6 |
| 327 |
`, |
| 328 |
expected: ` |
| 329 |
a: 1 |
| 330 |
b: 2 |
| 331 |
c: |
| 332 |
d: 3 |
| 333 |
e: 4 |
| 334 |
f: 5 |
| 335 |
g: 6 |
| 336 |
`, |
| 337 |
}, |
| 338 |
{ |
| 339 |
path: "$.a.b", |
| 340 |
dst: ` |
| 341 |
a: |
| 342 |
b: |
| 343 |
- 1 |
| 344 |
- 2 |
| 345 |
`, |
| 346 |
src: ` |
| 347 |
- 3 |
| 348 |
- map: |
| 349 |
- 4 |
| 350 |
- 5 |
| 351 |
`, |
| 352 |
expected: ` |
| 353 |
a: |
| 354 |
b: |
| 355 |
- 1 |
| 356 |
- 2 |
| 357 |
- 3 |
| 358 |
- map: |
| 359 |
- 4 |
| 360 |
- 5 |
| 361 |
`, |
| 362 |
}, |
| 363 |
} |
| 364 |
for _, test := range tests { |
| 365 |
t.Run(test.path, func(t *testing.T) { |
| 366 |
path, err := yaml.PathString(test.path) |
| 367 |
if err != nil { |
| 368 |
t.Fatalf("%+v", err) |
| 369 |
} |
| 370 |
t.Run("FromReader", func(t *testing.T) { |
| 371 |
file, err := parser.ParseBytes([]byte(test.dst), 0) |
| 372 |
if err != nil { |
| 373 |
t.Fatalf("%+v", err) |
| 374 |
} |
| 375 |
if err := path.MergeFromReader(file, strings.NewReader(test.src)); err != nil { |
| 376 |
t.Fatalf("%+v", err) |
| 377 |
} |
| 378 |
actual := "\n" + file.String() |
| 379 |
if test.expected != actual { |
| 380 |
t.Fatalf("expected: %q. but got %q", test.expected, actual) |
| 381 |
} |
| 382 |
}) |
| 383 |
t.Run("FromFile", func(t *testing.T) { |
| 384 |
file, err := parser.ParseBytes([]byte(test.dst), 0) |
| 385 |
if err != nil { |
| 386 |
t.Fatalf("%+v", err) |
| 387 |
} |
| 388 |
src, err := parser.ParseBytes([]byte(test.src), 0) |
| 389 |
if err != nil { |
| 390 |
t.Fatalf("%+v", err) |
| 391 |
} |
| 392 |
if err := path.MergeFromFile(file, src); err != nil { |
| 393 |
t.Fatalf("%+v", err) |
| 394 |
} |
| 395 |
actual := "\n" + file.String() |
| 396 |
if test.expected != actual { |
| 397 |
t.Fatalf("expected: %q. but got %q", test.expected, actual) |
| 398 |
} |
| 399 |
}) |
| 400 |
t.Run("FromNode", func(t *testing.T) { |
| 401 |
file, err := parser.ParseBytes([]byte(test.dst), 0) |
| 402 |
if err != nil { |
| 403 |
t.Fatalf("%+v", err) |
| 404 |
} |
| 405 |
src, err := parser.ParseBytes([]byte(test.src), 0) |
| 406 |
if err != nil { |
| 407 |
t.Fatalf("%+v", err) |
| 408 |
} |
| 409 |
if len(src.Docs) == 0 { |
| 410 |
t.Fatalf("failed to parse") |
| 411 |
} |
| 412 |
if err := path.MergeFromNode(file, src.Docs[0]); err != nil { |
| 413 |
t.Fatalf("%+v", err) |
| 414 |
} |
| 415 |
actual := "\n" + file.String() |
| 416 |
if test.expected != actual { |
| 417 |
t.Fatalf("expected: %q. but got %q", test.expected, actual) |
| 418 |
} |
| 419 |
}) |
| 420 |
}) |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
func TestPath_Replace(t *testing.T) { |
| 425 |
tests := []struct { |
| 426 |
path string |
| 427 |
dst string |
| 428 |
src string |
| 429 |
expected string |
| 430 |
}{ |
| 431 |
{ |
| 432 |
path: "$.a", |
| 433 |
dst: ` |
| 434 |
a: 1 |
| 435 |
b: 2 |
| 436 |
`, |
| 437 |
src: `3`, |
| 438 |
expected: ` |
| 439 |
a: 3 |
| 440 |
b: 2 |
| 441 |
`, |
| 442 |
}, |
| 443 |
{ |
| 444 |
path: "$.b", |
| 445 |
dst: ` |
| 446 |
b: 1 |
| 447 |
c: 2 |
| 448 |
`, |
| 449 |
src: ` |
| 450 |
d: e |
| 451 |
f: |
| 452 |
g: h |
| 453 |
i: j |
| 454 |
`, |
| 455 |
expected: ` |
| 456 |
b: |
| 457 |
d: e |
| 458 |
f: |
| 459 |
g: h |
| 460 |
i: j |
| 461 |
c: 2 |
| 462 |
`, |
| 463 |
}, |
| 464 |
{ |
| 465 |
path: "$.a.b[0]", |
| 466 |
dst: ` |
| 467 |
a: |
| 468 |
b: |
| 469 |
- hello |
| 470 |
c: 2 |
| 471 |
`, |
| 472 |
src: `world`, |
| 473 |
expected: ` |
| 474 |
a: |
| 475 |
b: |
| 476 |
- world |
| 477 |
c: 2 |
| 478 |
`, |
| 479 |
}, |
| 480 |
|
| 481 |
{ |
| 482 |
path: "$.books[*].author", |
| 483 |
dst: ` |
| 484 |
books: |
| 485 |
- name: book_a |
| 486 |
author: none |
| 487 |
- name: book_b |
| 488 |
author: none |
| 489 |
pictures: |
| 490 |
- name: picture_a |
| 491 |
author: none |
| 492 |
- name: picture_b |
| 493 |
author: none |
| 494 |
building: |
| 495 |
author: none |
| 496 |
`, |
| 497 |
src: `ken`, |
| 498 |
expected: ` |
| 499 |
books: |
| 500 |
- name: book_a |
| 501 |
author: ken |
| 502 |
- name: book_b |
| 503 |
author: ken |
| 504 |
pictures: |
| 505 |
- name: picture_a |
| 506 |
author: none |
| 507 |
- name: picture_b |
| 508 |
author: none |
| 509 |
building: |
| 510 |
author: none |
| 511 |
`, |
| 512 |
}, |
| 513 |
{ |
| 514 |
path: "$..author", |
| 515 |
dst: ` |
| 516 |
books: |
| 517 |
- name: book_a |
| 518 |
author: none |
| 519 |
- name: book_b |
| 520 |
author: none |
| 521 |
pictures: |
| 522 |
- name: picture_a |
| 523 |
author: none |
| 524 |
- name: picture_b |
| 525 |
author: none |
| 526 |
building: |
| 527 |
author: none |
| 528 |
`, |
| 529 |
src: `ken`, |
| 530 |
expected: ` |
| 531 |
books: |
| 532 |
- name: book_a |
| 533 |
author: ken |
| 534 |
- name: book_b |
| 535 |
author: ken |
| 536 |
pictures: |
| 537 |
- name: picture_a |
| 538 |
author: ken |
| 539 |
- name: picture_b |
| 540 |
author: ken |
| 541 |
building: |
| 542 |
author: ken |
| 543 |
`, |
| 544 |
}, |
| 545 |
} |
| 546 |
for _, test := range tests { |
| 547 |
t.Run(test.path, func(t *testing.T) { |
| 548 |
path, err := yaml.PathString(test.path) |
| 549 |
if err != nil { |
| 550 |
t.Fatalf("%+v", err) |
| 551 |
} |
| 552 |
t.Run("WithReader", func(t *testing.T) { |
| 553 |
file, err := parser.ParseBytes([]byte(test.dst), 0) |
| 554 |
if err != nil { |
| 555 |
t.Fatalf("%+v", err) |
| 556 |
} |
| 557 |
if err := path.ReplaceWithReader(file, strings.NewReader(test.src)); err != nil { |
| 558 |
t.Fatalf("%+v", err) |
| 559 |
} |
| 560 |
actual := "\n" + file.String() |
| 561 |
if test.expected != actual { |
| 562 |
t.Fatalf("expected: %q. but got %q", test.expected, actual) |
| 563 |
} |
| 564 |
}) |
| 565 |
t.Run("WithFile", func(t *testing.T) { |
| 566 |
file, err := parser.ParseBytes([]byte(test.dst), 0) |
| 567 |
if err != nil { |
| 568 |
t.Fatalf("%+v", err) |
| 569 |
} |
| 570 |
src, err := parser.ParseBytes([]byte(test.src), 0) |
| 571 |
if err != nil { |
| 572 |
t.Fatalf("%+v", err) |
| 573 |
} |
| 574 |
if err := path.ReplaceWithFile(file, src); err != nil { |
| 575 |
t.Fatalf("%+v", err) |
| 576 |
} |
| 577 |
actual := "\n" + file.String() |
| 578 |
if test.expected != actual { |
| 579 |
t.Fatalf("expected: %q. but got %q", test.expected, actual) |
| 580 |
} |
| 581 |
}) |
| 582 |
t.Run("WithNode", func(t *testing.T) { |
| 583 |
file, err := parser.ParseBytes([]byte(test.dst), 0) |
| 584 |
if err != nil { |
| 585 |
t.Fatalf("%+v", err) |
| 586 |
} |
| 587 |
src, err := parser.ParseBytes([]byte(test.src), 0) |
| 588 |
if err != nil { |
| 589 |
t.Fatalf("%+v", err) |
| 590 |
} |
| 591 |
if len(src.Docs) == 0 { |
| 592 |
t.Fatalf("failed to parse") |
| 593 |
} |
| 594 |
if err := path.ReplaceWithNode(file, src.Docs[0]); err != nil { |
| 595 |
t.Fatalf("%+v", err) |
| 596 |
} |
| 597 |
actual := "\n" + file.String() |
| 598 |
if test.expected != actual { |
| 599 |
t.Fatalf("expected: %q. but got %q", test.expected, actual) |
| 600 |
} |
| 601 |
}) |
| 602 |
}) |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
func ExamplePath_AnnotateSource() { |
| 607 |
yml := ` |
| 608 |
a: 1 |
| 609 |
b: "hello" |
| 610 |
` |
| 611 |
var v struct { |
| 612 |
A int |
| 613 |
B string |
| 614 |
} |
| 615 |
if err := yaml.Unmarshal([]byte(yml), &v); err != nil { |
| 616 |
panic(err) |
| 617 |
} |
| 618 |
if v.A != 2 { |
| 619 |
// output error with YAML source |
| 620 |
path, err := yaml.PathString("$.a") |
| 621 |
if err != nil { |
| 622 |
log.Fatal(err) |
| 623 |
} |
| 624 |
source, err := path.AnnotateSource([]byte(yml), false) |
| 625 |
if err != nil { |
| 626 |
log.Fatal(err) |
| 627 |
} |
| 628 |
fmt.Printf("a value expected 2 but actual %d:\n%s\n", v.A, string(source)) |
| 629 |
} |
| 630 |
// OUTPUT: |
| 631 |
// a value expected 2 but actual 1: |
| 632 |
// > 2 | a: 1 |
| 633 |
// ^ |
| 634 |
// 3 | b: "hello" |
| 635 |
} |
| 636 |
|
| 637 |
func ExamplePath_AnnotateSourceWithComment() { |
| 638 |
yml := ` |
| 639 |
# This is my document |
| 640 |
doc: |
| 641 |
# This comment should be line 3 |
| 642 |
map: |
| 643 |
# And below should be line 5 |
| 644 |
- value1 |
| 645 |
- value2 |
| 646 |
other: value3 |
| 647 |
` |
| 648 |
path, err := yaml.PathString("$.doc.map[0]") |
| 649 |
if err != nil { |
| 650 |
log.Fatal(err) |
| 651 |
} |
| 652 |
msg, err := path.AnnotateSource([]byte(yml), false) |
| 653 |
if err != nil { |
| 654 |
log.Fatal(err) |
| 655 |
} |
| 656 |
fmt.Println(string(msg)) |
| 657 |
// OUTPUT: |
| 658 |
// 4 | # This comment should be line 3 |
| 659 |
// 5 | map: |
| 660 |
// 6 | # And below should be line 5 |
| 661 |
// > 7 | - value1 |
| 662 |
// ^ |
| 663 |
// 8 | - value2 |
| 664 |
// 9 | other: value3 |
| 665 |
// 10 | |
| 666 |
} |
| 667 |
|
| 668 |
func ExamplePath_PathString() { |
| 669 |
yml := ` |
| 670 |
store: |
| 671 |
book: |
| 672 |
- author: john |
| 673 |
price: 10 |
| 674 |
- author: ken |
| 675 |
price: 12 |
| 676 |
bicycle: |
| 677 |
color: red |
| 678 |
price: 19.95 |
| 679 |
` |
| 680 |
path, err := yaml.PathString("$.store.book[*].author") |
| 681 |
if err != nil { |
| 682 |
log.Fatal(err) |
| 683 |
} |
| 684 |
var authors []string |
| 685 |
if err := path.Read(strings.NewReader(yml), &authors); err != nil { |
| 686 |
log.Fatal(err) |
| 687 |
} |
| 688 |
fmt.Println(authors) |
| 689 |
// OUTPUT: |
| 690 |
// [john ken] |
| 691 |
} |