Skip to content

Commit

Permalink
fix rendering objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsayde committed Sep 19, 2022
1 parent 9162f53 commit 5ed8aa2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions internal/renderer/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math"
"strings"
"time"

"gopkg.in/yaml.v3"
)

var (
Expand Down Expand Up @@ -41,6 +43,8 @@ var (
"second": second,
// types processors
"bool": boolean,
// converters
"toYaml": toYaml,
}
)

Expand Down Expand Up @@ -90,3 +94,11 @@ func boolean(s string) (bool, error) {
return false, fmt.Errorf("cannot convert %s to boolean", s)
}
}

func toYaml(in interface{}) string {
raw, err := yaml.Marshal(in)
if err != nil {
return ""
}
return strings.TrimSuffix(string(raw), "\n")
}
15 changes: 14 additions & 1 deletion internal/renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package renderer

import (
"bytes"
"fmt"
"strings"
"text/template"

"gopkg.in/yaml.v3"
Expand All @@ -17,6 +19,13 @@ var (
Funcs(processors).
Option("missingkey=error").
Delims(leftDelim, rightDelim)

replacer = strings.NewReplacer(
leftDelim,
fmt.Sprintf("%s toYaml (", leftDelim),
rightDelim,
fmt.Sprintf(") %s", rightDelim),
)
)

type Renderable struct {
Expand Down Expand Up @@ -44,7 +53,6 @@ func (r *Renderable) init(value interface{}) error {
return err
}
}

return nil
}

Expand All @@ -58,6 +66,11 @@ func newRenderable(value interface{}) (*Renderable, error) {
}

func (r *Renderable) UnmarshalYAML(n *yaml.Node) error {
if n.Kind == yaml.ScalarNode {
if strings.HasPrefix(n.Value, leftDelim) && strings.HasSuffix(n.Value, rightDelim) {
n.Value = replacer.Replace(n.Value)
}
}
var value interface{}
err := n.Decode(&value)
if err != nil {
Expand Down

0 comments on commit 5ed8aa2

Please sign in to comment.