Added load_base64

This commit is contained in:
Mike Farah 2022-02-23 09:36:03 +11:00
parent d9bca65626
commit 61978b34df
5 changed files with 58 additions and 6 deletions

1
examples/base64.txt Normal file
View File

@ -0,0 +1 @@
bXkgc2VjcmV0IGNoaWxsaSByZWNpcGUgaXMuLi4u

View File

@ -13,20 +13,33 @@ You can load files of the following supported types:
| Properties | load_props |
| Plain String | load_str |
Lets say there is a file `../../examples/thing.yml`:
## Samples files for tests:
### yaml
`../../examples/thing.yml`:
```yaml
a: apple is included
b: cool
```
and a file `small.xml`:
### xml
`small.xml`:
```xml
<this>is some xml</this>
```
and `small.properties`:
### properties
`small.properties`:
```properties
this.is = a properties file
```
### base64
`base64.txt`:
```
bXkgc2VjcmV0IGNoaWxsaSByZWNpcGUgaXMuLi4u
```

View File

@ -13,24 +13,37 @@ You can load files of the following supported types:
| Properties | load_props |
| Plain String | load_str |
Lets say there is a file `../../examples/thing.yml`:
## Samples files for tests:
### yaml
`../../examples/thing.yml`:
```yaml
a: apple is included
b: cool
```
and a file `small.xml`:
### xml
`small.xml`:
```xml
<this>is some xml</this>
```
and `small.properties`:
### properties
`small.properties`:
```properties
this.is = a properties file
```
### base64
`base64.txt`:
```
bXkgc2VjcmV0IGNoaWxsaSByZWNpcGUgaXMuLi4u
```
{% hint style="warning" %}
Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#x20;
@ -169,3 +182,18 @@ this:
cool: ay
```
## Load from base64 encoded file
Given a sample.yml file of:
```yaml
cool: things
```
then
```bash
yq '.more_stuff = load_base64("../../examples/base64.txt")' sample.yml
```
will output
```yaml
cool: things
more_stuff: my secret chilli recipe is....
```

View File

@ -383,6 +383,8 @@ func initLexer() (*lex.Lexer, error) {
lexer.Add([]byte(`load_xml`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName)}))
lexer.Add([]byte(`loadxml`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName)}))
lexer.Add([]byte(`load_base64`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewBase64Decoder()}))
lexer.Add([]byte(`load_props`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewPropertiesDecoder()}))
lexer.Add([]byte(`loadprops`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewPropertiesDecoder()}))

View File

@ -65,6 +65,14 @@ var loadScenarios = []expressionScenario{
"D0, P[], (!!map)::this:\n is: a properties file\n cool: ay\n",
},
},
{
description: "Load from base64 encoded file",
document: "cool: things",
expression: `.more_stuff = load_base64("../../examples/base64.txt")`,
expected: []string{
"D0, P[], (doc)::cool: things\nmore_stuff: my secret chilli recipe is....\n",
},
},
}
func TestLoadScenarios(t *testing.T) {