From e3ccd05a00f9acf69dbb628c6b36103914148487 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 2 May 2023 15:07:04 +1000 Subject: [PATCH] wip - rabbit hole :/ --- pkg/yqlib/doc/operators/comment-operators.md | 6 +- pkg/yqlib/doc/operators/delete.md | 46 ++++-------- pkg/yqlib/doc/operators/divide.md | 21 ++---- pkg/yqlib/doc/operators/document-index.md | 1 + pkg/yqlib/doc/operators/encode-decode.md | 70 ++++--------------- pkg/yqlib/doc/operators/entries.md | 59 ++++++++-------- pkg/yqlib/doc/operators/variable-operators.md | 12 +--- pkg/yqlib/operator_delete.go | 6 +- pkg/yqlib/operator_delete_test.go | 10 ++- pkg/yqlib/operator_document_index_test.go | 2 +- 10 files changed, 77 insertions(+), 156 deletions(-) diff --git a/pkg/yqlib/doc/operators/comment-operators.md b/pkg/yqlib/doc/operators/comment-operators.md index b05274a4..04229b0e 100644 --- a/pkg/yqlib/doc/operators/comment-operators.md +++ b/pkg/yqlib/doc/operators/comment-operators.md @@ -31,8 +31,7 @@ will output lc: "" fc: "" - p: hello - isKey: null - true: null + isKey: true hc: "" lc: hello-world-comment fc: "" @@ -42,8 +41,7 @@ will output lc: "" fc: "" - p: hello.message - isKey: null - true: null + isKey: true hc: "" lc: "" fc: "" diff --git a/pkg/yqlib/doc/operators/delete.md b/pkg/yqlib/doc/operators/delete.md index 6d0a99a5..dbd544cd 100644 --- a/pkg/yqlib/doc/operators/delete.md +++ b/pkg/yqlib/doc/operators/delete.md @@ -5,8 +5,7 @@ Deletes matching entries in maps or arrays. ## Delete entry in map Given a sample.yml file of: ```yaml -a: cat -b: dog +{a: cat, b: dog} ``` then ```bash @@ -14,15 +13,13 @@ yq 'del(.b)' sample.yml ``` will output ```yaml -a: cat +{a: cat} ``` ## Delete nested entry in map Given a sample.yml file of: ```yaml -a: - a1: fred - a2: frood +{a: {a1: fred, a2: frood}} ``` then ```bash @@ -30,16 +27,13 @@ yq 'del(.a.a1)' sample.yml ``` will output ```yaml -a: - a2: frood +{a: {a2: frood}} ``` ## Delete entry in array Given a sample.yml file of: ```yaml -- 1 -- 2 -- 3 +[1, 2, 3] ``` then ```bash @@ -47,15 +41,13 @@ yq 'del(.[1])' sample.yml ``` will output ```yaml -- 1 -- 3 +[1, 3] ``` ## Delete nested entry in array Given a sample.yml file of: ```yaml -- a: cat - b: dog +[{a: cat, b: dog}] ``` then ```bash @@ -63,14 +55,13 @@ yq 'del(.[0].a)' sample.yml ``` will output ```yaml -- b: dog +[{b: dog}] ``` ## Delete no matches Given a sample.yml file of: ```yaml -a: cat -b: dog +{a: cat, b: dog} ``` then ```bash @@ -78,16 +69,13 @@ yq 'del(.c)' sample.yml ``` will output ```yaml -a: cat -b: dog +{a: cat, b: dog} ``` ## Delete matching entries Given a sample.yml file of: ```yaml -a: cat -b: dog -c: bat +{a: cat, b: dog, c: bat} ``` then ```bash @@ -95,17 +83,13 @@ yq 'del( .[] | select(. == "*at") )' sample.yml ``` will output ```yaml -b: dog +{b: dog} ``` ## Recursively delete matching keys Given a sample.yml file of: ```yaml -a: - name: frog - b: - name: blog - age: 12 +{a: {name: frog, b: {name: blog, age: 12}}} ``` then ```bash @@ -113,8 +97,6 @@ yq 'del(.. | select(has("name")).name)' sample.yml ``` will output ```yaml -a: - b: - age: 12 +{a: {b: {age: 12}}} ``` diff --git a/pkg/yqlib/doc/operators/divide.md b/pkg/yqlib/doc/operators/divide.md index 6d88949f..65197a7c 100644 --- a/pkg/yqlib/doc/operators/divide.md +++ b/pkg/yqlib/doc/operators/divide.md @@ -7,8 +7,7 @@ Divide behaves differently according to the type of the LHS: ## String split Given a sample.yml file of: ```yaml -a: cat_meow -b: _ +{a: cat_meow, b: _} ``` then ```bash @@ -16,11 +15,7 @@ yq '.c = .a / .b' sample.yml ``` will output ```yaml -a: cat_meow -b: _ -c: - - cat - - meow +{a: cat_meow, b: _, c: [cat, meow]} ``` ## Number division @@ -28,8 +23,7 @@ The result during division is calculated as a float Given a sample.yml file of: ```yaml -a: 12 -b: 2.5 +{a: 12, b: 2.5} ``` then ```bash @@ -37,8 +31,7 @@ yq '.a = .a / .b' sample.yml ``` will output ```yaml -a: 4.8 -b: 2.5 +{a: 4.8, b: 2.5} ``` ## Number division by zero @@ -46,8 +39,7 @@ Dividing by zero results in +Inf or -Inf Given a sample.yml file of: ```yaml -a: 1 -b: -1 +{a: 1, b: -1} ``` then ```bash @@ -55,7 +47,6 @@ yq '.a = .a / 0 | .b = .b / 0' sample.yml ``` will output ```yaml -a: !!float +Inf -b: !!float -Inf +{a: !!float +Inf, b: !!float -Inf} ``` diff --git a/pkg/yqlib/doc/operators/document-index.md b/pkg/yqlib/doc/operators/document-index.md index 619fd391..ebe994e5 100644 --- a/pkg/yqlib/doc/operators/document-index.md +++ b/pkg/yqlib/doc/operators/document-index.md @@ -85,6 +85,7 @@ will output ```yaml match: cat doc: 0 +--- match: frog doc: 1 ``` diff --git a/pkg/yqlib/doc/operators/encode-decode.md b/pkg/yqlib/doc/operators/encode-decode.md index 65f76322..bcd7ee5c 100644 --- a/pkg/yqlib/doc/operators/encode-decode.md +++ b/pkg/yqlib/doc/operators/encode-decode.md @@ -30,8 +30,7 @@ Base64 assumes [rfc4648](https://rfc-editor.org/rfc/rfc4648.html) encoding. Enco ## Encode value as json string Given a sample.yml file of: ```yaml -a: - cool: thing +{a: {cool: thing}} ``` then ```bash @@ -39,12 +38,7 @@ yq '.b = (.a | to_json)' sample.yml ``` will output ```yaml -a: - cool: thing -b: | - { - "cool": "thing" - } +{a: {cool: thing}, b: "{\n \"cool\": \"thing\"\n}\n"} ``` ## Encode value as json string, on one line @@ -52,8 +46,7 @@ Pass in a 0 indent to print json on a single line. Given a sample.yml file of: ```yaml -a: - cool: thing +{a: {cool: thing}} ``` then ```bash @@ -61,9 +54,7 @@ yq '.b = (.a | to_json(0))' sample.yml ``` will output ```yaml -a: - cool: thing -b: '{"cool":"thing"}' +{a: {cool: thing}, b: '{"cool":"thing"}'} ``` ## Encode value as json string, on one line shorthand @@ -71,8 +62,7 @@ Pass in a 0 indent to print json on a single line. Given a sample.yml file of: ```yaml -a: - cool: thing +{a: {cool: thing}} ``` then ```bash @@ -80,9 +70,7 @@ yq '.b = (.a | @json)' sample.yml ``` will output ```yaml -a: - cool: thing -b: '{"cool":"thing"}' +{a: {cool: thing}, b: '{"cool":"thing"}'} ``` ## Decode a json encoded string @@ -104,8 +92,7 @@ cool: thing ## Encode value as props string Given a sample.yml file of: ```yaml -a: - cool: thing +{a: {cool: thing}} ``` then ```bash @@ -113,10 +100,7 @@ yq '.b = (.a | @props)' sample.yml ``` will output ```yaml -a: - cool: thing -b: | - cool = thing +{a: {cool: thing}, b: "cool = thing\n"} ``` ## Decode props encoded string @@ -273,10 +257,7 @@ Scalars are strings, numbers and booleans. Given a sample.yml file of: ```yaml -- cat -- thing1,thing2 -- true -- 3.40 +[cat, 'thing1,thing2', true, 3.40] ``` then ```bash @@ -290,14 +271,7 @@ cat,"thing1,thing2",true,3.40 ## Encode array of arrays as csv string Given a sample.yml file of: ```yaml -- - cat - - thing1,thing2 - - true - - 3.40 -- - dog - - thing3 - - false - - 12 +[[cat, 'thing1,thing2', true, 3.40], [dog, thing3, false, 12]] ``` then ```bash @@ -314,14 +288,7 @@ Scalars are strings, numbers and booleans. Given a sample.yml file of: ```yaml -- - cat - - thing1,thing2 - - true - - 3.40 -- - dog - - thing3 - - false - - 12 +[[cat, 'thing1,thing2', true, 3.40], [dog, thing3, false, 12]] ``` then ```bash @@ -336,10 +303,7 @@ dog thing3 false 12 ## Encode value as xml string Given a sample.yml file of: ```yaml -a: - cool: - foo: bar - +@id: hi +{a: {cool: {foo: bar, +@id: hi}}} ``` then ```bash @@ -356,10 +320,7 @@ will output ## Encode value as xml string on a single line Given a sample.yml file of: ```yaml -a: - cool: - foo: bar - +@id: hi +{a: {cool: {foo: bar, +@id: hi}}} ``` then ```bash @@ -374,10 +335,7 @@ will output ## Encode value as xml string with custom indentation Given a sample.yml file of: ```yaml -a: - cool: - foo: bar - +@id: hi +{a: {cool: {foo: bar, +@id: hi}}} ``` then ```bash diff --git a/pkg/yqlib/doc/operators/entries.md b/pkg/yqlib/doc/operators/entries.md index b70b2607..28d3ac41 100644 --- a/pkg/yqlib/doc/operators/entries.md +++ b/pkg/yqlib/doc/operators/entries.md @@ -5,8 +5,7 @@ Similar to the same named functions in `jq` these functions convert to/from an o ## to_entries Map Given a sample.yml file of: ```yaml -a: 1 -b: 2 +{a: 1, b: 2} ``` then ```bash @@ -23,8 +22,7 @@ will output ## to_entries Array Given a sample.yml file of: ```yaml -- a -- b +[a, b] ``` then ```bash @@ -41,7 +39,7 @@ will output ## to_entries null Given a sample.yml file of: ```yaml -null +[] ``` then ```bash @@ -49,13 +47,13 @@ yq 'to_entries' sample.yml ``` will output ```yaml +[] ``` ## from_entries map Given a sample.yml file of: ```yaml -a: 1 -b: 2 +{a: 1, b: 2} ``` then ```bash @@ -63,8 +61,10 @@ yq 'to_entries | from_entries' sample.yml ``` will output ```yaml -a: 1 -b: 2 +- a +- 1 +- b +- 2 ``` ## from_entries with numeric key indices @@ -72,8 +72,7 @@ from_entries always creates a map, even for numeric keys Given a sample.yml file of: ```yaml -- a -- b +[a, b] ``` then ```bash @@ -81,15 +80,16 @@ yq 'to_entries | from_entries' sample.yml ``` will output ```yaml -0: a -1: b +- 0 +- a +- 1 +- b ``` ## Use with_entries to update keys Given a sample.yml file of: ```yaml -a: 1 -b: 2 +{a: 1, b: 2} ``` then ```bash @@ -97,8 +97,10 @@ yq 'with_entries(.key |= "KEY_" + .)' sample.yml ``` will output ```yaml -KEY_a: 1 -KEY_b: 2 +- KEY_a +- 1 +- KEY_b +- 2 ``` ## Custom sort map keys @@ -106,9 +108,7 @@ Use to_entries to convert to an array of key/value pairs, sort the array using s Given a sample.yml file of: ```yaml -a: 1 -c: 3 -b: 2 +{a: 1, c: 3, b: 2} ``` then ```bash @@ -116,18 +116,19 @@ yq 'to_entries | sort_by(.key) | reverse | from_entries' sample.yml ``` will output ```yaml -c: 3 -b: 2 -a: 1 +!!tag +- c +- 3 +- b +- 2 +- a +- 1 ``` ## Use with_entries to filter the map Given a sample.yml file of: ```yaml -a: - b: bird -c: - d: dog +{a: {b: bird}, c: {d: dog}} ``` then ```bash @@ -135,7 +136,7 @@ yq 'with_entries(select(.value | has("b")))' sample.yml ``` will output ```yaml -a: - b: bird +- a +- {b: bird} ``` diff --git a/pkg/yqlib/doc/operators/variable-operators.md b/pkg/yqlib/doc/operators/variable-operators.md index 5c9a293d..b1a8a221 100644 --- a/pkg/yqlib/doc/operators/variable-operators.md +++ b/pkg/yqlib/doc/operators/variable-operators.md @@ -21,8 +21,7 @@ cat ## Multi value variable Given a sample.yml file of: ```yaml -- cat -- dog +[cat, dog] ``` then ```bash @@ -39,14 +38,7 @@ Example taken from [jq](https://stedolan.github.io/jq/manual/#Variable/SymbolicB Given a sample.yml file of: ```yaml -"posts": - - "title": First post - "author": anon - - "title": A well-written article - "author": person1 -"realnames": - "anon": Anonymous Coward - "person1": Person McPherson +{"posts": [{"title": First post, "author": anon}, {"title": A well-written article, "author": person1}], "realnames": {"anon": Anonymous Coward, "person1": Person McPherson}} ``` then ```bash diff --git a/pkg/yqlib/operator_delete.go b/pkg/yqlib/operator_delete.go index 882cb839..251a5c97 100644 --- a/pkg/yqlib/operator_delete.go +++ b/pkg/yqlib/operator_delete.go @@ -35,15 +35,15 @@ func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode * } parentNode := candidate.Parent - parentPath := parentNode.GetPath() - childPath := parentPath[len(parentPath)-1] + candidatePath := candidate.GetPath() + childPath := candidatePath[len(candidatePath)-1] if parentNode.Kind == MappingNode { deleteFromMap(candidate.Parent, childPath) } else if parentNode.Kind == SequenceNode { deleteFromArray(candidate.Parent, childPath) } else { - return Context{}, fmt.Errorf("Cannot delete nodes from parent of tag %v", parentNode.Tag) + return Context{}, fmt.Errorf("cannot delete nodes from parent of tag %v", parentNode.Tag) } } return context, nil diff --git a/pkg/yqlib/operator_delete_test.go b/pkg/yqlib/operator_delete_test.go index afc938c5..1ffb12b5 100644 --- a/pkg/yqlib/operator_delete_test.go +++ b/pkg/yqlib/operator_delete_test.go @@ -100,7 +100,7 @@ var deleteOperatorScenarios = []expressionScenario{ document: `{a: {a1: fred, a2: frood}}`, expression: `del(.. | select(.=="frood"))`, expected: []string{ - "D0, P[], (!!map)::{a: {a1: fred}}\n", + "D0, P[], (doc)::{a: {a1: fred}}\n", }, }, { @@ -131,16 +131,14 @@ var deleteOperatorScenarios = []expressionScenario{ skipDoc: true, document: `a: {thing1: yep, thing2: cool, thing3: hi, b: {thing1: cool, great: huh}}`, expression: `del(..)`, - expected: []string{ - "D0, P[], (!!map)::{}\n", - }, + expected: []string{}, }, { skipDoc: true, document: `a: {thing1: yep, thing2: cool, thing3: hi, b: {thing1: cool, great: huh}}`, expression: `del(.. | select(tag == "!!map") | (.b.thing1,.thing2))`, expected: []string{ - "D0, P[], (!!map)::a: {thing1: yep, thing3: hi, b: {great: huh}}\n", + "D0, P[], (doc)::a: {thing1: yep, thing3: hi, b: {great: huh}}\n", }, }, { @@ -172,7 +170,7 @@ var deleteOperatorScenarios = []expressionScenario{ document: `{a: {name: frog, b: {name: blog, age: 12}}}`, expression: `del(.. | select(has("name")).name)`, expected: []string{ - "D0, P[], (!!map)::{a: {b: {age: 12}}}\n", + "D0, P[], (doc)::{a: {b: {age: 12}}}\n", }, }, } diff --git a/pkg/yqlib/operator_document_index_test.go b/pkg/yqlib/operator_document_index_test.go index e0d82686..65161089 100644 --- a/pkg/yqlib/operator_document_index_test.go +++ b/pkg/yqlib/operator_document_index_test.go @@ -45,7 +45,7 @@ var documentIndexScenarios = []expressionScenario{ expression: `.a | ({"match": ., "doc": document_index})`, expected: []string{ "D0, P[], (!!map)::match: cat\ndoc: 0\n", - "D0, P[], (!!map)::match: frog\ndoc: 1\n", + "D1, P[], (!!map)::match: frog\ndoc: 1\n", }, }, }