Added github action fix for parsing xml, updated linter

This commit is contained in:
Mike Farah 2022-02-07 11:55:55 +11:00
parent 06e944dcb6
commit 26356ff4be
48 changed files with 238 additions and 339 deletions

View File

@ -2,7 +2,7 @@
setUp() { setUp() {
rm test*.yml || true rm test*.yml || true
rm .xyz -f rm .xyz -f || true
} }
testBasicEvalRoundTrip() { testBasicEvalRoundTrip() {

View File

@ -1,83 +0,0 @@
package cmd
// import (
// "strings"
// "testing"
// "github.com/mikefarah/yq/v3/test"
// "github.com/spf13/cobra"
// )
// func getRootCommand() *cobra.Command {
// return New()
// }
// func TestRootCmd(t *testing.T) {
// cmd := getRootCommand()
// result := test.RunCmd(cmd, "")
// if result.Error != nil {
// t.Error(result.Error)
// }
// if !strings.Contains(result.Output, "Usage:") {
// t.Error("Expected usage message to be printed out, but the usage message was not found.")
// }
// }
// func TestRootCmd_Help(t *testing.T) {
// cmd := getRootCommand()
// result := test.RunCmd(cmd, "--help")
// if result.Error != nil {
// t.Error(result.Error)
// }
// if !strings.Contains(result.Output, "yq is a lightweight and portable command-line YAML processor. It aims to be the jq or sed of yaml files.") {
// t.Error("Expected usage message to be printed out, but the usage message was not found.")
// }
// }
// func TestRootCmd_VerboseLong(t *testing.T) {
// cmd := getRootCommand()
// result := test.RunCmd(cmd, "--verbose")
// if result.Error != nil {
// t.Error(result.Error)
// }
// if !verbose {
// t.Error("Expected verbose to be true")
// }
// }
// func TestRootCmd_VerboseShort(t *testing.T) {
// cmd := getRootCommand()
// result := test.RunCmd(cmd, "-v")
// if result.Error != nil {
// t.Error(result.Error)
// }
// if !verbose {
// t.Error("Expected verbose to be true")
// }
// }
// func TestRootCmd_VersionShort(t *testing.T) {
// cmd := getRootCommand()
// result := test.RunCmd(cmd, "-V")
// if result.Error != nil {
// t.Error(result.Error)
// }
// if !strings.Contains(result.Output, "yq version") {
// t.Error("expected version message to be printed out, but the message was not found.")
// }
// }
// func TestRootCmd_VersionLong(t *testing.T) {
// cmd := getRootCommand()
// result := test.RunCmd(cmd, "--version")
// if result.Error != nil {
// t.Error(result.Error)
// }
// if !strings.Contains(result.Output, "yq version") {
// t.Error("expected version message to be printed out, but the message was not found.")
// }
// }

View File

@ -49,8 +49,8 @@ yq -i '.stuff = "foo"' myfile.yml # update myfile.yml inplace
logging.SetBackend(backend) logging.SetBackend(backend)
yqlib.InitExpressionParser() yqlib.InitExpressionParser()
yqlib.XmlPreferences.AttributePrefix = xmlAttributePrefix yqlib.XMLPreferences.AttributePrefix = xmlAttributePrefix
yqlib.XmlPreferences.ContentName = xmlContentName yqlib.XMLPreferences.ContentName = xmlContentName
}, },
} }

View File

