summaryrefslogtreecommitdiff
path: root/internal/funcs/base64.go
blob: b7c866579e481141113fd34c524dea1707d9b3db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package funcs

import (
	"context"

	"github.com/hairyhenderson/gomplate/v4/base64"
	"github.com/hairyhenderson/gomplate/v4/conv"
)

// CreateBase64Funcs -
func CreateBase64Funcs(ctx context.Context) map[string]any {
	f := map[string]any{}

	ns := &Base64Funcs{ctx}
	f["base64"] = func() any { return ns }

	return f
}

// Base64Funcs -
type Base64Funcs struct {
	ctx context.Context
}

// Encode -
func (Base64Funcs) Encode(in any) (string, error) {
	b := toBytes(in)
	return base64.Encode(b)
}

// Decode -
func (Base64Funcs) Decode(in any) (string, error) {
	out, err := base64.Decode(conv.ToString(in))
	return string(out), err
}

// DecodeBytes -
func (Base64Funcs) DecodeBytes(in any) ([]byte, error) {
	out, err := base64.Decode(conv.ToString(in))
	return out, err
}

type byter interface {
	Bytes() []byte
}

func toBytes(in any) []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))
}