Skip to content

Commit a9e9385

Browse files
committed
add hexdec encoding function
1 parent 53f76d8 commit a9e9385

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

docs/encoding.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Sprig has the following encoding and decoding functions:
44

55
- `b64enc`/`b64dec`: Encode or decode with Base64
66
- `b32enc`/`b32dec`: Encode or decode with Base32
7+
- `printf "%x"` / `hexdec`: Encode or decode as hexadecimal

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The Sprig library provides over 70 template functions for Go's template language
99
- [Float Math Functions](mathf.md): `addf`, `maxf`, `mulf`, etc.
1010
- [Date Functions](date.md): `now`, `date`, etc.
1111
- [Defaults Functions](defaults.md): `default`, `empty`, `coalesce`, `fromJson`, `toJson`, `toPrettyJson`, `toRawJson`, `ternary`
12-
- [Encoding Functions](encoding.md): `b64enc`, `b64dec`, etc.
12+
- [Encoding Functions](encoding.md): `b64enc`, `b64dec`, `hexdec`
1313
- [Lists and List Functions](lists.md): `list`, `first`, `uniq`, etc.
1414
- [Dictionaries and Dict Functions](dicts.md): `get`, `set`, `dict`, `hasKey`, `pluck`, `dig`, `deepCopy`, etc.
1515
- [Type Conversion Functions](conversion.md): `atoi`, `int64`, `toString`, etc.

functions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ var genericMap = map[string]interface{}{
289289
"b64dec": base64decode,
290290
"b32enc": base32encode,
291291
"b32dec": base32decode,
292+
"hexdec": hexdecode,
292293

293294
// Data Structures:
294295
"tuple": list, // FIXME: with the addition of append/prepend these are no longer immutable.

strings.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sprig
33
import (
44
"encoding/base32"
55
"encoding/base64"
6+
"encoding/hex"
67
"fmt"
78
"reflect"
89
"strconv"
@@ -35,6 +36,14 @@ func base32decode(v string) string {
3536
return string(data)
3637
}
3738

39+
func hexdecode(v string) string {
40+
data, err := hex.DecodeString(v)
41+
if err != nil {
42+
return err.Error()
43+
}
44+
return string(data)
45+
}
46+
3847
func abbrev(width int, s string) string {
3948
if width < 4 {
4049
return s

strings_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ func TestBase64EncodeDecode(t *testing.T) {
171171
t.Error(err)
172172
}
173173
}
174+
175+
func TestHexEncodeDecode(t *testing.T) {
176+
magicWord := "\x00coffee\xfb"
177+
178+
tpl := `{{printf "%x" "\x00coffee\xfb" | hexdec }}`
179+
if err := runt(tpl, magicWord); err != nil {
180+
t.Error(err)
181+
}
182+
}
183+
174184
func TestBase32EncodeDecode(t *testing.T) {
175185
magicWord := "coffee"
176186
expect := base32.StdEncoding.EncodeToString([]byte(magicWord))

0 commit comments

Comments
 (0)