Skip to content

Commit

Permalink
feat: add mustfromyaml missing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed May 16, 2024
1 parent 13114c8 commit 94f0dae
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
21 changes: 21 additions & 0 deletions encoding_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,27 @@ func (fh *FunctionHandler) MustToRawJson(v any) (string, error) {
return strings.TrimSuffix(buf.String(), "\n"), nil
}

// MustFromYaml deserializes a YAML string into a Go data structure, returning
// the result along with any error that occurs.
//
// Parameters:
//
// v string - the YAML string to deserialize.
//
// Returns:
//
// any - the Go data structure representing the deserialized YAML content.
// error - an error if the YAML content cannot be deserialized.
//
// Example:
//
// {{ "name: John Doe\nage: 30" | mustFromYaml }} // Output: map[name:John Doe age:30], nil
func (fh *FunctionHandler) MustFromYAML(v string) (any, error) {
var output any
err := yaml.Unmarshal([]byte(v), &output)
return output, err
}

// MustToYAML serializes a Go data structure to a YAML string and returns any error that occurs during the serialization.
//
// Parameters:
Expand Down
30 changes: 20 additions & 10 deletions encoding_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@ func TestToYAML(t *testing.T) {
runTestCases(t, tests)
}

func TestMustToYAML(t *testing.T) {
var tests = mustTestCases{
{testCase{"TestEmptyInput", `{{ "" | mustToYaml }}`, "\"\"", nil}, ""},
{testCase{"TestVariableInput", `{{ .V | mustToYaml }}`, "bar: baz\nfoo: 55", map[string]any{"V": map[string]any{"foo": 55, "bar": "baz"}}}, ""},
{testCase{"TestInvalidInput", `{{ .V | mustToYaml }}`, "", map[string]any{"V": make(chan int)}}, "json: unsupported type: chan int"},
}

runMustTestCases(t, tests)
}

func TestMustFromJson(t *testing.T) {
var tests = mustTestCases{
{testCase{"TestEmptyInput", `{{ "" | mustFromJson }}`, "", nil}, "unexpected end"},
Expand Down Expand Up @@ -150,3 +140,23 @@ func TestMustToRawJson(t *testing.T) {

runMustTestCases(t, tests)
}

func TestMustFromYAML(t *testing.T) {
var tests = mustTestCases{
{testCase{"TestEmptyInput", `{{ "foo: :: baz" | mustFromYaml }}`, "", nil}, "error converting YAML to JSON"},
{testCase{"TestVariableInput", `{{ .V | mustFromYaml }}`, "map[bar:map[baz:1] foo:55]", map[string]any{"V": "foo: 55\nbar:\n baz: 1\n"}}, ""},
{testCase{"TestInvalidInput", `{{ .V | mustFromYaml }}`, "", map[string]any{"V": ":"}}, "did not find expected key"},
}

runMustTestCases(t, tests)
}

func TestMustToYAML(t *testing.T) {
var tests = mustTestCases{
{testCase{"TestEmptyInput", `{{ "" | mustToYaml }}`, "\"\"", nil}, ""},
{testCase{"TestVariableInput", `{{ .V | mustToYaml }}`, "bar: baz\nfoo: 55", map[string]any{"V": map[string]any{"foo": 55, "bar": "baz"}}}, ""},
{testCase{"TestInvalidInput", `{{ .V | mustToYaml }}`, "", map[string]any{"V": make(chan int)}}, "json: unsupported type: chan int"},
}

runMustTestCases(t, tests)
}
1 change: 1 addition & 0 deletions sprout.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func FuncMap(opts ...FunctionHandlerOption) template.FuncMap {
fnHandler.funcMap["mustToJson"] = fnHandler.MustToJson
fnHandler.funcMap["mustToPrettyJson"] = fnHandler.MustToPrettyJson
fnHandler.funcMap["mustToRawJson"] = fnHandler.MustToRawJson
fnHandler.funcMap["mustFromYaml"] = fnHandler.MustFromYAML
fnHandler.funcMap["mustToYaml"] = fnHandler.MustToYAML
fnHandler.funcMap["ternary"] = fnHandler.Ternary
fnHandler.funcMap["deepCopy"] = fnHandler.DeepCopy
Expand Down

0 comments on commit 94f0dae

Please sign in to comment.