Skip to content

Commit c06a4fb

Browse files
authored
new data layout, plus params in config (#25)
1 parent 72b134d commit c06a4fb

28 files changed

+737
-439
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Of course, contributions are more than welcome. Please read these guidelines for
77
Development discussion should take place on the #gnorm channel of [gopher
88
slack](https://gophers.slack.com/).
99

10+
There is a separate #gnorm-dev channel that has the github app to post github activity to the channel, to make it easy to follow.
11+
1012
## Tasks
1113

1214
See the [Nearterm Todos](https://github.com/gnormal/gnorm/projects/1) github

cli/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,10 @@ type Config struct {
9797
// TypeMap and NullableTypeMap must be at the end of your configuration
9898
// file.
9999
NullableTypeMap map[string]string
100+
101+
// Params contains any data you may want to pass to your templates. This is
102+
// a good way to make templates reusable with different configuration values
103+
// for different situations. The values in this field will be available in
104+
// the .Params value for all templates.
105+
Params map[string]interface{}
100106
}

cli/config_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cli
2+
3+
import (
4+
"testing"
5+
6+
"github.com/BurntSushi/toml"
7+
)
8+
9+
func TestExampleToml(t *testing.T) {
10+
c := Config{}
11+
m, err := toml.DecodeFile("gnorm.toml", &c)
12+
if err != nil {
13+
t.Fatal("error parsing config file", err)
14+
}
15+
undec := m.Undecoded()
16+
if len(undec) > 0 {
17+
t.Fatal("Warning: unknown values present in config file:", undec)
18+
}
19+
}

cli/gnorm.toml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
# ConnStr is the connection string for the database.
2-
# Postgres example:
3-
# ConnStr = "dbname=mydb host=127.0.0.1 sslmode=disable user=admin"
42
# MySQL example:
53
# ConnStr = "root:admin@tcp/"
6-
ConnStr = ""
4+
# Postgres example:
5+
ConnStr = "dbname=mydb host=127.0.0.1 sslmode=disable user=admin"
76

87
# DBType holds the type of db you're connecting to. Possible values are
98
# "postgres" or "mysql".
10-
DBType = ""
9+
DBType = "postgres"
1110

1211
# Schemas holds the names of schemas to generate code for.
13-
Schemas = []
12+
Schemas = ["public"]
1413

1514
# NameConversion defines how the DBName of tables, schemas, and enums are
1615
# converted into their Name value. This is a template that may use all the
1716
# regular functions. The "." value is the DB name of the item. Thus, to make an
1817
# item's Name the same as its DBName, you'd use a template of "{{.}}". To make
1918
# the Name the PascalCase version, you'd use "{{pascal .}}".
20-
NameConversion = ""
19+
NameConversion = "{{.}}"
2120

2221
# IncludeTables is a whitelist of tables to generate data for. Tables not
2322
# in this list will not be included in data geenrated by gnorm. You cannot
@@ -118,4 +117,12 @@ PostRun = []
118117
# "character varying" = "sql.NullString"
119118
# "integer" = "sql.NullInt64"
120119
# "numeric" = "sql.NullFloat64"
121-
[NullableTypeMap]
120+
[NullableTypeMap]
121+
122+
123+
# Params contains any data you may want to pass to your templates. This is a
124+
# good way to make templates reusable with different configuration values for
125+
# different situations. The values in this field will be available in the
126+
# .Params value for all templates.
127+
[Params]
128+
# mySpecalValue = "some value"

cli/parse.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"gnorm.org/gnorm/database/drivers/postgres"
1717
"gnorm.org/gnorm/environ"
1818
"gnorm.org/gnorm/run"
19+
"gnorm.org/gnorm/run/data"
1920
)
2021

2122
func parseFile(env environ.Values, file string) (*run.Config, error) {
@@ -61,13 +62,14 @@ func parse(env environ.Values, r io.Reader) (*run.Config, error) {
6162
}
6263

6364
cfg := &run.Config{
64-
ConnStr: c.ConnStr,
65-
Schemas: c.Schemas,
66-
NullableTypeMap: c.NullableTypeMap,
67-
TypeMap: c.TypeMap,
68-
PostRun: c.PostRun,
69-
ExcludeTables: exclude,
70-
IncludeTables: include,
65+
ConfigData: data.ConfigData{
66+
Schemas: c.Schemas,
67+
NullableTypeMap: c.NullableTypeMap,
68+
TypeMap: c.TypeMap,
69+
PostRun: c.PostRun,
70+
ExcludeTables: exclude,
71+
IncludeTables: include,
72+
},
7173
}
7274
d, err := getDriver(strings.ToLower(c.DBType))
7375
if err != nil {

cli/parse_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package cli
22

3-
import "testing"
3+
import (
4+
"testing"
5+
6+
"github.com/BurntSushi/toml"
7+
)
48

59
func TestParseTables(t *testing.T) {
610
tables := []string{"table", "schema.table2"}
@@ -35,6 +39,18 @@ func TestParseTables(t *testing.T) {
3539
}
3640
}
3741

42+
func TestParseGnormTolm(t *testing.T) {
43+
c := Config{}
44+
m, err := toml.DecodeFile("gnorm.toml", &c)
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
undec := m.Undecoded()
49+
if len(undec) > 0 {
50+
t.Fatalf("unknown values present in config file: %s", undec)
51+
}
52+
}
53+
3854
func contains(list []string, s string) bool {
3955
for x := range list {
4056
if list[x] == s {

cli/sample.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,24 @@ package cli
2525
// }
2626
// gocog]]]
2727
const sample = `# ConnStr is the connection string for the database.
28-
# Postgres example:
29-
# ConnStr = "dbname=mydb host=127.0.0.1 sslmode=disable user=admin"
3028
# MySQL example:
3129
# ConnStr = "root:admin@tcp/"
32-
ConnStr = ""
30+
# Postgres example:
31+
ConnStr = "dbname=mydb host=127.0.0.1 sslmode=disable user=admin"
3332
3433
# DBType holds the type of db you're connecting to. Possible values are
3534
# "postgres" or "mysql".
36-
DBType = ""
35+
DBType = "postgres"
3736
3837
# Schemas holds the names of schemas to generate code for.
39-
Schemas = []
38+
Schemas = ["public"]
4039
4140
# NameConversion defines how the DBName of tables, schemas, and enums are
4241
# converted into their Name value. This is a template that may use all the
4342
# regular functions. The "." value is the DB name of the item. Thus, to make an
4443
# item's Name the same as its DBName, you'd use a template of "{{.}}". To make
4544
# the Name the PascalCase version, you'd use "{{pascal .}}".
46-
NameConversion = ""
45+
NameConversion = "{{.}}"
4746
4847
# IncludeTables is a whitelist of tables to generate data for. Tables not
4948
# in this list will not be included in data geenrated by gnorm. You cannot
@@ -144,5 +143,13 @@ PostRun = []
144143
# "character varying" = "sql.NullString"
145144
# "integer" = "sql.NullInt64"
146145
# "numeric" = "sql.NullFloat64"
147-
[NullableTypeMap]`
146+
[NullableTypeMap]
147+
148+
149+
# Params contains any data you may want to pass to your templates. This is a
150+
# good way to make templates reusable with different configuration values for
151+
# different situations. The values in this field will be available in the
152+
# .Params value for all templates.
153+
[Params]
154+
# mySpecalValue = "some value"`
148155
// [[[end]]]

database/drivers/mysql/parse.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ func parse(log *log.Logger, conn string, schemaNames []string, filterTables func
3636
return nil, err
3737
}
3838

39-
schemas := make(map[string]map[string]database.Columns, len(schemaNames))
39+
schemas := make(map[string]map[string][]*database.Column, len(schemaNames))
4040
for _, name := range schemaNames {
41-
schemas[name] = map[string]database.Columns{}
41+
schemas[name] = map[string][]*database.Column{}
4242
}
4343

4444
for _, t := range tables {
@@ -80,21 +80,20 @@ func parse(log *log.Logger, conn string, schemaNames []string, filterTables func
8080
}
8181
schema[c.TableName] = append(schema[c.TableName], col)
8282
if enum != nil {
83-
enum.DBTable = c.TableName
84-
enum.DBSchema = c.TableSchema
85-
enums[enum.DBSchema] = append(enums[enum.DBSchema], enum)
83+
enum.Table = c.TableName
84+
enums[c.TableSchema] = append(enums[c.TableSchema], enum)
8685
}
8786
}
8887

8988
res := &database.Info{Schemas: make([]*database.Schema, 0, len(schemas))}
9089
for _, schema := range schemaNames {
9190
tables := schemas[schema]
9291
s := &database.Schema{
93-
DBName: schema,
94-
Enums: enums[schema],
92+
Name: schema,
93+
Enums: enums[schema],
9594
}
9695
for tname, columns := range tables {
97-
s.Tables = append(s.Tables, &database.Table{DBName: tname, DBSchema: schema, Columns: columns})
96+
s.Tables = append(s.Tables, &database.Table{Name: tname, Columns: columns})
9897
}
9998
res.Schemas = append(res.Schemas, s)
10099
}
@@ -104,10 +103,10 @@ func parse(log *log.Logger, conn string, schemaNames []string, filterTables func
104103

105104
func toDBColumn(c *columns.Row, log *log.Logger) (*database.Column, *database.Enum, error) {
106105
col := &database.Column{
107-
DBName: c.ColumnName,
106+
Name: c.ColumnName,
108107
Nullable: c.IsNullable == "YES",
109108
HasDefault: c.ColumnDefault.String != "",
110-
DBType: c.DataType,
109+
Type: c.DataType,
111110
Orig: *c,
112111
}
113112

@@ -132,7 +131,7 @@ func toDBColumn(c *columns.Row, log *log.Logger) (*database.Column, *database.En
132131
// we'll call the enum the same as the column name.
133132
// the function above will set the table name etc
134133
enum := &database.Enum{
135-
DBName: col.DBName,
134+
Name: col.Name,
136135
}
137136
// strip off the enum and parens
138137
s := c.ColumnType[5 : len(c.ColumnType)-1]
@@ -141,7 +140,7 @@ func toDBColumn(c *columns.Row, log *log.Logger) (*database.Column, *database.En
141140
for x := range vals {
142141
enum.Values[x] = &database.EnumValue{
143142
// strip off the quotes
144-
DBName: vals[x][1 : len(vals[x])-1],
143+
Name: vals[x][1 : len(vals[x])-1],
145144
// enum values start at 1 in mysql
146145
Value: x + 1,
147146
}

database/drivers/mysql/templates/schema.gotmpl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,4 @@ func (f {{$fieldName}}Field) In(vals []{{.}}) WhereClause {
7575
}
7676

7777

78-
79-
80-
{{end}}
78+
{{end}}

database/drivers/mysql/templates/table.gotmpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Code generated by gnorm, DO NOT EDIT!
2-
2+
{{with .Table}}
33
package {{toLower .Name}}
44

55
import "gnorm.org/gnorm/database/drivers/mysql/gnorm"
@@ -52,3 +52,4 @@ func Query(db gnorm.DB, where gnorm.WhereClause) ([]*Row, error) {
5252
return vals, nil
5353
}
5454

55+
{{end}}

0 commit comments

Comments
 (0)