From c69c656a28ef26706e1c9ba7eeae42919f6f4fc3 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Tue, 7 Mar 2023 16:26:54 -0600 Subject: [PATCH] :art: (visitor): Improve readability of template Run funcs --- internal/visitor/find_args.go | 25 +++++++++++------------ internal/visitor/template_comments.go | 29 ++++++++++++--------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/internal/visitor/find_args.go b/internal/visitor/find_args.go index 9b7f0ea..170de21 100644 --- a/internal/visitor/find_args.go +++ b/internal/visitor/find_args.go @@ -60,30 +60,29 @@ type FindArgs struct { } func (visitor *FindArgs) Run(n *yaml.Node) error { - if len(n.Content) == 0 { + switch { + case len(n.Content) == 0: + // Node has no children. Search current node. if err := visitor.FindArgs(n, n.Value); err != nil { return err } - return nil - } - - switch n.Kind { - case yaml.MappingNode: + case n.Kind == yaml.MappingNode: for i := 0; i < len(n.Content); i += 2 { // Attempt to fetch template from comments on the key. key, val := n.Content[i], n.Content[i+1] tmplSrc, _ := comment.Parse(visitor.conf.Prefix, key) - if tmplSrc != "" { + if tmplSrc == "" { + // Key did not have comment, traversing children. + if err := visitor.Run(val); err != nil { + return err + } + } else { + // Template is on key's comment instead of value. + // This typically happens if the value is left empty with an implied null. if err := visitor.FindArgs(key, val.Value); err != nil { return err } - continue - } - - // Key did not have comment, traversing children. - if err := visitor.Run(val); err != nil { - return err } } default: diff --git a/internal/visitor/template_comments.go b/internal/visitor/template_comments.go index 860ea29..84e7136 100644 --- a/internal/visitor/template_comments.go +++ b/internal/visitor/template_comments.go @@ -22,7 +22,8 @@ type TemplateComments struct { } func (t TemplateComments) Run(n *yaml.Node) error { - if len(n.Content) == 0 { + switch { + case len(n.Content) == 0: // Node has no children. Template current node. tmplSrc, tmplTag := comment.Parse(t.conf.Prefix, n) if tmplSrc != "" { @@ -38,17 +39,21 @@ func (t TemplateComments) Run(n *yaml.Node) error { } } } - return nil - } - - switch n.Kind { - case yaml.MappingNode: + case n.Kind == yaml.MappingNode: for i := 0; i < len(n.Content); i += 2 { // Attempt to fetch template from comments on the key. key, val := n.Content[i], n.Content[i+1] tmplSrc, tmplTag := comment.Parse(t.conf.Prefix, key) - if tmplSrc != "" { + if tmplSrc == "" { + // Key did not have comment, traversing children. + if err := t.Run(val); err != nil { + return err + } + } else { + // Template is on key's comment instead of value. + // This typically happens if the value is left empty with an implied null. + if t.conf.Strip { key.LineComment = "" } @@ -59,21 +64,13 @@ func (t TemplateComments) Run(n *yaml.Node) error { } else { t.conf.Log.WithError(err).Warn("skipping value due to template error") } - } else { - // Current node was templated, do not need to traverse children - comment.Move(key, val) continue } } - - // Key did not have comment, traversing children. - if err := t.Run(val); err != nil { - return err - } - comment.Move(key, val) } default: + // Iterate over children for _, n := range n.Content { if err := t.Run(n); err != nil { return err