Skip to content

Commit

Permalink
Update to latest gocommon
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Aug 2, 2024
1 parent 219b632 commit 74e0019
Show file tree
Hide file tree
Showing 51 changed files with 150 additions and 150 deletions.
6 changes: 3 additions & 3 deletions cmd/docgen/docs/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ func buildTemplateContext(items map[string][]*TaggedItem) (map[string]string, er

defer random.SetGenerator(random.DefaultGenerator)
defer uuids.SetGenerator(uuids.DefaultGenerator)
defer dates.SetNowSource(dates.DefaultNowSource)
defer dates.SetNowFunc(time.Now)

random.SetGenerator(random.NewSeededGenerator(123456))
uuids.SetGenerator(uuids.NewSeededGenerator(123456))
dates.SetNowSource(dates.NewFixedNowSource(time.Date(2018, 4, 11, 18, 24, 30, 123456000, time.UTC)))
uuids.SetGenerator(uuids.NewSeededGenerator(123456, time.Now))
dates.SetNowFunc(dates.NewFixedNow(time.Date(2018, 4, 11, 18, 24, 30, 123456000, time.UTC)))

session, _, err := test.CreateTestSession(server.URL, envs.RedactionPolicyNone)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/flowrunner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func RunFlow(eng flows.Engine, assetsPath string, flowUUID assets.FlowUUID, init
}

func createMessage(contact *flows.Contact, text string) *flows.MsgIn {
return flows.NewMsgIn(flows.MsgUUID(uuids.New()), contact.URNs()[0].URN(), nil, text, []utils.Attachment{})
return flows.NewMsgIn(flows.MsgUUID(uuids.NewV4()), contact.URNs()[0].URN(), nil, text, []utils.Attachment{})
}

func printEvents(log []flows.Event, out io.Writer) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/flowxgettext/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

func TestFlowXGetText(t *testing.T) {
defer dates.SetNowSource(dates.DefaultNowSource)
dates.SetNowSource(dates.NewFixedNowSource(time.Date(2020, 3, 25, 13, 57, 30, 123456789, time.UTC)))
defer dates.SetNowFunc(time.Now)
dates.SetNowFunc(dates.NewFixedNow(time.Date(2020, 3, 25, 13, 57, 30, 123456789, time.UTC)))

out := &strings.Builder{}

Expand Down
4 changes: 2 additions & 2 deletions envs/dates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func TestDateTimeFromString(t *testing.T) {
{envs.DateFormatYearMonthDay, envs.TimeFormatHourMinute, "UTC", false, "2001-02-01 03:15:34.123456", "01-02-2001 03:15:34.123456 +0000 UTC", false},
}

dates.SetNowSource(dates.NewFixedNowSource(time.Date(2018, 9, 13, 13, 36, 30, 123456789, time.UTC)))
defer dates.SetNowSource(dates.DefaultNowSource)
dates.SetNowFunc(dates.NewFixedNow(time.Date(2018, 9, 13, 13, 36, 30, 123456789, time.UTC)))
defer dates.SetNowFunc(time.Now)

for _, tc := range testCases {
timezone, err := time.LoadLocation(tc.Timezone)
Expand Down
4 changes: 2 additions & 2 deletions excellent/functions/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,10 +779,10 @@ func TestFunctions(t *testing.T) {
}

defer random.SetGenerator(random.DefaultGenerator)
defer dates.SetNowSource(dates.DefaultNowSource)
defer dates.SetNowFunc(time.Now)

random.SetGenerator(random.NewSeededGenerator(123456))
dates.SetNowSource(dates.NewFixedNowSource(time.Date(2018, 4, 11, 13, 24, 30, 123456000, time.UTC)))
dates.SetNowFunc(dates.NewFixedNow(time.Date(2018, 4, 11, 13, 24, 30, 123456000, time.UTC)))

for _, tc := range funcTests {
testID := fmt.Sprintf("%s(%#v)", tc.name, tc.args)
Expand Down
4 changes: 2 additions & 2 deletions excellent/types/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func TestToXDateTime(t *testing.T) {
}

func TestToXDateTimeWithTimeFill(t *testing.T) {
dates.SetNowSource(dates.NewFixedNowSource(time.Date(2018, 9, 13, 13, 36, 30, 123456789, time.UTC)))
defer dates.SetNowSource(dates.DefaultNowSource)
dates.SetNowFunc(dates.NewFixedNow(time.Date(2018, 9, 13, 13, 36, 30, 123456789, time.UTC)))
defer dates.SetNowFunc(time.Now)

env := envs.NewBuilder().Build()
result, err := types.ToXDateTimeWithTimeFill(env, types.NewXText("2018/12/20"))
Expand Down
12 changes: 6 additions & 6 deletions flows/actions/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ func testActionType(t *testing.T, assetsJSON json.RawMessage, typeName string) {

jsonx.MustUnmarshal(testFile, &tests)

defer dates.SetNowSource(dates.DefaultNowSource)
defer dates.SetNowFunc(time.Now)
defer uuids.SetGenerator(uuids.DefaultGenerator)
defer httpx.SetRequestor(httpx.DefaultRequestor)
defer smtpx.SetSender(smtpx.DefaultSender)

for i, tc := range tests {
dates.SetNowSource(dates.NewFixedNowSource(time.Date(2018, 10, 18, 14, 20, 30, 123456, time.UTC)))
uuids.SetGenerator(uuids.NewSeededGenerator(12345))
dates.SetNowFunc(dates.NewFixedNow(time.Date(2018, 10, 18, 14, 20, 30, 123456, time.UTC)))
uuids.SetGenerator(uuids.NewSeededGenerator(12345, time.Now))

var clonedMocks *httpx.MockRequestor
if tc.HTTPMocks != nil {
Expand Down Expand Up @@ -751,10 +751,10 @@ func TestReadAction(t *testing.T) {
}

func TestResthookPayload(t *testing.T) {
uuids.SetGenerator(uuids.NewSeededGenerator(123456))
dates.SetNowSource(dates.NewSequentialNowSource(time.Date(2018, 7, 6, 12, 30, 0, 123456789, time.UTC)))
uuids.SetGenerator(uuids.NewSeededGenerator(123456, time.Now))
dates.SetNowFunc(dates.NewSequentialNow(time.Date(2018, 7, 6, 12, 30, 0, 123456789, time.UTC), time.Second))
defer uuids.SetGenerator(uuids.DefaultGenerator)
defer dates.SetNowSource(dates.DefaultNowSource)
defer dates.SetNowFunc(time.Now)

server := test.NewTestHTTPServer(49999)
defer server.Close()
Expand Down
4 changes: 2 additions & 2 deletions flows/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flows_test

import (
"testing"
"time"

"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"
Expand All @@ -10,14 +11,13 @@ import (
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/test"

"github.com/stretchr/testify/assert"
)

func TestChannel(t *testing.T) {
env := envs.NewBuilder().Build()

uuids.SetGenerator(uuids.NewSeededGenerator(1234))
uuids.SetGenerator(uuids.NewSeededGenerator(1234, time.Now))
defer uuids.SetGenerator(uuids.DefaultGenerator)

rolesDefault := []assets.ChannelRole{assets.ChannelRoleSend, assets.ChannelRoleReceive}
Expand Down
2 changes: 1 addition & 1 deletion flows/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func NewContact(
// NewEmptyContact creates a new empy contact with the passed in name, language and location
func NewEmptyContact(sa SessionAssets, name string, language i18n.Language, timezone *time.Location) *Contact {
return &Contact{
uuid: ContactUUID(uuids.New()),
uuid: ContactUUID(uuids.NewV4()),
name: name,
language: language,
status: ContactStatusActive,
Expand Down
6 changes: 3 additions & 3 deletions flows/contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func TestContact(t *testing.T) {

android := sa.Channels().Get("294a14d4-c998-41e5-a314-5941b97b89d7")

uuids.SetGenerator(uuids.NewSeededGenerator(1234))
uuids.SetGenerator(uuids.NewSeededGenerator(1234, time.Now))
defer uuids.SetGenerator(uuids.DefaultGenerator)

tz, _ := time.LoadLocation("America/Bogota")

contact, err := flows.NewContact(
sa,
flows.ContactUUID(uuids.New()),
flows.ContactUUID(uuids.NewV4()),
flows.ContactID(12345),
"Joe Bloggs",
i18n.Language("eng"),
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestContactFormat(t *testing.T) {
// if not we fallback to URN
contact, _ = flows.NewContact(
sa,
flows.ContactUUID(uuids.New()),
flows.ContactUUID(uuids.NewV4()),
flows.ContactID(1234),
"",
i18n.NilLanguage,
Expand Down
14 changes: 7 additions & 7 deletions flows/definition/legacy/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ func migrateRuleSet(lang i18n.Language, r RuleSet, validDests map[uuids.UUID]boo
flowRef := assets.NewFlowReference(assets.FlowUUID(config.Flow.UUID), config.Flow.Name)

newActions = []migratedAction{
newEnterFlowAction(uuids.New(), flowRef, false),
newEnterFlowAction(uuids.NewV4(), flowRef, false),
}

// subflow rulesets operate on the child flow status
Expand Down Expand Up @@ -572,7 +572,7 @@ func migrateRuleSet(lang i18n.Language, r RuleSet, validDests map[uuids.UUID]boo
}

newActions = []migratedAction{
newCallWebhookAction(uuids.New(), method, migratedURL, headers, body, resultName),
newCallWebhookAction(uuids.NewV4(), method, migratedURL, headers, body, resultName),
}

// webhook rulesets operate on the webhook status, saved as category
Expand All @@ -582,7 +582,7 @@ func migrateRuleSet(lang i18n.Language, r RuleSet, validDests map[uuids.UUID]boo

case "resthook":
newActions = []migratedAction{
newCallResthookAction(uuids.New(), config.Resthook, resultName),
newCallResthookAction(uuids.NewV4(), config.Resthook, resultName),
}

// resthook rulesets operate on the webhook status, saved as category
Expand Down Expand Up @@ -711,7 +711,7 @@ func migrateRuleSet(lang i18n.Language, r RuleSet, validDests map[uuids.UUID]boo
}

newActions = []migratedAction{
newTransferAirtimeAction(uuids.New(), currencyAmounts, resultName),
newTransferAirtimeAction(uuids.NewV4(), currencyAmounts, resultName),
}

operand := fmt.Sprintf("@results.%s", utils.Snakify(resultName))
Expand Down Expand Up @@ -781,7 +781,7 @@ func migrateRules(baseLanguage i18n.Language, r RuleSet, validDests map[uuids.UU
exit := newExit(rule.UUID, destinationUUID)
exits = append(exits, exit)

category := newCategory(uuids.New(), baseName, exit.UUID())
category := newCategory(uuids.NewV4(), baseName, exit.UUID())
categories = append(categories, category)

converted = &categoryAndExit{category, exit}
Expand Down Expand Up @@ -835,7 +835,7 @@ func migrateRule(baseLanguage i18n.Language, r Rule, category migratedCategory,
var arguments []string
var err error

caseUUID := uuids.New()
caseUUID := uuids.NewV4()
var caseUI map[string]any

switch r.Test.Type {
Expand Down Expand Up @@ -1094,7 +1094,7 @@ func (f *Flow) Migrate(baseMediaURL string) ([]byte, error) {
if uuid == "" {
uuid = f.UUID
if uuid == "" {
uuid = uuids.New()
uuid = uuids.NewV4()
}
}
if name == "" {
Expand Down
7 changes: 4 additions & 3 deletions flows/definition/legacy/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/uuids"
Expand Down Expand Up @@ -139,7 +140,7 @@ func TestFlowMigration(t *testing.T) {
defer uuids.SetGenerator(uuids.DefaultGenerator)

for _, tc := range tests {
uuids.SetGenerator(uuids.NewSeededGenerator(123456))
uuids.SetGenerator(uuids.NewSeededGenerator(123456, time.Now))

migratedFlowJSON, err := legacy.MigrateDefinition(tc.Legacy, "https://myfiles.com")
require.NoError(t, err)
Expand Down Expand Up @@ -188,7 +189,7 @@ func TestTestMigration(t *testing.T) {
defer uuids.SetGenerator(uuids.DefaultGenerator)

for _, tc := range tests {
uuids.SetGenerator(uuids.NewSeededGenerator(123456))
uuids.SetGenerator(uuids.NewSeededGenerator(123456, time.Now))

legacyFlowJSON := fmt.Sprintf(legacyTestHolderDef, string(tc.LegacyTest))
migratedFlowJSON, err := legacy.MigrateDefinition(json.RawMessage(legacyFlowJSON), "https://myfiles.com")
Expand Down Expand Up @@ -232,7 +233,7 @@ func TestRuleSetMigration(t *testing.T) {
defer uuids.SetGenerator(uuids.DefaultGenerator)

for _, tc := range tests {
uuids.SetGenerator(uuids.NewSeededGenerator(123456))
uuids.SetGenerator(uuids.NewSeededGenerator(123456, time.Now))

legacyFlowJSON := fmt.Sprintf(legacyRuleSetHolderDef, string(tc.LegacyRuleSet))
migratedFlowJSON, err := legacy.MigrateDefinition(json.RawMessage(legacyFlowJSON), "https://myfiles.com")
Expand Down
4 changes: 2 additions & 2 deletions flows/definition/legacy/expressions/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func TestMigrateFunctionCall(t *testing.T) {
session, _, err := test.CreateTestSession(server.URL, envs.RedactionPolicyNone)
require.NoError(t, err)

defer dates.SetNowSource(dates.DefaultNowSource)
dates.SetNowSource(dates.NewFixedNowSource(time.Date(2018, 4, 11, 13, 24, 30, 123456000, time.UTC)))
defer dates.SetNowFunc(time.Now)
dates.SetNowFunc(dates.NewFixedNow(time.Date(2018, 4, 11, 13, 24, 30, 123456000, time.UTC)))

for _, tc := range tests {
migratedTemplate, err := expressions.MigrateTemplate(tc.old, nil)
Expand Down
4 changes: 2 additions & 2 deletions flows/definition/legacy/expressions/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ func TestLegacyTests(t *testing.T) {

env := envs.NewBuilder().WithDateFormat(envs.DateFormatDayMonthYear).WithTimezone(tz).Build()
if tc.Context.Now != nil {
dates.SetNowSource(dates.NewFixedNowSource(*tc.Context.Now))
defer dates.SetNowSource(dates.DefaultNowSource)
dates.SetNowFunc(dates.NewFixedNow(*tc.Context.Now))
defer dates.SetNowFunc(time.Now)
}

migratedVars := tc.Context.Variables.Migrate().Context(env)
Expand Down
2 changes: 1 addition & 1 deletion flows/definition/legacy/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (u *UI) AddNode(uuid uuids.UUID, nodeDetails *NodeUI) {

// AddSticky adds a new sticky note
func (u *UI) AddSticky(sticky Sticky) {
u.Stickies[uuids.New()] = sticky
u.Stickies[uuids.NewV4()] = sticky
}

// Position is a position of a node in the editor canvas
Expand Down
4 changes: 2 additions & 2 deletions flows/definition/migrations/13_x.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func Migrate13_4(f Flow, cfg *Config) (Flow, error) {
templating, _ := action["templating"].(map[string]any)
if templating != nil {
templatingUUID := GetObjectUUID(templating)
bodyCompUUID := uuids.New()
bodyCompUUID := uuids.NewV4()
variables, _ := templating["variables"].([]any)
if variables == nil {
variables = []any{}
Expand Down Expand Up @@ -167,7 +167,7 @@ func Migrate13_1(f Flow, cfg *Config) (Flow, error) {
if action.Type() == "send_msg" {
templating, _ := action["templating"].(map[string]any)
if templating != nil {
templating["uuid"] = uuids.New()
templating["uuid"] = uuids.NewV4()
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions flows/definition/migrations/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func remapUUIDs(data map[string]any, depMapping map[uuids.UUID]uuids.UUID) {
}
mapped, exists := mapping[u]
if !exists {
mapped = uuids.New()
mapped = uuids.NewV4()
mapping[u] = mapped
}
return mapped
Expand All @@ -157,7 +157,7 @@ func remapUUIDs(data map[string]any, depMapping map[uuids.UUID]uuids.UUID) {
if isString {
obj[p] = replaceUUID(uuids.UUID(asString))
}
} else if uuids.IsV4(p) {
} else if uuids.Is(p) {
newProperty := string(replaceUUID(uuids.UUID(p)))
obj[newProperty] = v
delete(obj, p)
Expand All @@ -168,7 +168,7 @@ func remapUUIDs(data map[string]any, depMapping map[uuids.UUID]uuids.UUID) {
arrayCallback := func(path string, arr []any) {
for i, v := range arr {
asString, isString := v.(string)
if isString && uuids.IsV4(asString) {
if isString && uuids.Is(asString) {
arr[i] = replaceUUID(uuids.UUID(asString))
}
}
Expand Down
13 changes: 7 additions & 6 deletions flows/definition/migrations/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"sort"
"testing"
"time"

"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/uuids"
Expand Down Expand Up @@ -49,7 +50,7 @@ func TestMigrateToVersion(t *testing.T) {
for _, tc := range tests {
testName := fmt.Sprintf("version %s with '%s'", version, tc.Description)

uuids.SetGenerator(uuids.NewSeededGenerator(123456))
uuids.SetGenerator(uuids.NewSeededGenerator(123456, time.Now))

actual, err := migrations.MigrateToVersion(tc.Original, version, cfg)
assert.NoError(t, err, "unexpected error in %s", testName)
Expand Down Expand Up @@ -154,7 +155,7 @@ func TestClone(t *testing.T) {
}

for _, tc := range testCases {
uuids.SetGenerator(uuids.NewSeededGenerator(12345))
uuids.SetGenerator(uuids.NewSeededGenerator(12345, time.Now))
defer uuids.SetGenerator(uuids.DefaultGenerator)

flow, err := test.LoadFlowFromAssets(env, tc.path, assets.FlowUUID(tc.uuid))
Expand All @@ -180,9 +181,9 @@ func TestClone(t *testing.T) {
assert.Equal(t, flow.Revision(), clone.Revision())
assert.Equal(t, len(flow.Nodes()), len(clone.Nodes()))

// extract all UUIDs from originaland cloned definitions
originalUUIDs := uuids.V4Regex.FindAllString(string(flowJSON), -1)
cloneUUIDs := uuids.V4Regex.FindAllString(string(cloneJSON), -1)
// extract all UUIDs from original and cloned definitions
originalUUIDs := uuids.Regex.FindAllString(string(flowJSON), -1)
cloneUUIDs := uuids.Regex.FindAllString(string(cloneJSON), -1)

assert.Equal(t, len(originalUUIDs), len(cloneUUIDs))
assert.NotContains(t, cloneUUIDs, []string{"2aad21f6-30b7-42c5-bd7f-1b720c154817"}) // group used in has_group test
Expand Down Expand Up @@ -210,7 +211,7 @@ func TestClone(t *testing.T) {
}

func TestCloneOlderVersion(t *testing.T) {
uuids.SetGenerator(uuids.NewSeededGenerator(12345))
uuids.SetGenerator(uuids.NewSeededGenerator(12345, time.Now))
defer uuids.SetGenerator(uuids.DefaultGenerator)

cloneJSON, err := migrations.Clone([]byte(`{
Expand Down
2 changes: 1 addition & 1 deletion flows/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type engine struct {
// NewSession creates a new session
func (e *engine) NewSession(sa flows.SessionAssets, trigger flows.Trigger) (flows.Session, flows.Sprint, error) {
s := &session{
uuid: flows.SessionUUID(uuids.New()),
uuid: flows.SessionUUID(uuids.NewV4()),
env: envs.NewBuilder().Build(),
engine: e,
assets: sa,
Expand Down
Loading

0 comments on commit 74e0019

Please sign in to comment.