From 90261a2fdd270b9a7b2dbab9189de877b546fde4 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 27 Apr 2022 09:11:43 +1000 Subject: [PATCH] Added type as an alias for tag #1195 --- pkg/yqlib/doc/operators/tag.md | 23 +++++++++++++++++++++++ pkg/yqlib/expression_tokeniser.go | 3 ++- pkg/yqlib/operator_tag_test.go | 22 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkg/yqlib/doc/operators/tag.md b/pkg/yqlib/doc/operators/tag.md index 31bc4643f2..33e11f65a5 100644 --- a/pkg/yqlib/doc/operators/tag.md +++ b/pkg/yqlib/doc/operators/tag.md @@ -31,6 +31,29 @@ will output !!seq ``` +## type is an alias for tag +Given a sample.yml file of: +```yaml +a: cat +b: 5 +c: 3.2 +e: true +f: [] +``` +then +```bash +yq '.. | type' sample.yml +``` +will output +```yaml +!!map +!!str +!!int +!!float +!!bool +!!seq +``` + ## Set custom tag Given a sample.yml file of: ```yaml diff --git a/pkg/yqlib/expression_tokeniser.go b/pkg/yqlib/expression_tokeniser.go index b9085935ff..9b9fb5c8be 100644 --- a/pkg/yqlib/expression_tokeniser.go +++ b/pkg/yqlib/expression_tokeniser.go @@ -478,7 +478,8 @@ func initLexer() (*lex.Lexer, error) { lexer.Add([]byte(`style`), opAssignableToken(getStyleOpType, assignStyleOpType)) - lexer.Add([]byte(`tag`), opAssignableToken(getTagOpType, assignTagOpType)) + lexer.Add([]byte(`tag|type`), opAssignableToken(getTagOpType, assignTagOpType)) + lexer.Add([]byte(`anchor`), opAssignableToken(getAnchorOpType, assignAnchorOpType)) lexer.Add([]byte(`alias`), opAssignableToken(getAliasOptype, assignAliasOpType)) lexer.Add([]byte(`filename`), opToken(getFilenameOpType)) diff --git a/pkg/yqlib/operator_tag_test.go b/pkg/yqlib/operator_tag_test.go index 380f60286d..0466e5f393 100644 --- a/pkg/yqlib/operator_tag_test.go +++ b/pkg/yqlib/operator_tag_test.go @@ -18,6 +18,19 @@ var tagOperatorScenarios = []expressionScenario{ "D0, P[f], (!!str)::!!seq\n", }, }, + { + description: "type is an alias for tag", + document: `{a: cat, b: 5, c: 3.2, e: true, f: []}`, + expression: `.. | type`, + expected: []string{ + "D0, P[], (!!str)::!!map\n", + "D0, P[a], (!!str)::!!str\n", + "D0, P[b], (!!str)::!!int\n", + "D0, P[c], (!!str)::!!float\n", + "D0, P[e], (!!str)::!!bool\n", + "D0, P[f], (!!str)::!!seq\n", + }, + }, { skipDoc: true, document: `{a: cat, b: 5, c: 3.2, e: true, f: []}`, @@ -42,6 +55,15 @@ var tagOperatorScenarios = []expressionScenario{ "D0, P[], (doc)::{a: !!mikefarah str}\n", }, }, + { + skipDoc: true, + description: "Set custom type", + document: `{a: str}`, + expression: `.a type = "!!mikefarah"`, + expected: []string{ + "D0, P[], (doc)::{a: !!mikefarah str}\n", + }, + }, { description: "Find numbers and convert them to strings", document: `{a: cat, b: 5, c: 3.2, e: true}`,