Skip to content

Commit

Permalink
implements gzip interpolation func
Browse files Browse the repository at this point in the history
  • Loading branch information
glerchundi committed Jan 25, 2016
1 parent 5f8b0e8 commit 243c662
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
27 changes: 27 additions & 0 deletions config/interpolate_funcs.go
Expand Up @@ -2,6 +2,7 @@ package config

import (
"bytes"
"compress/gzip"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
Expand Down Expand Up @@ -33,6 +34,7 @@ func Funcs() map[string]ast.Function {
"file": interpolationFuncFile(),
"format": interpolationFuncFormat(),
"formatlist": interpolationFuncFormatList(),
"gzip": interpolationFuncGzip(),
"index": interpolationFuncIndex(),
"join": interpolationFuncJoin(),
"length": interpolationFuncLength(),
Expand Down Expand Up @@ -320,6 +322,31 @@ func interpolationFuncFormatList() 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
},
}
}

// interpolationFuncIndex implements the "index" function that allows one to
// find the index of a specific element in a list
func interpolationFuncIndex() ast.Function {
Expand Down
12 changes: 12 additions & 0 deletions config/interpolate_funcs_test.go
Expand Up @@ -369,6 +369,18 @@ func TestInterpolateFuncFormatList(t *testing.T) {
})
}

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

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

0 comments on commit 243c662

Please sign in to comment.