From e41d6c0e1166921e7cc4b9b8d74052c5ce447fa1 Mon Sep 17 00:00:00 2001 From: "B.G.Adrian" Date: Mon, 19 Nov 2018 19:22:20 +0200 Subject: [PATCH] upgrade to FastFaker 2.0 and removed the old code --- CUSTOM.md | 115 -------------------------- Dockerfile | 2 +- LICENSE | 53 ------------ Makefile | 5 +- README.md | 20 ++--- cmd/custom-key-generator/main.go | 135 ------------------------------- go.mod | 3 +- go.sum | 6 +- handlers/GetCustomCount.go | 69 +++------------- handlers/GetCustomCount_test.go | 4 +- handlers/GetHealth.go | 4 +- handlers/GetUsers.go | 6 +- handlers/custom_keys.go | 118 --------------------------- handlers/custom_keys_test.go | 25 ------ 14 files changed, 37 insertions(+), 528 deletions(-) delete mode 100644 CUSTOM.md delete mode 100644 cmd/custom-key-generator/main.go delete mode 100644 handlers/custom_keys.go delete mode 100644 handlers/custom_keys_test.go diff --git a/CUSTOM.md b/CUSTOM.md deleted file mode 100644 index 601a67a..0000000 --- a/CUSTOM.md +++ /dev/null @@ -1,115 +0,0 @@ -### Custom data -##### template variables and examples - -All the variables that can be used with the **/custom/** entrypoint. -For more info see [Readme](./README.md). - -| Variable | Example | -|---------|--------| -| **~beeralcohol~** | **4.7%** | -| **~beerblg~** | **9.3°Blg** | -| **~beerhop~** | **Glacier** | -| **~beeribu~** | **42 IBU** | -| **~beermalt~** | **Victory** | -| **~beername~** | **Trappistes Rochefort 10** | -| **~beerstyle~** | **Strong Ale** | -| **~beeryeast~** | **2035 - American Lager** | -| **~bs~** | **plug-and-play** | -| **~buzzword~** | **content-based** | -| **~carmaker~** | **BMW** | -| **~carmodel~** | **Cts** | -| **~chromeuseragent~** | **Mozilla/5.0 (Windows NT 4.0) AppleWebKit/5312 (KHTML, like Gecko) Chrome/39.0.874.0 Mobile Safari/5312** | -| **~city~** | **West Swaniawski** | -| **~color~** | **AliceBlue** | -| **~company~** | **Padberg, Lehner and Lang** | -| **~companysuffix~** | **Inc** | -| **~country~** | **Cyprus** | -| **~countryabr~** | **GL** | -| **~creditcardcvv~** | **778** | -| **~creditcardexp~** | **08/26** | -| **~creditcardnumber~** | **6527420300252086** | -| **~creditcardnumberluhn~** | **4605543610742645** | -| **~creditcardtype~** | **American Express** | -| **~currencylong~** | **Trinidad and Tobago Dollar** | -| **~currencyshort~** | **SLL** | -| **~day~** | **11** | -| **~digit~** | **0** | -| **~domainname~** | **humanvisionary.name** | -| **~domainsuffix~** | **biz** | -| **~email~** | **eldahahn@murray.org** | -| **~extension~** | **doc** | -| **~firefoxuseragent~** | **Mozilla/5.0 (Windows NT 5.01; en-US; rv:1.9.1.20) Gecko/1983-24-08 Firefox/35.0** | -| **~firstname~** | **Monte** | -| **~float32~** | **3.1917442e+38** | -| **~float64~** | **1.240504735476119e+308** | -| **~fueltype~** | **Methanol** | -| **~gender~** | **female** | -| **~hackerabbreviation~** | **PCI** | -| **~hackeradjective~** | **redundant** | -| **~hackeringverb~** | **copying** | -| **~hackernoun~** | **feed** | -| **~hackerphrase~** | **You can't calculate the program without calculating the optical TCP firewall!** | -| **~hackerverb~** | **copy** | -| **~hexcolor~** | **#n4u8jo** | -| **~hipsterword~** | **lomo** | -| **~hour~** | **19** | -| **~httpmethod~** | **PATCH** | -| **~int16~** | **-4680** | -| **~int32~** | **-814555329** | -| **~int64~** | **-899361807533148519** | -| **~int8~** | **16** | -| **~ipv4address~** | **49.240.114.228** | -| **~ipv6address~** | **2001:cafe:2d10:48c:bec1:2adb:ea5f:e318** | -| **~jobdescriptor~** | **Human** | -| **~joblevel~** | **Implementation** | -| **~jobtitle~** | **Strategist** | -| **~lastname~** | **Erdman** | -| **~latitude~** | **12.386576760734286** | -| **~letter~** | **t** | -| **~longitude~** | **-173.07884672238632** | -| **~mimetype~** | **video/x-scm** | -| **~minute~** | **25** | -| **~month~** | **June** | -| **~name~** | **Dewitt Orn** | -| **~nameprefix~** | **Dr.** | -| **~namesuffix~** | **II** | -| **~nanosecond~** | **442237102** | -| **~operauseragent~** | **Opera/8.95 (Macintosh; PPC Mac OS X 10_6_0; en-US) Presto/2.12.282 Version/12.00** | -| **~phone~** | **8767066027** | -| **~phoneformatted~** | **705-764-6140** | -| **~safariuseragent~** | **Mozilla/5.0 (iPad; CPU OS 9_3_2 like Mac OS X; en-US) AppleWebKit/533.28.8 (KHTML, like Gecko) Version/5.0.5 Mobile/8B119 Safari/6533.28.8** | -| **~safecolor~** | **teal** | -| **~second~** | **9** | -| **~simplestatuscode~** | **200** | -| **~ssn~** | **859461448** | -| **~state~** | **Massachusetts** | -| **~stateabr~** | **IL** | -| **~statuscode~** | **403** | -| **~street~** | **1848 Estatetown** | -| **~streetname~** | **Ports** | -| **~streetnumber~** | **130** | -| **~streetprefix~** | **Port** | -| **~streetsuffix~** | **town** | -| **~timezone~** | **Pacific Standard Time** | -| **~timezoneabv~** | **MST** | -| **~timezonefull~** | **(UTC-11:00) Coordinated Universal Time-11** | -| **~timezoneoffset~** | **3** | -| **~transmissiongeartype~** | **Manual** | -| **~uint16~** | **58530** | -| **~uint32~** | **138464171** | -| **~uint64~** | **4273607561850633676** | -| **~uint8~** | **90** | -| **~url~** | **http://www.principalintegrated.com/strategic** | -| **~useragent~** | **Mozilla/5.0 (X11; Linux i686) AppleWebKit/5331 (KHTML, like Gecko) Chrome/36.0.863.0 Mobile Safari/5331** | -| **~username~** | **Littel5445** | -| **~uuid~** | **7789ae1a-9d4c-41ed-800c-6695cc1541c1** | -| **~vehicletype~** | **Passenger car medium** | -| **~weekday~** | **Friday** | -| **~word~** | **perferendis** | -| **~year~** | **1919** | -| **~zip~** | **13340** | - - - -> This file is generated automatically at build. - diff --git a/Dockerfile b/Dockerfile index fd18c78..a40710a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,7 @@ RUN go mod download COPY . . RUN make build -FROM scratch +FROM alpine COPY --from=builder /src/build/pseudoservice . ENV PORT=8080 EXPOSE 8080 diff --git a/LICENSE b/LICENSE index f288702..810fce6 100644 --- a/LICENSE +++ b/LICENSE @@ -619,56 +619,3 @@ Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. diff --git a/Makefile b/Makefile index 9eb86b0..6caa96f 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,6 @@ pre: env GO111MODULE=on go get -d ./ env GO111MODULE=on go test -race ./... -generate: - go run cmd/custom-key-generator/main.go go > handlers/custom_keys.go - go run cmd/custom-key-generator/main.go mark > CUSTOM.md - run: pre env PORT=8080 go run $(source) @@ -19,6 +15,7 @@ build: pre @echo "See ./build/pseudoservice --help" buildall: pre + rm -f ./build/pseudoservice mkdir -p ./build/pseudoservice/windows mkdir -p ./build/pseudoservice/linux mkdir -p ./build/pseudoservice/macos diff --git a/README.md b/README.md index 1e2e127..4e44bc5 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,14 @@ # Pseudo service ![travis-mater](https://travis-ci.com/bgadrian/pseudoservice.svg?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/bgadrian/pseudoservice)](https://goreportcard.com/report/github.com/bgadrian/pseudoservice) Pseudo random (deterministic) data generator as a (micro) service. +It is a wrapper for the [FastFaker Go package](https://github.com/bgadrian/fastfaker). #### The problem -The need to generate random (user or other type) data, to (stress) test your own system. +The need to generate random (user or other type) data, to mockup, test or stress your own systems. #### The solution I've built this project to be used as an internal service, to generate large amounts of fake data. -It was originally built for this project: [davidescus/10minFor300Gb](https://github.com/davidescus/10minFor300Gb) as a wrapper on [bgadrian/fastfaker](https://github.com/bgadrian/gofakeit). - ### Performance On my localhost (4 Core 3Ghz) I was able to generate over **14.000 users/second** (`/users/`) with 100 batches, on an 600Mhz B2 AppEngine (1 Core) can deliver 900 users/second. @@ -22,7 +21,7 @@ For the best performance: ### How it works -You run it as a simple HTTP server, in a private network, and clients can make requests to get random data. +You run it as a simple HTTP server, in a private network, and the clients can make requests to get random data, based on their custom templates/needs. Global optional parameters: * `?seed=42` - if given, the result will be deterministic, as in for all calls the same data will be returned. @@ -40,7 +39,7 @@ Returns `200` if the service is available. ##### /custom/{count}?template="Hello ~name~!" Generate random data based on a given template. Supports any string, including JSON schema. -The template can contains variables like `~name~` or `~email~`. +The template can contains variables like `~name~` or `~email~`. For a full list see the [FastFaker variables](https://github.com/bgadrian/fastfaker/blob/master/TEMPLATE_VARIABLES.md), but instead of `{}` you must use the `~` delimiter (it is more URL friendly). ```bash #the template is URL encoded: Hello ~name~! @@ -69,12 +68,15 @@ curl -X GET "http://localhost:8080/custom/6?token=SECRET42&seed=42&template=~cou "Ethiopia", "Afghanistan" ],"seed":42} - ``` -For all supported template variables see [CUSTOM.md](./CUSTOM.md) + +For more examples see the FastFaker [examples](https://github.com/bgadrian/fastfaker/tree/master/example) and [GoDoc](https://godoc.org/github.com/bgadrian/fastfaker/faker#pkg-examples). ##### /users/{count} -Generate random users based on a preconfigured set of properties. +Generate random users based on a preconfigured set of properties, with a friend relationship between them. + +This endpoint was created to simulate a Social network and test a Graph database: [davidescus/10minFor300Gb](https://github.com/davidescus/10minFor300Gb). + ```bash #fast and random user curl "http://localhost:8080/api/v1/users/1?token=SECRET42" @@ -90,7 +92,7 @@ curl "http://localhost:8080/api/v1/users/1?token=SECRET42" } ]} -#slow and deterministic call (seed=42) +#slower but deterministic call (seed=42) curl "http://localhost:8080/api/v1/users/1?token=SECRET42&seed=42" {"seed":42,"nextseed":43,"users":[ diff --git a/cmd/custom-key-generator/main.go b/cmd/custom-key-generator/main.go deleted file mode 100644 index 7dd638a..0000000 --- a/cmd/custom-key-generator/main.go +++ /dev/null @@ -1,135 +0,0 @@ -// +build ignore - -//this file generates the handlers/custom_keys.go file -//use go generate to update it - -package main - -import ( - "fmt" - "html/template" - "log" - "os" - "reflect" - "sort" - "strconv" - "strings" - - "github.com/bgadrian/fastfaker" -) - -func main() { - - if len(os.Args) != 2 { - log.Panicf("requires one parameter, of ['go', 'markdown']") - } - - type funct struct { - Key, Call, Example string - } - list := make([]funct, 0, 100) - - faker := fastfaker.NewFastFaker() - faker.Seed(42) - fakerType := reflect.TypeOf(faker) - fakerValue := reflect.ValueOf(faker) - - countOfMethods := fakerType.NumMethod() - for i := 0; i < countOfMethods; i++ { - method := fakerType.Method(i) - - //here we only collect simple functions, no parameters - //the first parameter is the actual struct (because is a method) - if method.Type.NumIn() > 1 { - continue - } - - var example string - - call := "f." + method.Name - g := byte('g') - - res := fakerValue.Method(i).Call([]reflect.Value{}) - if len(res) != 1 { - log.Panicf("weird method: %s", fakerValue.Method(i).String()) - } - switch res[0].Interface().(type) { - case string: - example = res[0].String() - call = fmt.Sprintf("func(f *fastfaker.Faker) string { return %s() }", call) - case int, int8, int16, int32, int64: - example = strconv.Itoa(int(res[0].Int())) - call = fmt.Sprintf("func(f *fastfaker.Faker) string { return strconv.Itoa(int(%s())) }", call) - case uint8, uint16, uint32, uint64: - example = strconv.Itoa(int(res[0].Uint())) - call = fmt.Sprintf("func(f *fastfaker.Faker) string { return strconv.Itoa(int(%s())) }", call) - case float32: - example = strconv.FormatFloat(float64(res[0].Float()), 'g', -1, 32) - call = fmt.Sprintf("func(f *fastfaker.Faker) string { return strconv.FormatFloat(float64(%s()), %d, -1, 32) }", call, g) - case float64: - example = strconv.FormatFloat(float64(res[0].Float()), 'g', -1, 64) - call = fmt.Sprintf("func(f *fastfaker.Faker) string { return strconv.FormatFloat(float64(%s()), %d, -1, 64) }", call, g) - default: - continue - //example = fmt.Sprintf("%v", res[0]) - //bytes, err := json.Marshal(res[0]) - //if err != nil { - // log.Panic(err) - //} - //example = string(bytes) - } - - list = append(list, funct{ - Key: strings.ToLower(method.Name), - Call: call, - Example: example, - }) - } - - sort.Slice(list, func(i, j int) bool { - return list[i].Key < list[j].Key - }) - - //TODO learn how to use the go generate - - if os.Args[1] == "go" { - packageTemplate.Execute(os.Stdout, list) - } else { - readmeTemplate.Execute(os.Stdout, list) - } -} - -var packageTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT. -package handlers - -import ( - "github.com/bgadrian/fastfaker" - "strconv" -) - -type fakerer func(*fastfaker.Faker) string -var keys map[string]fakerer - -func init() { - keys = make(map[string]fakerer) - - {{range .}}keys["{{.Key}}"] = {{.Call}} - {{end}} -} -`)) - -var readmeTemplate = template.Must(template.New("").Parse(`### Custom data -##### template variables and examples - -All the variables that can be used with the **/custom/** entrypoint. -For more info see [Readme](./README.md). - -| Variable | Example | -|---------|--------| -{{range .}}| **~{{.Key}}~** | **{{.Example}}** | -{{end}} - - -> This file is generated automatically at build. - -`)) diff --git a/go.mod b/go.mod index f979df3..2842449 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,7 @@ module github.com/bgadrian/pseudoservice require ( - github.com/bgadrian/fastfaker v0.0.0-20181115195422-fe25bb80cc52 + github.com/bgadrian/fastfaker v2.0.0+incompatible github.com/engoengine/math v1.0.1 // indirect github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 // indirect github.com/go-openapi/analysis v0.17.2 // indirect @@ -17,6 +17,7 @@ require ( github.com/google/go-cmp v0.2.0 github.com/jessevdk/go-flags v1.4.0 github.com/kr/pretty v0.1.0 // indirect + github.com/pkg/errors v0.8.0 // indirect golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) diff --git a/go.sum b/go.sum index d28589a..600eb86 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,8 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf h1:eg0MeVzsP1G42dRafH3vf+al2vQIJU0YHX+1Tw87oco= github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY= -github.com/bgadrian/fastfaker v0.0.0-20181115195422-fe25bb80cc52 h1:xEW5bDqCmMo42pTDDs7Cmpl+etl+UC2C27GYSr7fIu8= -github.com/bgadrian/fastfaker v0.0.0-20181115195422-fe25bb80cc52/go.mod h1:aQSXCiKk6GFZoS6DBRP8w2L81E6kIm2JgjpfhMkl64M= +github.com/bgadrian/fastfaker v2.0.0+incompatible h1:aW4jyBhYEq2dKCZvROm1QNfcgnl3CRxXOjQlqnTfa6E= +github.com/bgadrian/fastfaker v2.0.0+incompatible/go.mod h1:aQSXCiKk6GFZoS6DBRP8w2L81E6kIm2JgjpfhMkl64M= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/go-units v0.3.3 h1:Xk8S3Xj5sLGlG5g67hJmYMmUgXv5N4PhkjJHHqrwnTk= @@ -72,6 +72,8 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= +github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= diff --git a/handlers/GetCustomCount.go b/handlers/GetCustomCount.go index 82f321b..e6c819a 100644 --- a/handlers/GetCustomCount.go +++ b/handlers/GetCustomCount.go @@ -1,22 +1,14 @@ package handlers import ( - "regexp" - "strings" + "log" - "github.com/bgadrian/fastfaker" + "github.com/bgadrian/fastfaker/faker" "github.com/bgadrian/pseudoservice/models" "github.com/bgadrian/pseudoservice/restapi/operations" "github.com/go-openapi/runtime/middleware" ) -// keyPos the position of a valid key in the template, and the function to call -//to replace it with random data -type keyPos struct { - start, end int - f fakerer -} - // GetCustomCountParams sanitize the HTTP input and add the GenerateCustom result to the HTTP response. func (h *MyHandlers) GetCustomCountParams(params operations.GetCustomCountParams, principal interface{}) middleware.Responder { @@ -25,7 +17,7 @@ func (h *MyHandlers) GetCustomCountParams(params operations.GetCustomCountParams if seedGiven { seed = *params.Seed } else { - seed = fastfaker.Global.Int64() + seed = faker.Global.Int64() } template := params.Template count := params.Count @@ -39,59 +31,20 @@ func (h *MyHandlers) GetCustomCountParams(params operations.GetCustomCountParams return nil } -var pattern = regexp.MustCompile(`(~[a-zA-Z0-9_-]+~)`) - // GenerateCustom will replace all known ~keys~ from the template with random data, based on the seed. The result will contain (count) such strings. //For each unique seed/template/count group the result should be the same (deterministic). func GenerateCustom(seed int64, template string, count int32) []string { - templateAsByte := []byte(template) - indexes := pattern.FindAllIndex(templateAsByte, -1) - result := make([]string, 0, count) - - //filter keys - var toReplace []keyPos - for _, match := range indexes { - start := match[0] - end := match[1] - - key := string(templateAsByte[start:end]) - if len(key) < 3 { - //key is empty "~~" - continue - } - //remove the first and last ~ - inner := strings.ToLower(key[1 : len(key)-1]) - - f, exists := keys[inner] - if exists == false { - //key does not exists - continue - } - toReplace = append(toReplace, keyPos{start, end, f}) - } - //add random data - faker := fastfaker.NewFastFaker() - faker.Seed(seed) - var i int32 - for ; i < count; i++ { - if len(indexes) == 0 { - result = append(result, template) + fastFaker := faker.NewFastFaker() + fastFaker.Seed(seed) + result := make([]string, 0, count) + for i := 0; i < int(count); i++ { + res, err := fastFaker.TemplateCustom(template, "~", "~") + if err != nil { + log.Printf("faker failed: %s", err) continue } - - buff := strings.Builder{} - buff.Grow(len(templateAsByte)) //at least the input - - var lastEnd int - for _, posToReplace := range toReplace { - buff.Write(templateAsByte[lastEnd:posToReplace.start]) - buff.WriteString(posToReplace.f(faker)) - lastEnd = posToReplace.end - } - buff.Write(templateAsByte[lastEnd:]) - - result = append(result, buff.String()) + result = append(result, res) } return result } diff --git a/handlers/GetCustomCount_test.go b/handlers/GetCustomCount_test.go index 99a9690..0d5c7dd 100644 --- a/handlers/GetCustomCount_test.go +++ b/handlers/GetCustomCount_test.go @@ -47,7 +47,7 @@ func TestGenerateCustomJson(t *testing.T) { {`{name:"~name~"}`, `{name:"Jeromy Schmeler"}`}, {`["~name~", "~name~"]`, `["Jeromy Schmeler", "Kim Steuber"]`}, - {`{name:"~name~", age: ~digit~}`, `{name:"Jeromy Schmeler", age: 5}`}, + {`{name:"~name~", age: ~digit~}`, `{name:"Jeromy Schmeler", age: 8}`}, } { got := GenerateCustom(42, test.template, 1) @@ -87,5 +87,5 @@ func ExampleGenerateCustomJson() { for _, result := range randomData { fmt.Printf("%s\n", result) } - // Output:{name:"Jeromy Schmeler", age: 5} + // Output:{name:"Jeromy Schmeler", age: 8} } diff --git a/handlers/GetHealth.go b/handlers/GetHealth.go index b3ca94f..753a10f 100644 --- a/handlers/GetHealth.go +++ b/handlers/GetHealth.go @@ -4,7 +4,7 @@ import ( "net/http" "time" - "github.com/bgadrian/fastfaker" + "github.com/bgadrian/fastfaker/faker" "github.com/bgadrian/pseudoservice/restapi/operations" "github.com/go-openapi/runtime/middleware" ) @@ -15,7 +15,7 @@ func (*MyHandlers) GetHealthHandler(params operations.GetHealthParams, principal timeout := time.NewTicker(time.Millisecond * 200) var done chan bool go func() { - fastfaker.Global.Int64() + faker.Global.Int64() done <- true }() select { diff --git a/handlers/GetUsers.go b/handlers/GetUsers.go index 605af29..d314fe6 100644 --- a/handlers/GetUsers.go +++ b/handlers/GetUsers.go @@ -11,7 +11,7 @@ import ( "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/strfmt" - "github.com/bgadrian/fastfaker" + "github.com/bgadrian/fastfaker/faker" ) // GetUsersHandler /users/{count} handler with real data @@ -27,7 +27,7 @@ func (h *MyHandlers) GetUsersHandler(params operations.GetUsersCountParams, prin if seedGiven { seed = *params.Seed } else { - seed = fastfaker.Global.Int64() + seed = faker.Global.Int64() } users, nextseed, err := GenerateUsers(seed, int(params.Count), seedGiven) @@ -58,7 +58,7 @@ func GenerateUsers(seed int64, count int, deterministic bool) ([]*models.User, i //chance to being here is like ... 2^63-count ...is like winning the lottery return nil, 0, fmt.Errorf("int overflow, need a smaller seed: %d count: %d", seed, count) } - faker := fastfaker.NewFastFaker() + faker := faker.NewFastFaker() if deterministic == false { faker.Seed(seed) diff --git a/handlers/custom_keys.go b/handlers/custom_keys.go deleted file mode 100644 index d9cf335..0000000 --- a/handlers/custom_keys.go +++ /dev/null @@ -1,118 +0,0 @@ -// Code generated by go generate; DO NOT EDIT. -package handlers - -import ( - "github.com/bgadrian/fastfaker" - "strconv" -) - -type fakerer func(*fastfaker.Faker) string -var keys map[string]fakerer - -func init() { - keys = make(map[string]fakerer) - - keys["beeralcohol"] = func(f *fastfaker.Faker) string { return f.BeerAlcohol() } - keys["beerblg"] = func(f *fastfaker.Faker) string { return f.BeerBlg() } - keys["beerhop"] = func(f *fastfaker.Faker) string { return f.BeerHop() } - keys["beeribu"] = func(f *fastfaker.Faker) string { return f.BeerIbu() } - keys["beermalt"] = func(f *fastfaker.Faker) string { return f.BeerMalt() } - keys["beername"] = func(f *fastfaker.Faker) string { return f.BeerName() } - keys["beerstyle"] = func(f *fastfaker.Faker) string { return f.BeerStyle() } - keys["beeryeast"] = func(f *fastfaker.Faker) string { return f.BeerYeast() } - keys["bs"] = func(f *fastfaker.Faker) string { return f.BS() } - keys["buzzword"] = func(f *fastfaker.Faker) string { return f.BuzzWord() } - keys["carmaker"] = func(f *fastfaker.Faker) string { return f.CarMaker() } - keys["carmodel"] = func(f *fastfaker.Faker) string { return f.CarModel() } - keys["chromeuseragent"] = func(f *fastfaker.Faker) string { return f.ChromeUserAgent() } - keys["city"] = func(f *fastfaker.Faker) string { return f.City() } - keys["color"] = func(f *fastfaker.Faker) string { return f.Color() } - keys["company"] = func(f *fastfaker.Faker) string { return f.Company() } - keys["companysuffix"] = func(f *fastfaker.Faker) string { return f.CompanySuffix() } - keys["country"] = func(f *fastfaker.Faker) string { return f.Country() } - keys["countryabr"] = func(f *fastfaker.Faker) string { return f.CountryAbr() } - keys["creditcardcvv"] = func(f *fastfaker.Faker) string { return f.CreditCardCvv() } - keys["creditcardexp"] = func(f *fastfaker.Faker) string { return f.CreditCardExp() } - keys["creditcardnumber"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.CreditCardNumber())) } - keys["creditcardnumberluhn"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.CreditCardNumberLuhn())) } - keys["creditcardtype"] = func(f *fastfaker.Faker) string { return f.CreditCardType() } - keys["currencylong"] = func(f *fastfaker.Faker) string { return f.CurrencyLong() } - keys["currencyshort"] = func(f *fastfaker.Faker) string { return f.CurrencyShort() } - keys["day"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Day())) } - keys["digit"] = func(f *fastfaker.Faker) string { return f.Digit() } - keys["domainname"] = func(f *fastfaker.Faker) string { return f.DomainName() } - keys["domainsuffix"] = func(f *fastfaker.Faker) string { return f.DomainSuffix() } - keys["email"] = func(f *fastfaker.Faker) string { return f.Email() } - keys["extension"] = func(f *fastfaker.Faker) string { return f.Extension() } - keys["firefoxuseragent"] = func(f *fastfaker.Faker) string { return f.FirefoxUserAgent() } - keys["firstname"] = func(f *fastfaker.Faker) string { return f.FirstName() } - keys["float32"] = func(f *fastfaker.Faker) string { return strconv.FormatFloat(float64(f.Float32()), 103, -1, 32) } - keys["float64"] = func(f *fastfaker.Faker) string { return strconv.FormatFloat(float64(f.Float64()), 103, -1, 64) } - keys["fueltype"] = func(f *fastfaker.Faker) string { return f.FuelType() } - keys["gender"] = func(f *fastfaker.Faker) string { return f.Gender() } - keys["hackerabbreviation"] = func(f *fastfaker.Faker) string { return f.HackerAbbreviation() } - keys["hackeradjective"] = func(f *fastfaker.Faker) string { return f.HackerAdjective() } - keys["hackeringverb"] = func(f *fastfaker.Faker) string { return f.HackerIngverb() } - keys["hackernoun"] = func(f *fastfaker.Faker) string { return f.HackerNoun() } - keys["hackerphrase"] = func(f *fastfaker.Faker) string { return f.HackerPhrase() } - keys["hackerverb"] = func(f *fastfaker.Faker) string { return f.HackerVerb() } - keys["hexcolor"] = func(f *fastfaker.Faker) string { return f.HexColor() } - keys["hipsterword"] = func(f *fastfaker.Faker) string { return f.HipsterWord() } - keys["hour"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Hour())) } - keys["httpmethod"] = func(f *fastfaker.Faker) string { return f.HTTPMethod() } - keys["int16"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Int16())) } - keys["int32"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Int32())) } - keys["int64"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Int64())) } - keys["int8"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Int8())) } - keys["ipv4address"] = func(f *fastfaker.Faker) string { return f.IPv4Address() } - keys["ipv6address"] = func(f *fastfaker.Faker) string { return f.IPv6Address() } - keys["jobdescriptor"] = func(f *fastfaker.Faker) string { return f.JobDescriptor() } - keys["joblevel"] = func(f *fastfaker.Faker) string { return f.JobLevel() } - keys["jobtitle"] = func(f *fastfaker.Faker) string { return f.JobTitle() } - keys["lastname"] = func(f *fastfaker.Faker) string { return f.LastName() } - keys["latitude"] = func(f *fastfaker.Faker) string { return strconv.FormatFloat(float64(f.Latitude()), 103, -1, 64) } - keys["letter"] = func(f *fastfaker.Faker) string { return f.Letter() } - keys["longitude"] = func(f *fastfaker.Faker) string { return strconv.FormatFloat(float64(f.Longitude()), 103, -1, 64) } - keys["mimetype"] = func(f *fastfaker.Faker) string { return f.MimeType() } - keys["minute"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Minute())) } - keys["month"] = func(f *fastfaker.Faker) string { return f.Month() } - keys["name"] = func(f *fastfaker.Faker) string { return f.Name() } - keys["nameprefix"] = func(f *fastfaker.Faker) string { return f.NamePrefix() } - keys["namesuffix"] = func(f *fastfaker.Faker) string { return f.NameSuffix() } - keys["nanosecond"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.NanoSecond())) } - keys["operauseragent"] = func(f *fastfaker.Faker) string { return f.OperaUserAgent() } - keys["phone"] = func(f *fastfaker.Faker) string { return f.Phone() } - keys["phoneformatted"] = func(f *fastfaker.Faker) string { return f.PhoneFormatted() } - keys["safariuseragent"] = func(f *fastfaker.Faker) string { return f.SafariUserAgent() } - keys["safecolor"] = func(f *fastfaker.Faker) string { return f.SafeColor() } - keys["second"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Second())) } - keys["simplestatuscode"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.SimpleStatusCode())) } - keys["ssn"] = func(f *fastfaker.Faker) string { return f.SSN() } - keys["state"] = func(f *fastfaker.Faker) string { return f.State() } - keys["stateabr"] = func(f *fastfaker.Faker) string { return f.StateAbr() } - keys["statuscode"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.StatusCode())) } - keys["street"] = func(f *fastfaker.Faker) string { return f.Street() } - keys["streetname"] = func(f *fastfaker.Faker) string { return f.StreetName() } - keys["streetnumber"] = func(f *fastfaker.Faker) string { return f.StreetNumber() } - keys["streetprefix"] = func(f *fastfaker.Faker) string { return f.StreetPrefix() } - keys["streetsuffix"] = func(f *fastfaker.Faker) string { return f.StreetSuffix() } - keys["timezone"] = func(f *fastfaker.Faker) string { return f.TimeZone() } - keys["timezoneabv"] = func(f *fastfaker.Faker) string { return f.TimeZoneAbv() } - keys["timezonefull"] = func(f *fastfaker.Faker) string { return f.TimeZoneFull() } - keys["timezoneoffset"] = func(f *fastfaker.Faker) string { return strconv.FormatFloat(float64(f.TimeZoneOffset()), 103, -1, 32) } - keys["transmissiongeartype"] = func(f *fastfaker.Faker) string { return f.TransmissionGearType() } - keys["uint16"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Uint16())) } - keys["uint32"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Uint32())) } - keys["uint64"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Uint64())) } - keys["uint8"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Uint8())) } - keys["url"] = func(f *fastfaker.Faker) string { return f.URL() } - keys["useragent"] = func(f *fastfaker.Faker) string { return f.UserAgent() } - keys["username"] = func(f *fastfaker.Faker) string { return f.Username() } - keys["uuid"] = func(f *fastfaker.Faker) string { return f.UUID() } - keys["vehicletype"] = func(f *fastfaker.Faker) string { return f.VehicleType() } - keys["weekday"] = func(f *fastfaker.Faker) string { return f.WeekDay() } - keys["word"] = func(f *fastfaker.Faker) string { return f.Word() } - keys["year"] = func(f *fastfaker.Faker) string { return strconv.Itoa(int(f.Year())) } - keys["zip"] = func(f *fastfaker.Faker) string { return f.Zip() } - -} diff --git a/handlers/custom_keys_test.go b/handlers/custom_keys_test.go deleted file mode 100644 index 17347d3..0000000 --- a/handlers/custom_keys_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package handlers - -import ( - "fmt" - "testing" -) - -func TestGenerateCustom(t *testing.T) { - for param := range keys { - template := fmt.Sprintf("~%s~", param) - gotList := GenerateCustom(42, template, 5) - - for _, got := range gotList { - if template == got { - t.Errorf("template var did not got replaced for '%s'", template) - break - } - - if len(got) == 0 { - t.Errorf("result is empty for '%s'", template) - break - } - } - } -}