@ -52,8 +52,8 @@ func configureDecoder() (yqlib.Decoder, error) {
return nil, err return nil, err
} }
switch yqlibInputFormat { switch yqlibInputFormat {
case yqlib.XmlInputFormat: case yqlib.XMLInputFormat:
return yqlib.NewXmlDecoder(xmlAttributePrefix, xmlContentName), nil return yqlib.NewXMLDecoder(xmlAttributePrefix, xmlContentName), nil
} }
return yqlib.NewYamlDecoder(), nil return yqlib.NewYamlDecoder(), nil
} }
@ -77,18 +77,18 @@ func configurePrinterWriter(format yqlib.PrinterOutputFormat, out io.Writer) (yq
func configureEncoder(format yqlib.PrinterOutputFormat) yqlib.Encoder { func configureEncoder(format yqlib.PrinterOutputFormat) yqlib.Encoder {
switch format { switch format {
case yqlib.JsonOutputFormat: case yqlib.JSONOutputFormat:
return yqlib.NewJsonEncoder(indent) return yqlib.NewJONEncoder(indent)
case yqlib.PropsOutputFormat: case yqlib.PropsOutputFormat:
return yqlib.NewPropertiesEncoder() return yqlib.NewPropertiesEncoder()
case yqlib.CsvOutputFormat: case yqlib.CSVOutputFormat:
return yqlib.NewCsvEncoder(',') return yqlib.NewCsvEncoder(',')
case yqlib.TsvOutputFormat: case yqlib.TSVOutputFormat:
return yqlib.NewCsvEncoder('\t') return yqlib.NewCsvEncoder('\t')
case yqlib.YamlOutputFormat: case yqlib.YamlOutputFormat:
return yqlib.NewYamlEncoder(indent, colorsEnabled, !noDocSeparators, unwrapScalar) return yqlib.NewYamlEncoder(indent, colorsEnabled, !noDocSeparators, unwrapScalar)
case yqlib.XmlOutputFormat: case yqlib.XMLOutputFormat:
return yqlib.NewXmlEncoder(indent, xmlAttributePrefix, xmlContentName) return yqlib.NewXMLEncoder(indent, xmlAttributePrefix, xmlContentName)
} }
panic("invalid encoder") panic("invalid encoder")
} }

View File

@ -15,7 +15,7 @@ type InputFormat uint
const ( const (
YamlInputFormat = 1 << iota YamlInputFormat = 1 << iota
XmlInputFormat XMLInputFormat
) )
func InputFormatFromString(format string) (InputFormat, error) { func InputFormatFromString(format string) (InputFormat, error) {
@ -23,7 +23,7 @@ func InputFormatFromString(format string) (InputFormat, error) {
case "yaml", "y": case "yaml", "y":
return YamlInputFormat, nil return YamlInputFormat, nil
case "xml", "x": case "xml", "x":
return XmlInputFormat, nil return XMLInputFormat, nil
default: default:
return 0, fmt.Errorf("unknown format '%v' please use [yaml|xml]", format) return 0, fmt.Errorf("unknown format '%v' please use [yaml|xml]", format)
} }
@ -36,7 +36,7 @@ type xmlDecoder struct {
finished bool finished bool
} }
func NewXmlDecoder(attributePrefix string, contentName string) Decoder { func NewXMLDecoder(attributePrefix string, contentName string) Decoder {
if contentName == "" { if contentName == "" {
contentName = "content" contentName = "content"
} }
@ -143,7 +143,7 @@ func (dec *xmlDecoder) Decode(rootYamlNode *yaml.Node) error {
} }
root := &xmlNode{} root := &xmlNode{}
// cant use xj - it doesn't keep map order. // cant use xj - it doesn't keep map order.
err := dec.decodeXml(root) err := dec.decodeXML(root)
if err != nil { if err != nil {
return err return err
@ -205,7 +205,7 @@ type element struct {
// this code is heavily based on https://github.com/basgys/goxml2json // this code is heavily based on https://github.com/basgys/goxml2json
// main changes are to decode into a structure that preserves the original order // main changes are to decode into a structure that preserves the original order
// of the map keys. // of the map keys.
func (dec *xmlDecoder) decodeXml(root *xmlNode) error { func (dec *xmlDecoder) decodeXML(root *xmlNode) error {
xmlDec := xml.NewDecoder(dec.reader) xmlDec := xml.NewDecoder(dec.reader)
// That will convert the charset if the provided XML is non-UTF-8 // That will convert the charset if the provided XML is non-UTF-8

View File

@ -26,7 +26,7 @@ func mapKeysToStrings(node *yaml.Node) {
} }
} }
func NewJsonEncoder(indent int) Encoder { func NewJONEncoder(indent int) Encoder {
var indentString = "" var indentString = ""
for index := 0; index < indent; index++ { for index := 0; index < indent; index++ {

View File

@ -31,18 +31,18 @@ func yamlToProps(sampleYaml string) string {
func TestPropertiesEncoderSimple(t *testing.T) { func TestPropertiesEncoderSimple(t *testing.T) {
var sampleYaml = `a: 'bob cool'` var sampleYaml = `a: 'bob cool'`
var expectedJson = `a = bob cool` var expectedProps = `a = bob cool`
var actualProps = yamlToProps(sampleYaml) var actualProps = yamlToProps(sampleYaml)
test.AssertResult(t, expectedJson, actualProps) test.AssertResult(t, expectedProps, actualProps)
} }
func TestPropertiesEncoderSimpleWithComments(t *testing.T) { func TestPropertiesEncoderSimpleWithComments(t *testing.T) {
var sampleYaml = `a: 'bob cool' # line` var sampleYaml = `a: 'bob cool' # line`
var expectedJson = `# line var expectedProps = `# line
a = bob cool` a = bob cool`
var actualProps = yamlToProps(sampleYaml) var actualProps = yamlToProps(sampleYaml)
test.AssertResult(t, expectedJson, actualProps) test.AssertResult(t, expectedProps, actualProps)
} }
func TestPropertiesEncoderDeep(t *testing.T) { func TestPropertiesEncoderDeep(t *testing.T) {
@ -50,9 +50,9 @@ func TestPropertiesEncoderDeep(t *testing.T) {
b: "bob cool" b: "bob cool"
` `
var expectedJson = `a.b = bob cool` var expectedProps = `a.b = bob cool`
var actualProps = yamlToProps(sampleYaml) var actualProps = yamlToProps(sampleYaml)
test.AssertResult(t, expectedJson, actualProps) test.AssertResult(t, expectedProps, actualProps)
} }
func TestPropertiesEncoderDeepWithComments(t *testing.T) { func TestPropertiesEncoderDeepWithComments(t *testing.T) {
@ -60,10 +60,10 @@ func TestPropertiesEncoderDeepWithComments(t *testing.T) {
b: "bob cool" # b thing b: "bob cool" # b thing
` `
var expectedJson = `# b thing var expectedProps = `# b thing
a.b = bob cool` a.b = bob cool`
var actualProps = yamlToProps(sampleYaml) var actualProps = yamlToProps(sampleYaml)
test.AssertResult(t, expectedJson, actualProps) test.AssertResult(t, expectedProps, actualProps)
} }
func TestPropertiesEncoderArray(t *testing.T) { func TestPropertiesEncoderArray(t *testing.T) {
@ -71,8 +71,8 @@ func TestPropertiesEncoderArray(t *testing.T) {
b: [{c: dog}, {c: cat}] b: [{c: dog}, {c: cat}]
` `
var expectedJson = `a.b.0.c = dog var expectedProps = `a.b.0.c = dog
a.b.1.c = cat` a.b.1.c = cat`
var actualProps = yamlToProps(sampleYaml) var actualProps = yamlToProps(sampleYaml)
test.AssertResult(t, expectedJson, actualProps) test.AssertResult(t, expectedProps, actualProps)
} }

View File

@ -9,11 +9,11 @@ import (
"github.com/mikefarah/yq/v4/test" "github.com/mikefarah/yq/v4/test"
) )
func yamlToJson(sampleYaml string, indent int) string { func yamlToJSON(sampleYaml string, indent int) string {
var output bytes.Buffer var output bytes.Buffer
writer := bufio.NewWriter(&output) writer := bufio.NewWriter(&output)
var jsonEncoder = NewJsonEncoder(indent) var jsonEncoder = NewJONEncoder(indent)
inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder()) inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder())
if err != nil { if err != nil {
panic(err) panic(err)
@ -28,13 +28,13 @@ func yamlToJson(sampleYaml string, indent int) string {
return strings.TrimSuffix(output.String(), "\n") return strings.TrimSuffix(output.String(), "\n")
} }
func TestJsonEncoderPreservesObjectOrder(t *testing.T) { func TestJSONEncoderPreservesObjectOrder(t *testing.T) {
var sampleYaml = `zabbix: winner var sampleYaml = `zabbix: winner
apple: great apple: great
banana: banana:
- {cobra: kai, angus: bob} - {cobra: kai, angus: bob}
` `
var expectedJson = `{ var expectedJSON = `{
"zabbix": "winner", "zabbix": "winner",
"apple": "great", "apple": "great",
"banana": [ "banana": [
@ -44,31 +44,31 @@ banana:
} }
] ]
}` }`
var actualJson = yamlToJson(sampleYaml, 2) var actualJSON = yamlToJSON(sampleYaml, 2)
test.AssertResult(t, expectedJson, actualJson) test.AssertResult(t, expectedJSON, actualJSON)
} }
func TestJsonNullInArray(t *testing.T) { func TestJsonNullInArray(t *testing.T) {
var sampleYaml = `[null]` var sampleYaml = `[null]`
var actualJson = yamlToJson(sampleYaml, 0) var actualJSON = yamlToJSON(sampleYaml, 0)
test.AssertResult(t, sampleYaml, actualJson) test.AssertResult(t, sampleYaml, actualJSON)
} }
func TestJsonNull(t *testing.T) { func TestJsonNull(t *testing.T) {
var sampleYaml = `null` var sampleYaml = `null`
var actualJson = yamlToJson(sampleYaml, 0) var actualJSON = yamlToJSON(sampleYaml, 0)
test.AssertResult(t, sampleYaml, actualJson) test.AssertResult(t, sampleYaml, actualJSON)
} }
func TestJsonNullInObject(t *testing.T) { func TestJsonNullInObject(t *testing.T) {
var sampleYaml = `{x: null}` var sampleYaml = `{x: null}`
var actualJson = yamlToJson(sampleYaml, 0) var actualJSON = yamlToJSON(sampleYaml, 0)
test.AssertResult(t, `{"x":null}`, actualJson) test.AssertResult(t, `{"x":null}`, actualJSON)
} }
func TestJsonEncoderDoesNotEscapeHTMLChars(t *testing.T) { func TestJsonEncoderDoesNotEscapeHTMLChars(t *testing.T) {
var sampleYaml = `build: "( ./lint && ./format && ./compile ) < src.code"` var sampleYaml = `build: "( ./lint && ./format && ./compile ) < src.code"`
var expectedJson = `{"build":"( ./lint && ./format && ./compile ) < src.code"}` var expectedJSON = `{"build":"( ./lint && ./format && ./compile ) < src.code"}`
var actualJson = yamlToJson(sampleYaml, 0) var actualJSON = yamlToJSON(sampleYaml, 0)
test.AssertResult(t, expectedJson, actualJson) test.AssertResult(t, expectedJSON, actualJSON)
} }

View File

@ -9,7 +9,7 @@ import (
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
) )
var XmlPreferences = xmlPreferences{AttributePrefix: "+", ContentName: "+content"} var XMLPreferences = xmlPreferences{AttributePrefix: "+", ContentName: "+content"}
type xmlEncoder struct { type xmlEncoder struct {
attributePrefix string attributePrefix string
@ -17,7 +17,7 @@ type xmlEncoder struct {
indentString string indentString string
} }
func NewXmlEncoder(indent int, attributePrefix string, contentName string) Encoder { func NewXMLEncoder(indent int, attributePrefix string, contentName string) Encoder {
var indentString = "" var indentString = ""
for index := 0; index < indent; index++ { for index := 0; index < indent; index++ {

View File

@ -7,8 +7,8 @@ import (
type ExpressionNode struct { type ExpressionNode struct {
Operation *Operation Operation *Operation
Lhs *ExpressionNode LHS *ExpressionNode
Rhs *ExpressionNode RHS *ExpressionNode
} }
type ExpressionParserInterface interface { type ExpressionParserInterface interface {
@ -55,15 +55,15 @@ func (p *expressionParserImpl) createExpressionTree(postFixPath []*Operation) (*
return nil, fmt.Errorf("'%v' expects 1 arg but received none", strings.TrimSpace(Operation.StringValue)) return nil, fmt.Errorf("'%v' expects 1 arg but received none", strings.TrimSpace(Operation.StringValue))
} }
remaining, rhs := stack[:len(stack)-1], stack[len(stack)-1] remaining, rhs := stack[:len(stack)-1], stack[len(stack)-1]
newNode.Rhs = rhs newNode.RHS = rhs
stack = remaining stack = remaining
} else if numArgs == 2 { } else if numArgs == 2 {
if len(stack) < 2 { if len(stack) < 2 {
return nil, fmt.Errorf("'%v' expects 2 args but there is %v", strings.TrimSpace(Operation.StringValue), len(stack)) return nil, fmt.Errorf("'%v' expects 2 args but there is %v", strings.TrimSpace(Operation.StringValue), len(stack))
} }
remaining, lhs, rhs := stack[:len(stack)-2], stack[len(stack)-2], stack[len(stack)-1] remaining, lhs, rhs := stack[:len(stack)-2], stack[len(stack)-2], stack[len(stack)-1]
newNode.Lhs = lhs newNode.LHS = lhs
newNode.Rhs = rhs newNode.RHS = rhs
stack = remaining stack = remaining
} }
} }

View File

@ -327,53 +327,53 @@ func initLexer() (*lex.Lexer, error) {
lexer.Add([]byte(`toyaml\([0-9]+\)`), encodeWithIndent(YamlOutputFormat)) lexer.Add([]byte(`toyaml\([0-9]+\)`), encodeWithIndent(YamlOutputFormat))
lexer.Add([]byte(`to_yaml\([0-9]+\)`), encodeWithIndent(YamlOutputFormat)) lexer.Add([]byte(`to_yaml\([0-9]+\)`), encodeWithIndent(YamlOutputFormat))
lexer.Add([]byte(`toxml\([0-9]+\)`), encodeWithIndent(XmlOutputFormat)) lexer.Add([]byte(`toxml\([0-9]+\)`), encodeWithIndent(XMLOutputFormat))
lexer.Add([]byte(`to_xml\([0-9]+\)`), encodeWithIndent(XmlOutputFormat)) lexer.Add([]byte(`to_xml\([0-9]+\)`), encodeWithIndent(XMLOutputFormat))
lexer.Add([]byte(`tojson\([0-9]+\)`), encodeWithIndent(JsonOutputFormat)) lexer.Add([]byte(`tojson\([0-9]+\)`), encodeWithIndent(JSONOutputFormat))
lexer.Add([]byte(`to_json\([0-9]+\)`), encodeWithIndent(JsonOutputFormat)) lexer.Add([]byte(`to_json\([0-9]+\)`), encodeWithIndent(JSONOutputFormat))
lexer.Add([]byte(`toyaml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: YamlOutputFormat, indent: 2})) lexer.Add([]byte(`toyaml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: YamlOutputFormat, indent: 2}))
lexer.Add([]byte(`to_yaml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: YamlOutputFormat, indent: 2})) lexer.Add([]byte(`to_yaml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: YamlOutputFormat, indent: 2}))
// 0 indent doesn't work with yaml. // 0 indent doesn't work with yaml.
lexer.Add([]byte(`@yaml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: YamlOutputFormat, indent: 2})) lexer.Add([]byte(`@yaml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: YamlOutputFormat, indent: 2}))
lexer.Add([]byte(`tojson`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: JsonOutputFormat, indent: 2})) lexer.Add([]byte(`tojson`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: JSONOutputFormat, indent: 2}))
lexer.Add([]byte(`to_json`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: JsonOutputFormat, indent: 2})) lexer.Add([]byte(`to_json`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: JSONOutputFormat, indent: 2}))
lexer.Add([]byte(`@json`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: JsonOutputFormat, indent: 0})) lexer.Add([]byte(`@json`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: JSONOutputFormat, indent: 0}))
lexer.Add([]byte(`toprops`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: PropsOutputFormat, indent: 2})) lexer.Add([]byte(`toprops`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: PropsOutputFormat, indent: 2}))
lexer.Add([]byte(`to_props`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: PropsOutputFormat, indent: 2})) lexer.Add([]byte(`to_props`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: PropsOutputFormat, indent: 2}))
lexer.Add([]byte(`@props`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: PropsOutputFormat, indent: 2})) lexer.Add([]byte(`@props`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: PropsOutputFormat, indent: 2}))
lexer.Add([]byte(`tocsv`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: CsvOutputFormat})) lexer.Add([]byte(`tocsv`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: CSVOutputFormat}))
lexer.Add([]byte(`to_csv`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: CsvOutputFormat})) lexer.Add([]byte(`to_csv`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: CSVOutputFormat}))
lexer.Add([]byte(`@csv`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: CsvOutputFormat})) lexer.Add([]byte(`@csv`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: CSVOutputFormat}))
lexer.Add([]byte(`totsv`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: TsvOutputFormat})) lexer.Add([]byte(`totsv`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: TSVOutputFormat}))
lexer.Add([]byte(`to_tsv`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: TsvOutputFormat})) lexer.Add([]byte(`to_tsv`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: TSVOutputFormat}))
lexer.Add([]byte(`@tsv`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: TsvOutputFormat})) lexer.Add([]byte(`@tsv`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: TSVOutputFormat}))
lexer.Add([]byte(`toxml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: XmlOutputFormat})) lexer.Add([]byte(`toxml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: XMLOutputFormat}))
lexer.Add([]byte(`to_xml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: XmlOutputFormat, indent: 2})) lexer.Add([]byte(`to_xml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: XMLOutputFormat, indent: 2}))
lexer.Add([]byte(`@xml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: XmlOutputFormat, indent: 0})) lexer.Add([]byte(`@xml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: XMLOutputFormat, indent: 0}))
lexer.Add([]byte(`fromyaml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat})) lexer.Add([]byte(`fromyaml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat}))
lexer.Add([]byte(`fromjson`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat})) lexer.Add([]byte(`fromjson`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat}))
lexer.Add([]byte(`fromxml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: XmlInputFormat})) lexer.Add([]byte(`fromxml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: XMLInputFormat}))
lexer.Add([]byte(`from_yaml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat})) 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_json`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: YamlInputFormat}))
lexer.Add([]byte(`from_xml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: XmlInputFormat})) lexer.Add([]byte(`from_xml`), opTokenWithPrefs(decodeOpType, nil, decoderPreferences{format: XMLInputFormat}))
lexer.Add([]byte(`sortKeys`), opToken(sortKeysOpType)) lexer.Add([]byte(`sortKeys`), opToken(sortKeysOpType))
lexer.Add([]byte(`sort_keys`), opToken(sortKeysOpType)) lexer.Add([]byte(`sort_keys`), opToken(sortKeysOpType))
lexer.Add([]byte(`load`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewYamlDecoder()})) lexer.Add([]byte(`load`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewYamlDecoder()}))
lexer.Add([]byte(`xmlload`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewXmlDecoder(XmlPreferences.AttributePrefix, XmlPreferences.ContentName)})) lexer.Add([]byte(`xmlload`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName)}))
lexer.Add([]byte(`load_xml`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewXmlDecoder(XmlPreferences.AttributePrefix, XmlPreferences.ContentName)})) 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(`loadxml`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: false, decoder: NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName)}))
lexer.Add([]byte(`strload`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: true})) lexer.Add([]byte(`strload`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: true}))
lexer.Add([]byte(`load_str`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: true})) lexer.Add([]byte(`load_str`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: true}))

View File

@ -71,7 +71,7 @@ var jsonScenarios = []formatScenario{
}, },
} }
func decodeJson(t *testing.T, jsonString string) *CandidateNode { func decodeJSON(t *testing.T, jsonString string) *CandidateNode {
docs, err := readDocumentWithLeadingContent(jsonString, "sample.json", 0) docs, err := readDocumentWithLeadingContent(jsonString, "sample.json", 0)
if err != nil { if err != nil {
@ -96,11 +96,11 @@ func decodeJson(t *testing.T, jsonString string) *CandidateNode {
return context.MatchingNodes.Front().Value.(*CandidateNode) return context.MatchingNodes.Front().Value.(*CandidateNode)
} }
func testJsonScenario(t *testing.T, s formatScenario) { func testJSONScenario(t *testing.T, s formatScenario) {
if s.scenarioType == "encode" || s.scenarioType == "roundtrip" { if s.scenarioType == "encode" || s.scenarioType == "roundtrip" {
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewJsonEncoder(s.indent)), s.description) test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewJONEncoder(s.indent)), s.description)
} else { } else {
var actual = resultToString(t, decodeJson(t, s.input)) var actual = resultToString(t, decodeJSON(t, s.input))
test.AssertResultWithContext(t, s.expected, actual, s.description) test.AssertResultWithContext(t, s.expected, actual, s.description)
} }
} }
@ -145,7 +145,7 @@ func processFormatScenario(s formatScenario, encoder Encoder) string {
} }
func documentJsonDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario) { func documentJSONDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario) {
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
if s.subdescription != "" { if s.subdescription != "" {
@ -163,7 +163,7 @@ func documentJsonDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario)
var output bytes.Buffer var output bytes.Buffer
printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), YamlOutputFormat, true, false, 2, true) printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), YamlOutputFormat, true, false, 2, true)
node := decodeJson(t, s.input) node := decodeJSON(t, s.input)
err := printer.PrintResults(node.AsList()) err := printer.PrintResults(node.AsList())
if err != nil { if err != nil {
@ -174,20 +174,20 @@ func documentJsonDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario)
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", output.String())) writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", output.String()))
} }
func documentJsonScenario(t *testing.T, w *bufio.Writer, i interface{}) { func documentJSONScenario(t *testing.T, w *bufio.Writer, i interface{}) {
s := i.(formatScenario) s := i.(formatScenario)
if s.skipDoc { if s.skipDoc {
return return
} }
if s.scenarioType == "encode" { if s.scenarioType == "encode" {
documentJsonEncodeScenario(w, s) documentJSONEncodeScenario(w, s)
} else { } else {
documentJsonDecodeScenario(t, w, s) documentJSONDecodeScenario(t, w, s)
} }
} }
func documentJsonEncodeScenario(w *bufio.Writer, s formatScenario) { func documentJSONEncodeScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
if s.subdescription != "" { if s.subdescription != "" {
@ -212,16 +212,16 @@ func documentJsonEncodeScenario(w *bufio.Writer, s formatScenario) {
} }
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
writeOrPanic(w, fmt.Sprintf("```json\n%v```\n\n", processFormatScenario(s, NewJsonEncoder(s.indent)))) writeOrPanic(w, fmt.Sprintf("```json\n%v```\n\n", processFormatScenario(s, NewJONEncoder(s.indent))))
} }
func TestJsonScenarios(t *testing.T) { func TestJSONScenarios(t *testing.T) {
for _, tt := range jsonScenarios { for _, tt := range jsonScenarios {
testJsonScenario(t, tt) testJSONScenario(t, tt)
} }
genericScenarios := make([]interface{}, len(jsonScenarios)) genericScenarios := make([]interface{}, len(jsonScenarios))
for i, s := range jsonScenarios { for i, s := range jsonScenarios {
genericScenarios[i] = s genericScenarios[i] = s
} }
documentScenarios(t, "usage", "convert", genericScenarios, documentJsonScenario) documentScenarios(t, "usage", "convert", genericScenarios, documentJSONScenario)
} }

View File

@ -193,9 +193,9 @@ func recurseNodeObjectEqual(lhs *yaml.Node, rhs *yaml.Node) bool {
key := lhs.Content[index] key := lhs.Content[index]
value := lhs.Content[index+1] value := lhs.Content[index+1]
indexInRhs := findInArray(rhs, key) indexInRHS := findInArray(rhs, key)
if indexInRhs == -1 || !recursiveNodeEqual(value, rhs.Content[indexInRhs+1]) { if indexInRHS == -1 || !recursiveNodeEqual(value, rhs.Content[indexInRHS+1]) {
return false return false
} }
} }
@ -289,7 +289,7 @@ func footComment(node *yaml.Node) string {
} }
func createValueOperation(value interface{}, stringValue string) *Operation { func createValueOperation(value interface{}, stringValue string) *Operation {
var node *yaml.Node = createScalarNode(value, stringValue) var node = createScalarNode(value, stringValue)
return &Operation{ return &Operation{
OperationType: valueOpType, OperationType: valueOpType,

View File

@ -10,8 +10,8 @@ import (
func createAddOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode { func createAddOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode {
return &ExpressionNode{Operation: &Operation{OperationType: addOpType}, return &ExpressionNode{Operation: &Operation{OperationType: addOpType},
Lhs: lhs, LHS: lhs,
Rhs: rhs} RHS: rhs}
} }
func addAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { func addAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
@ -179,14 +179,14 @@ func addMaps(target *CandidateNode, lhsC *CandidateNode, rhsC *CandidateNode) {
key := rhs.Content[index] key := rhs.Content[index]
value := rhs.Content[index+1] value := rhs.Content[index+1]
log.Debug("finding %v", key.Value) log.Debug("finding %v", key.Value)
indexInLhs := findInArray(target.Node, key) indexInLHS := findInArray(target.Node, key)
log.Debug("indexInLhs %v", indexInLhs) log.Debug("indexInLhs %v", indexInLHS)
if indexInLhs < 0 { if indexInLHS < 0 {
// not in there, append it // not in there, append it
target.Node.Content = append(target.Node.Content, key, value) target.Node.Content = append(target.Node.Content, key, value)
} else { } else {
// it's there, replace it // it's there, replace it
target.Node.Content[indexInLhs+1] = value target.Node.Content[indexInLHS+1] = value
} }
} }
target.Node.Kind = yaml.MappingNode target.Node.Kind = yaml.MappingNode

View File

@ -12,7 +12,7 @@ func assignAliasOperator(d *dataTreeNavigator, context Context, expressionNode *
aliasName := "" aliasName := ""
if !expressionNode.Operation.UpdateAssign { if !expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -21,7 +21,7 @@ func assignAliasOperator(d *dataTreeNavigator, context Context, expressionNode *
} }
} }
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
@ -32,7 +32,7 @@ func assignAliasOperator(d *dataTreeNavigator, context Context, expressionNode *
log.Debugf("Setting aliasName : %v", candidate.GetKey()) log.Debugf("Setting aliasName : %v", candidate.GetKey())
if expressionNode.Operation.UpdateAssign { if expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -68,7 +68,7 @@ func assignAnchorOperator(d *dataTreeNavigator, context Context, expressionNode
anchorName := "" anchorName := ""
if !expressionNode.Operation.UpdateAssign { if !expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -78,7 +78,7 @@ func assignAnchorOperator(d *dataTreeNavigator, context Context, expressionNode
} }
} }
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
@ -89,7 +89,7 @@ func assignAnchorOperator(d *dataTreeNavigator, context Context, expressionNode
log.Debugf("Setting anchorName of : %v", candidate.GetKey()) log.Debugf("Setting anchorName of : %v", candidate.GetKey())
if expressionNode.Operation.UpdateAssign { if expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -124,7 +124,7 @@ func explodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expr
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
rhs, err := d.GetMatchingNodes(context.SingleChildContext(candidate), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.SingleChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err

View File

@ -16,7 +16,7 @@ func assignUpdateFunc(prefs assignPreferences) crossFunctionCalculation {
} }
func assignUpdateOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { func assignUpdateOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -35,7 +35,7 @@ func assignUpdateOperator(d *dataTreeNavigator, context Context, expressionNode
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() { for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
rhs, err := d.GetMatchingNodes(context.SingleChildContext(candidate), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.SingleChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
@ -57,14 +57,14 @@ func assignUpdateOperator(d *dataTreeNavigator, context Context, expressionNode
// does not update content or values // does not update content or values
func assignAttributesOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { func assignAttributesOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debug("getting lhs matching nodes for update") log.Debug("getting lhs matching nodes for update")
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() { for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err

View File

@ -103,7 +103,7 @@ func allOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi
if candidateNode.Kind != yaml.SequenceNode { if candidateNode.Kind != yaml.SequenceNode {
return Context{}, fmt.Errorf("any only supports arrays, was %v", candidateNode.Tag) return Context{}, fmt.Errorf("any only supports arrays, was %v", candidateNode.Tag)
} }
booleanResult, err := findBoolean(false, d, context, expressionNode.Rhs, candidateNode) booleanResult, err := findBoolean(false, d, context, expressionNode.RHS, candidateNode)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -122,7 +122,7 @@ func anyOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi
if candidateNode.Kind != yaml.SequenceNode { if candidateNode.Kind != yaml.SequenceNode {
return Context{}, fmt.Errorf("any only supports arrays, was %v", candidateNode.Tag) return Context{}, fmt.Errorf("any only supports arrays, was %v", candidateNode.Tag)
} }
booleanResult, err := findBoolean(true, d, context, expressionNode.Rhs, candidateNode) booleanResult, err := findBoolean(true, d, context, expressionNode.RHS, candidateNode)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -41,7 +41,7 @@ func collectOperator(d *dataTreeNavigator, context Context, expressionNode *Expr
} }
if evaluateAllTogether { if evaluateAllTogether {
collectedNode, err := collectTogether(d, context, expressionNode.Rhs) collectedNode, err := collectTogether(d, context, expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -56,7 +56,7 @@ func collectOperator(d *dataTreeNavigator, context Context, expressionNode *Expr
collectedNode := &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"} collectedNode := &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
collectCandidate := candidate.CreateReplacement(collectedNode) collectCandidate := candidate.CreateReplacement(collectedNode)
collectExpResults, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.Rhs) collectExpResults, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -19,7 +19,7 @@ func assignCommentsOperator(d *dataTreeNavigator, context Context, expressionNod
log.Debugf("AssignComments operator!") log.Debugf("AssignComments operator!")
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
@ -29,7 +29,7 @@ func assignCommentsOperator(d *dataTreeNavigator, context Context, expressionNod
comment := "" comment := ""
if !expressionNode.Operation.UpdateAssign { if !expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -43,7 +43,7 @@ func assignCommentsOperator(d *dataTreeNavigator, context Context, expressionNod
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
if expressionNode.Operation.UpdateAssign { if expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -7,7 +7,7 @@ import (
) )
func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { func deleteChildOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
nodesToDelete, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) nodesToDelete, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err

View File

@ -12,18 +12,18 @@ import (
func configureEncoder(format PrinterOutputFormat, indent int) Encoder { func configureEncoder(format PrinterOutputFormat, indent int) Encoder {
switch format { switch format {
case JsonOutputFormat: case JSONOutputFormat:
return NewJsonEncoder(indent) return NewJONEncoder(indent)
case PropsOutputFormat: case PropsOutputFormat:
return NewPropertiesEncoder() return NewPropertiesEncoder()
case CsvOutputFormat: case CSVOutputFormat:
return NewCsvEncoder(',') return NewCsvEncoder(',')
case TsvOutputFormat: case TSVOutputFormat:
return NewCsvEncoder('\t') return NewCsvEncoder('\t')
case YamlOutputFormat: case YamlOutputFormat:
return NewYamlEncoder(indent, false, true, true) return NewYamlEncoder(indent, false, true, true)
case XmlOutputFormat: case XMLOutputFormat:
return NewXmlEncoder(indent, XmlPreferences.AttributePrefix, XmlPreferences.ContentName) return NewXMLEncoder(indent, XMLPreferences.AttributePrefix, XMLPreferences.ContentName)
} }
panic("invalid encoder") panic("invalid encoder")
} }
@ -76,9 +76,9 @@ func encodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
} }
// dont print a new line when printing json on a single line. // dont print a new line when printing json on a single line.
if (preferences.format == JsonOutputFormat && preferences.indent == 0) || if (preferences.format == JSONOutputFormat && preferences.indent == 0) ||
preferences.format == CsvOutputFormat || preferences.format == CSVOutputFormat ||
preferences.format == TsvOutputFormat { preferences.format == TSVOutputFormat {
stringValue = chomper.ReplaceAllString(stringValue, "") stringValue = chomper.ReplaceAllString(stringValue, "")
} }
@ -101,8 +101,8 @@ func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
switch preferences.format { switch preferences.format {
case YamlInputFormat: case YamlInputFormat:
decoder = NewYamlDecoder() decoder = NewYamlDecoder()
case XmlInputFormat: case XMLInputFormat:
decoder = NewXmlDecoder(XmlPreferences.AttributePrefix, XmlPreferences.ContentName) decoder = NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName)
} }
var results = list.New() var results = list.New()

View File

@ -148,8 +148,8 @@ func withEntriesOperator(d *dataTreeNavigator, context Context, expressionNode *
return Context{}, err return Context{}, err
} }
result, err := d.GetMatchingNodes(splatted, expressionNode.Rhs) result, err := d.GetMatchingNodes(splatted, expressionNode.RHS)
log.Debug("expressionNode.Rhs %v", expressionNode.Rhs.Operation.OperationType) log.Debug("expressionNode.Rhs %v", expressionNode.RHS.Operation.OperationType)
log.Debug("result %v", result) log.Debug("result %v", result)
if err != nil { if err != nil {
return Context{}, err return Context{}, err

View File

@ -6,7 +6,7 @@ import (
func evalOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { func evalOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debugf("Eval") log.Debugf("Eval")
pathExpStrResults, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) pathExpStrResults, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -50,7 +50,7 @@ func groupBy(d *dataTreeNavigator, context Context, expressionNode *ExpressionNo
return Context{}, fmt.Errorf("Only arrays are supported for group by") return Context{}, fmt.Errorf("Only arrays are supported for group by")
} }
newMatches, err := processIntoGroups(d, context, expressionNode.Rhs, candidateNode) newMatches, err := processIntoGroups(d, context, expressionNode.RHS, candidateNode)
if err != nil { if err != nil {
return Context{}, err return Context{}, err

View File

@ -12,7 +12,7 @@ func hasOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi
log.Debugf("-- hasOperation") log.Debugf("-- hasOperation")
var results = list.New() var results = list.New()
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err

View File

@ -67,7 +67,7 @@ func loadYamlOperator(d *dataTreeNavigator, context Context, expressionNode *Exp
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -17,7 +17,7 @@ func mapValuesOperator(d *dataTreeNavigator, context Context, expressionNode *Ex
assignUpdateExp := &ExpressionNode{ assignUpdateExp := &ExpressionNode{
Operation: &Operation{OperationType: assignOpType, UpdateAssign: true}, Operation: &Operation{OperationType: assignOpType, UpdateAssign: true},
Rhs: expressionNode.Rhs, RHS: expressionNode.RHS,
} }
_, err = assignUpdateOperator(d, splatted, assignUpdateExp) _, err = assignUpdateOperator(d, splatted, assignUpdateExp)
if err != nil { if err != nil {
@ -42,8 +42,8 @@ func mapOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi
return Context{}, err return Context{}, err
} }
result, err := d.GetMatchingNodes(splatted, expressionNode.Rhs) result, err := d.GetMatchingNodes(splatted, expressionNode.RHS)
log.Debug("expressionNode.Rhs %v", expressionNode.Rhs.Operation.OperationType) log.Debug("expressionNode.Rhs %v", expressionNode.RHS.Operation.OperationType)
log.Debug("result %v", result) log.Debug("result %v", result)
if err != nil { if err != nil {
return Context{}, err return Context{}, err

View File

@ -20,8 +20,8 @@ type multiplyPreferences struct {
func createMultiplyOp(prefs interface{}) func(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode { func createMultiplyOp(prefs interface{}) func(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode {
return func(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode { return func(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode {
return &ExpressionNode{Operation: &Operation{OperationType: multiplyOpType, Preferences: prefs}, return &ExpressionNode{Operation: &Operation{OperationType: multiplyOpType, Preferences: prefs},
Lhs: lhs, LHS: lhs,
Rhs: rhs} RHS: rhs}
} }
} }
@ -199,8 +199,8 @@ func applyAssignment(d *dataTreeNavigator, context Context, pathIndexToStartFrom
assignmentOpNode := &ExpressionNode{ assignmentOpNode := &ExpressionNode{
Operation: assignmentOp, Operation: assignmentOp,
Lhs: createTraversalTree(lhsPath, preferences.TraversePrefs, rhs.IsMapKey), LHS: createTraversalTree(lhsPath, preferences.TraversePrefs, rhs.IsMapKey),
Rhs: &ExpressionNode{Operation: rhsOp}, RHS: &ExpressionNode{Operation: rhsOp},
} }
_, err := d.GetMatchingNodes(context.SingleChildContext(lhs), assignmentOpNode) _, err := d.GetMatchingNodes(context.SingleChildContext(lhs), assignmentOpNode)

View File

@ -6,11 +6,11 @@ func pipeOperator(d *dataTreeNavigator, context Context, expressionNode *Express
// BUT we still return the original context back (see jq) // BUT we still return the original context back (see jq)
// https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|... // https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
rhs, err := d.GetMatchingNodes(lhs, expressionNode.Rhs) rhs, err := d.GetMatchingNodes(lhs, expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -15,13 +15,13 @@ func reduceOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
//ensure lhs is actually an assignment //ensure lhs is actually an assignment
//and rhs is a block (empty) //and rhs is a block (empty)
if expressionNode.Lhs.Operation.OperationType != assignVariableOpType { if expressionNode.LHS.Operation.OperationType != assignVariableOpType {
return Context{}, fmt.Errorf("reduce must be given a variables assignment, got %v instead", expressionNode.Lhs.Operation.OperationType.Type) return Context{}, fmt.Errorf("reduce must be given a variables assignment, got %v instead", expressionNode.LHS.Operation.OperationType.Type)
} else if expressionNode.Rhs.Operation.OperationType != blockOpType { } else if expressionNode.RHS.Operation.OperationType != blockOpType {
return Context{}, fmt.Errorf("reduce must be given a block, got %v instead", expressionNode.Rhs.Operation.OperationType.Type) return Context{}, fmt.Errorf("reduce must be given a block, got %v instead", expressionNode.RHS.Operation.OperationType.Type)
} }
arrayExpNode := expressionNode.Lhs.Lhs arrayExpNode := expressionNode.LHS.LHS
array, err := d.GetMatchingNodes(context, arrayExpNode) array, err := d.GetMatchingNodes(context, arrayExpNode)
log.Debugf("array of %v things", array.MatchingNodes.Len()) log.Debugf("array of %v things", array.MatchingNodes.Len())
@ -30,9 +30,9 @@ func reduceOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
return Context{}, err return Context{}, err
} }
variableName := expressionNode.Lhs.Rhs.Operation.StringValue variableName := expressionNode.LHS.RHS.Operation.StringValue
initExp := expressionNode.Rhs.Lhs initExp := expressionNode.RHS.LHS
accum, err := d.GetMatchingNodes(context, initExp) accum, err := d.GetMatchingNodes(context, initExp)
if err != nil { if err != nil {
@ -41,7 +41,7 @@ func reduceOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
log.Debugf("with variable %v", variableName) log.Debugf("with variable %v", variableName)
blockExp := expressionNode.Rhs.Rhs blockExp := expressionNode.RHS.RHS
for el := array.MatchingNodes.Front(); el != nil; el = el.Next() { for el := array.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
log.Debugf("REDUCING WITH %v", NodeToString(candidate)) log.Debugf("REDUCING WITH %v", NodeToString(candidate))

View File

@ -11,7 +11,7 @@ func selectOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err

View File

@ -12,7 +12,7 @@ import (
func sortOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { func sortOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}} selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}}
expressionNode.Rhs = selfExpression expressionNode.RHS = selfExpression
return sortByOperator(d, context, expressionNode) return sortByOperator(d, context, expressionNode)
} }
@ -36,7 +36,7 @@ func sortByOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
for i, originalNode := range candidateNode.Content { for i, originalNode := range candidateNode.Content {
childCandidate := candidate.CreateChildInArray(i, originalNode) childCandidate := candidate.CreateChildInArray(i, originalNode)
compareContext, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(childCandidate), expressionNode.Rhs) compareContext, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(childCandidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -10,7 +10,7 @@ func sortKeysOperator(d *dataTreeNavigator, context Context, expressionNode *Exp
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -13,7 +13,7 @@ func getSubstituteParameters(d *dataTreeNavigator, block *ExpressionNode, contex
regEx := "" regEx := ""
replacementText := "" replacementText := ""
regExNodes, err := d.GetMatchingNodes(context.ReadOnlyClone(), block.Lhs) regExNodes, err := d.GetMatchingNodes(context.ReadOnlyClone(), block.LHS)
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
@ -23,7 +23,7 @@ func getSubstituteParameters(d *dataTreeNavigator, block *ExpressionNode, contex
log.Debug("regEx %v", regEx) log.Debug("regEx %v", regEx)
replacementNodes, err := d.GetMatchingNodes(context, block.Rhs) replacementNodes, err := d.GetMatchingNodes(context, block.RHS)
if err != nil { if err != nil {
return "", "", err return "", "", err
} }
@ -43,7 +43,7 @@ func substituteStringOperator(d *dataTreeNavigator, context Context, expressionN
//rhs block operator //rhs block operator
//lhs of block = regex //lhs of block = regex
//rhs of block = replacement expression //rhs of block = replacement expression
block := expressionNode.Rhs block := expressionNode.RHS
regExStr, replacementText, err := getSubstituteParameters(d, block, context) regExStr, replacementText, err := getSubstituteParameters(d, block, context)
@ -194,15 +194,15 @@ func capture(matchPrefs matchPreferences, regEx *regexp.Regexp, candidate *Candi
} }
func extractMatchArguments(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (*regexp.Regexp, matchPreferences, error) { func extractMatchArguments(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (*regexp.Regexp, matchPreferences, error) {
regExExpNode := expressionNode.Rhs regExExpNode := expressionNode.RHS
matchPrefs := matchPreferences{} matchPrefs := matchPreferences{}
// we got given parameters e.g. match(exp; params) // we got given parameters e.g. match(exp; params)
if expressionNode.Rhs.Operation.OperationType == blockOpType { if expressionNode.RHS.Operation.OperationType == blockOpType {
block := expressionNode.Rhs block := expressionNode.RHS
regExExpNode = block.Lhs regExExpNode = block.LHS
replacementNodes, err := d.GetMatchingNodes(context, block.Rhs) replacementNodes, err := d.GetMatchingNodes(context, block.RHS)
if err != nil { if err != nil {
return nil, matchPrefs, err return nil, matchPrefs, err
} }
@ -304,7 +304,7 @@ func joinStringOperator(d *dataTreeNavigator, context Context, expressionNode *E
log.Debugf("-- joinStringOperator") log.Debugf("-- joinStringOperator")
joinStr := "" joinStr := ""
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -345,7 +345,7 @@ func splitStringOperator(d *dataTreeNavigator, context Context, expressionNode *
log.Debugf("-- splitStringOperator") log.Debugf("-- splitStringOperator")
splitStr := "" splitStr := ""
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -31,7 +31,7 @@ func assignStyleOperator(d *dataTreeNavigator, context Context, expressionNode *
log.Debugf("AssignStyleOperator: %v") log.Debugf("AssignStyleOperator: %v")
var style yaml.Style var style yaml.Style
if !expressionNode.Operation.UpdateAssign { if !expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -44,7 +44,7 @@ func assignStyleOperator(d *dataTreeNavigator, context Context, expressionNode *
} }
} }
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
@ -54,7 +54,7 @@ func assignStyleOperator(d *dataTreeNavigator, context Context, expressionNode *
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
log.Debugf("Setting style of : %v", candidate.GetKey()) log.Debugf("Setting style of : %v", candidate.GetKey())
if expressionNode.Operation.UpdateAssign { if expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -10,8 +10,8 @@ import (
func createSubtractOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode { func createSubtractOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode {
return &ExpressionNode{Operation: &Operation{OperationType: subtractOpType}, return &ExpressionNode{Operation: &Operation{OperationType: subtractOpType},
Lhs: lhs, LHS: lhs,
Rhs: rhs} RHS: rhs}
} }
func subtractAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { func subtractAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
@ -25,7 +25,7 @@ func subtractOperator(d *dataTreeNavigator, context Context, expressionNode *Exp
} }
func subtractArray(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) { func subtractArray(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
newLhsArray := make([]*yaml.Node, 0) newLHSArray := make([]*yaml.Node, 0)
for lindex := 0; lindex < len(lhs.Node.Content); lindex = lindex + 1 { for lindex := 0; lindex < len(lhs.Node.Content); lindex = lindex + 1 {
shouldInclude := true shouldInclude := true
@ -35,10 +35,10 @@ func subtractArray(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, erro
} }
} }
if shouldInclude { if shouldInclude {
newLhsArray = append(newLhsArray, lhs.Node.Content[lindex]) newLHSArray = append(newLHSArray, lhs.Node.Content[lindex])
} }
} }
lhs.Node.Content = newLhsArray lhs.Node.Content = newLHSArray
return lhs, nil return lhs, nil
} }

View File

@ -12,7 +12,7 @@ func assignTagOperator(d *dataTreeNavigator, context Context, expressionNode *Ex
tag := "" tag := ""
if !expressionNode.Operation.UpdateAssign { if !expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -22,7 +22,7 @@ func assignTagOperator(d *dataTreeNavigator, context Context, expressionNode *Ex
} }
} }
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
@ -32,7 +32,7 @@ func assignTagOperator(d *dataTreeNavigator, context Context, expressionNode *Ex
candidate := el.Value.(*CandidateNode) candidate := el.Value.(*CandidateNode)
log.Debugf("Setting tag of : %v", candidate.GetKey()) log.Debugf("Setting tag of : %v", candidate.GetKey())
if expressionNode.Operation.UpdateAssign { if expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }

View File

@ -82,14 +82,14 @@ func traverseArrayOperator(d *dataTreeNavigator, context Context, expressionNode
// BUT we still return the original context back (see jq) // BUT we still return the original context back (see jq)
// https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|... // https://stedolan.github.io/jq/manual/#Variable/SymbolicBindingOperator:...as$identifier|...
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
// rhs is a collect expression that will yield indexes to retrieve of the arrays // rhs is a collect expression that will yield indexes to retrieve of the arrays
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err

View File

@ -5,14 +5,14 @@ import "container/list"
func unionOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { func unionOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
log.Debug("unionOperator") log.Debug("unionOperator")
log.Debug("context: %v", NodesToString(context.MatchingNodes)) log.Debug("context: %v", NodesToString(context.MatchingNodes))
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
log.Debug("lhs: %v", NodesToString(lhs.MatchingNodes)) log.Debug("lhs: %v", NodesToString(lhs.MatchingNodes))
log.Debug("rhs input: %v", NodesToString(context.MatchingNodes)) log.Debug("rhs input: %v", NodesToString(context.MatchingNodes))
log.Debug("rhs: %v", expressionNode.Rhs.Operation.toString()) log.Debug("rhs: %v", expressionNode.RHS.Operation.toString())
rhs, err := d.GetMatchingNodes(context, expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context, expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err

View File

@ -10,7 +10,7 @@ import (
func unique(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { func unique(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}} selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}}
uniqueByExpression := &ExpressionNode{Operation: &Operation{OperationType: uniqueByOpType}, Rhs: selfExpression} uniqueByExpression := &ExpressionNode{Operation: &Operation{OperationType: uniqueByOpType}, RHS: selfExpression}
return uniqueBy(d, context, uniqueByExpression) return uniqueBy(d, context, uniqueByExpression)
} }
@ -31,7 +31,7 @@ func uniqueBy(d *dataTreeNavigator, context Context, expressionNode *ExpressionN
var newMatches = orderedmap.NewOrderedMap() var newMatches = orderedmap.NewOrderedMap()
for _, node := range candidateNode.Content { for _, node := range candidateNode.Content {
child := &CandidateNode{Node: node} child := &CandidateNode{Node: node}
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(child), expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(child), expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err

View File

@ -20,14 +20,14 @@ type assignVarPreferences struct {
} }
func assignVariableOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { func assignVariableOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
lhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, nil return Context{}, nil
} }
if expressionNode.Rhs.Operation.OperationType.Type != "GET_VARIABLE" { if expressionNode.RHS.Operation.OperationType.Type != "GET_VARIABLE" {
return Context{}, fmt.Errorf("RHS of 'as' operator must be a variable name e.g. $foo") return Context{}, fmt.Errorf("RHS of 'as' operator must be a variable name e.g. $foo")
} }
variableName := expressionNode.Rhs.Operation.StringValue variableName := expressionNode.RHS.Operation.StringValue
prefs := expressionNode.Operation.Preferences.(assignVarPreferences) prefs := expressionNode.Operation.Preferences.(assignVarPreferences)

View File

@ -6,11 +6,11 @@ func withOperator(d *dataTreeNavigator, context Context, expressionNode *Express
log.Debugf("-- withOperator") log.Debugf("-- withOperator")
// with(path, exp) // with(path, exp)
if expressionNode.Rhs.Operation.OperationType != blockOpType { if expressionNode.RHS.Operation.OperationType != blockOpType {
return Context{}, fmt.Errorf("with must be given a block, got %v instead", expressionNode.Rhs.Operation.OperationType.Type) return Context{}, fmt.Errorf("with must be given a block, got %v instead", expressionNode.RHS.Operation.OperationType.Type)
} }
pathExp := expressionNode.Rhs.Lhs pathExp := expressionNode.RHS.LHS
updateContext, err := d.GetMatchingNodes(context, pathExp) updateContext, err := d.GetMatchingNodes(context, pathExp)
@ -18,7 +18,7 @@ func withOperator(d *dataTreeNavigator, context Context, expressionNode *Express
return Context{}, err return Context{}, err
} }
updateExp := expressionNode.Rhs.Rhs updateExp := expressionNode.RHS.RHS
_, err = d.GetMatchingNodes(updateContext, updateExp) _, err = d.GetMatchingNodes(updateContext, updateExp)
if err != nil { if err != nil {

View File

@ -13,7 +13,7 @@ type operatorHandler func(d *dataTreeNavigator, context Context, expressionNode
type compoundCalculation func(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode type compoundCalculation func(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode
func compoundAssignFunction(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, calculation compoundCalculation) (Context, error) { func compoundAssignFunction(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, calculation compoundCalculation) (Context, error) {
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -26,7 +26,7 @@ func compoundAssignFunction(d *dataTreeNavigator, context Context, expressionNod
valueOp.CandidateNode = candidate valueOp.CandidateNode = candidate
valueExpression := &ExpressionNode{Operation: valueOp} valueExpression := &ExpressionNode{Operation: valueOp}
assignmentOpNode := &ExpressionNode{Operation: assignmentOp, Lhs: valueExpression, Rhs: calculation(valueExpression, expressionNode.Rhs)} assignmentOpNode := &ExpressionNode{Operation: assignmentOp, LHS: valueExpression, RHS: calculation(valueExpression, expressionNode.RHS)}
_, err = d.GetMatchingNodes(context, assignmentOpNode) _, err = d.GetMatchingNodes(context, assignmentOpNode)
if err != nil { if err != nil {
@ -50,7 +50,7 @@ func emptyOperator(d *dataTreeNavigator, context Context, expressionNode *Expres
type crossFunctionCalculation func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) type crossFunctionCalculation func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error)
func resultsForRhs(d *dataTreeNavigator, context Context, lhsCandidate *CandidateNode, rhs Context, calculation crossFunctionCalculation, results *list.List, calcWhenEmpty bool) error { func resultsForRHS(d *dataTreeNavigator, context Context, lhsCandidate *CandidateNode, rhs Context, calculation crossFunctionCalculation, results *list.List, calcWhenEmpty bool) error {
if calcWhenEmpty && rhs.MatchingNodes.Len() == 0 { if calcWhenEmpty && rhs.MatchingNodes.Len() == 0 {
resultCandidate, err := calculation(d, context, lhsCandidate, nil) resultCandidate, err := calculation(d, context, lhsCandidate, nil)
@ -79,20 +79,20 @@ func resultsForRhs(d *dataTreeNavigator, context Context, lhsCandidate *Candidat
func doCrossFunc(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, calculation crossFunctionCalculation, calcWhenEmpty bool) (Context, error) { func doCrossFunc(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, calculation crossFunctionCalculation, calcWhenEmpty bool) (Context, error) {
var results = list.New() var results = list.New()
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
log.Debugf("crossFunction LHS len: %v", lhs.MatchingNodes.Len()) log.Debugf("crossFunction LHS len: %v", lhs.MatchingNodes.Len())
rhs, err := d.GetMatchingNodes(context, expressionNode.Rhs) rhs, err := d.GetMatchingNodes(context, expressionNode.RHS)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
if calcWhenEmpty && lhs.MatchingNodes.Len() == 0 { if calcWhenEmpty && lhs.MatchingNodes.Len() == 0 {
err := resultsForRhs(d, context, nil, rhs, calculation, results, calcWhenEmpty) err := resultsForRHS(d, context, nil, rhs, calculation, results, calcWhenEmpty)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -101,7 +101,7 @@ func doCrossFunc(d *dataTreeNavigator, context Context, expressionNode *Expressi
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() { for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
lhsCandidate := el.Value.(*CandidateNode) lhsCandidate := el.Value.(*CandidateNode)
err := resultsForRhs(d, context, lhsCandidate, rhs, calculation, results, calcWhenEmpty) err := resultsForRHS(d, context, lhsCandidate, rhs, calculation, results, calcWhenEmpty)
if err != nil { if err != nil {
return Context{}, err return Context{}, err
} }
@ -165,7 +165,7 @@ func createTraversalTree(path []interface{}, traversePrefs traversePreferences,
return &ExpressionNode{ return &ExpressionNode{
Operation: &Operation{OperationType: shortPipeOpType}, Operation: &Operation{OperationType: shortPipeOpType},
Lhs: createTraversalTree(path[0:1], traversePrefs, false), LHS: createTraversalTree(path[0:1], traversePrefs, false),
Rhs: createTraversalTree(path[1:], traversePrefs, targetKey), RHS: createTraversalTree(path[1:], traversePrefs, targetKey),
} }
} }

View File

@ -21,11 +21,11 @@ type PrinterOutputFormat uint32
const ( const (
YamlOutputFormat = 1 << iota YamlOutputFormat = 1 << iota
JsonOutputFormat JSONOutputFormat
PropsOutputFormat PropsOutputFormat
CsvOutputFormat CSVOutputFormat
TsvOutputFormat TSVOutputFormat
XmlOutputFormat XMLOutputFormat
) )
func OutputFormatFromString(format string) (PrinterOutputFormat, error) { func OutputFormatFromString(format string) (PrinterOutputFormat, error) {
@ -33,15 +33,15 @@ func OutputFormatFromString(format string) (PrinterOutputFormat, error) {
case "yaml", "y": case "yaml", "y":
return YamlOutputFormat, nil return YamlOutputFormat, nil
case "json", "j": case "json", "j":
return JsonOutputFormat, nil return JSONOutputFormat, nil
case "props", "p": case "props", "p":
return PropsOutputFormat, nil return PropsOutputFormat, nil
case "csv", "c": case "csv", "c":
return CsvOutputFormat, nil return CSVOutputFormat, nil
case "tsv", "t": case "tsv", "t":
return TsvOutputFormat, nil return TSVOutputFormat, nil
case "xml", "x": case "xml", "x":
return XmlOutputFormat, nil return XMLOutputFormat, nil
default: default:
return 0, fmt.Errorf("unknown format '%v' please use [yaml|json|props|csv|tsv|xml]", format) return 0, fmt.Errorf("unknown format '%v' please use [yaml|json|props|csv|tsv|xml]", format)
} }

View File

@ -314,7 +314,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) {
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
// note printDocSeparators is true, it should still not print document separators // note printDocSeparators is true, it should still not print document separators
// when outputing JSON. // when outputing JSON.
printer := NewPrinter(NewJsonEncoder(0), NewSinglePrinterWriter(writer)) printer := NewPrinter(NewJONEncoder(0), NewSinglePrinterWriter(writer))
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder()) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0, NewYamlDecoder())
if err != nil { if err != nil {

View File

@ -38,7 +38,7 @@ func NewMultiPrinterWriter(expression *ExpressionNode, format PrinterOutputForma
extension := "yml" extension := "yml"
switch format { switch format {
case JsonOutputFormat: case JSONOutputFormat:
extension = "json" extension = "json"
case PropsOutputFormat: case PropsOutputFormat:
extension = "properties" extension = "properties"

View File

@ -3,6 +3,7 @@ package yqlib
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"strings" "strings"
@ -12,14 +13,14 @@ import (
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
) )
func decodeXml(t *testing.T, s formatScenario) *CandidateNode { func decodeXML(t *testing.T, s formatScenario) *CandidateNode {
decoder := NewXmlDecoder("+", "+content") decoder := NewXMLDecoder("+", "+content")
decoder.Init(strings.NewReader(s.input)) decoder.Init(strings.NewReader(s.input))
node := &yaml.Node{} node := &yaml.Node{}
err := decoder.Decode(node) err := decoder.Decode(node)
if err != nil && err != io.EOF { if err != nil && !errors.Is(err, io.EOF) {
t.Error(err, "fail to decode", s.input) t.Error(err, "fail to decode", s.input)
} }
@ -47,15 +48,15 @@ func decodeXml(t *testing.T, s formatScenario) *CandidateNode {
return context.MatchingNodes.Front().Value.(*CandidateNode) return context.MatchingNodes.Front().Value.(*CandidateNode)
} }
func processXmlScenario(s formatScenario) string { func processXMLScenario(s formatScenario) string {
var output bytes.Buffer var output bytes.Buffer
writer := bufio.NewWriter(&output) writer := bufio.NewWriter(&output)
var encoder = NewXmlEncoder(2, "+", "+content") var encoder = NewXMLEncoder(2, "+", "+content")
var decoder = NewYamlDecoder() var decoder = NewYamlDecoder()
if s.scenarioType == "roundtrip" { if s.scenarioType == "roundtrip" {
decoder = NewXmlDecoder("+", "+content") decoder = NewXMLDecoder("+", "+content")
} }
inputs, err := readDocuments(strings.NewReader(s.input), "sample.yml", 0, decoder) inputs, err := readDocuments(strings.NewReader(s.input), "sample.yml", 0, decoder)
@ -83,7 +84,7 @@ type formatScenario struct {
scenarioType string scenarioType string
} }
var inputXmlWithComments = ` var inputXMLWithComments = `
<!-- before cat --> <!-- before cat -->
<cat> <cat>
<!-- in cat before --> <!-- in cat before -->
@ -101,7 +102,7 @@ for x --></x>
</cat> </cat>
<!-- after cat --> <!-- after cat -->
` `
var inputXmlWithCommentsWithSubChild = ` var inputXMLWithCommentsWithSubChild = `
<!-- before cat --> <!-- before cat -->
<cat> <cat>
<!-- in cat before --> <!-- in cat before -->
@ -140,7 +141,7 @@ cat:
# after cat # after cat
` `
var inputXmlWithCommentsWithArray = ` var inputXMLWithCommentsWithArray = `
<!-- before cat --> <!-- before cat -->
<cat> <cat>
<!-- in cat before --> <!-- in cat before -->
@ -201,7 +202,7 @@ cat:
# after cat # after cat
` `
var expectedRoundtripXmlWithComments = `<!-- before cat --><cat><!-- in cat before --> var expectedRoundtripXMLWithComments = `<!-- before cat --><cat><!-- in cat before -->
<x>3<!-- multi <x>3<!-- multi
line comment line comment
for x --></x><!-- before y --> for x --></x><!-- before y -->
@ -222,7 +223,7 @@ cat: # inline_cat
# below_cat # below_cat
` `
var expectedXmlWithComments = `<!-- above_cat inline_cat --><cat><!-- above_array inline_array --> var expectedXMLWithComments = `<!-- above_cat inline_cat --><cat><!-- above_array inline_array -->
<array>val1<!-- inline_val1 --></array> <array>val1<!-- inline_val1 --></array>
<array><!-- above_val2 -->val2<!-- inline_val2 --></array> <array><!-- above_val2 -->val2<!-- inline_val2 --></array>
</cat><!-- below_cat --> </cat><!-- below_cat -->
@ -263,7 +264,7 @@ var xmlScenarios = []formatScenario{
{ {
description: "Parse xml: with comments", description: "Parse xml: with comments",
subdescription: "A best attempt is made to preserve comments.", subdescription: "A best attempt is made to preserve comments.",
input: inputXmlWithComments, input: inputXMLWithComments,
expected: expectedDecodeYamlWithComments, expected: expectedDecodeYamlWithComments,
scenarioType: "decode", scenarioType: "decode",
}, },
@ -298,14 +299,14 @@ var xmlScenarios = []formatScenario{
{ {
description: "Parse xml: with comments subchild", description: "Parse xml: with comments subchild",
skipDoc: true, skipDoc: true,
input: inputXmlWithCommentsWithSubChild, input: inputXMLWithCommentsWithSubChild,
expected: expectedDecodeYamlWithSubChild, expected: expectedDecodeYamlWithSubChild,
scenarioType: "decode", scenarioType: "decode",
}, },
{ {
description: "Parse xml: with comments array", description: "Parse xml: with comments array",
skipDoc: true, skipDoc: true,
input: inputXmlWithCommentsWithArray, input: inputXMLWithCommentsWithArray,
expected: expectedDecodeYamlWithArray, expected: expectedDecodeYamlWithArray,
scenarioType: "decode", scenarioType: "decode",
}, },
@ -345,44 +346,44 @@ var xmlScenarios = []formatScenario{
description: "Encode xml: comments", description: "Encode xml: comments",
subdescription: "A best attempt is made to copy comments to xml.", subdescription: "A best attempt is made to copy comments to xml.",
input: yamlWithComments, input: yamlWithComments,
expected: expectedXmlWithComments, expected: expectedXMLWithComments,
scenarioType: "encode", scenarioType: "encode",
}, },
{ {
description: "Round trip: with comments", description: "Round trip: with comments",
subdescription: "A best effort is made, but comment positions and white space are not preserved perfectly.", subdescription: "A best effort is made, but comment positions and white space are not preserved perfectly.",
input: inputXmlWithComments, input: inputXMLWithComments,
expected: expectedRoundtripXmlWithComments, expected: expectedRoundtripXMLWithComments,
scenarioType: "roundtrip", scenarioType: "roundtrip",
}, },
} }
func testXmlScenario(t *testing.T, s formatScenario) { func testXMLScenario(t *testing.T, s formatScenario) {
if s.scenarioType == "encode" || s.scenarioType == "roundtrip" { if s.scenarioType == "encode" || s.scenarioType == "roundtrip" {
test.AssertResultWithContext(t, s.expected, processXmlScenario(s), s.description) test.AssertResultWithContext(t, s.expected, processXMLScenario(s), s.description)
} else { } else {
var actual = resultToString(t, decodeXml(t, s)) var actual = resultToString(t, decodeXML(t, s))
test.AssertResultWithContext(t, s.expected, actual, s.description) test.AssertResultWithContext(t, s.expected, actual, s.description)
} }
} }
func documentXmlScenario(t *testing.T, w *bufio.Writer, i interface{}) { func documentXMLScenario(t *testing.T, w *bufio.Writer, i interface{}) {
s := i.(formatScenario) s := i.(formatScenario)
if s.skipDoc { if s.skipDoc {
return return
} }
if s.scenarioType == "encode" { if s.scenarioType == "encode" {
documentXmlEncodeScenario(w, s) documentXMLEncodeScenario(w, s)
} else if s.scenarioType == "roundtrip" { } else if s.scenarioType == "roundtrip" {
documentXmlRoundTripScenario(w, s) documentXMLRoundTripScenario(w, s)
} else { } else {
documentXmlDecodeScenario(t, w, s) documentXMLDecodeScenario(t, w, s)
} }
} }
func documentXmlDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario) { func documentXMLDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario) {
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
if s.subdescription != "" { if s.subdescription != "" {
@ -404,7 +405,7 @@ func documentXmlDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario)
var output bytes.Buffer var output bytes.Buffer
printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), YamlOutputFormat, true, false, 2, true) printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), YamlOutputFormat, true, false, 2, true)
node := decodeXml(t, s) node := decodeXML(t, s)
err := printer.PrintResults(node.AsList()) err := printer.PrintResults(node.AsList())
if err != nil { if err != nil {
@ -415,7 +416,7 @@ func documentXmlDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario)
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", output.String())) writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", output.String()))
} }
func documentXmlEncodeScenario(w *bufio.Writer, s formatScenario) { func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
if s.subdescription != "" { if s.subdescription != "" {
@ -430,10 +431,10 @@ func documentXmlEncodeScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, "```bash\nyq -o=xml '.' sample.yml\n```\n") writeOrPanic(w, "```bash\nyq -o=xml '.' sample.yml\n```\n")
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processXmlScenario(s))) writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processXMLScenario(s)))
} }
func documentXmlRoundTripScenario(w *bufio.Writer, s formatScenario) { func documentXMLRoundTripScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
if s.subdescription != "" { if s.subdescription != "" {
@ -448,16 +449,16 @@ func documentXmlRoundTripScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, "```bash\nyq -p=xml -o=xml '.' sample.xml\n```\n") writeOrPanic(w, "```bash\nyq -p=xml -o=xml '.' sample.xml\n```\n")
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processXmlScenario(s))) writeOrPanic(w, fmt.Sprintf("```xml\n%v```\n\n", processXMLScenario(s)))
} }
func TestXmlScenarios(t *testing.T) { func TestXMLScenarios(t *testing.T) {
for _, tt := range xmlScenarios { for _, tt := range xmlScenarios {
testXmlScenario(t, tt) testXMLScenario(t, tt)
} }
genericScenarios := make([]interface{}, len(xmlScenarios)) genericScenarios := make([]interface{}, len(xmlScenarios))
for i, s := range xmlScenarios { for i, s := range xmlScenarios {
genericScenarios[i] = s genericScenarios[i] = s
} }
documentScenarios(t, "usage", "xml", genericScenarios, documentXmlScenario) documentScenarios(t, "usage", "xml", genericScenarios, documentXMLScenario)
} }

View File

@ -6,32 +6,13 @@ import (
"fmt" "fmt"
"os" "os"
"reflect" "reflect"
"strings"
"testing" "testing"
"github.com/pkg/diff" "github.com/pkg/diff"
"github.com/pkg/diff/write" "github.com/pkg/diff/write"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v3" yaml "gopkg.in/yaml.v3"
) )
type resulter struct {
Error error
Output string
Command *cobra.Command
}
func RunCmd(c *cobra.Command, input string) resulter {
buf := new(bytes.Buffer)
c.SetOutput(buf)
c.SetArgs(strings.Split(input, " "))
err := c.Execute()
output := buf.String()
return resulter{err, output, c}
}
func ParseData(rawData string) yaml.Node { func ParseData(rawData string) yaml.Node {
var parsedData yaml.Node var parsedData yaml.Node
err := yaml.Unmarshal([]byte(rawData), &parsedData) err := yaml.Unmarshal([]byte(rawData), &parsedData)