Skip to content

Commit 0f66782

Browse files
authored
Add relative date support (#10)
1 parent f11ea53 commit 0f66782

File tree

9 files changed

+52
-11
lines changed

9 files changed

+52
-11
lines changed

.github/workflows/lint.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ jobs:
2020
- name: golangci-lint
2121
uses: golangci/golangci-lint-action@v6
2222
with:
23-
version: v1.60.1
23+
version: v1.64.5

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ For more (sometimes wildly complex) examples, see `./testdata`.
4343

4444
## More on valueFuncs
4545

46-
valueFuncs allow you to generate random data that's seeded with a static string. This ensures that repeat runs of ripoff are deterministic, which enables upserts (consistent primary keys).
46+
Most valueFuncs allow you to generate random data that's seeded with a static string. This ensures that repeat runs of ripoff are deterministic, which enables upserts (consistent primary keys).
4747

4848
ripoff provides:
4949

5050
- `uuid(seedString)` - generates a v1 UUID
5151
- `int(seedString)` - generates an integer (note: might be awkward on auto incrementing tables)
52+
- `naturalDate(human readable text)` - generates a date using syntax defined by [go-naturaldate](https://github.com/tj/go-naturaldate), for example `naturalDate(one day ago)` (note: non-deterministic)
5253

5354
and also all functions from [gofakeit](https://github.com/brianvoe/gofakeit?tab=readme-ov-file#functions) that have no arguments and return a string (called in camelcase, ex: `email(seedString)`). For the full list, see `./gofakeit.go`.
5455

db.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import (
1010
"math/rand"
1111
"regexp"
1212
"strings"
13+
"time"
1314

1415
"github.com/brianvoe/gofakeit/v7"
1516
"github.com/dominikbraun/graph"
1617
"github.com/google/uuid"
1718
"github.com/jackc/pgx/v5"
1819
"github.com/lib/pq"
20+
"github.com/tj/go-naturaldate"
1921
)
2022

2123
// Runs ripoff from start to finish, without committing the transaction.
@@ -117,6 +119,7 @@ func prepareValue(rawValue string) (string, error) {
117119
}
118120
methodName := valueFuncMatches[1]
119121
value := valueFuncMatches[2]
122+
valueParts := strings.Split(strings.ReplaceAll(" ", "", valueFuncMatches[2]), ",")
120123

121124
// Create a new random seed based on a sha256 hash of the value.
122125
h := sha256.New()
@@ -136,11 +139,14 @@ func prepareValue(rawValue string) (string, error) {
136139
return fmt.Sprint(randSeed.Int()), nil
137140
case "literal":
138141
return value, nil
142+
case "naturalDate":
143+
parsed, err := naturaldate.Parse(value, time.Now())
144+
return parsed.Format(time.RFC3339), err
139145
}
140146

141147
// Assume the user meant to call a gofakeit.Faker method.
142148
faker := gofakeit.NewFaker(randSeed, true)
143-
fakerResult, err := callFakerMethod(methodName, faker)
149+
fakerResult, err := callFakerMethod(methodName, faker, valueParts...)
144150
if err != nil {
145151
return "", err
146152
}

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/jackc/pgx/v5 v5.6.0
1212
github.com/lib/pq v1.10.9
1313
github.com/stretchr/testify v1.9.0
14+
github.com/tj/go-naturaldate v1.3.0
1415
gopkg.in/yaml.v3 v3.0.1
1516
)
1617

@@ -21,7 +22,7 @@ require (
2122
github.com/kr/text v0.2.0 // indirect
2223
github.com/pmezard/go-difflib v1.0.0 // indirect
2324
github.com/rogpeppe/go-internal v1.12.0 // indirect
24-
golang.org/x/crypto v0.17.0 // indirect
25+
golang.org/x/crypto v0.21.0 // indirect
2526
golang.org/x/sync v0.3.0 // indirect
2627
golang.org/x/text v0.14.0 // indirect
2728
)

go.sum

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,24 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU
2828
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
2929
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3030
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
31+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
3132
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3233
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
3334
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
34-
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
35-
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
35+
github.com/tj/assert v0.0.0-20190920132354-ee03d75cd160 h1:NSWpaDaurcAJY7PkL8Xt0PhZE7qpvbZl5ljd8r6U0bI=
36+
github.com/tj/assert v0.0.0-20190920132354-ee03d75cd160/go.mod h1:mZ9/Rh9oLWpLLDRpvE+3b7gP/C2YyLFYxNmcLnPTMe0=
37+
github.com/tj/go-naturaldate v1.3.0 h1:OgJIPkR/Jk4bFMBLbxZ8w+QUxwjqSvzd9x+yXocY4RI=
38+
github.com/tj/go-naturaldate v1.3.0/go.mod h1:rpUbjivDKiS1BlfMGc2qUKNZ/yxgthOfmytQs8d8hKk=
39+
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
40+
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
3641
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
3742
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
3843
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
3944
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
4045
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4146
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
4247
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
48+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
4349
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4450
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
4551
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

gofakeit.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,42 @@ package ripoff
22

33
import (
44
"fmt"
5+
"strconv"
56

67
"github.com/brianvoe/gofakeit/v7"
78
)
89

10+
// Helper method to cast user-supplied args to ints if provided.
11+
func defaultArgsInt(args []string, defaults []int) ([]int, error) {
12+
if len(args) != len(defaults) {
13+
return defaults, nil
14+
}
15+
ret := make([]int, len(defaults))
16+
for i := 0; i < len(defaults); i++ {
17+
argInt, err := strconv.Atoi(args[i])
18+
if err != nil {
19+
return []int{}, err
20+
}
21+
ret[i] = argInt
22+
}
23+
return ret, nil
24+
}
25+
926
// Calls a `func() string` shaped method in a given faker instance.
10-
func callFakerMethod(method string, faker *gofakeit.Faker) (string, error) {
27+
func callFakerMethod(method string, faker *gofakeit.Faker, args ...string) (string, error) {
1128
switch method {
12-
// Note: added these in for my own use, probably should let valueFuncs take multiple params
1329
case "loremIpsumSentence":
14-
return faker.LoremIpsumSentence(20), nil
30+
argsInt, err := defaultArgsInt(args[1:], []int{20})
31+
if err != nil {
32+
return "", err
33+
}
34+
return faker.LoremIpsumSentence(argsInt[0]), nil
1535
case "loremIpsumParagraph":
16-
return faker.LoremIpsumParagraph(1, 4, 20, ""), nil
36+
argsInt, err := defaultArgsInt(args[1:], []int{1, 4, 20})
37+
if err != nil {
38+
return "", err
39+
}
40+
return faker.LoremIpsumParagraph(argsInt[0], argsInt[1], argsInt[2], ""), nil
1741
case "achAccount":
1842
return faker.AchAccount(), nil
1943
case "achRouting":

testdata/import/faker/faker.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
rows:
22
users:uuid(fooBar):
33
email: email(fooBar)
4+
last_logged_in_at: naturalDate(10 days ago)

testdata/import/faker/schema.sql

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CREATE TABLE users (
22
id UUID NOT NULL,
33
email TEXT NOT NULL,
4+
last_logged_in_at TIMESTAMPTZ NOT NULL,
45
PRIMARY KEY (id)
56
);

testdata/import/faker/validate.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ WITH test AS (
22
SELECT count(*) as count FROM users
33
WHERE email = '[email protected]'
44
AND id = '6b30cfb0-a35b-4584-a035-1334515f846b'
5+
AND date_trunc('day', last_logged_in_at) = date_trunc('day', now() - interval '10 days')
56
)
6-
SELECT (select count from test),'email: ' || users.email || ' id: ' || users.id
7+
SELECT (select count from test),'email: ' || users.email || ' id: ' || users.id || ' last_logged_in_at: ' || users.last_logged_in_at
78
FROM users;

0 commit comments

Comments
 (0)