File: uuid_v4.go

package info (click to toggle)
golang-github-hashicorp-go-cty-funcs 0.0~git20241120.c51673e-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: makefile: 2
file content (28 lines) | stat: -rw-r--r-- 711 bytes parent folder | download | duplicates (3)
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
package uuid

import (
	"github.com/google/uuid"
	"github.com/zclconf/go-cty/cty"
	"github.com/zclconf/go-cty/cty/function"
)

var V4Func = function.New(&function.Spec{
	Params: []function.Parameter{},
	Type:   function.StaticReturnType(cty.String),
	Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) {
		uuid, err := uuid.NewRandom()
		if err != nil {
			return cty.UnknownVal(cty.String), err
		}
		return cty.StringVal(uuid.String()), nil
	},
})

// V4 generates and returns a Type-4 UUID in the standard hexadecimal string
// format.
//
// This is not a "pure" function: it will generate a different result for each
// call.
func V4() (cty.Value, error) {
	return V4Func.Call(nil)
}