github.com/avenga/couper@v1.12.2/eval/lib/base64.go (about) 1 package lib 2 3 import ( 4 "encoding/base64" 5 6 "github.com/zclconf/go-cty/cty" 7 "github.com/zclconf/go-cty/cty/function" 8 ) 9 10 var ( 11 Base64DecodeFunc = newBase64DecodeFunction() 12 Base64EncodeFunc = newBase64EncodeFunction() 13 ) 14 15 func newBase64DecodeFunction() function.Function { 16 return function.New(&function.Spec{ 17 Params: []function.Parameter{{ 18 Name: "base64_decode", 19 Type: cty.String, 20 }}, 21 Type: function.StaticReturnType(cty.String), 22 Impl: func(args []cty.Value, _ cty.Type) (ret cty.Value, err error) { 23 first := args[0] 24 result, err := base64.StdEncoding.DecodeString(first.AsString()) 25 if err != nil { 26 return cty.StringVal(""), err 27 } 28 return cty.StringVal(string(result)), nil 29 }, 30 }) 31 } 32 33 func newBase64EncodeFunction() function.Function { 34 return function.New(&function.Spec{ 35 Params: []function.Parameter{{ 36 Name: "base64_encode", 37 Type: cty.String, 38 }}, 39 Type: function.StaticReturnType(cty.String), 40 Impl: func(args []cty.Value, _ cty.Type) (ret cty.Value, err error) { 41 first := args[0] 42 result := base64.StdEncoding.EncodeToString([]byte(first.AsString())) 43 return cty.StringVal(result), nil 44 }, 45 }) 46 }