From cd2020c310212c88922f78d3b920fe2ca00adc08 Mon Sep 17 00:00:00 2001 From: ivan katliarchuk Date: Sun, 12 Jan 2025 00:12:22 +0000 Subject: [PATCH 1/2] issue-281: add tests and fix panic for * Signed-off-by: ivan katliarchuk --- .gitignore | 2 + ast/ast.go | 3 + path_test.go | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) diff --git a/.gitignore b/.gitignore index 869fe9f8..76839bd2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ bin/ cover.out +.DS_Store +.idea/ diff --git a/ast/ast.go b/ast/ast.go index c9bbf9d7..50428a2f 100644 --- a/ast/ast.go +++ b/ast/ast.go @@ -1598,6 +1598,9 @@ func (n *SequenceNode) blockStyleString() string { } for idx, value := range n.Values { + if value == nil { + continue + } valueStr := value.String() newLinePrefix := "" if strings.HasPrefix(valueStr, "\n") { diff --git a/path_test.go b/path_test.go index 5b62d26f..efdee3df 100644 --- a/path_test.go +++ b/path_test.go @@ -84,6 +84,36 @@ store: path: builder().Root().Child("store").Child("book").IndexAll().Child("author").Build(), expected: []interface{}{"john", "ken"}, }, + { + name: "$.store.book[*]", + path: builder().Root().Child("store").Child("book").IndexAll().Build(), + expected: []interface{}{ + map[string]interface{}{ + "author": "john", + "price": uint64(10), + }, + map[string]interface{}{ + "author": "ken", + "price": uint64(12), + }, + }, + }, + { + name: "$..book[*]", + path: builder().Root().Recursive("book").IndexAll().Build(), + expected: []interface{}{ + []interface{}{ + map[string]interface{}{ + "author": "john", + "price": uint64(10), + }, + map[string]interface{}{ + "author": "ken", + "price": uint64(12), + }, + }, + }, + }, { name: "$.store.book[0]", path: builder().Root().Child("store").Child("book").Index(0).Build(), @@ -305,6 +335,143 @@ func TestPath_Invalid(t *testing.T) { } } +func TestPath_ReadNode(t *testing.T) { + tests := []struct { + name string + path string + src string + expected interface{} + }{ + { + name: "nested array sequence", + path: `$.a.b[0].c`, + src: ` +a: + b: + - c: 123 + e: | + Line1 + Line2 +`, + expected: uint64(123), + }, + { + name: "nested array sequence issue#281", + path: `$..a.c`, + src: ` +s: + - a: + b: u1 + c: get1 + d: i1 + - w: + c: bad + e: + - a: + b: u2 + c: get2 + d: i2 +`, + // The expected values are + // - get1 + // - get2 + expected: []interface{}{ + map[string]interface{}{ + "b": "u1", + "c": "get1", + "d": "i1", + }, + map[string]interface{}{ + "b": "u2", + "c": "get2", + "d": "i2", + }, + }, + }, + { + name: "nested array sequence issue#281", + path: `$..c`, + src: ` +s: + - a: + b: u1 + c: get1 + d: i1 + - w: + c: bad + e: + - a: + b: u2 + c: get2 + d: i2 +`, + expected: []interface{}{"get1", "bad", "get2"}, + }, + { + name: "nested array sequence issue#281", + path: `$.s[0].a.c`, + src: ` +s: + - a: + b: u1 + c: get1 + d: i1 + - w: + c: bad + e: + - a: + b: u2 + c: get2 + d: i2 +`, + expected: "get1", + }, + { + name: "nested array sequence issue#281", + path: "$.s[*].a.c", + src: ` +s: + - a: + b: u1 + c: get1 + d: i1 + - w: + c: bad + e: + - a: + b: u2 + c: get2 + d: i2 +`, + expected: []interface{}{"get1"}, + }, + } + for _, test := range tests { + path, err := yaml.PathString(test.path) + if err != nil { + t.Fatal(err) + } + t.Run(fmt.Sprintf("path.ReadNode %s path %s", test.name, test.path), func(t *testing.T) { + file, err := parser.ParseBytes([]byte(test.src), 0) + if err != nil { + t.Fatal(err) + } + n, err := path.ReadNode(file) + if err != nil { + t.Fatal("expected error", err) + } + var v interface{} + err = yaml.Unmarshal([]byte(n.String()), &v) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(test.expected, v) { + t.Fatalf("expected %v(%T) but got %v(%T)", test.expected, test.expected, v, v) + } + }) + } +} + func TestPath_Merge(t *testing.T) { tests := []struct { path string From 07f580f72f000cc00475133c637ebe8e0cfe9460 Mon Sep 17 00:00:00 2001 From: ivan katliarchuk Date: Sun, 12 Jan 2025 23:09:43 +0000 Subject: [PATCH 2/2] code review. rollback .gitignore changes Signed-off-by: ivan katliarchuk --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index 76839bd2..869fe9f8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,2 @@ bin/ cover.out -.DS_Store -.idea/