Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements gzip interpolation func #3858

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions config/interpolate_funcs.go
@@ -1,6 +1,8 @@
package config

import (
"bytes"
"compress/gzip"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
Expand Down Expand Up @@ -57,6 +59,7 @@ func Funcs() map[string]ast.Function {
"basename": interpolationFuncBasename(),
"base64decode": interpolationFuncBase64Decode(),
"base64encode": interpolationFuncBase64Encode(),
"base64gzip": interpolationFuncBase64Gzip(),
"base64sha256": interpolationFuncBase64Sha256(),
"base64sha512": interpolationFuncBase64Sha512(),
"ceil": interpolationFuncCeil(),
Expand Down Expand Up @@ -1178,6 +1181,32 @@ func interpolationFuncBase64Decode() ast.Function {
}
}

// interpolationFuncBase64Gzip implements the "gzip" function that allows gzip
// compression encoding the result using base64
func interpolationFuncBase64Gzip() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
s := args[0].(string)

var b bytes.Buffer
gz := gzip.NewWriter(&b)
if _, err := gz.Write([]byte(s)); err != nil {
return "", fmt.Errorf("failed to write gzip raw data: '%s'", s)
}
if err := gz.Flush(); err != nil {
return "", fmt.Errorf("failed to flush gzip writer: '%s'", s)
}
if err := gz.Close(); err != nil {
return "", fmt.Errorf("failed to close gzip writer: '%s'", s)
}

return base64.StdEncoding.EncodeToString(b.Bytes()), nil
},
}
}

// interpolationFuncLower implements the "lower" function that does
// string lower casing.
func interpolationFuncLower() ast.Function {
Expand Down
12 changes: 12 additions & 0 deletions config/interpolate_funcs_test.go
Expand Up @@ -2215,6 +2215,18 @@ func TestInterpolateFuncTrimSpace(t *testing.T) {
})
}

func TestInterpolateFuncBase64Gzip(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${base64gzip("test")}`,
"H4sIAAAAAAAA/ypJLS4BAAAA//8BAAD//wx+f9gEAAAA",
false,
},
},
})
}

func TestInterpolateFuncBase64Sha256(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
Expand Down