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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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