-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(backward): reimport functions from sprig to maintain backward com…
…patibility (#30) ## Description This pull request reimports several functions from the Sprig library to maintain backward compatibility. This is marked as a regression issue ## Changes - Reimported `HermeticTxtFuncMap`, `HermeticHtmlFuncMap`, `TxtFuncMap`, `HtmlFuncMap`, `GenericFuncMap` from Sprig. ## Fixes #29 ## Checklist - [x] I have read the **CONTRIBUTING.md** document. - [x] My code follows the code style of this project. - [x] I have added tests to cover my changes. - [x] All new and existing tests passed. - [x] I have updated the documentation accordingly. - [ ] This change requires a change to the documentation on the website.
- Loading branch information
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package sprout | ||
|
||
import ( | ||
htemplate "html/template" | ||
ttemplate "text/template" | ||
) | ||
|
||
// HermeticTxtFuncMap returns a 'text/template'.FuncMap with only repeatable functions. | ||
func HermeticTxtFuncMap(opts ...FunctionHandlerOption) ttemplate.FuncMap { | ||
r := TxtFuncMap(opts...) | ||
for _, name := range nonhermeticFunctions { | ||
delete(r, name) | ||
} | ||
return r | ||
} | ||
|
||
// HermeticHtmlFuncMap returns an 'html/template'.Funcmap with only repeatable functions. | ||
func HermeticHtmlFuncMap(opts ...FunctionHandlerOption) htemplate.FuncMap { | ||
r := HtmlFuncMap(opts...) | ||
for _, name := range nonhermeticFunctions { | ||
delete(r, name) | ||
} | ||
return r | ||
} | ||
|
||
// TxtFuncMap returns a 'text/template'.FuncMap | ||
func TxtFuncMap(opts ...FunctionHandlerOption) ttemplate.FuncMap { | ||
return ttemplate.FuncMap(FuncMap(opts...)) | ||
} | ||
|
||
// HtmlFuncMap returns an 'html/template'.Funcmap | ||
func HtmlFuncMap(opts ...FunctionHandlerOption) htemplate.FuncMap { | ||
return htemplate.FuncMap(FuncMap(opts...)) | ||
} | ||
|
||
// GenericFuncMap returns a copy of the basic function map as a map[string]interface{}. | ||
func GenericFuncMap(opts ...FunctionHandlerOption) map[string]interface{} { | ||
return FuncMap(opts...) | ||
} | ||
|
||
// These functions are not guaranteed to evaluate to the same result for given input, because they | ||
// refer to the environment or global state. | ||
var nonhermeticFunctions = []string{ | ||
// Date functions | ||
"date", | ||
"dateInZone", | ||
"dateModify", | ||
"now", | ||
"htmlDate", | ||
"htmlDateInZone", | ||
|
||
// Strings | ||
"randAlphaNum", | ||
"randAlpha", | ||
"randAscii", | ||
"randNumeric", | ||
"randBytes", | ||
"uuidv4", | ||
|
||
// OS | ||
"env", | ||
"expandenv", | ||
|
||
// Network | ||
"getHostByName", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package sprout | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const sprigFunctionCount = 203 | ||
|
||
func TestSprig_backward_compatibility(t *testing.T) { | ||
gfm := GenericFuncMap() | ||
assert.NotNil(t, gfm) | ||
assert.GreaterOrEqual(t, len(gfm), sprigFunctionCount) | ||
|
||
hfm := HtmlFuncMap() | ||
assert.NotNil(t, hfm) | ||
assert.GreaterOrEqual(t, len(hfm), sprigFunctionCount) | ||
|
||
tfm := TxtFuncMap() | ||
assert.NotNil(t, tfm) | ||
assert.GreaterOrEqual(t, len(tfm), sprigFunctionCount) | ||
|
||
hhfm := HermeticHtmlFuncMap() | ||
assert.NotNil(t, hhfm) | ||
assert.Equal(t, len(hhfm), len(gfm)-len(nonhermeticFunctions)) | ||
|
||
htfm := HermeticTxtFuncMap() | ||
assert.NotNil(t, htfm) | ||
assert.Equal(t, len(htfm), len(gfm)-len(nonhermeticFunctions)) | ||
} |