Added snake_case version of camelCase operators (snake case now prefered)

This commit is contained in:
Mike Farah 2022-02-11 09:05:17 +11:00
parent a9c3617b4f
commit a5f6a80cf6
9 changed files with 48 additions and 38 deletions

View File

@ -23,7 +23,7 @@ a: cat
```
then
```bash
yq '.a lineComment="single"' sample.yml
yq '.a line_comment="single"' sample.yml
```
will output
```yaml
@ -38,7 +38,7 @@ b: dog
```
then
```bash
yq '.. lineComment |= .' sample.yml
yq '.. line_comment |= .' sample.yml
```
will output
```yaml
@ -53,7 +53,7 @@ a: cat
```
then
```bash
yq '. headComment="single"' sample.yml
yq '. head_comment="single"' sample.yml
```
will output
```yaml
@ -69,7 +69,7 @@ a: cat
```
then
```bash
yq '. footComment=.a' sample.yml
yq '. foot_comment=.a' sample.yml
```
will output
```yaml
@ -86,7 +86,7 @@ b: dog # leave this
```
then
```bash
yq '.a lineComment=""' sample.yml
yq '.a line_comment=""' sample.yml
```
will output
```yaml
@ -120,7 +120,7 @@ a: cat # meow
```
then
```bash
yq '.a | lineComment' sample.yml
yq '.a | line_comment' sample.yml
```
will output
```yaml
@ -138,7 +138,7 @@ a: cat # meow
```
then
```bash
yq '. | headComment' sample.yml
yq '. | head_comment' sample.yml
```
will output
```yaml
@ -157,7 +157,7 @@ a: cat # meow
```
then
```bash
yq 'headComment' sample.yml
yq 'head_comment' sample.yml
```
will output
```yaml
@ -177,7 +177,7 @@ a: cat # meow
```
then
```bash
yq '. | footComment' sample.yml
yq '. | foot_comment' sample.yml
```
will output
```yaml

View File

@ -17,7 +17,7 @@ a: frog
```
then
```bash
yq '.a | documentIndex' sample.yml
yq '.a | document_index' sample.yml
```
will output
```yaml
@ -53,7 +53,7 @@ a: frog
```
then
```bash
yq 'select(documentIndex == 1)' sample.yml
yq 'select(document_index == 1)' sample.yml
```
will output
```yaml
@ -85,7 +85,7 @@ a: frog
```
then
```bash
yq '.a | ({"match": ., "doc": documentIndex})' sample.yml
yq '.a | ({"match": ., "doc": document_index})' sample.yml
```
will output
```yaml

View File

@ -37,7 +37,7 @@ a: cat
```
then
```bash
yq 'fileIndex' sample.yml
yq 'file_index' sample.yml
```
will output
```yaml
@ -55,7 +55,7 @@ a: cat
```
then
```bash
yq eval-all 'fileIndex' sample.yml another.yml
yq eval-all 'file_index' sample.yml another.yml
```
will output
```yaml

View File

