From b8155b86e4f8f98c76c78eb325d1e487ec5e82f2 Mon Sep 17 00:00:00 2001 From: Gerred Dillon Date: Fri, 30 Sep 2016 12:33:35 -0600 Subject: [PATCH] Add UUIDv4 function --- README.md | 1 + functions.go | 10 ++++++++++ functions_test.go | 21 +++++++++++++++++++++ glide.lock | 8 +++++--- glide.yaml | 2 ++ 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2ae36808..04c071f5 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ parse, it returns the time unaltered. See `time.ParseDuration` for info on durat - replace: Replace an old with a new in a string: `$name | replace " " "-"` - plural: Choose singular or plural based on length: `len $fish | plural "one anchovy" "many anchovies"` +- uuidv4: Generate a UUID v4 string ### String Slice Functions: diff --git a/functions.go b/functions.go index 2b0469a2..d9a63a73 100644 --- a/functions.go +++ b/functions.go @@ -181,6 +181,7 @@ import ( "time" util "github.com/aokoli/goutils" + uuid "github.com/satori/go.uuid" ) // Produce the function map. @@ -239,6 +240,7 @@ var nonhermeticFunctions = []string{ "randAlpha", "randAscii", "randNumeric", + "uuidv4", // OS "env", @@ -365,6 +367,9 @@ var genericMap = map[string]interface{}{ // Crypto: "genPrivateKey": generatePrivateKey, + + // UUIDs: + "uuidv4": uuidv4, } func split(sep, orig string) map[string]string { @@ -795,3 +800,8 @@ func untilStep(start, stop, step int) []int { } return v } + +// uuidv4 provides a safe and secure UUID v4 implementation +func uuidv4() string { + return fmt.Sprintf("%s", uuid.NewV4()) +} diff --git a/functions_test.go b/functions_test.go index 62f1af19..af67b54b 100644 --- a/functions_test.go +++ b/functions_test.go @@ -529,6 +529,27 @@ func TestGenPrivateKey(t *testing.T) { } } +func TestUUIDGeneration(t *testing.T) { + tpl := `{{uuidv4}}` + out, err := runRaw(tpl, nil) + if err != nil { + t.Error(err) + } + + if len(out) != 36 { + t.Error("Expected UUID of length 36") + } + + out2, err := runRaw(tpl, nil) + if err != nil { + t.Error(err) + } + + if out == out2 { + t.Error("Expected subsequent UUID generations to be different") + } +} + func runt(tpl, expect string) error { return runtv(tpl, expect, map[string]string{}) } diff --git a/glide.lock b/glide.lock index 07145619..c3894b56 100644 --- a/glide.lock +++ b/glide.lock @@ -1,6 +1,8 @@ -hash: e3e0c601d62e05e5e182697a6c20bdd7ea534577ae8e08b7196f33ca5378acc4 -updated: 2015-12-23T07:41:07.419749413-07:00 +hash: a8ed42a70698b4d199b5de7fa33e7c48251651e6ccf97d007f546cb72a5d0f8f +updated: 2016-09-30T12:23:39.512939213-06:00 imports: - name: github.com/aokoli/goutils version: 9c37978a95bd5c709a15883b6242714ea6709e64 -devImports: [] +- name: github.com/satori/go.uuid + version: 879c5887cd475cd7864858769793b2ceb0d44feb +testImports: [] diff --git a/glide.yaml b/glide.yaml index 87f8ef0b..2fc21e24 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,3 +1,5 @@ package: github.com/Masterminds/sprig import: - package: github.com/aokoli/goutils +- package: github.com/satori/go.uuid + version: ^1.1.0