Skip to content

Commit

Permalink
fix(backward): sprigin needs to still align with sprig no error
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed Sep 1, 2024
1 parent b9f7b71 commit bec98d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
14 changes: 14 additions & 0 deletions sprigin/sprig_backward_compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package sprigin
import (
htemplate "html/template"
"log/slog"
gostrings "strings"
ttemplate "text/template"

"github.com/go-sprout/sprout"
"github.com/go-sprout/sprout/internal/runtime"
"github.com/go-sprout/sprout/registry/backward"
"github.com/go-sprout/sprout/registry/checksum"
"github.com/go-sprout/sprout/registry/conversion"
Expand Down Expand Up @@ -192,6 +194,18 @@ func (sh *SprigHandler) Build() sprout.FunctionMap {
sprout.AssignAliases(sh)
sprout.AssignNotices(sh)

// BACKWARDS COMPATIBILITY
// Ensure error handling is consistent with sprig functions
for funcName, fn := range sh.funcsMap {
if !gostrings.HasPrefix(funcName, "must") {
sh.funcsMap[funcName] = func(args ...any) (any, error) {
out, _ := runtime.SafeCall(fn, args...)
return out, nil
}
}
}
//\ BACKWARDS COMPATIBILITY

return sh.funcsMap
}

Expand Down
10 changes: 6 additions & 4 deletions sprigin/sprig_backward_compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ func TestFuncMap_IncludesHello(t *testing.T) {
_, exists := funcMap["hello"]
assert.True(t, exists)

helloFunc, ok := funcMap["hello"].(func() string)
helloFunc, ok := funcMap["hello"].(func() (string, error))
assert.True(t, ok)

assert.Equal(t, "Hello!", helloFunc())
result, err := helloFunc()
assert.NoError(t, err)
assert.Equal(t, "Hello!", result)
}

func TestSprigHandler(t *testing.T) {
Expand All @@ -51,9 +53,9 @@ func TestSprigHandler(t *testing.T) {
handler.Build()

assert.GreaterOrEqual(t, len(handler.Functions()), sprigFunctionCount)
assert.Len(t, handler.Aliases(), 7)
assert.Len(t, handler.Aliases(), 37) // Hardcoded for backward compatibility

assert.Len(t, handler.registries, 18)
assert.Len(t, handler.registries, 18) // Hardcoded for backward compatibility

registriesUids := []string{}
for _, registry := range handler.registries {
Expand Down

0 comments on commit bec98d0

Please sign in to comment.