Skip to content

Commit

Permalink
oops, forgot to commit
Browse files Browse the repository at this point in the history
  • Loading branch information
matronator committed Mar 23, 2024
0 parents commit e317d77
Show file tree
Hide file tree
Showing 25 changed files with 1,361 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .amock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"host": "localhost",
"port": 8080,
"entities": [
"user.json"
]
}
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### Go template
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
vendor/
pkg/

# Go workspace file
go.work
10 changes: 10 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/amock.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/developer-tools.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/golinter.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions .idea/jsonSchemas.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added amock
Binary file not shown.
Binary file added amock.db
Binary file not shown.
65 changes: 65 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"encoding/json"
"log"
"os"

"github.com/timshannon/bolthold"
)

func GenerateEntity(entity EntityJSON, table *Table) (Entity, *Table) {
fields := make(Entity, len(entity))

for key, value := range entity {
fields[key], table = GenerateField(value, table)
}

return fields, table
}

func HydrateDatabase(db Database) Database {
var entityJSON EntityJSON

store, err := bolthold.Open("amock.db", 0666, nil)
if err != nil {
log.Fatal(err)
}

for key, table := range db.Tables {
var updated Table
updated, store = CreateTable(&table, entityJSON, store)
db.Tables[key] = updated
}

return db
}

func CreateTable(table *Table, entityJSON EntityJSON, store *bolthold.Store) (Table, *bolthold.Store) {
raw, err := os.ReadFile(table.Definition)

if err != nil {
log.Fatal(err)
}

err = json.Unmarshal(raw, &entityJSON)
if err != nil {
log.Fatal(err)
}

entities := make([]Entity, config.InitCount)

for i := 0; i < config.InitCount; i++ {
entities[i], table = GenerateEntity(entityJSON, table)
}

b, _ := json.Marshal(entities)

filename := table.Name + ".amock.json"

_ = os.WriteFile(filename, b, os.ModePerm)

table.File = filename

return *table, store
}
124 changes: 124 additions & 0 deletions generator/date.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package generator

import (
"strings"
"time"

"github.com/brianvoe/gofakeit/v7"
)

type DateGenerator struct {
Generator
}

func (g DateGenerator) Date(args ...string) any {
if len(args) > 0 {
format := args[0]
switch format {
case "ANSIC":
return gofakeit.Date().Format(time.ANSIC)
case "UnixDate":
return gofakeit.Date().Format(time.UnixDate)
case "RubyDate":
return gofakeit.Date().Format(time.RubyDate)
case "RFC822":
return gofakeit.Date().Format(time.RFC822)
case "RFC822Z":
return gofakeit.Date().Format(time.RFC822Z)
case "RFC850":
return gofakeit.Date().Format(time.RFC850)
case "RFC1123":
return gofakeit.Date().Format(time.RFC1123)
case "RFC1123Z":
return gofakeit.Date().Format(time.RFC1123Z)
case "RFC3339":
return gofakeit.Date().Format(time.RFC3339)
case "RFC3339Nano":
return gofakeit.Date().Format(time.RFC3339Nano)
default:
return gofakeit.Date().Format(formatToGoFormat(format))
}
}

return gofakeit.Date()
}

func (g DateGenerator) Timestamp() int64 {
return gofakeit.DateRange(time.Unix(0, 0), gofakeit.FutureDate()).Unix()
}

func (g DateGenerator) Day() int {
return gofakeit.Day()
}

func (g DateGenerator) Month(args ...string) any {
if len(args) > 0 {
if args[0] == "string" {
return gofakeit.MonthString()
}
}

return gofakeit.Month()
}

func (g DateGenerator) Year() int {
return gofakeit.Year()
}

func (g DateGenerator) WeekDay() string {
return gofakeit.WeekDay()
}

func (g DateGenerator) Future() time.Time {
return gofakeit.FutureDate()
}

func (g DateGenerator) Past() time.Time {
return gofakeit.PastDate()
}

func formatToGoFormat(format string) string {
format = strings.Replace(format, "ddd", "_2", -1)
format = strings.Replace(format, "dd", "02", -1)
format = strings.Replace(format, "d", "2", -1)

format = strings.Replace(format, "HH", "15", -1)

format = strings.Replace(format, "hh", "03", -1)
format = strings.Replace(format, "h", "3", -1)

format = strings.Replace(format, "mm", "04", -1)
format = strings.Replace(format, "m", "4", -1)

format = strings.Replace(format, "ss", "05", -1)
format = strings.Replace(format, "s", "5", -1)

format = strings.Replace(format, "yyyy", "2006", -1)
format = strings.Replace(format, "yy", "06", -1)
format = strings.Replace(format, "y", "06", -1)

format = strings.Replace(format, "SSS", "000", -1)

format = strings.Replace(format, "a", "pm", -1)
format = strings.Replace(format, "aa", "PM", -1)

format = strings.Replace(format, "MMMM", "January", -1)
format = strings.Replace(format, "MMM", "Jan", -1)
format = strings.Replace(format, "MM", "01", -1)
format = strings.Replace(format, "M", "1", -1)

format = strings.Replace(format, "ZZ", "-0700", -1)

if !strings.Contains(format, "Z07") {
format = strings.Replace(format, "Z", "-07", -1)
}

format = strings.Replace(format, "zz:zz", "Z07:00", -1)
format = strings.Replace(format, "zzzz", "Z0700", -1)
format = strings.Replace(format, "z", "MST", -1)

format = strings.Replace(format, "EEEE", "Monday", -1)
format = strings.Replace(format, "E", "Mon", -1)

return format
}
11 changes: 11 additions & 0 deletions generator/enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package generator

import "github.com/brianvoe/gofakeit/v7"

type EnumGenerator struct {
Generator
}

func (g EnumGenerator) Enum(args ...string) string {
return gofakeit.RandomString(args)
}
13 changes: 13 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package generator

import "github.com/brianvoe/gofakeit/v7"

type Generator struct{}

type BoolGenerator struct {
Generator
}

func (g BoolGenerator) Bool() bool {
return gofakeit.Bool()
}
25 changes: 25 additions & 0 deletions generator/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package generator

import (
"strconv"

"github.com/brianvoe/gofakeit/v7"
)

type IDGenerator struct {
Generator
}

func (g IDGenerator) UUID() string {
return gofakeit.UUID()
}

func (g IDGenerator) Sequence(args ...string) uint {
if len(args) > 0 {
id, _ := strconv.Atoi(args[0])

return uint(id)
}

return 1
}
Loading

0 comments on commit e317d77

Please sign in to comment.