Skip to content

Commit

Permalink
feat(config) add gzip interpolation func from hashicorp#3858 on top o…
Browse files Browse the repository at this point in the history
…f v0.7.1
  • Loading branch information
rochacon committed Aug 24, 2016
1 parent 55ba6eb commit b1fcb3a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
28 changes: 28 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 @@ -65,6 +67,7 @@ func Funcs() map[string]ast.Function {
"file": interpolationFuncFile(),
"format": interpolationFuncFormat(),
"formatlist": interpolationFuncFormatList(),
"gzip": interpolationFuncGzip(),
"index": interpolationFuncIndex(),
"join": interpolationFuncJoin(),
"jsonencode": interpolationFuncJSONEncode(),
Expand Down Expand Up @@ -996,3 +999,28 @@ func interpolationFuncUUID() ast.Function {
},
}
}

// interpolationFuncGzip implements the "gzip" function that allows gzip compression.
func interpolationFuncGzip() 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 gzip raw data: '%s'", s)
}
if err := gz.Flush(); err != nil {
return "", fmt.Errorf("failed to gzip raw data: '%s'", s)
}
if err := gz.Close(); err != nil {
return "", fmt.Errorf("failed to gzip raw data: '%s'", s)
}

return b.String(), nil
},
}
}
12 changes: 12 additions & 0 deletions config/interpolate_funcs_test.go
Expand Up @@ -1626,6 +1626,18 @@ func TestInterpolateFuncUUID(t *testing.T) {
}
}

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

type testFunctionConfig struct {
Cases []testFunctionCase
Vars map[string]ast.Variable
Expand Down

0 comments on commit b1fcb3a

Please sign in to comment.