From 186b319093cf6bd67122aa9fd3f1008d2ebbe08c Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 28 Mar 2022 19:48:30 +1100 Subject: [PATCH] Added from_props --- pkg/yqlib/doc/operators/encode-decode.md | 20 ++++++++++++++++++- .../doc/operators/headers/encode-decode.md | 2 +- pkg/yqlib/expression_tokeniser.go | 1 + pkg/yqlib/operator_encoder_decoder.go | 2 ++ pkg/yqlib/operator_encoder_decoder_test.go | 8 ++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/pkg/yqlib/doc/operators/encode-decode.md b/pkg/yqlib/doc/operators/encode-decode.md index cf73bcd3..ba902955 100644 --- a/pkg/yqlib/doc/operators/encode-decode.md +++ b/pkg/yqlib/doc/operators/encode-decode.md @@ -11,7 +11,7 @@ These operators are useful to process yaml documents that have stringified embed | --- | -- | --| | Yaml | from_yaml | to_yaml(i)/@yaml | | JSON | from_json | to_json(i)/@json | -| Properties | | to_props/@props | +| Properties | from_props | to_props/@props | | CSV | | to_csv/@csv | | TSV | | to_tsv/@tsv | | XML | from_xml | to_xml(i)/@xml | @@ -123,6 +123,24 @@ b: | 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 Indent defaults to 2 diff --git a/pkg/yqlib/doc/operators/headers/encode-decode.md b/pkg/yqlib/doc/operators/headers/encode-decode.md index fc77bd68..1099ad7b 100644 --- a/pkg/yqlib/doc/operators/headers/encode-decode.md +++ b/pkg/yqlib/doc/operators/headers/encode-decode.md @@ -11,7 +11,7 @@ These operators are useful to process yaml documents that have stringified embed | --- | -- | --| | Yaml | from_yaml | to_yaml(i)/@yaml | | JSON | from_json | to_json(i)/@json | -| Properties | | to_props/@props | +| Properties | from_props | to_props/@props | | CSV | | to_csv/@csv | | TSV | | to_tsv/@tsv | | XML | from_xml | to_xml(i)/@xml | diff --git a/pkg/yqlib/expression_tokeniser.go b/pkg/yqlib/expression_tokeniser.go index 148cda8a..b9085935 100644 --- a/pkg/yqlib/expression_tokeniser.go +++ b/pkg/yqlib/expression_tokeniser.go @@ -407,6 +407,7 @@ func initLexer() (*lex.Lexer, error) { 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_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(`sort_keys`), opToken(sortKeysOpType)) diff --git a/pkg/yqlib/operator_encoder_decoder.go b/pkg/yqlib/operator_encoder_decoder.go index b2fcb43d..0a74276d 100644 --- a/pkg/yqlib/operator_encoder_decoder.go +++ b/pkg/yqlib/operator_encoder_decoder.go @@ -107,6 +107,8 @@ func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre decoder = NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName, XMLPreferences.StrictMode) case Base64InputFormat: decoder = NewBase64Decoder() + case PropertiesInputFormat: + decoder = NewPropertiesDecoder() } var results = list.New() diff --git a/pkg/yqlib/operator_encoder_decoder_test.go b/pkg/yqlib/operator_encoder_decoder_test.go index 4a5990e5..b8fc9c31 100644 --- a/pkg/yqlib/operator_encoder_decoder_test.go +++ b/pkg/yqlib/operator_encoder_decoder_test.go @@ -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, document: "a:\n cool:\n bob: dylan",