diff --git a/db.go b/db.go index 7c29d6a..6334094 100644 --- a/db.go +++ b/db.go @@ -87,7 +87,11 @@ func buildQueryForRow(rowId string, row Row, dependencyGraph graph.Graph[string, values := []string{} setStatements := []string{} onConflictColumn := "" - for column, value := range row { + for column, valueRaw := range row { + // Technically we allow more than strings in ripoff files for templating purposes, + // but full support (ex: escaping arrays, what to do with maps, etc.) is quite hard so tabling that for now. + value := fmt.Sprint(valueRaw) + // Rows can explicitly mark what columns they should conflict with, in cases like composite primary keys. if column == "~conflict" { // Really novice way of escaping these. diff --git a/ripoff_file.go b/ripoff_file.go index cf2d28d..082539d 100644 --- a/ripoff_file.go +++ b/ripoff_file.go @@ -6,14 +6,13 @@ import ( "os" "path/filepath" "regexp" - "strconv" "strings" "text/template" "gopkg.in/yaml.v3" ) -type Row map[string]string +type Row map[string]interface{} type RipoffFile struct { Rows map[string]Row `yaml:"rows"` @@ -21,12 +20,8 @@ type RipoffFile struct { var funcMap = template.FuncMap{ // Convenient way to loop a set amount of times. - "intSlice": func(countStr string) ([]int, error) { - countInt, err := strconv.Atoi(countStr) - if err != nil { - return []int{}, err - } - ret := make([]int, countInt) + "intSlice": func(count int) ([]int, error) { + ret := make([]int, count) for i := range ret { ret[i] = i } @@ -43,7 +38,7 @@ func concatRows(templates *template.Template, existingRows map[string]Row, newRo if rowExists { return fmt.Errorf("row %s is defined more than once", rowId) } - templateName, usesTemplate := row["template"] + templateName, usesTemplate := row["template"].(string) if usesTemplate { // "rowId" allows dependencies between templated rows to be clear outside of the template. // Templates can additionally use it to seed random generators. diff --git a/testdata/templates/template_users.yml b/testdata/templates/template_users.yml new file mode 100644 index 0000000..345d985 --- /dev/null +++ b/testdata/templates/template_users.yml @@ -0,0 +1,10 @@ +rows: + {{ range $email, $avatarUrl := .users }} + users:uuid({{ $email }}): + id: users:uuid({{ $email }}) + email: {{ $email }} + avatar_id: avatars:uuid({{ $avatarUrl }}) + avatars:uuid({{ $avatarUrl }}): + id: avatars:uuid({{ $avatarUrl }}) + url: {{ $avatarUrl }} + {{ end }} diff --git a/testdata/templates/templates.yml b/testdata/templates/templates.yml index b22f1ed..2f8db89 100644 --- a/testdata/templates/templates.yml +++ b/testdata/templates/templates.yml @@ -4,3 +4,12 @@ rows: email: foobar@example.com avatarUrl: image.png avatarGrayscale: false + # Templates actually end up replacing the row, so if you don't use rowId in + # the template you can get away with using other variables as seeds. + bulk_users: + template: template_users.yml + users: + a@example.com: a.png + b@example.com: b.png + c@example.com: c.png + d@example.com: d.png