summaryrefslogtreecommitdiff
path: root/internal/funcs/base64.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2024-01-25 20:11:31 -0500
committerGitHub <noreply@github.com>2024-01-25 20:11:31 -0500
commitebb97fb7367fb983cffc1935a8fb57e4b80f5249 (patch)
tree43ef6cd01f629f60f59efe1e5b003f7c8e3a1257 /internal/funcs/base64.go
parentf1d9158ea99abbe556251c1ff2fe970f3b460ee9 (diff)
Move funcs package to internal (#1977)
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'internal/funcs/base64.go')
-rw-r--r--internal/funcs/base64.go77
1 files changed, 77 insertions, 0 deletions
diff --git a/internal/funcs/base64.go b/internal/funcs/base64.go
new file mode 100644
index 00000000..b25a9091
--- /dev/null
+++ b/internal/funcs/base64.go
@@ -0,0 +1,77 @@
+package funcs
+
+import (
+ "context"
+
+ "github.com/hairyhenderson/gomplate/v4/base64"
+ "github.com/hairyhenderson/gomplate/v4/conv"
+)
+
+// Base64NS - the base64 namespace
+//
+// Deprecated: don't use
+func Base64NS() *Base64Funcs {
+ return &Base64Funcs{}
+}
+
+// AddBase64Funcs -
+//
+// Deprecated: use [CreateBase64Funcs] instead
+func AddBase64Funcs(f map[string]interface{}) {
+ for k, v := range CreateBase64Funcs(context.Background()) {
+ f[k] = v
+ }
+}
+
+// CreateBase64Funcs -
+func CreateBase64Funcs(ctx context.Context) map[string]interface{} {
+ f := map[string]interface{}{}
+
+ ns := &Base64Funcs{ctx}
+ f["base64"] = func() interface{} { return ns }
+
+ return f
+}
+
+// Base64Funcs -
+type Base64Funcs struct {
+ ctx context.Context
+}
+
+// Encode -
+func (Base64Funcs) Encode(in interface{}) (string, error) {
+ b := toBytes(in)
+ return base64.Encode(b)
+}
+
+// Decode -
+func (Base64Funcs) Decode(in interface{}) (string, error) {
+ out, err := base64.Decode(conv.ToString(in))
+ return string(out), err
+}
+
+// DecodeBytes -
+func (Base64Funcs) DecodeBytes(in interface{}) ([]byte, error) {
+ out, err := base64.Decode(conv.ToString(in))
+ return out, err
+}
+
+type byter interface {
+ Bytes() []byte
+}
+
+func toBytes(in interface{}) []byte {
+ if in == nil {
+ return []byte{}
+ }
+ if s, ok := in.([]byte); ok {
+ return s
+ }
+ if s, ok := in.(byter); ok {
+ return s.Bytes()
+ }
+ if s, ok := in.(string); ok {
+ return []byte(s)
+ }
+ return []byte(conv.ToString(in))
+}