Skip to content

Commit

Permalink
refactor(template): add method for parsing template strings
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoksr committed Dec 23, 2022
1 parent ef0c743 commit a613bd4
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,14 @@ func (t *TemplateEngine) parse(ctx context.Context, w io.Writer, data []byte) er
func (t *TemplateEngine) Parse(ctx context.Context, w io.Writer, data []byte) error {
return t.parse(ctx, w, data)
}

// ParseString is similar to Parse but accepts a string as input instead of a byte slice. It returns an error if the
// template cannot be parsed or rendered. If the template is parsed and rendered successfully, the result is returned.
func (t *TemplateEngine) ParseString(ctx context.Context, data string) (string, error) {
var b strings.Builder
if err := t.parse(ctx, &b, []byte(data)); err != nil {
return "", err
}

return b.String(), nil
}
116 changes: 116 additions & 0 deletions pkg/templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,119 @@ func Greet() {
})
}
}

func TestTemplateEngine_ParseString(t *testing.T) {
t.Parallel()

type fields struct {
StartTag string
EndTag string
MissingKeyFn MissingKeyFn
}
type args struct {
ctx context.Context
data string
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
{
name: "empty",
fields: fields{
StartTag: "%{{",
EndTag: "}}%",
MissingKeyFn: defaultMissingKeyFn,
},
args: args{
ctx: context.Background(),
data: "",
},
want: "",
wantErr: false,
},
{
name: "simple",
fields: fields{
StartTag: "%{{",
EndTag: "}}%",
MissingKeyFn: func(key string) (string, error) {
if key == "Name" {
return "Proji", nil
}
return "", errors.Newf("unexpected key: %s", key)
},
},
args: args{
ctx: context.Background(),
data: "Hello, %{{name}}%!",
},
want: "Hello, Proji!",
wantErr: false,
},
{
name: "advanced",
fields: fields{
StartTag: "%{{",
EndTag: "}}%",
MissingKeyFn: func(key string) (string, error) {
if key == "Name" {
return "Proji", nil
} else if key == "Message" {
return "I hope you have a great day.", nil
}
return "", errors.Newf("unexpected key: %s", key)
},
},
args: args{
ctx: context.Background(),
data: "Hello, %{{name}}%! %{{message}}%",
},
want: "Hello, Proji! I hope you have a great day.",
wantErr: false,
},
{
name: "invalid template",
fields: fields{
StartTag: "%{{",
EndTag: "}}%",
MissingKeyFn: func(key string) (string, error) {
if key == "Name" {
return "Proji", nil
}
return "", errors.Newf("unexpected key: %s", key)
},
},
args: args{
ctx: context.Background(),
data: "Hello, %{{name}}!",
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

engine := NewEngine(tt.fields.StartTag, tt.fields.EndTag)
if engine == nil {
t.Fatal("engine is nil")
}
engine.MissingKeyFn = tt.fields.MissingKeyFn

got, err := engine.ParseString(tt.args.ctx, tt.args.data)
if (err != nil) != tt.wantErr {
t.Fatalf("unexpected error: %v", err)
}

if diff := cmp.Diff(tt.want, got); diff != "" {
t.Fatalf("mismatch (-want +got):\n%s", diff)
}
})
}
}

0 comments on commit a613bd4

Please sign in to comment.