Skip to content

Commit

Permalink
add uuid_v7 function with specified timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
rockwotj committed Jan 30, 2025
1 parent eac37d8 commit 4a0d66b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
26 changes: 20 additions & 6 deletions internal/bloblang/query/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,18 +923,32 @@ var _ = registerSimpleFunction(
},
)

var _ = registerSimpleFunction(
var _ = registerFunction(
NewFunctionSpec(
FunctionCategoryGeneral, "uuid_v7",
"Generates a new time ordered UUID each time it is invoked and prints a string representation.",
NewExampleSpec("", `root.id = uuid_v7()`),
),
func(_ FunctionContext) (any, error) {
u7, err := uuid.NewV7()
NewExampleSpec("It is also possible to specify the timestamp for the uuid_v7", `root.id = uuid_v7(now().ts_sub_iso8601("PT1M"))`),
).Param(ParamTimestamp("time", "An optional timestamp to use for the time ordered portion of the UUID.").Optional()),
func(args *ParsedParams) (Function, error) {
time, err := args.FieldOptionalTimestamp("time")
if err != nil {
panic(err)
return nil, err
}
return u7.String(), nil
return ClosureFunction("function uuid_v7", func(fctx FunctionContext) (any, error) {
if time == nil {
u7, err := uuid.NewV7()
if err != nil {
return nil, fmt.Errorf("unable to generate uuid v7: %w", err)
}
return u7.String(), nil
}
u7, err := uuid.NewV7AtTime(*time)
if err != nil {
return nil, fmt.Errorf("unable to generate uuid v7 at time %s: %w", *time, err)
}
return u7.String(), nil
}, nil), nil
},
)

Expand Down
29 changes: 29 additions & 0 deletions internal/bloblang/query/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"math"
"sync"
"testing"
"time"

"github.com/gofrs/uuid/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -244,6 +246,33 @@ func TestFunctionTargets(t *testing.T) {
}
}

func TestUUIDV7Function(t *testing.T) {
e, err := InitFunctionHelper("uuid_v7")
require.NoError(t, err)

res, err := e.Exec(FunctionContext{})
require.NoError(t, err)
require.IsType(t, "", res)
u7 := uuid.FromStringOrNil(res.(string))
assert.Equal(t, u7.Version(), byte(7))
}

func TestUUIDV7FunctionAtTime(t *testing.T) {
ts := time.Now().Add(-5 * time.Hour)
e, err := InitFunctionHelper("uuid_v7", ts)
require.NoError(t, err)

res, err := e.Exec(FunctionContext{})
require.NoError(t, err)
require.IsType(t, "", res)
u7 := uuid.FromStringOrNil(res.(string))
u7ts, err := uuid.TimestampFromV7(u7)
require.NoError(t, err)
actual, err := u7ts.Time()
require.NoError(t, err)
assert.Equal(t, ts.Truncate(time.Millisecond), actual, "expected: %s, got: %s", ts, actual)
}

func TestNanoidFunction(t *testing.T) {
e, err := InitFunctionHelper("nanoid")
require.NoError(t, err)
Expand Down

0 comments on commit 4a0d66b

Please sign in to comment.