Added type as an alias for tag #1195

This commit is contained in:
Mike Farah 2022-04-27 09:11:43 +10:00
parent 3ad5355777
commit 90261a2fdd
3 changed files with 47 additions and 1 deletions

View File

@ -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

View File

@ -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))

View File

@ -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}`,