@ -11,7 +11,7 @@ Note that versions prior to 4.18 require the 'eval/e' command to be specified.&#
## Split empty
Running
```bash
yq --null-input 'splitDoc'
yq --null-input 'split_doc'
```
will output
```yaml
@ -26,7 +26,7 @@ Given a sample.yml file of:
```
then
```bash
yq '.[] | splitDoc' sample.yml
yq '.[] | split_doc' sample.yml
```
will output
```yaml

View File

@ -393,8 +393,12 @@ func initLexer() (*lex.Lexer, error) {
lexer.Add([]byte(`\/\/`), opToken(alternativeOpType))
lexer.Add([]byte(`documentIndex`), opToken(getDocumentIndexOpType))
lexer.Add([]byte(`document_index`), opToken(getDocumentIndexOpType))
lexer.Add([]byte(`di`), opToken(getDocumentIndexOpType))
lexer.Add([]byte(`splitDoc`), opToken(splitDocumentOpType))
lexer.Add([]byte(`split_doc`), opToken(splitDocumentOpType))
lexer.Add([]byte(`join`), opToken(joinStringOpType))
lexer.Add([]byte(`sub`), opToken(subStringOpType))
@ -423,7 +427,10 @@ func initLexer() (*lex.Lexer, error) {
lexer.Add([]byte(`anchor`), opAssignableToken(getAnchorOpType, assignAnchorOpType))
lexer.Add([]byte(`alias`), opAssignableToken(getAliasOptype, assignAliasOpType))
lexer.Add([]byte(`filename`), opToken(getFilenameOpType))
lexer.Add([]byte(`fileIndex`), opToken(getFileIndexOpType))
lexer.Add([]byte(`file_index`), opToken(getFileIndexOpType))
lexer.Add([]byte(`fi`), opToken(getFileIndexOpType))
lexer.Add([]byte(`path`), opToken(getPathOpType))
lexer.Add([]byte(`to_entries`), opToken(toEntriesOpType))
@ -433,10 +440,13 @@ func initLexer() (*lex.Lexer, error) {
lexer.Add([]byte(`with`), opToken(withOpType))
lexer.Add([]byte(`lineComment`), opTokenWithPrefs(getCommentOpType, assignCommentOpType, commentOpPreferences{LineComment: true}))
lexer.Add([]byte(`line_comment`), opTokenWithPrefs(getCommentOpType, assignCommentOpType, commentOpPreferences{LineComment: true}))
lexer.Add([]byte(`headComment`), opTokenWithPrefs(getCommentOpType, assignCommentOpType, commentOpPreferences{HeadComment: true}))
lexer.Add([]byte(`head_comment`), opTokenWithPrefs(getCommentOpType, assignCommentOpType, commentOpPreferences{HeadComment: true}))
lexer.Add([]byte(`footComment`), opTokenWithPrefs(getCommentOpType, assignCommentOpType, commentOpPreferences{FootComment: true}))
lexer.Add([]byte(`foot_comment`), opTokenWithPrefs(getCommentOpType, assignCommentOpType, commentOpPreferences{FootComment: true}))
lexer.Add([]byte(`comments\s*=`), assignAllCommentsOp(false))
lexer.Add([]byte(`comments\s*\|=`), assignAllCommentsOp(true))
@ -519,7 +529,7 @@ func (p *expressionTokeniserImpl) Tokenise(expression string) ([]*token, error)
scanner, err := p.lexer.Scanner([]byte(expression))
if err != nil {
return nil, fmt.Errorf("Parsing expression: %w", err)
return nil, fmt.Errorf("parsing expression: %w", err)
}
var tokens []*token
for tok, err, eof := scanner.Next(); !eof; tok, err, eof = scanner.Next() {
@ -530,7 +540,7 @@ func (p *expressionTokeniserImpl) Tokenise(expression string) ([]*token, error)
tokens = append(tokens, currentToken)
}
if err != nil {
return nil, fmt.Errorf("Parsing expression: %w", err)
return nil, fmt.Errorf("parsing expression: %w", err)
}
}
var postProcessedTokens = make([]*token, 0)

View File

@ -8,7 +8,7 @@ var commentOperatorScenarios = []expressionScenario{
{
description: "Set line comment",
document: `a: cat`,
expression: `.a lineComment="single"`,
expression: `.a line_comment="single"`,
expected: []string{
"D0, P[], (doc)::a: cat # single\n",
},
@ -16,7 +16,7 @@ var commentOperatorScenarios = []expressionScenario{
{
skipDoc: true,
document: "a: cat\nb: dog",
expression: `.a lineComment=.b`,
expression: `.a line_comment=.b`,
expected: []string{
"D0, P[], (doc)::a: cat # dog\nb: dog\n",
},
@ -24,7 +24,7 @@ var commentOperatorScenarios = []expressionScenario{
{
skipDoc: true,
document: "a: cat\n---\na: dog",
expression: `.a lineComment |= documentIndex`,
expression: `.a line_comment |= documentIndex`,
expected: []string{
"D0, P[], (doc)::a: cat # 0\n",
"D1, P[], (doc)::a: dog # 1\n",
@ -33,7 +33,7 @@ var commentOperatorScenarios = []expressionScenario{
{
description: "Use update assign to perform relative updates",
document: "a: cat\nb: dog",
expression: `.. lineComment |= .`,
expression: `.. line_comment |= .`,
expected: []string{
"D0, P[], (!!map)::a: cat # cat\nb: dog # dog\n",
},
@ -49,7 +49,7 @@ var commentOperatorScenarios = []expressionScenario{
{
description: "Set head comment",
document: `a: cat`,
expression: `. headComment="single"`,
expression: `. head_comment="single"`,
expected: []string{
"D0, P[], (doc)::# single\n\na: cat\n",
},
@ -57,7 +57,7 @@ var commentOperatorScenarios = []expressionScenario{
{
description: "Set foot comment, using an expression",
document: `a: cat`,
expression: `. footComment=.a`,
expression: `. foot_comment=.a`,
expected: []string{
"D0, P[], (doc)::a: cat\n\n# cat\n",
},
@ -65,7 +65,7 @@ var commentOperatorScenarios = []expressionScenario{
{
skipDoc: true,
document: `a: cat`,
expression: `. footComment=.b.d`,
expression: `. foot_comment=.b.d`,
expected: []string{
"D0, P[], (doc)::a: cat\n",
},
@ -73,7 +73,7 @@ var commentOperatorScenarios = []expressionScenario{
{
skipDoc: true,
document: `a: cat`,
expression: `. footComment|=.b.d`,
expression: `. foot_comment|=.b.d`,
expected: []string{
"D0, P[], (doc)::a: cat\n",
},
@ -81,7 +81,7 @@ var commentOperatorScenarios = []expressionScenario{
{
description: "Remove comment",
document: "a: cat # comment\nb: dog # leave this",
expression: `.a lineComment=""`,
expression: `.a line_comment=""`,
expected: []string{
"D0, P[], (doc)::a: cat\nb: dog # leave this\n",
},
@ -98,7 +98,7 @@ var commentOperatorScenarios = []expressionScenario{
{
description: "Get line comment",
document: "# welcome!\n\na: cat # meow\n\n# have a great day",
expression: `.a | lineComment`,
expression: `.a | line_comment`,
expected: []string{
"D0, P[a], (!!str)::meow\n",
},
@ -107,7 +107,7 @@ var commentOperatorScenarios = []expressionScenario{
description: "Get head comment",
dontFormatInputForDoc: true,
document: "# welcome!\n\na: cat # meow\n\n# have a great day",
expression: `. | headComment`,
expression: `. | head_comment`,
expected: []string{
"D0, P[], (!!str)::welcome!\n",
},
@ -116,7 +116,7 @@ var commentOperatorScenarios = []expressionScenario{
description: "Head comment with document split",
dontFormatInputForDoc: true,
document: "# welcome!\n---\n# bob\na: cat # meow\n\n# have a great day",
expression: `headComment`,
expression: `head_comment`,
expected: []string{
"D0, P[], (!!str)::welcome!\nbob\n",
},
@ -125,7 +125,7 @@ var commentOperatorScenarios = []expressionScenario{
description: "Get foot comment",
dontFormatInputForDoc: true,
document: "# welcome!\n\na: cat # meow\n\n# have a great day\n# no really",
expression: `. | footComment`,
expression: `. | foot_comment`,
expected: []string{
"D0, P[], (!!str)::have a great day\nno really\n",
},

View File

@ -8,7 +8,7 @@ var documentIndexScenarios = []expressionScenario{
{
description: "Retrieve a document index",
document: "a: cat\n---\na: frog\n",
expression: `.a | documentIndex`,
expression: `.a | document_index`,
expected: []string{
"D0, P[a], (!!int)::0\n",
"D1, P[a], (!!int)::1\n",
@ -26,7 +26,7 @@ var documentIndexScenarios = []expressionScenario{
{
description: "Filter by document index",
document: "a: cat\n---\na: frog\n",
expression: `select(documentIndex == 1)`,
expression: `select(document_index == 1)`,
expected: []string{
"D1, P[], (doc)::a: frog\n",
},
@ -42,7 +42,7 @@ var documentIndexScenarios = []expressionScenario{
{
description: "Print Document Index with matches",
document: "a: cat\n---\na: frog\n",
expression: `.a | ({"match": ., "doc": documentIndex})`,
expression: `.a | ({"match": ., "doc": document_index})`,
expected: []string{
"D0, P[], (!!map)::match: cat\ndoc: 0\n",
"D0, P[], (!!map)::match: frog\ndoc: 1\n",

View File

@ -16,7 +16,7 @@ var fileOperatorScenarios = []expressionScenario{
{
description: "Get file index",
document: `{a: cat}`,
expression: `fileIndex`,
expression: `file_index`,
expected: []string{
"D0, P[], (!!int)::0\n",
},
@ -25,7 +25,7 @@ var fileOperatorScenarios = []expressionScenario{
description: "Get file indices of multiple documents",
document: `{a: cat}`,
document2: `{a: cat}`,
expression: `fileIndex`,
expression: `file_index`,
expected: []string{
"D0, P[], (!!int)::0\n",
"D0, P[], (!!int)::1\n",

View File

@ -8,7 +8,7 @@ var splitDocOperatorScenarios = []expressionScenario{
{
description: "Split empty",
document: ``,
expression: `splitDoc`,
expression: `split_doc`,
expected: []string{
"D0, P[], (!!null)::\n",
},
@ -16,7 +16,7 @@ var splitDocOperatorScenarios = []expressionScenario{
{
description: "Split array",
document: `[{a: cat}, {b: dog}]`,
expression: `.[] | splitDoc`,
expression: `.[] | split_doc`,
expected: []string{
"D0, P[0], (!!map)::{a: cat}\n",
"D1, P[1], (!!map)::{b: dog}\n",