1
0
mirror of synced 2024-11-25 06:26:03 +03:00

fixes in template (un)marshaling and tests

This commit is contained in:
Pavel 2020-04-08 13:50:02 +03:00
parent 99d9784f73
commit d5a94653da
2 changed files with 75 additions and 4 deletions

View File

@ -11,7 +11,7 @@ const TemplateTypeText = "text"
const (
// TemplateItemTypeText is a type for text chunk in template
TemplateItemTypeText = iota
TemplateItemTypeText uint8 = iota
// TemplateItemTypeVar is a type for variable in template
TemplateItemTypeVar
)
@ -52,12 +52,17 @@ type TemplateItem struct {
VarType string
}
// MarshalJSON will correctly marshal TemplateItem
func (t *TemplateItem) MarshalJSON() ([]byte, error) {
// MarshalJSON controls how TemplateItem will be marshaled into JSON
func (t TemplateItem) MarshalJSON() ([]byte, error) {
switch t.Type {
case TemplateItemTypeText:
return json.Marshal(t.Text)
case TemplateItemTypeVar:
// {} case, fast output without marshaling
if t.VarType == "" || t.VarType == TemplateVarCustom {
return []byte("{}"), nil
}
return json.Marshal(map[string]interface{}{
"var": t.VarType,
})
@ -87,11 +92,12 @@ func (t *TemplateItem) UnmarshalJSON(b []byte) error {
}
if varTypeCurr, ok := v["var"].(string); ok {
if _, ok := templateVarAssoc[t.VarType]; !ok {
if _, ok := templateVarAssoc[varTypeCurr]; !ok {
return fmt.Errorf("invalid placeholder var '%s'", varTypeCurr)
}
t.Type = TemplateItemTypeVar
t.VarType = varTypeCurr
} else {
return errors.New("invalid TemplateItem")
}

65
v1/template_test.go Normal file
View File

@ -0,0 +1,65 @@
package v1
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTemplateItem_MarshalJSON(t *testing.T) {
text := TemplateItem{
Type: TemplateItemTypeText,
Text: "text item",
}
variable := TemplateItem{
Type: TemplateItemTypeVar,
VarType: TemplateVarFirstName,
}
emptyVariable := TemplateItem{
Type: TemplateItemTypeVar,
VarType: "",
}
data, err := json.Marshal(text)
assert.NoError(t, err)
assert.Equal(t, "\""+text.Text+"\"", string(data))
data, err = json.Marshal(variable)
assert.NoError(t, err)
assert.Equal(t, `{"var":"first_name"}`, string(data))
data, err = json.Marshal(emptyVariable)
assert.NoError(t, err)
assert.Equal(t, "{}", string(data))
}
func TestTemplateItem_UnmarshalJSON(t *testing.T) {
var (
textResult TemplateItem
variableResult TemplateItem
emptyVariableResult TemplateItem
)
text := []byte("\"text block\"")
variable := []byte(`{"var":"first_name"}`)
emptyVariable := []byte("{}")
require.NoError(t, json.Unmarshal(text, &textResult))
require.NoError(t, json.Unmarshal(variable, &variableResult))
require.NoError(t, json.Unmarshal(emptyVariable, &emptyVariableResult))
assert.Equal(t, TemplateItemTypeText, textResult.Type)
assert.Equal(t, string(text)[1:11], textResult.Text)
assert.Equal(t, TemplateItemTypeVar, variableResult.Type)
assert.Equal(t, TemplateVarFirstName, variableResult.VarType)
assert.Empty(t, variableResult.Text)
assert.Equal(t, TemplateItemTypeVar, emptyVariableResult.Type)
assert.Equal(t, TemplateVarCustom, emptyVariableResult.VarType)
assert.Empty(t, emptyVariableResult.Text)
}