Skip to content
This repository was archived by the owner on Jun 9, 2023. It is now read-only.

Commit 6840802

Browse files
author
robreid
committed
Added random generators from go-randomdata
1 parent 5b8635a commit 6840802

File tree

5 files changed

+223
-21
lines changed

5 files changed

+223
-21
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cover:
2020
github.com/codingconcepts/datagen/internal/pkg/runner;\
2121
go tool cover -html=coverage.out
2222

23-
build:
23+
release:
2424
# linux
2525
GOOS=linux go build -ldflags "-X main.semver=${VERSION}" -o datagen ;\
2626
tar -zcvf datagen_${VERSION}_linux.tar.gz ./datagen ;\

README.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ If you need to generate a lot of random data for your database tables but don't
1010
1111
> "right now datagen and [modelgen](https://github.com/LUSHDigital/modelgen) are god sends to me"
1212
13+
## Credit
14+
15+
* [go-randomdata](https://github.com/Pallinder/go-randomdata) for the following generators:
16+
17+
title, namef, namel, name, email, phone, postcode, address, street, city, county, state, currency, locale, country, country2, country3, ip4, ip6, and user-agent.
18+
1319
## Installation
1420

1521
To build from source, either clone the repo, or use `go get` as follows (datagen will automatically be built by Go):
@@ -317,6 +323,174 @@ Works in a simliar way to `row` but references _sequential_ rows from a previous
317323
{{end}}
318324
```
319325

326+
##### title
327+
328+
Generates a random title for a random gender.
329+
330+
```
331+
{{title}}
332+
```
333+
334+
##### namef
335+
336+
Generates a random first name for a random gender.
337+
338+
```
339+
{{namef}}
340+
```
341+
342+
##### namel
343+
344+
Generates a random last name.
345+
346+
```
347+
{{namel}}
348+
```
349+
350+
##### name
351+
352+
Generates a random full name for a random gender.
353+
354+
```
355+
{{name}}
356+
```
357+
358+
##### email
359+
360+
Generates a random email address.
361+
362+
```
363+
{{email}}
364+
```
365+
366+
##### phone
367+
368+
Generates a random phone number in E164 format.
369+
370+
```
371+
{{phone}}
372+
```
373+
374+
##### postcode
375+
376+
Generates a random postcode, taking a 2-letter country code.
377+
378+
```
379+
{{postcode "GB"}}
380+
```
381+
382+
##### address
383+
384+
Generates a random American address. It's possible to create addresses for other countries using the various other functions available.
385+
386+
```
387+
{{address}}
388+
```
389+
390+
##### street
391+
392+
Generates a random street name, taking a 2-letter country code.
393+
394+
```
395+
{{street "GB"}}
396+
```
397+
398+
##### city
399+
400+
Generates a random American city name.
401+
402+
```
403+
{{city}}
404+
```
405+
406+
##### county
407+
408+
Generates a random county/state name, taking a 2-letter country code. To generate a random state name without having to pass "US" as an argument, use the `state` function.
409+
410+
```
411+
{{county "GB"}}
412+
```
413+
414+
##### state
415+
416+
Generates a random American state name.
417+
418+
```
419+
{{state}}
420+
```
421+
422+
##### state2
423+
424+
Generates a random American state name in its 2-letter format.
425+
426+
```
427+
{{state2}}
428+
```
429+
430+
##### currency
431+
432+
Generates a random currency in ISO 4217 format.
433+
434+
```
435+
{{currency}}
436+
```
437+
438+
##### locale
439+
440+
Generates a random locale.
441+
442+
```
443+
{{locale}}
444+
```
445+
446+
##### country
447+
448+
Generates a random country name.
449+
450+
```
451+
{{country}}
452+
```
453+
454+
##### country2
455+
456+
Generates a random country name in ISO 3166-1 alpha-2 format.
457+
458+
```
459+
{{country2}}
460+
```
461+
462+
##### country3
463+
464+
Generates a random country name in ISO 3166-1 alpha-3 format.
465+
466+
```
467+
{{country3}}
468+
```
469+
470+
##### ip4
471+
472+
Generates a random v4 IP address.
473+
474+
```
475+
{{ip4}}
476+
```
477+
478+
##### ip6
479+
480+
Generates a random v6 IP address.
481+
482+
```
483+
{{ip5}}
484+
```
485+
486+
##### user-agent
487+
488+
Generates a random user agent to simulate an API client.
489+
490+
```
491+
{{agent}}
492+
```
493+
320494
## Other database types:
321495

322496
### MySQL

examples/script.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ insert into "pet" ("pid", "name", "type") values
1717
{{if $i}},{{end}}
1818
(
1919
'{{ref "owner" "id"}}',
20-
'{{string 10 10 "p-" "abcde"}}',
20+
'{{name}}',
2121
'{{wset "dog" 60 "cat" 40}}'
2222
)
2323
{{end}};

internal/pkg/random/random.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ func Set(set ...interface{}) interface{} {
181181
return set[i]
182182
}
183183

184+
// NTimes returns a slice of empty structs of random length. If you know
185+
// the size of the slice you'd like, just pass the min argument, if you'd
186+
// like a slice between a minimum and maximum size, pass a value for extra.
187+
func NTimes(min int64, extra ...int64) []struct{} {
188+
max := min
189+
if len(extra) > 0 {
190+
max = extra[0]
191+
}
192+
return make([]struct{}, Int(min, max))
193+
}
194+
184195
func between64(min, max int64) int64 {
185196
if min == max {
186197
return min

internal/pkg/runner/runner.go

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616

1717
"github.com/codingconcepts/datagen/internal/pkg/parse"
1818
"github.com/pkg/errors"
19+
20+
"github.com/Pallinder/go-randomdata"
1921
)
2022

2123
// Runner holds the configuration that will be used at runtime.
@@ -55,25 +57,40 @@ func New(db *sql.DB, opts ...Option) *Runner {
5557
}
5658

5759
r.funcs = template.FuncMap{
58-
"string": random.String,
59-
"stringf": random.StringF(r.stringFdefaults),
60-
"int": random.Int,
61-
"date": random.Date(r.dateFormat),
62-
"float": random.Float,
63-
"uuid": func() string { return uuid.New().String() },
64-
"set": random.Set,
65-
"wset": r.wset,
66-
"fset": r.loadAndSet,
67-
"ref": r.store.reference,
68-
"row": r.store.row,
69-
"each": r.store.each,
70-
"ntimes": func(min int64, extra ...int64) []struct{} {
71-
max := min
72-
if len(extra) > 0 {
73-
max = extra[0]
74-
}
75-
return make([]struct{}, random.Int(min, max))
76-
},
60+
"string": random.String,
61+
"stringf": random.StringF(r.stringFdefaults),
62+
"int": random.Int,
63+
"date": random.Date(r.dateFormat),
64+
"float": random.Float,
65+
"ntimes": random.NTimes,
66+
"set": random.Set,
67+
"uuid": func() string { return uuid.New().String() },
68+
"wset": r.wset,
69+
"fset": r.loadAndSet,
70+
"ref": r.store.reference,
71+
"row": r.store.row,
72+
"each": r.store.each,
73+
"title": func() string { return randomdata.Title(randomdata.RandomGender) },
74+
"namef": func() string { return randomdata.FirstName(randomdata.RandomGender) },
75+
"namel": randomdata.LastName,
76+
"name": func() string { return randomdata.FullName(randomdata.RandomGender) },
77+
"email": randomdata.Email,
78+
"phone": randomdata.PhoneNumber,
79+
"postcode": randomdata.PostalCode,
80+
"address": randomdata.Address,
81+
"street": randomdata.StreetForCountry,
82+
"city": randomdata.City,
83+
"county": randomdata.ProvinceForCountry,
84+
"state": func() string { return randomdata.State(randomdata.Large) },
85+
"state2": func() string { return randomdata.State(randomdata.Small) },
86+
"currency": randomdata.Currency,
87+
"locale": randomdata.Locale, // BCP 47
88+
"country": func() string { return randomdata.Country(randomdata.FullCountry) },
89+
"country2": func() string { return randomdata.Country(randomdata.TwoCharCountry) }, // ISO 3166-1 alpha-2
90+
"country3": func() string { return randomdata.Country(randomdata.ThreeCharCountry) }, // ISO 3166-1 alpha-3
91+
"ip4": randomdata.IpV4Address,
92+
"ip6": randomdata.IpV6Address,
93+
"agent": randomdata.UserAgentString,
7794
}
7895

7996
return &r

0 commit comments

Comments
 (0)