Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Sprig has the following encoding and decoding functions:

- `b64enc`/`b64dec`: Encode or decode with Base64
- `b32enc`/`b32dec`: Encode or decode with Base32
- `printf "%x"` / `hexdec`: Encode or decode as hexadecimal
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The Sprig library provides over 70 template functions for Go's template language
- [Float Math Functions](mathf.md): `addf`, `maxf`, `mulf`, etc.
- [Date Functions](date.md): `now`, `date`, etc.
- [Defaults Functions](defaults.md): `default`, `empty`, `coalesce`, `fromJson`, `toJson`, `toPrettyJson`, `toRawJson`, `ternary`
- [Encoding Functions](encoding.md): `b64enc`, `b64dec`, etc.
- [Encoding Functions](encoding.md): `b64enc`, `b64dec`, `hexdec`
- [Lists and List Functions](lists.md): `list`, `first`, `uniq`, etc.
- [Dictionaries and Dict Functions](dicts.md): `get`, `set`, `dict`, `hasKey`, `pluck`, `dig`, `deepCopy`, etc.
- [Type Conversion Functions](conversion.md): `atoi`, `int64`, `toString`, etc.
Expand Down
1 change: 1 addition & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ var genericMap = map[string]interface{}{
"b64dec": base64decode,
"b32enc": base32encode,
"b32dec": base32decode,
"hexdec": hexdecode,

// Data Structures:
"tuple": list, // FIXME: with the addition of append/prepend these are no longer immutable.
Expand Down
9 changes: 9 additions & 0 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sprig
import (
"encoding/base32"
"encoding/base64"
"encoding/hex"
"fmt"
"reflect"
"strconv"
Expand Down Expand Up @@ -35,6 +36,14 @@ func base32decode(v string) string {
return string(data)
}

func hexdecode(v string) string {
data, err := hex.DecodeString(v)
if err != nil {
return err.Error()
}
return string(data)
}

func abbrev(width int, s string) string {
if width < 4 {
return s
Expand Down
10 changes: 10 additions & 0 deletions strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ func TestBase64EncodeDecode(t *testing.T) {
t.Error(err)
}
}

func TestHexEncodeDecode(t *testing.T) {
magicWord := "\x00coffee\xfb"

tpl := `{{printf "%x" "\x00coffee\xfb" | hexdec }}`
if err := runt(tpl, magicWord); err != nil {
t.Error(err)
}
}

func TestBase32EncodeDecode(t *testing.T) {
magicWord := "coffee"
expect := base32.StdEncoding.EncodeToString([]byte(magicWord))
Expand Down