From 26356ff4be633ccf837492b75b653ec9ca4a5947 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 7 Feb 2022 11:55:55 +1100 Subject: [PATCH] Added github action fix for parsing xml, updated linter --- acceptance_tests/basic.sh | 2 +- cmd/commands_test.go | 83 --------------------------- cmd/root.go | 4 +- cmd/utils.go | 16 +++--- pkg/yqlib/decoder_xml.go | 10 ++-- pkg/yqlib/encoder_json.go | 2 +- pkg/yqlib/encoder_properties_test.go | 20 +++---- pkg/yqlib/encoder_test.go | 30 +++++----- pkg/yqlib/encoder_xml.go | 4 +- pkg/yqlib/expression_parser.go | 10 ++-- pkg/yqlib/expression_tokeniser.go | 42 +++++++------- pkg/yqlib/json_test.go | 28 ++++----- pkg/yqlib/lib.go | 6 +- pkg/yqlib/operator_add.go | 12 ++-- pkg/yqlib/operator_anchors_aliases.go | 14 ++--- pkg/yqlib/operator_assign.go | 8 +-- pkg/yqlib/operator_booleans.go | 4 +- pkg/yqlib/operator_collect.go | 4 +- pkg/yqlib/operator_comments.go | 6 +- pkg/yqlib/operator_delete.go | 2 +- pkg/yqlib/operator_encoder_decoder.go | 22 +++---- pkg/yqlib/operator_entries.go | 4 +- pkg/yqlib/operator_eval.go | 2 +- pkg/yqlib/operator_group_by.go | 2 +- pkg/yqlib/operator_has.go | 2 +- pkg/yqlib/operator_load.go | 2 +- pkg/yqlib/operator_map.go | 6 +- pkg/yqlib/operator_multiply.go | 8 +-- pkg/yqlib/operator_pipe.go | 4 +- pkg/yqlib/operator_reduce.go | 16 +++--- pkg/yqlib/operator_select.go | 2 +- pkg/yqlib/operator_sort.go | 4 +- pkg/yqlib/operator_sort_keys.go | 2 +- pkg/yqlib/operator_strings.go | 20 +++---- pkg/yqlib/operator_style.go | 6 +- pkg/yqlib/operator_subtract.go | 10 ++-- pkg/yqlib/operator_tag.go | 6 +- pkg/yqlib/operator_traverse_path.go | 4 +- pkg/yqlib/operator_union.go | 6 +- pkg/yqlib/operator_unique.go | 4 +- pkg/yqlib/operator_variables.go | 6 +- pkg/yqlib/operator_with.go | 8 +-- pkg/yqlib/operators.go | 18 +++--- pkg/yqlib/printer.go | 16 +++--- pkg/yqlib/printer_test.go | 2 +- pkg/yqlib/printer_writer.go | 2 +- pkg/yqlib/xml_test.go | 67 ++++++++++----------- test/utils.go | 19 ------ 48 files changed, 238 insertions(+), 339 deletions(-) delete mode 100644 cmd/commands_test.go diff --git a/acceptance_tests/basic.sh b/acceptance_tests/basic.sh index 01ef9c13..be9d8f6e 100755 --- a/acceptance_tests/basic.sh +++ b/acceptance_tests/basic.sh @@ -2,7 +2,7 @@ setUp() { rm test*.yml || true - rm .xyz -f + rm .xyz -f || true } testBasicEvalRoundTrip() { diff --git a/cmd/commands_test.go b/cmd/commands_test.go deleted file mode 100644 index f6d79256..00000000 --- a/cmd/commands_test.go +++ /dev/null @@ -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.") -// } -// } diff --git a/cmd/root.go b/cmd/root.go index 5fa07c28..6d4758da 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -49,8 +49,8 @@ yq -i '.stuff = "foo"' myfile.yml # update myfile.yml inplace logging.SetBackend(backend) yqlib.InitExpressionParser() - yqlib.XmlPreferences.AttributePrefix = xmlAttributePrefix - yqlib.XmlPreferences.ContentName = xmlContentName + yqlib.XMLPreferences.AttributePrefix = xmlAttributePrefix + yqlib.XMLPreferences.ContentName = xmlContentName }, } diff --git a/cmd/utils.go b/cmd/utils.go index 83b21d2d..7381b956 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -52,8 +52,8 @@ func configureDecoder() (yqlib.Decoder, error) { return nil, err } switch yqlibInputFormat { - case yqlib.XmlInputFormat: - return yqlib.NewXmlDecoder(xmlAttributePrefix, xmlContentName), nil + case yqlib.XMLInputFormat: + return yqlib.NewXMLDecoder(xmlAttributePrefix, xmlContentName), 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 { switch format { - case yqlib.JsonOutputFormat: - return yqlib.NewJsonEncoder(indent) + case yqlib.JSONOutputFormat: + return yqlib.NewJONEncoder(indent) case yqlib.PropsOutputFormat: return yqlib.NewPropertiesEncoder() - case yqlib.CsvOutputFormat: + case yqlib.CSVOutputFormat: return yqlib.NewCsvEncoder(',') - case yqlib.TsvOutputFormat: + case yqlib.TSVOutputFormat: return yqlib.NewCsvEncoder('\t') case yqlib.YamlOutputFormat: return yqlib.NewYamlEncoder(indent, colorsEnabled, !noDocSeparators, unwrapScalar) - case yqlib.XmlOutputFormat: - return yqlib.NewXmlEncoder(indent, xmlAttributePrefix, xmlContentName) + case yqlib.XMLOutputFormat: + return yqlib.NewXMLEncoder(indent, xmlAttributePrefix, xmlContentName) } panic("invalid encoder") } diff --git a/pkg/yqlib/decoder_xml.go b/pkg/yqlib/decoder_xml.go index 04c8f9e5..7db03ffa 100644 --- a/pkg/yqlib/decoder_xml.go +++ b/pkg/yqlib/decoder_xml.go @@ -15,7 +15,7 @@ type InputFormat uint const ( YamlInputFormat = 1 << iota - XmlInputFormat + XMLInputFormat ) func InputFormatFromString(format string) (InputFormat, error) { @@ -23,7 +23,7 @@ func InputFormatFromString(format string) (InputFormat, error) { case "yaml", "y": return YamlInputFormat, nil case "xml", "x": - return XmlInputFormat, nil + return XMLInputFormat, nil default: return 0, fmt.Errorf("unknown format '%v' please use [yaml|xml]", format) } @@ -36,7 +36,7 @@ type xmlDecoder struct { finished bool } -func NewXmlDecoder(attributePrefix string, contentName string) Decoder { +func NewXMLDecoder(attributePrefix string, contentName string) Decoder { if contentName == "" { contentName = "content" } @@ -143,7 +143,7 @@ func (dec *xmlDecoder) Decode(rootYamlNode *yaml.Node) error { } root := &xmlNode{} // cant use xj - it doesn't keep map order. - err := dec.decodeXml(root) + err := dec.decodeXML(root) if err != nil { return err @@ -205,7 +205,7 @@ type element struct { // this code is heavily based on https://github.com/basgys/goxml2json // main changes are to decode into a structure that preserves the original order // of the map keys. -func (dec *xmlDecoder) decodeXml(root *xmlNode) error { +func (dec *xmlDecoder) decodeXML(root *xmlNode) error { xmlDec := xml.NewDecoder(dec.reader) // That will convert the charset if the provided XML is non-UTF-8 diff --git a/pkg/yqlib/encoder_json.go b/pkg/yqlib/encoder_json.go index cfe96e9e..e76a0180 100644 --- a/pkg/yqlib/encoder_json.go +++ b/pkg/yqlib/encoder_json.go @@ -26,7 +26,7 @@ func mapKeysToStrings(node *yaml.Node) { } } -func NewJsonEncoder(indent int) Encoder { +func NewJONEncoder(indent int) Encoder { var indentString = "" for index := 0; index < indent; index++ { diff --git a/pkg/yqlib/encoder_properties_test.go b/pkg/yqlib/encoder_properties_test.go index 97e1c62b..647010a6 100644 --- a/pkg/yqlib/encoder_properties_test.go +++ b/pkg/yqlib/encoder_properties_test.go @@ -31,18 +31,18 @@ func yamlToProps(sampleYaml string) string { func TestPropertiesEncoderSimple(t *testing.T) { var sampleYaml = `a: 'bob cool'` - var expectedJson = `a = bob cool` + var expectedProps = `a = bob cool` var actualProps = yamlToProps(sampleYaml) - test.AssertResult(t, expectedJson, actualProps) + test.AssertResult(t, expectedProps, actualProps) } func TestPropertiesEncoderSimpleWithComments(t *testing.T) { var sampleYaml = `a: 'bob cool' # line` - var expectedJson = `# line + var expectedProps = `# line a = bob cool` var actualProps = yamlToProps(sampleYaml) - test.AssertResult(t, expectedJson, actualProps) + test.AssertResult(t, expectedProps, actualProps) } func TestPropertiesEncoderDeep(t *testing.T) { @@ -50,9 +50,9 @@ func TestPropertiesEncoderDeep(t *testing.T) { b: "bob cool" ` - var expectedJson = `a.b = bob cool` + var expectedProps = `a.b = bob cool` var actualProps = yamlToProps(sampleYaml) - test.AssertResult(t, expectedJson, actualProps) + test.AssertResult(t, expectedProps, actualProps) } func TestPropertiesEncoderDeepWithComments(t *testing.T) { @@ -60,10 +60,10 @@ func TestPropertiesEncoderDeepWithComments(t *testing.T) { b: "bob cool" # b thing ` - var expectedJson = `# b thing + var expectedProps = `# b thing a.b = bob cool` var actualProps = yamlToProps(sampleYaml) - test.AssertResult(t, expectedJson, actualProps) + test.AssertResult(t, expectedProps, actualProps) } func TestPropertiesEncoderArray(t *testing.T) { @@ -71,8 +71,8 @@ func TestPropertiesEncoderArray(t *testing.T) { b: [{c: dog}, {c: cat}] ` - var expectedJson = `a.b.0.c = dog + var expectedProps = `a.b.0.c = dog a.b.1.c = cat` var actualProps = yamlToProps(sampleYaml) - test.AssertResult(t, expectedJson, actualProps) + test.AssertResult(t, expectedProps, actualProps) } diff --git a/pkg/yqlib/encoder_test.go b/pkg/yqlib/encoder_test.go index e21fe695..5e616a65 100644 --- a/pkg/yqlib/encoder_test.go +++ b/pkg/yqlib/encoder_test.go @@ -9,11 +9,11 @@ import ( "github.com/mikefarah/yq/v4/test" ) -func yamlToJson(sampleYaml string, indent int) string { +func yamlToJSON(sampleYaml string, indent int) string { var output bytes.Buffer writer := bufio.NewWriter(&output) - var jsonEncoder = NewJsonEncoder(indent) + var jsonEncoder = NewJONEncoder(indent) inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder()) if err != nil { panic(err) @@ -28,13 +28,13 @@ func yamlToJson(sampleYaml string, indent int) string { return strings.TrimSuffix(output.String(), "\n") } -func TestJsonEncoderPreservesObjectOrder(t *testing.T) { +func TestJSONEncoderPreservesObjectOrder(t *testing.T) { var sampleYaml = `zabbix: winner apple: great banana: - {cobra: kai, angus: bob} ` - var expectedJson = `{ + var expectedJSON = `{ "zabbix": "winner", "apple": "great", "banana": [ @@ -44,31 +44,31 @@ banana: } ] }` - var actualJson = yamlToJson(sampleYaml, 2) - test.AssertResult(t, expectedJson, actualJson) + var actualJSON = yamlToJSON(sampleYaml, 2) + test.AssertResult(t, expectedJSON, actualJSON) } func TestJsonNullInArray(t *testing.T) { var sampleYaml = `[null]` - var actualJson = yamlToJson(sampleYaml, 0) - test.AssertResult(t, sampleYaml, actualJson) + var actualJSON = yamlToJSON(sampleYaml, 0) + test.AssertResult(t, sampleYaml, actualJSON) } func TestJsonNull(t *testing.T) { var sampleYaml = `null` - var actualJson = yamlToJson(sampleYaml, 0) - test.AssertResult(t, sampleYaml, actualJson) + var actualJSON = yamlToJSON(sampleYaml, 0) + test.AssertResult(t, sampleYaml, actualJSON) } func TestJsonNullInObject(t *testing.T) { var sampleYaml = `{x: null}` - var actualJson = yamlToJson(sampleYaml, 0) - test.AssertResult(t, `{"x":null}`, actualJson) + var actualJSON = yamlToJSON(sampleYaml, 0) + test.AssertResult(t, `{"x":null}`, actualJSON) } func TestJsonEncoderDoesNotEscapeHTMLChars(t *testing.T) { var sampleYaml = `build: "( ./lint && ./format && ./compile ) < src.code"` - var expectedJson = `{"build":"( ./lint && ./format && ./compile ) < src.code"}` - var actualJson = yamlToJson(sampleYaml, 0) - test.AssertResult(t, expectedJson, actualJson) + var expectedJSON = `{"build":"( ./lint && ./format && ./compile ) < src.code"}` + var actualJSON = yamlToJSON(sampleYaml, 0) + test.AssertResult(t, expectedJSON, actualJSON) } diff --git a/pkg/yqlib/encoder_xml.go b/pkg/yqlib/encoder_xml.go index 7914b8d4..ee5369d7 100644 --- a/pkg/yqlib/encoder_xml.go +++ b/pkg/yqlib/encoder_xml.go @@ -9,7 +9,7 @@ import ( yaml "gopkg.in/yaml.v3" ) -var XmlPreferences = xmlPreferences{AttributePrefix: "+", ContentName: "+content"} +var XMLPreferences = xmlPreferences{AttributePrefix: "+", ContentName: "+content"} type xmlEncoder struct { attributePrefix string @@ -17,7 +17,7 @@ type xmlEncoder struct { indentString string } -func NewXmlEncoder(indent int, attributePrefix string, contentName string) Encoder { +func NewXMLEncoder(indent int, attributePrefix string, contentName string) Encoder { var indentString = "" for index := 0; index < indent; index++ { diff --git a/pkg/yqlib/expression_parser.go b/pkg/yqlib/expression_parser.go index 3d578fac..26caf933 100644 --- a/pkg/yqlib/expression_parser.go +++ b/pkg/yqlib/expression_parser.go @@ -7,8 +7,8 @@ import ( type ExpressionNode struct { Operation *Operation - Lhs *ExpressionNode - Rhs *ExpressionNode + LHS *ExpressionNode + RHS *ExpressionNode } 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)) } remaining, rhs := stack[:len(stack)-1], stack[len(stack)-1] - newNode.Rhs = rhs + newNode.RHS = rhs stack = remaining } else if numArgs == 2 { if len(stack) < 2 { 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] - newNode.Lhs = lhs - newNode.Rhs = rhs + newNode.LHS = lhs + newNode.RHS = rhs stack = remaining } } diff --git a/pkg/yqlib/expression_tokeniser.go b/pkg/yqlib/expression_tokeniser.go index 2662a57e..067f25e1 100644 --- a/pkg/yqlib/expression_tokeniser.go +++ b/pkg/yqlib/expression_tokeniser.go @@ -327,53 +327,53 @@ func initLexer() (*lex.Lexer, error) { lexer.Add([]byte(`toyaml\([0-9]+\)`), encodeWithIndent(YamlOutputFormat)) lexer.Add([]byte(`to_yaml\([0-9]+\)`), encodeWithIndent(YamlOutputFormat)) - lexer.Add([]byte(`toxml\([0-9]+\)`), encodeWithIndent(XmlOutputFormat)) - lexer.Add([]byte(`to_xml\([0-9]+\)`), encodeWithIndent(XmlOutputFormat)) + lexer.Add([]byte(`toxml\([0-9]+\)`), encodeWithIndent(XMLOutputFormat)) + lexer.Add([]byte(`to_xml\([0-9]+\)`), encodeWithIndent(XMLOutputFormat)) - lexer.Add([]byte(`tojson\([0-9]+\)`), encodeWithIndent(JsonOutputFormat)) - lexer.Add([]byte(`to_json\([0-9]+\)`), encodeWithIndent(JsonOutputFormat)) + lexer.Add([]byte(`tojson\([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(`to_yaml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: YamlOutputFormat, indent: 2})) // 0 indent doesn't work with yaml. 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(`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(`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(`@json`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: JSONOutputFormat, indent: 0})) 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(`@props`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: PropsOutputFormat, indent: 2})) - lexer.Add([]byte(`tocsv`), 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(`tocsv`), 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(`totsv`), 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(`totsv`), 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(`toxml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: XmlOutputFormat})) - 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(`toxml`), opTokenWithPrefs(encodeOpType, nil, encoderPreferences{format: XMLOutputFormat})) + 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(`fromyaml`), 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_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(`sort_keys`), opToken(sortKeysOpType)) 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(`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(`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(`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(`load_str`), opTokenWithPrefs(loadOpType, nil, loadPrefs{loadAsString: true})) diff --git a/pkg/yqlib/json_test.go b/pkg/yqlib/json_test.go index 0680e09b..2a3cd65a 100644 --- a/pkg/yqlib/json_test.go +++ b/pkg/yqlib/json_test.go @@ -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) if err != nil { @@ -96,11 +96,11 @@ func decodeJson(t *testing.T, jsonString string) *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" { - 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 { - var actual = resultToString(t, decodeJson(t, s.input)) + var actual = resultToString(t, decodeJSON(t, s.input)) 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)) if s.subdescription != "" { @@ -163,7 +163,7 @@ func documentJsonDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario) var output bytes.Buffer 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()) 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())) } -func documentJsonScenario(t *testing.T, w *bufio.Writer, i interface{}) { +func documentJSONScenario(t *testing.T, w *bufio.Writer, i interface{}) { s := i.(formatScenario) if s.skipDoc { return } if s.scenarioType == "encode" { - documentJsonEncodeScenario(w, s) + documentJSONEncodeScenario(w, s) } 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)) if s.subdescription != "" { @@ -212,16 +212,16 @@ func documentJsonEncodeScenario(w *bufio.Writer, s formatScenario) { } 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 { - testJsonScenario(t, tt) + testJSONScenario(t, tt) } genericScenarios := make([]interface{}, len(jsonScenarios)) for i, s := range jsonScenarios { genericScenarios[i] = s } - documentScenarios(t, "usage", "convert", genericScenarios, documentJsonScenario) + documentScenarios(t, "usage", "convert", genericScenarios, documentJSONScenario) } diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 2725880e..2e80c0b7 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -193,9 +193,9 @@ func recurseNodeObjectEqual(lhs *yaml.Node, rhs *yaml.Node) bool { key := lhs.Content[index] 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 } } @@ -289,7 +289,7 @@ func footComment(node *yaml.Node) string { } func createValueOperation(value interface{}, stringValue string) *Operation { - var node *yaml.Node = createScalarNode(value, stringValue) + var node = createScalarNode(value, stringValue) return &Operation{ OperationType: valueOpType, diff --git a/pkg/yqlib/operator_add.go b/pkg/yqlib/operator_add.go index 8e678f0d..d1ba7d32 100644 --- a/pkg/yqlib/operator_add.go +++ b/pkg/yqlib/operator_add.go @@ -10,8 +10,8 @@ import ( func createAddOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode { return &ExpressionNode{Operation: &Operation{OperationType: addOpType}, - Lhs: lhs, - Rhs: rhs} + LHS: lhs, + RHS: rhs} } 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] value := rhs.Content[index+1] log.Debug("finding %v", key.Value) - indexInLhs := findInArray(target.Node, key) - log.Debug("indexInLhs %v", indexInLhs) - if indexInLhs < 0 { + indexInLHS := findInArray(target.Node, key) + log.Debug("indexInLhs %v", indexInLHS) + if indexInLHS < 0 { // not in there, append it target.Node.Content = append(target.Node.Content, key, value) } else { // it's there, replace it - target.Node.Content[indexInLhs+1] = value + target.Node.Content[indexInLHS+1] = value } } target.Node.Kind = yaml.MappingNode diff --git a/pkg/yqlib/operator_anchors_aliases.go b/pkg/yqlib/operator_anchors_aliases.go index 6e7ef987..9d512bcd 100644 --- a/pkg/yqlib/operator_anchors_aliases.go +++ b/pkg/yqlib/operator_anchors_aliases.go @@ -12,7 +12,7 @@ func assignAliasOperator(d *dataTreeNavigator, context Context, expressionNode * aliasName := "" if !expressionNode.Operation.UpdateAssign { - rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) + rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS) if err != nil { 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 { return Context{}, err @@ -32,7 +32,7 @@ func assignAliasOperator(d *dataTreeNavigator, context Context, expressionNode * log.Debugf("Setting aliasName : %v", candidate.GetKey()) 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 { return Context{}, err } @@ -68,7 +68,7 @@ func assignAnchorOperator(d *dataTreeNavigator, context Context, expressionNode anchorName := "" if !expressionNode.Operation.UpdateAssign { - rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) + rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS) if err != nil { 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 { return Context{}, err @@ -89,7 +89,7 @@ func assignAnchorOperator(d *dataTreeNavigator, context Context, expressionNode log.Debugf("Setting anchorName of : %v", candidate.GetKey()) 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 { 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() { 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 { return Context{}, err diff --git a/pkg/yqlib/operator_assign.go b/pkg/yqlib/operator_assign.go index b120c5f8..1e1058ed 100644 --- a/pkg/yqlib/operator_assign.go +++ b/pkg/yqlib/operator_assign.go @@ -16,7 +16,7 @@ func assignUpdateFunc(prefs assignPreferences) crossFunctionCalculation { } 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 { return Context{}, err } @@ -35,7 +35,7 @@ func assignUpdateOperator(d *dataTreeNavigator, context Context, expressionNode for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() { 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 { return Context{}, err @@ -57,14 +57,14 @@ func assignUpdateOperator(d *dataTreeNavigator, context Context, expressionNode // does not update content or values func assignAttributesOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { 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 { return Context{}, err } for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() { 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 { return Context{}, err diff --git a/pkg/yqlib/operator_booleans.go b/pkg/yqlib/operator_booleans.go index b8f37ac3..cbc558c9 100644 --- a/pkg/yqlib/operator_booleans.go +++ b/pkg/yqlib/operator_booleans.go @@ -103,7 +103,7 @@ func allOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi if candidateNode.Kind != yaml.SequenceNode { 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 { return Context{}, err } @@ -122,7 +122,7 @@ func anyOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi if candidateNode.Kind != yaml.SequenceNode { 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 { return Context{}, err } diff --git a/pkg/yqlib/operator_collect.go b/pkg/yqlib/operator_collect.go index 764359ec..0122d1ce 100644 --- a/pkg/yqlib/operator_collect.go +++ b/pkg/yqlib/operator_collect.go @@ -41,7 +41,7 @@ func collectOperator(d *dataTreeNavigator, context Context, expressionNode *Expr } if evaluateAllTogether { - collectedNode, err := collectTogether(d, context, expressionNode.Rhs) + collectedNode, err := collectTogether(d, context, expressionNode.RHS) if err != nil { return Context{}, err } @@ -56,7 +56,7 @@ func collectOperator(d *dataTreeNavigator, context Context, expressionNode *Expr collectedNode := &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"} 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 { return Context{}, err } diff --git a/pkg/yqlib/operator_comments.go b/pkg/yqlib/operator_comments.go index 50a88a82..23aa5c6a 100644 --- a/pkg/yqlib/operator_comments.go +++ b/pkg/yqlib/operator_comments.go @@ -19,7 +19,7 @@ func assignCommentsOperator(d *dataTreeNavigator, context Context, expressionNod log.Debugf("AssignComments operator!") - lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) + lhs, err := d.GetMatchingNodes(context, expressionNode.LHS) if err != nil { return Context{}, err @@ -29,7 +29,7 @@ func assignCommentsOperator(d *dataTreeNavigator, context Context, expressionNod comment := "" if !expressionNode.Operation.UpdateAssign { - rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) + rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS) if err != nil { return Context{}, err } @@ -43,7 +43,7 @@ func assignCommentsOperator(d *dataTreeNavigator, context Context, expressionNod candidate := el.Value.(*CandidateNode) 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 { return Context{}, err } diff --git a/pkg/yqlib/operator_delete.go b/pkg/yqlib/operator_delete.go index 39adaf23..aa38960e 100644 --- a/pkg/yqlib/operator_delete.go +++ b/pkg/yqlib/operator_delete.go @@ -7,7 +7,7 @@ import ( ) 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 { return Context{}, err diff --git a/pkg/yqlib/operator_encoder_decoder.go b/pkg/yqlib/operator_encoder_decoder.go index 940c87c4..0f27a31c 100644 --- a/pkg/yqlib/operator_encoder_decoder.go +++ b/pkg/yqlib/operator_encoder_decoder.go @@ -12,18 +12,18 @@ import ( func configureEncoder(format PrinterOutputFormat, indent int) Encoder { switch format { - case JsonOutputFormat: - return NewJsonEncoder(indent) + case JSONOutputFormat: + return NewJONEncoder(indent) case PropsOutputFormat: return NewPropertiesEncoder() - case CsvOutputFormat: + case CSVOutputFormat: return NewCsvEncoder(',') - case TsvOutputFormat: + case TSVOutputFormat: return NewCsvEncoder('\t') case YamlOutputFormat: return NewYamlEncoder(indent, false, true, true) - case XmlOutputFormat: - return NewXmlEncoder(indent, XmlPreferences.AttributePrefix, XmlPreferences.ContentName) + case XMLOutputFormat: + return NewXMLEncoder(indent, XMLPreferences.AttributePrefix, XMLPreferences.ContentName) } 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. - if (preferences.format == JsonOutputFormat && preferences.indent == 0) || - preferences.format == CsvOutputFormat || - preferences.format == TsvOutputFormat { + if (preferences.format == JSONOutputFormat && preferences.indent == 0) || + preferences.format == CSVOutputFormat || + preferences.format == TSVOutputFormat { stringValue = chomper.ReplaceAllString(stringValue, "") } @@ -101,8 +101,8 @@ func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre switch preferences.format { case YamlInputFormat: decoder = NewYamlDecoder() - case XmlInputFormat: - decoder = NewXmlDecoder(XmlPreferences.AttributePrefix, XmlPreferences.ContentName) + case XMLInputFormat: + decoder = NewXMLDecoder(XMLPreferences.AttributePrefix, XMLPreferences.ContentName) } var results = list.New() diff --git a/pkg/yqlib/operator_entries.go b/pkg/yqlib/operator_entries.go index d3d8bc3d..8f4285e9 100644 --- a/pkg/yqlib/operator_entries.go +++ b/pkg/yqlib/operator_entries.go @@ -148,8 +148,8 @@ func withEntriesOperator(d *dataTreeNavigator, context Context, expressionNode * return Context{}, err } - result, err := d.GetMatchingNodes(splatted, expressionNode.Rhs) - log.Debug("expressionNode.Rhs %v", expressionNode.Rhs.Operation.OperationType) + result, err := d.GetMatchingNodes(splatted, expressionNode.RHS) + log.Debug("expressionNode.Rhs %v", expressionNode.RHS.Operation.OperationType) log.Debug("result %v", result) if err != nil { return Context{}, err diff --git a/pkg/yqlib/operator_eval.go b/pkg/yqlib/operator_eval.go index 27aa77a8..78037e97 100644 --- a/pkg/yqlib/operator_eval.go +++ b/pkg/yqlib/operator_eval.go @@ -6,7 +6,7 @@ import ( func evalOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { log.Debugf("Eval") - pathExpStrResults, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) + pathExpStrResults, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS) if err != nil { return Context{}, err } diff --git a/pkg/yqlib/operator_group_by.go b/pkg/yqlib/operator_group_by.go index 3acc8fa9..3bb70b50 100644 --- a/pkg/yqlib/operator_group_by.go +++ b/pkg/yqlib/operator_group_by.go @@ -50,7 +50,7 @@ func groupBy(d *dataTreeNavigator, context Context, expressionNode *ExpressionNo 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 { return Context{}, err diff --git a/pkg/yqlib/operator_has.go b/pkg/yqlib/operator_has.go index a8a68d55..01d7a1fe 100644 --- a/pkg/yqlib/operator_has.go +++ b/pkg/yqlib/operator_has.go @@ -12,7 +12,7 @@ func hasOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi log.Debugf("-- hasOperation") var results = list.New() - rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) + rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS) if err != nil { return Context{}, err diff --git a/pkg/yqlib/operator_load.go b/pkg/yqlib/operator_load.go index f32e4cc7..70e77e5c 100644 --- a/pkg/yqlib/operator_load.go +++ b/pkg/yqlib/operator_load.go @@ -67,7 +67,7 @@ func loadYamlOperator(d *dataTreeNavigator, context Context, expressionNode *Exp for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { 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 { return Context{}, err } diff --git a/pkg/yqlib/operator_map.go b/pkg/yqlib/operator_map.go index e3b8ec99..ec1a5ed0 100644 --- a/pkg/yqlib/operator_map.go +++ b/pkg/yqlib/operator_map.go @@ -17,7 +17,7 @@ func mapValuesOperator(d *dataTreeNavigator, context Context, expressionNode *Ex assignUpdateExp := &ExpressionNode{ Operation: &Operation{OperationType: assignOpType, UpdateAssign: true}, - Rhs: expressionNode.Rhs, + RHS: expressionNode.RHS, } _, err = assignUpdateOperator(d, splatted, assignUpdateExp) if err != nil { @@ -42,8 +42,8 @@ func mapOperator(d *dataTreeNavigator, context Context, expressionNode *Expressi return Context{}, err } - result, err := d.GetMatchingNodes(splatted, expressionNode.Rhs) - log.Debug("expressionNode.Rhs %v", expressionNode.Rhs.Operation.OperationType) + result, err := d.GetMatchingNodes(splatted, expressionNode.RHS) + log.Debug("expressionNode.Rhs %v", expressionNode.RHS.Operation.OperationType) log.Debug("result %v", result) if err != nil { return Context{}, err diff --git a/pkg/yqlib/operator_multiply.go b/pkg/yqlib/operator_multiply.go index 26f4a798..b8efb020 100644 --- a/pkg/yqlib/operator_multiply.go +++ b/pkg/yqlib/operator_multiply.go @@ -20,8 +20,8 @@ type multiplyPreferences struct { func createMultiplyOp(prefs interface{}) func(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode { return func(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode { return &ExpressionNode{Operation: &Operation{OperationType: multiplyOpType, Preferences: prefs}, - Lhs: lhs, - Rhs: rhs} + LHS: lhs, + RHS: rhs} } } @@ -199,8 +199,8 @@ func applyAssignment(d *dataTreeNavigator, context Context, pathIndexToStartFrom assignmentOpNode := &ExpressionNode{ Operation: assignmentOp, - Lhs: createTraversalTree(lhsPath, preferences.TraversePrefs, rhs.IsMapKey), - Rhs: &ExpressionNode{Operation: rhsOp}, + LHS: createTraversalTree(lhsPath, preferences.TraversePrefs, rhs.IsMapKey), + RHS: &ExpressionNode{Operation: rhsOp}, } _, err := d.GetMatchingNodes(context.SingleChildContext(lhs), assignmentOpNode) diff --git a/pkg/yqlib/operator_pipe.go b/pkg/yqlib/operator_pipe.go index 2e023521..77813c67 100644 --- a/pkg/yqlib/operator_pipe.go +++ b/pkg/yqlib/operator_pipe.go @@ -6,11 +6,11 @@ func pipeOperator(d *dataTreeNavigator, context Context, expressionNode *Express // BUT we still return the original context back (see jq) // 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 { return Context{}, err } - rhs, err := d.GetMatchingNodes(lhs, expressionNode.Rhs) + rhs, err := d.GetMatchingNodes(lhs, expressionNode.RHS) if err != nil { return Context{}, err } diff --git a/pkg/yqlib/operator_reduce.go b/pkg/yqlib/operator_reduce.go index cc908fad..42e7d3a5 100644 --- a/pkg/yqlib/operator_reduce.go +++ b/pkg/yqlib/operator_reduce.go @@ -15,13 +15,13 @@ func reduceOperator(d *dataTreeNavigator, context Context, expressionNode *Expre //ensure lhs is actually an assignment //and rhs is a block (empty) - 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) - } 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) + 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) + } 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) } - arrayExpNode := expressionNode.Lhs.Lhs + arrayExpNode := expressionNode.LHS.LHS array, err := d.GetMatchingNodes(context, arrayExpNode) log.Debugf("array of %v things", array.MatchingNodes.Len()) @@ -30,9 +30,9 @@ func reduceOperator(d *dataTreeNavigator, context Context, expressionNode *Expre 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) if err != nil { @@ -41,7 +41,7 @@ func reduceOperator(d *dataTreeNavigator, context Context, expressionNode *Expre log.Debugf("with variable %v", variableName) - blockExp := expressionNode.Rhs.Rhs + blockExp := expressionNode.RHS.RHS for el := array.MatchingNodes.Front(); el != nil; el = el.Next() { candidate := el.Value.(*CandidateNode) log.Debugf("REDUCING WITH %v", NodeToString(candidate)) diff --git a/pkg/yqlib/operator_select.go b/pkg/yqlib/operator_select.go index 269441b3..57b9e2ce 100644 --- a/pkg/yqlib/operator_select.go +++ b/pkg/yqlib/operator_select.go @@ -11,7 +11,7 @@ func selectOperator(d *dataTreeNavigator, context Context, expressionNode *Expre for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { 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 { return Context{}, err diff --git a/pkg/yqlib/operator_sort.go b/pkg/yqlib/operator_sort.go index 2225c8d6..66709f4c 100644 --- a/pkg/yqlib/operator_sort.go +++ b/pkg/yqlib/operator_sort.go @@ -12,7 +12,7 @@ import ( func sortOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}} - expressionNode.Rhs = selfExpression + expressionNode.RHS = selfExpression return sortByOperator(d, context, expressionNode) } @@ -36,7 +36,7 @@ func sortByOperator(d *dataTreeNavigator, context Context, expressionNode *Expre for i, originalNode := range candidateNode.Content { 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 { return Context{}, err } diff --git a/pkg/yqlib/operator_sort_keys.go b/pkg/yqlib/operator_sort_keys.go index f977a9fb..0ab1a3cb 100644 --- a/pkg/yqlib/operator_sort_keys.go +++ b/pkg/yqlib/operator_sort_keys.go @@ -10,7 +10,7 @@ func sortKeysOperator(d *dataTreeNavigator, context Context, expressionNode *Exp for el := context.MatchingNodes.Front(); el != nil; el = el.Next() { 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 { return Context{}, err } diff --git a/pkg/yqlib/operator_strings.go b/pkg/yqlib/operator_strings.go index cfcd3a17..0cd8f8f5 100644 --- a/pkg/yqlib/operator_strings.go +++ b/pkg/yqlib/operator_strings.go @@ -13,7 +13,7 @@ func getSubstituteParameters(d *dataTreeNavigator, block *ExpressionNode, contex regEx := "" replacementText := "" - regExNodes, err := d.GetMatchingNodes(context.ReadOnlyClone(), block.Lhs) + regExNodes, err := d.GetMatchingNodes(context.ReadOnlyClone(), block.LHS) if err != nil { return "", "", err } @@ -23,7 +23,7 @@ func getSubstituteParameters(d *dataTreeNavigator, block *ExpressionNode, contex log.Debug("regEx %v", regEx) - replacementNodes, err := d.GetMatchingNodes(context, block.Rhs) + replacementNodes, err := d.GetMatchingNodes(context, block.RHS) if err != nil { return "", "", err } @@ -43,7 +43,7 @@ func substituteStringOperator(d *dataTreeNavigator, context Context, expressionN //rhs block operator //lhs of block = regex //rhs of block = replacement expression - block := expressionNode.Rhs + block := expressionNode.RHS 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) { - regExExpNode := expressionNode.Rhs + regExExpNode := expressionNode.RHS matchPrefs := matchPreferences{} // we got given parameters e.g. match(exp; params) - if expressionNode.Rhs.Operation.OperationType == blockOpType { - block := expressionNode.Rhs - regExExpNode = block.Lhs - replacementNodes, err := d.GetMatchingNodes(context, block.Rhs) + if expressionNode.RHS.Operation.OperationType == blockOpType { + block := expressionNode.RHS + regExExpNode = block.LHS + replacementNodes, err := d.GetMatchingNodes(context, block.RHS) if err != nil { return nil, matchPrefs, err } @@ -304,7 +304,7 @@ func joinStringOperator(d *dataTreeNavigator, context Context, expressionNode *E log.Debugf("-- joinStringOperator") joinStr := "" - rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) + rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS) if err != nil { return Context{}, err } @@ -345,7 +345,7 @@ func splitStringOperator(d *dataTreeNavigator, context Context, expressionNode * log.Debugf("-- splitStringOperator") splitStr := "" - rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) + rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS) if err != nil { return Context{}, err } diff --git a/pkg/yqlib/operator_style.go b/pkg/yqlib/operator_style.go index 0df69c05..ed7f6549 100644 --- a/pkg/yqlib/operator_style.go +++ b/pkg/yqlib/operator_style.go @@ -31,7 +31,7 @@ func assignStyleOperator(d *dataTreeNavigator, context Context, expressionNode * log.Debugf("AssignStyleOperator: %v") var style yaml.Style if !expressionNode.Operation.UpdateAssign { - rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) + rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS) if err != nil { 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 { return Context{}, err @@ -54,7 +54,7 @@ func assignStyleOperator(d *dataTreeNavigator, context Context, expressionNode * candidate := el.Value.(*CandidateNode) log.Debugf("Setting style of : %v", candidate.GetKey()) 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 { return Context{}, err } diff --git a/pkg/yqlib/operator_subtract.go b/pkg/yqlib/operator_subtract.go index 89f7346f..c92e8bb3 100644 --- a/pkg/yqlib/operator_subtract.go +++ b/pkg/yqlib/operator_subtract.go @@ -10,8 +10,8 @@ import ( func createSubtractOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode { return &ExpressionNode{Operation: &Operation{OperationType: subtractOpType}, - Lhs: lhs, - Rhs: rhs} + LHS: lhs, + RHS: rhs} } 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) { - newLhsArray := make([]*yaml.Node, 0) + newLHSArray := make([]*yaml.Node, 0) for lindex := 0; lindex < len(lhs.Node.Content); lindex = lindex + 1 { shouldInclude := true @@ -35,10 +35,10 @@ func subtractArray(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, erro } } 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 } diff --git a/pkg/yqlib/operator_tag.go b/pkg/yqlib/operator_tag.go index de45842e..59b07d89 100644 --- a/pkg/yqlib/operator_tag.go +++ b/pkg/yqlib/operator_tag.go @@ -12,7 +12,7 @@ func assignTagOperator(d *dataTreeNavigator, context Context, expressionNode *Ex tag := "" if !expressionNode.Operation.UpdateAssign { - rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.Rhs) + rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS) if err != nil { 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 { return Context{}, err @@ -32,7 +32,7 @@ func assignTagOperator(d *dataTreeNavigator, context Context, expressionNode *Ex candidate := el.Value.(*CandidateNode) log.Debugf("Setting tag of : %v", candidate.GetKey()) 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 { return Context{}, err } diff --git a/pkg/yqlib/operator_traverse_path.go b/pkg/yqlib/operator_traverse_path.go index c8df17d6..759c0093 100644 --- a/pkg/yqlib/operator_traverse_path.go +++ b/pkg/yqlib/operator_traverse_path.go @@ -82,14 +82,14 @@ func traverseArrayOperator(d *dataTreeNavigator, context Context, expressionNode // BUT we still return the original context back (see jq) // 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 { return Context{}, err } // 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 { return Context{}, err diff --git a/pkg/yqlib/operator_union.go b/pkg/yqlib/operator_union.go index f2334b2d..310d827e 100644 --- a/pkg/yqlib/operator_union.go +++ b/pkg/yqlib/operator_union.go @@ -5,14 +5,14 @@ import "container/list" func unionOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { log.Debug("unionOperator") log.Debug("context: %v", NodesToString(context.MatchingNodes)) - lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) + lhs, err := d.GetMatchingNodes(context, expressionNode.LHS) if err != nil { return Context{}, err } log.Debug("lhs: %v", NodesToString(lhs.MatchingNodes)) log.Debug("rhs input: %v", NodesToString(context.MatchingNodes)) - log.Debug("rhs: %v", expressionNode.Rhs.Operation.toString()) - rhs, err := d.GetMatchingNodes(context, expressionNode.Rhs) + log.Debug("rhs: %v", expressionNode.RHS.Operation.toString()) + rhs, err := d.GetMatchingNodes(context, expressionNode.RHS) if err != nil { return Context{}, err diff --git a/pkg/yqlib/operator_unique.go b/pkg/yqlib/operator_unique.go index 23f29622..1cc113f2 100644 --- a/pkg/yqlib/operator_unique.go +++ b/pkg/yqlib/operator_unique.go @@ -10,7 +10,7 @@ import ( func unique(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) { 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) } @@ -31,7 +31,7 @@ func uniqueBy(d *dataTreeNavigator, context Context, expressionNode *ExpressionN var newMatches = orderedmap.NewOrderedMap() for _, node := range candidateNode.Content { 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 { return Context{}, err diff --git a/pkg/yqlib/operator_variables.go b/pkg/yqlib/operator_variables.go index eda93a2f..55a9948c 100644 --- a/pkg/yqlib/operator_variables.go +++ b/pkg/yqlib/operator_variables.go @@ -20,14 +20,14 @@ type assignVarPreferences struct { } 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 { 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") } - variableName := expressionNode.Rhs.Operation.StringValue + variableName := expressionNode.RHS.Operation.StringValue prefs := expressionNode.Operation.Preferences.(assignVarPreferences) diff --git a/pkg/yqlib/operator_with.go b/pkg/yqlib/operator_with.go index fd277f30..87726267 100644 --- a/pkg/yqlib/operator_with.go +++ b/pkg/yqlib/operator_with.go @@ -6,11 +6,11 @@ func withOperator(d *dataTreeNavigator, context Context, expressionNode *Express log.Debugf("-- withOperator") // with(path, exp) - if expressionNode.Rhs.Operation.OperationType != blockOpType { - return Context{}, fmt.Errorf("with must be given a block, got %v instead", expressionNode.Rhs.Operation.OperationType.Type) + if expressionNode.RHS.Operation.OperationType != blockOpType { + 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) @@ -18,7 +18,7 @@ func withOperator(d *dataTreeNavigator, context Context, expressionNode *Express return Context{}, err } - updateExp := expressionNode.Rhs.Rhs + updateExp := expressionNode.RHS.RHS _, err = d.GetMatchingNodes(updateContext, updateExp) if err != nil { diff --git a/pkg/yqlib/operators.go b/pkg/yqlib/operators.go index 5358eee2..afddf0f4 100644 --- a/pkg/yqlib/operators.go +++ b/pkg/yqlib/operators.go @@ -13,7 +13,7 @@ type operatorHandler func(d *dataTreeNavigator, context Context, expressionNode type compoundCalculation func(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode 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 { return Context{}, err } @@ -26,7 +26,7 @@ func compoundAssignFunction(d *dataTreeNavigator, context Context, expressionNod valueOp.CandidateNode = candidate 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) 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) -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 { 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) { var results = list.New() - lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs) + lhs, err := d.GetMatchingNodes(context, expressionNode.LHS) if err != nil { return Context{}, err } 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 { return Context{}, err } 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 { 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() { 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 { return Context{}, err } @@ -165,7 +165,7 @@ func createTraversalTree(path []interface{}, traversePrefs traversePreferences, return &ExpressionNode{ Operation: &Operation{OperationType: shortPipeOpType}, - Lhs: createTraversalTree(path[0:1], traversePrefs, false), - Rhs: createTraversalTree(path[1:], traversePrefs, targetKey), + LHS: createTraversalTree(path[0:1], traversePrefs, false), + RHS: createTraversalTree(path[1:], traversePrefs, targetKey), } } diff --git a/pkg/yqlib/printer.go b/pkg/yqlib/printer.go index 534dbfd7..4429ac72 100644 --- a/pkg/yqlib/printer.go +++ b/pkg/yqlib/printer.go @@ -21,11 +21,11 @@ type PrinterOutputFormat uint32 const ( YamlOutputFormat = 1 << iota - JsonOutputFormat + JSONOutputFormat PropsOutputFormat - CsvOutputFormat - TsvOutputFormat - XmlOutputFormat + CSVOutputFormat + TSVOutputFormat + XMLOutputFormat ) func OutputFormatFromString(format string) (PrinterOutputFormat, error) { @@ -33,15 +33,15 @@ func OutputFormatFromString(format string) (PrinterOutputFormat, error) { case "yaml", "y": return YamlOutputFormat, nil case "json", "j": - return JsonOutputFormat, nil + return JSONOutputFormat, nil case "props", "p": return PropsOutputFormat, nil case "csv", "c": - return CsvOutputFormat, nil + return CSVOutputFormat, nil case "tsv", "t": - return TsvOutputFormat, nil + return TSVOutputFormat, nil case "xml", "x": - return XmlOutputFormat, nil + return XMLOutputFormat, nil default: return 0, fmt.Errorf("unknown format '%v' please use [yaml|json|props|csv|tsv|xml]", format) } diff --git a/pkg/yqlib/printer_test.go b/pkg/yqlib/printer_test.go index 81f2ac79..a2d797e2 100644 --- a/pkg/yqlib/printer_test.go +++ b/pkg/yqlib/printer_test.go @@ -314,7 +314,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) { var writer = bufio.NewWriter(&output) // note printDocSeparators is true, it should still not print document separators // 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()) if err != nil { diff --git a/pkg/yqlib/printer_writer.go b/pkg/yqlib/printer_writer.go index d4292323..9a027ad2 100644 --- a/pkg/yqlib/printer_writer.go +++ b/pkg/yqlib/printer_writer.go @@ -38,7 +38,7 @@ func NewMultiPrinterWriter(expression *ExpressionNode, format PrinterOutputForma extension := "yml" switch format { - case JsonOutputFormat: + case JSONOutputFormat: extension = "json" case PropsOutputFormat: extension = "properties" diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go index 3e9d5ae4..9ff6a3f9 100644 --- a/pkg/yqlib/xml_test.go +++ b/pkg/yqlib/xml_test.go @@ -3,6 +3,7 @@ package yqlib import ( "bufio" "bytes" + "errors" "fmt" "io" "strings" @@ -12,14 +13,14 @@ import ( yaml "gopkg.in/yaml.v3" ) -func decodeXml(t *testing.T, s formatScenario) *CandidateNode { - decoder := NewXmlDecoder("+", "+content") +func decodeXML(t *testing.T, s formatScenario) *CandidateNode { + decoder := NewXMLDecoder("+", "+content") decoder.Init(strings.NewReader(s.input)) node := &yaml.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) } @@ -47,15 +48,15 @@ func decodeXml(t *testing.T, s formatScenario) *CandidateNode { return context.MatchingNodes.Front().Value.(*CandidateNode) } -func processXmlScenario(s formatScenario) string { +func processXMLScenario(s formatScenario) string { var output bytes.Buffer writer := bufio.NewWriter(&output) - var encoder = NewXmlEncoder(2, "+", "+content") + var encoder = NewXMLEncoder(2, "+", "+content") var decoder = NewYamlDecoder() if s.scenarioType == "roundtrip" { - decoder = NewXmlDecoder("+", "+content") + decoder = NewXMLDecoder("+", "+content") } inputs, err := readDocuments(strings.NewReader(s.input), "sample.yml", 0, decoder) @@ -83,7 +84,7 @@ type formatScenario struct { scenarioType string } -var inputXmlWithComments = ` +var inputXMLWithComments = ` @@ -101,7 +102,7 @@ for x --> ` -var inputXmlWithCommentsWithSubChild = ` +var inputXMLWithCommentsWithSubChild = ` @@ -140,7 +141,7 @@ cat: # after cat ` -var inputXmlWithCommentsWithArray = ` +var inputXMLWithCommentsWithArray = ` @@ -201,7 +202,7 @@ cat: # after cat ` -var expectedRoundtripXmlWithComments = ` +var expectedRoundtripXMLWithComments = ` 3 @@ -222,7 +223,7 @@ cat: # inline_cat # below_cat ` -var expectedXmlWithComments = ` +var expectedXMLWithComments = ` val1 val2 @@ -263,7 +264,7 @@ var xmlScenarios = []formatScenario{ { description: "Parse xml: with comments", subdescription: "A best attempt is made to preserve comments.", - input: inputXmlWithComments, + input: inputXMLWithComments, expected: expectedDecodeYamlWithComments, scenarioType: "decode", }, @@ -298,14 +299,14 @@ var xmlScenarios = []formatScenario{ { description: "Parse xml: with comments subchild", skipDoc: true, - input: inputXmlWithCommentsWithSubChild, + input: inputXMLWithCommentsWithSubChild, expected: expectedDecodeYamlWithSubChild, scenarioType: "decode", }, { description: "Parse xml: with comments array", skipDoc: true, - input: inputXmlWithCommentsWithArray, + input: inputXMLWithCommentsWithArray, expected: expectedDecodeYamlWithArray, scenarioType: "decode", }, @@ -345,44 +346,44 @@ var xmlScenarios = []formatScenario{ description: "Encode xml: comments", subdescription: "A best attempt is made to copy comments to xml.", input: yamlWithComments, - expected: expectedXmlWithComments, + expected: expectedXMLWithComments, scenarioType: "encode", }, { description: "Round trip: with comments", subdescription: "A best effort is made, but comment positions and white space are not preserved perfectly.", - input: inputXmlWithComments, - expected: expectedRoundtripXmlWithComments, + input: inputXMLWithComments, + expected: expectedRoundtripXMLWithComments, scenarioType: "roundtrip", }, } -func testXmlScenario(t *testing.T, s formatScenario) { +func testXMLScenario(t *testing.T, s formatScenario) { 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 { - var actual = resultToString(t, decodeXml(t, s)) + var actual = resultToString(t, decodeXML(t, s)) 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) if s.skipDoc { return } if s.scenarioType == "encode" { - documentXmlEncodeScenario(w, s) + documentXMLEncodeScenario(w, s) } else if s.scenarioType == "roundtrip" { - documentXmlRoundTripScenario(w, s) + documentXMLRoundTripScenario(w, s) } 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)) if s.subdescription != "" { @@ -404,7 +405,7 @@ func documentXmlDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario) var output bytes.Buffer printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), YamlOutputFormat, true, false, 2, true) - node := decodeXml(t, s) + node := decodeXML(t, s) err := printer.PrintResults(node.AsList()) 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())) } -func documentXmlEncodeScenario(w *bufio.Writer, s formatScenario) { +func documentXMLEncodeScenario(w *bufio.Writer, s formatScenario) { writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) 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, "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)) 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, "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 { - testXmlScenario(t, tt) + testXMLScenario(t, tt) } genericScenarios := make([]interface{}, len(xmlScenarios)) for i, s := range xmlScenarios { genericScenarios[i] = s } - documentScenarios(t, "usage", "xml", genericScenarios, documentXmlScenario) + documentScenarios(t, "usage", "xml", genericScenarios, documentXMLScenario) } diff --git a/test/utils.go b/test/utils.go index c2a1ce7a..7072cc27 100644 --- a/test/utils.go +++ b/test/utils.go @@ -6,32 +6,13 @@ import ( "fmt" "os" "reflect" - "strings" "testing" "github.com/pkg/diff" "github.com/pkg/diff/write" - "github.com/spf13/cobra" 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 { var parsedData yaml.Node err := yaml.Unmarshal([]byte(rawData), &parsedData)