Skip to content

Commit

Permalink
Added type as an alias for tag #1195
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Apr 26, 2022
1 parent 3ad5355 commit 90261a2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
23 changes: 23 additions & 0 deletions pkg/yqlib/doc/operators/tag.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/yqlib/expression_tokeniser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
22 changes: 22 additions & 0 deletions pkg/yqlib/operator_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: []}`,
Expand All @@ -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}`,
Expand Down

0 comments on commit 90261a2

Please sign in to comment.