From 8902f98db16ada13a2404eadda5d38bb6dabc7cc Mon Sep 17 00:00:00 2001 From: Giorgio Gallo Date: Tue, 2 May 2023 12:43:26 +0200 Subject: [PATCH] add fixes after code revieew --- pkg/yqlib/encoder_shellvariables.go | 14 +++++++------- pkg/yqlib/encoder_shellvariables_test.go | 8 ++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/pkg/yqlib/encoder_shellvariables.go b/pkg/yqlib/encoder_shellvariables.go index bc50e022..62d6ac8f 100644 --- a/pkg/yqlib/encoder_shellvariables.go +++ b/pkg/yqlib/encoder_shellvariables.go @@ -32,7 +32,7 @@ func (pe *shellVariablesEncoder) PrintLeadingContent(_ io.Writer, _ string) erro func (pe *shellVariablesEncoder) Encode(writer io.Writer, node *yaml.Node) error { mapKeysToStrings(node) - err := pe.doEncode(&writer, node, "", nil) + err := pe.doEncode(&writer, node, "") if err != nil { return err } @@ -40,7 +40,7 @@ func (pe *shellVariablesEncoder) Encode(writer io.Writer, node *yaml.Node) error return err } -func (pe *shellVariablesEncoder) doEncode(w *io.Writer, node *yaml.Node, path string, _ *yaml.Node) error { +func (pe *shellVariablesEncoder) doEncode(w *io.Writer, node *yaml.Node, path string) error { // Note this drops all comments. @@ -56,10 +56,10 @@ func (pe *shellVariablesEncoder) doEncode(w *io.Writer, node *yaml.Node, path st _, err := io.WriteString(*w, nonemptyPath+"="+quoteValue(node.Value)+"\n") return err case yaml.DocumentNode: - return pe.doEncode(w, node.Content[0], path, node) + return pe.doEncode(w, node.Content[0], path) case yaml.SequenceNode: for index, child := range node.Content { - err := pe.doEncode(w, child, appendPath(path, index), nil) + err := pe.doEncode(w, child, appendPath(path, index)) if err != nil { return err } @@ -69,14 +69,14 @@ func (pe *shellVariablesEncoder) doEncode(w *io.Writer, node *yaml.Node, path st for index := 0; index < len(node.Content); index = index + 2 { key := node.Content[index] value := node.Content[index+1] - err := pe.doEncode(w, value, appendPath(path, key.Value), key) + err := pe.doEncode(w, value, appendPath(path, key.Value)) if err != nil { return err } } return nil case yaml.AliasNode: - return pe.doEncode(w, node.Alias, path, nil) + return pe.doEncode(w, node.Alias, path) default: return fmt.Errorf("Unsupported node %v", node.Tag) } @@ -145,7 +145,7 @@ func quoteValue(value string) string { } func isAlphaOrUnderscore(r rune) bool { - return ('a' <= r && r <= 'z') || ('A' <= r && r <= 'Z') + return ('a' <= r && r <= 'z') || ('A' <= r && r <= 'Z') || r == '_' } func isAlphaNumericOrUnderscore(r rune) bool { diff --git a/pkg/yqlib/encoder_shellvariables_test.go b/pkg/yqlib/encoder_shellvariables_test.go index 02b1be38..b3259088 100644 --- a/pkg/yqlib/encoder_shellvariables_test.go +++ b/pkg/yqlib/encoder_shellvariables_test.go @@ -68,6 +68,14 @@ func TestShellVariablesEncoderRootKeyStartingWithDigit(t *testing.T) { assertEncodesTo(t, "1a: onea", "_1a=onea") } +func TestShellVariablesEncoderRootKeyStartingWithUnderscore(t *testing.T) { + assertEncodesTo(t, "_key: value", "_key=value") +} + +func TestShellVariablesEncoderChildStartingWithUnderscore(t *testing.T) { + assertEncodesTo(t, "root:\n _child: value", "root__child=value") +} + func TestShellVariablesEncoderEmptyValue(t *testing.T) { assertEncodesTo(t, "empty:", "empty=") }