Skip to content

Commit

Permalink
🎨 (visitor): Improve readability of template Run funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 7, 2023
1 parent a216a2b commit c69c656
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
25 changes: 12 additions & 13 deletions internal/visitor/find_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 13 additions & 16 deletions internal/visitor/template_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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 = ""
}
Expand All @@ -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
Expand Down

0 comments on commit c69c656

Please sign in to comment.