From b1fcb3a8b00225946f02e87967c069ad8aa846c2 Mon Sep 17 00:00:00 2001 From: Rodrigo Chacon Date: Wed, 24 Aug 2016 05:58:43 -0300 Subject: [PATCH] feat(config) add gzip interpolation func from #3858 on top of v0.7.1 --- config/interpolate_funcs.go | 28 ++++++++++++++++++++++++++++ config/interpolate_funcs_test.go | 12 ++++++++++++ 2 files changed, 40 insertions(+) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index d66073ec6806..f341310f335b 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -1,6 +1,8 @@ package config import ( + "bytes" + "compress/gzip" "crypto/md5" "crypto/sha1" "crypto/sha256" @@ -65,6 +67,7 @@ func Funcs() map[string]ast.Function { "file": interpolationFuncFile(), "format": interpolationFuncFormat(), "formatlist": interpolationFuncFormatList(), + "gzip": interpolationFuncGzip(), "index": interpolationFuncIndex(), "join": interpolationFuncJoin(), "jsonencode": interpolationFuncJSONEncode(), @@ -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 + }, + } +} diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 0b77af328913..1733e3fbb263 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -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