Skip to content

Commit

Permalink
Merge pull request #17 from gerred/add-uuidv4
Browse files Browse the repository at this point in the history
Add UUIDv4 function
  • Loading branch information
technosophos authored Sep 30, 2016
2 parents 89eb698 + b8155b8 commit 8f797f5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
10 changes: 10 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ import (
"time"

util "github.com/aokoli/goutils"
uuid "github.com/satori/go.uuid"
)

// Produce the function map.
Expand Down Expand Up @@ -239,6 +240,7 @@ var nonhermeticFunctions = []string{
"randAlpha",
"randAscii",
"randNumeric",
"uuidv4",

// OS
"env",
Expand Down Expand Up @@ -365,6 +367,9 @@ var genericMap = map[string]interface{}{

// Crypto:
"genPrivateKey": generatePrivateKey,

// UUIDs:
"uuidv4": uuidv4,
}

func split(sep, orig string) map[string]string {
Expand Down Expand Up @@ -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())
}
21 changes: 21 additions & 0 deletions functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
Expand Down
8 changes: 5 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8f797f5

Please sign in to comment.