Skip to content

Commit

Permalink
Added load_base64
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Feb 22, 2022
1 parent d9bca65 commit 61978b3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/base64.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bXkgc2VjcmV0IGNoaWxsaSByZWNpcGUgaXMuLi4u
19 changes: 16 additions & 3 deletions pkg/yqlib/doc/operators/headers/load.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
34 changes: 31 additions & 3 deletions pkg/yqlib/doc/operators/load.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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....
```

2 changes: 2 additions & 0 deletions pkg/yqlib/expression_tokeniser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}))

Expand Down
8 changes: 8 additions & 0 deletions pkg/yqlib/operator_load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 61978b3

Please sign in to comment.