Added from_props

This commit is contained in:
Mike Farah 2022-03-28 19:48:30 +11:00
parent bc2118736b
commit 186b319093
5 changed files with 31 additions and 2 deletions

View File

@ -11,7 +11,7 @@ These operators are useful to process yaml documents that have stringified embed
| --- | -- | --| | --- | -- | --|
| Yaml | from_yaml | to_yaml(i)/@yaml | | Yaml | from_yaml | to_yaml(i)/@yaml |
| JSON | from_json | to_json(i)/@json | | JSON | from_json | to_json(i)/@json |
| Properties | | to_props/@props | | Properties | from_props | to_props/@props |
| CSV | | to_csv/@csv | | CSV | | to_csv/@csv |
| TSV | | to_tsv/@tsv | | TSV | | to_tsv/@tsv |
| XML | from_xml | to_xml(i)/@xml | | XML | from_xml | to_xml(i)/@xml |
@ -123,6 +123,24 @@ b: |
cool = thing cool = thing
``` ```
## Decode props encoded string
Given a sample.yml file of:
```yaml
a: |-
cats=great
dogs=cool as well
```
then
```bash
yq '.a |= from_props' sample.yml
```
will output
```yaml
a:
cats: great
dogs: cool as well
```
## Encode value as yaml string ## Encode value as yaml string
Indent defaults to 2 Indent defaults to 2

View File

@ -11,7 +11,7 @@ These operators are useful to process yaml documents that have stringified embed
| --- | -- | --| | --- | -- | --|
| Yaml | from_yaml | to_yaml(i)/@yaml | | Yaml | from_yaml | to_yaml(i)/@yaml |
| JSON | from_json | to_json(i)/@json | | JSON | from_json | to_json(i)/@json |
| Properties | | to_props/@props | | Properties | from_props | to_props/@props |
| CSV | | to_csv/@csv | | CSV | | to_csv/@csv |
| TSV | | to_tsv/@tsv | | TSV | | to_tsv/@tsv |
| XML | from_xml | to_xml(i)/@xml | | XML | from_xml | to_xml(i)/@xml |

View File

@ -407,6 +407,7 @@ func initLexer() (*lex.Lexer, error) {
lexer.Add([]byte(`from_yaml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat})) lexer.Add([]byte(`from_yaml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat}))
lexer.Add([]byte(`from_json`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat})) lexer.Add([]byte(`from_json`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat}))
lexer.Add([]byte(`from_xml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: XMLInputFormat})) lexer.Add([]byte(`from_xml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: XMLInputFormat}))
lexer.Add([]byte(`from_props`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: PropertiesInputFormat}))
lexer.Add([]byte(`sortKeys`), opToken(sortKeysOpType)) lexer.Add([]byte(`sortKeys`), opToken(sortKeysOpType))
lexer.Add([]byte(`sort_keys`), opToken(sortKeysOpType)) lexer.Add([]byte(`sort_keys`), opToken(sortKeysOpType))

View File

@ -107,6 +107,8 @@ func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
decoder = NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName, XMLPreferences.StrictMode) decoder = NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName, XMLPreferences.StrictMode)
case Base64InputFormat: case Base64InputFormat:
decoder = NewBase64Decoder() decoder = NewBase64Decoder()
case PropertiesInputFormat:
decoder = NewPropertiesDecoder()
} }
var results = list.New() var results = list.New()

View File

@ -63,6 +63,14 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
`, `,
}, },
}, },
{
description: "Decode props encoded string",
document: `a: "cats=great\ndogs=cool as well"`,
expression: `.a |= from_props`,
expected: []string{
"D0, P[], (doc)::a:\n cats: great\n dogs: cool as well\n",
},
},
{ {
skipDoc: true, skipDoc: true,
document: "a:\n cool:\n bob: dylan", document: "a:\n cool:\n bob: dylan",