diff --git a/pkg/yaml2json/template.go b/pkg/yaml2json/convertor.go similarity index 72% rename from pkg/yaml2json/template.go rename to pkg/yaml2json/convertor.go index 3e4dc41..aa66fb2 100644 --- a/pkg/yaml2json/template.go +++ b/pkg/yaml2json/convertor.go @@ -2,15 +2,14 @@ package yaml2json import ( "github.com/goccy/go-yaml" - "github.com/tokopedia/gripmock/pkg/template" ) type Convertor struct { - engine *template.Engine + engine *engine } func New() *Convertor { - return &Convertor{engine: template.New()} + return &Convertor{engine: &engine{}} } func (t *Convertor) Execute(name string, data []byte) ([]byte, error) { diff --git a/pkg/yaml2json/convertor_test.go b/pkg/yaml2json/convertor_test.go new file mode 100644 index 0000000..9658181 --- /dev/null +++ b/pkg/yaml2json/convertor_test.go @@ -0,0 +1,26 @@ +package yaml2json_test + +import ( + "github.com/stretchr/testify/require" + "github.com/tokopedia/gripmock/pkg/yaml2json" + "testing" +) + +func TestConvertor(t *testing.T) { + convertor := yaml2json.New() + + bytes, err := convertor.Execute("hello", []byte(` +yaml2json: + base64: {{ uuidToBase64StdEncoding "77465064-a0ce-48a3-b7e4-d50f88e55093" }} + highLow: {{ uuidToHighLowLittleEndian "e351220b-4847-42f5-8abb-c052b87ff2d4" }} +`)) + + expected := `{ + "yaml2json": { + "base64": "d0ZQZKDOSKO35NUPiOVQkw==", + "highLow": {"high":-773977811204288029,"low":-3102276763665777782} + } +}` + require.NoError(t, err) + require.JSONEq(t, expected, string(bytes)) +} diff --git a/pkg/template/engine.go b/pkg/yaml2json/engine.go similarity index 77% rename from pkg/template/engine.go rename to pkg/yaml2json/engine.go index c334664..6624955 100644 --- a/pkg/template/engine.go +++ b/pkg/yaml2json/engine.go @@ -1,4 +1,4 @@ -package template +package yaml2json import ( "bytes" @@ -9,13 +9,9 @@ import ( "github.com/google/uuid" ) -type Engine struct{} +type engine struct{} -func New() *Engine { - return &Engine{} -} - -func (e *Engine) Execute(name string, data []byte) ([]byte, error) { +func (e *engine) Execute(name string, data []byte) ([]byte, error) { var buffer bytes.Buffer parse, err := template.New(name).Funcs(e.funcMap()).Parse(string(data)) @@ -30,10 +26,12 @@ func (e *Engine) Execute(name string, data []byte) ([]byte, error) { return buffer.Bytes(), nil } -func (e *Engine) funcMap() template.FuncMap { +func (e *engine) funcMap() template.FuncMap { return template.FuncMap{ - "base64StdEncoding": func(str string) string { - return base64.StdEncoding.EncodeToString([]byte(str)) + "uuidToBase64StdEncoding": func(guid string) string { + v := uuid.MustParse(guid) + + return base64.StdEncoding.EncodeToString(v[:]) }, "uuidToHighLowLittleEndian": func(guid string) string { v := uuid.MustParse(guid)