Skip to content

Commit

Permalink
Add missing config items to configdata (#67)
Browse files Browse the repository at this point in the history
* add the rest of config to configdata and add a test

* let's actually set params, eh?
  • Loading branch information
natefinch authored Oct 24, 2017
1 parent 5b20ff0 commit 4b0d4dc
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 106 deletions.
5 changes: 3 additions & 2 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ type Config struct {
// the .Params value for all templates.
Params map[string]interface{}

// PluginDirs a set of absolute/relative paths that will be used for
// plugin lookup.
// PluginDirs a list of paths that will be used for finding plugins. The
// list will be traversed in order, looking for a specifically named plugin.
// The first plugin that is found will be the one used.
PluginDirs []string

// OutputDir is the directory relative to the project root (where the
Expand Down
19 changes: 0 additions & 19 deletions cli/config_test.go

This file was deleted.

48 changes: 25 additions & 23 deletions cli/gnorm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ DBType = "postgres"
# Schemas holds the names of schemas to generate code for.
Schemas = ["public"]

# PluginDirs a list of paths that will be used for finding plugins. The list
# will be traversed in order, looking for a specifically named plugin. The first
# plugin that is found will be the one used.
PluginDirs = ["plugins"]

# NameConversion defines how the DBName of tables, schemas, and enums are
# converted into their Name value. This is a template that may use all the
# regular functions. The "." value is the DB name of the item. Thus, to make an
Expand All @@ -32,16 +37,15 @@ IncludeTables = []
# generation. You cannot set ExcludeTables if IncludeTables is set. By
# default, tables will be excluded from all schemas. To specify tables for
# a specific schema only, use the schema.tablenmae format.
ExcludeTables = []
ExcludeTables = ["xyzzx"]

# PostRun is a command with arguments that is run after each file is generated
# by GNORM. It is generally used to reformat the file, but it can be for any
# use. Environment variables will be expanded, and the special $GNORMFILE
# environment variable may be used, which will expand to the name of the file
# that was just generated.
# Example to run goimports on each output file:
# PostRun = ["goimports", "-w", "$GNORMFILE"]
PostRun = []
PostRun = ["echo", "$GNORMFILE"]

# OutputDir is the directory relative to the project root (where the
# gnorm.toml file is located) in which all the generated files are written
Expand Down Expand Up @@ -72,7 +76,7 @@ StaticDir = "static"
# "tables.gotmpl" would render tables.gotmpl template with data from the the
# "public.users" table to ./public/users/users.go.
[TablePaths]
"{{.Schema}}/tables/{{.Table}}.go" = "templates/table.gotmpl"
"{{.Schema}}/tables/{{.Table}}.go" = "testdata/table.tpl"

# SchemaPaths iis a map of output paths to template paths that tells Gnorm how
# to render and output its schema info. Each template will be rendered with
Expand All @@ -86,7 +90,7 @@ StaticDir = "static"
# schemas.gotmpl template with the "public" schema and output to
# ./schemas/public/public.go
[SchemaPaths]
"{{.Schema}}.go" = "templates/schema.gotmpl"
"{{.Schema}}.go" = "testdata/schema.tpl"

# EnumPaths is a is a map of output paths to template paths that tells Gnorm how
# to render and output its enum info. Each template will be rendered with each
Expand All @@ -100,7 +104,7 @@ StaticDir = "static"
# "enums.gotmpl" would render the enums.gotmpl template with data from the
# "public.book_type" enum to ./gnorm/public/enums/users.go.
[EnumPaths]
"{{.Schema}}/enums/{{.Enum}}.go" = "templates/enum.gotmpl"
"{{.Schema}}/enums/{{.Enum}}.go" = "testdata/enum.tpl"

# TypeMap is a mapping of database type names to replacement type names
# (generally types from your language for deserialization), specifically for
Expand All @@ -110,15 +114,14 @@ StaticDir = "static"
# of the way tables in TOML work, TypeMap and NullableTypeMap must be at the end
# of your configuration file.
# Example for mapping postgres types to Go types:
# [TypeMap]
# "timestamp with time zone" = "time.Time"
# "text" = "string"
# "boolean" = "bool"
# "uuid" = "uuid.UUID"
# "character varying" = "string"
# "integer" = "int"
# "numeric" = "float64"
[TypeMap]
"timestamp with time zone" = "time.Time"
"text" = "string"
"boolean" = "bool"
"uuid" = "uuid.UUID"
"character varying" = "string"
"integer" = "int"
"numeric" = "float64"

# NullableTypeMap is a mapping of database type names to replacement type names
# (generally types from your language for deserialization), specifically for
Expand All @@ -128,20 +131,19 @@ StaticDir = "static"
# of the way tables in TOML work, TypeMap and NullableTypeMap must be at the end
# of your configuration file.
# Example for mapping postgres types to Go types:
# [NullableTypeMap]
# "timestamp with time zone" = "pq.NullTime"
# "text" = "sql.NullString"
# "boolean" = "sql.NullBool"
# "uuid" = "uuid.NullUUID"
# "character varying" = "sql.NullString"
# "integer" = "sql.NullInt64"
# "numeric" = "sql.NullFloat64"
[NullableTypeMap]
"timestamp with time zone" = "pq.NullTime"
"text" = "sql.NullString"
"boolean" = "sql.NullBool"
"uuid" = "uuid.NullUUID"
"character varying" = "sql.NullString"
"integer" = "sql.NullInt64"
"numeric" = "sql.NullFloat64"


# Params contains any data you may want to pass to your templates. This is a
# good way to make templates reusable with different configuration values for
# different situations. The values in this field will be available in the
# .Params value for all templates.
[Params]
# mySpecalValue = "some value"
mySpecialValue = "some value"
8 changes: 6 additions & 2 deletions cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,19 @@ func parse(env environ.Values, r io.Reader) (*run.Config, error) {

cfg := &run.Config{
ConfigData: data.ConfigData{
ConnStr: c.ConnStr,
DBType: c.DBType,
Schemas: c.Schemas,
NullableTypeMap: c.NullableTypeMap,
TypeMap: c.TypeMap,
PostRun: c.PostRun,
ExcludeTables: exclude,
IncludeTables: include,
OutputDir: c.OutputDir,
StaticDir: c.StaticDir,
PluginDirs: c.PluginDirs,
},
OutputDir: c.OutputDir,
StaticDir: c.StaticDir,
Params: c.Params,
}
d, err := getDriver(strings.ToLower(c.DBType))
if err != nil {
Expand Down
59 changes: 59 additions & 0 deletions cli/parse_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package cli

import (
"bytes"
"log"
"testing"

"gnorm.org/gnorm/environ"
"gnorm.org/gnorm/run/data"

"github.com/BurntSushi/toml"
"github.com/google/go-cmp/cmp"
)

func TestParseTables(t *testing.T) {
Expand Down Expand Up @@ -39,6 +45,59 @@ func TestParseTables(t *testing.T) {
}
}

func TestParseConfig(t *testing.T) {
var stderr, stdout bytes.Buffer
env := environ.Values{
Stderr: &stderr,
Stdout: &stdout,
Log: log.New(&stderr, "", 0),
}
cfg, err := parseFile(env, "gnorm.toml")
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(cfg.Params, map[string]interface{}{"mySpecialValue": "some value"}); diff != "" {
t.Errorf("Params not copied correctly:\n%s", diff)
}
expected := data.ConfigData{
ConnStr: "dbname=mydb host=127.0.0.1 sslmode=disable user=admin",
DBType: "postgres",
Schemas: []string{"public"},
PostRun: []string{"echo", "$GNORMFILE"},
IncludeTables: map[string][]string{
"public": nil,
},
ExcludeTables: map[string][]string{
"public": []string{"xyzzx"},
},
TypeMap: map[string]string{
"timestamp with time zone": "time.Time",
"text": "string",
"boolean": "bool",
"uuid": "uuid.UUID",
"character varying": "string",
"integer": "int",
"numeric": "float64",
},
NullableTypeMap: map[string]string{
"timestamp with time zone": "pq.NullTime",
"text": "sql.NullString",
"boolean": "sql.NullBool",
"uuid": "uuid.NullUUID",
"character varying": "sql.NullString",
"integer": "sql.NullInt64",
"numeric": "sql.NullFloat64",
},
PluginDirs: []string{"plugins"},
OutputDir: "gnorm",
StaticDir: "static",
}
if diff := cmp.Diff(cfg.ConfigData, expected); diff != "" {
t.Fatalf("Actual differs from expected:\n%s", diff)
}

}

func TestParseGnormToml(t *testing.T) {
c := Config{}
m, err := toml.DecodeFile("gnorm.toml", &c)
Expand Down
48 changes: 25 additions & 23 deletions cli/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ DBType = "postgres"
# Schemas holds the names of schemas to generate code for.
Schemas = ["public"]
# PluginDirs a list of paths that will be used for finding plugins. The list
# will be traversed in order, looking for a specifically named plugin. The first
# plugin that is found will be the one used.
PluginDirs = ["plugins"]
# NameConversion defines how the DBName of tables, schemas, and enums are
# converted into their Name value. This is a template that may use all the
# regular functions. The "." value is the DB name of the item. Thus, to make an
Expand All @@ -58,16 +63,15 @@ IncludeTables = []
# generation. You cannot set ExcludeTables if IncludeTables is set. By
# default, tables will be excluded from all schemas. To specify tables for
# a specific schema only, use the schema.tablenmae format.
ExcludeTables = []
ExcludeTables = ["xyzzx"]
# PostRun is a command with arguments that is run after each file is generated
# by GNORM. It is generally used to reformat the file, but it can be for any
# use. Environment variables will be expanded, and the special $GNORMFILE
# environment variable may be used, which will expand to the name of the file
# that was just generated.
# Example to run goimports on each output file:
# PostRun = ["goimports", "-w", "$GNORMFILE"]
PostRun = []
PostRun = ["echo", "$GNORMFILE"]
# OutputDir is the directory relative to the project root (where the
# gnorm.toml file is located) in which all the generated files are written
Expand Down Expand Up @@ -98,7 +102,7 @@ StaticDir = "static"
# "tables.gotmpl" would render tables.gotmpl template with data from the the
# "public.users" table to ./public/users/users.go.
[TablePaths]
"{{.Schema}}/tables/{{.Table}}.go" = "templates/table.gotmpl"
"{{.Schema}}/tables/{{.Table}}.go" = "testdata/table.tpl"
# SchemaPaths iis a map of output paths to template paths that tells Gnorm how
# to render and output its schema info. Each template will be rendered with
Expand All @@ -112,7 +116,7 @@ StaticDir = "static"
# schemas.gotmpl template with the "public" schema and output to
# ./schemas/public/public.go
[SchemaPaths]
"{{.Schema}}.go" = "templates/schema.gotmpl"
"{{.Schema}}.go" = "testdata/schema.tpl"
# EnumPaths is a is a map of output paths to template paths that tells Gnorm how
# to render and output its enum info. Each template will be rendered with each
Expand All @@ -126,7 +130,7 @@ StaticDir = "static"
# "enums.gotmpl" would render the enums.gotmpl template with data from the
# "public.book_type" enum to ./gnorm/public/enums/users.go.
[EnumPaths]
"{{.Schema}}/enums/{{.Enum}}.go" = "templates/enum.gotmpl"
"{{.Schema}}/enums/{{.Enum}}.go" = "testdata/enum.tpl"
# TypeMap is a mapping of database type names to replacement type names
# (generally types from your language for deserialization), specifically for
Expand All @@ -136,15 +140,14 @@ StaticDir = "static"
# of the way tables in TOML work, TypeMap and NullableTypeMap must be at the end
# of your configuration file.
# Example for mapping postgres types to Go types:
# [TypeMap]
# "timestamp with time zone" = "time.Time"
# "text" = "string"
# "boolean" = "bool"
# "uuid" = "uuid.UUID"
# "character varying" = "string"
# "integer" = "int"
# "numeric" = "float64"
[TypeMap]
"timestamp with time zone" = "time.Time"
"text" = "string"
"boolean" = "bool"
"uuid" = "uuid.UUID"
"character varying" = "string"
"integer" = "int"
"numeric" = "float64"
# NullableTypeMap is a mapping of database type names to replacement type names
# (generally types from your language for deserialization), specifically for
Expand All @@ -154,21 +157,20 @@ StaticDir = "static"
# of the way tables in TOML work, TypeMap and NullableTypeMap must be at the end
# of your configuration file.
# Example for mapping postgres types to Go types:
# [NullableTypeMap]
# "timestamp with time zone" = "pq.NullTime"
# "text" = "sql.NullString"
# "boolean" = "sql.NullBool"
# "uuid" = "uuid.NullUUID"
# "character varying" = "sql.NullString"
# "integer" = "sql.NullInt64"
# "numeric" = "sql.NullFloat64"
[NullableTypeMap]
"timestamp with time zone" = "pq.NullTime"
"text" = "sql.NullString"
"boolean" = "sql.NullBool"
"uuid" = "uuid.NullUUID"
"character varying" = "sql.NullString"
"integer" = "sql.NullInt64"
"numeric" = "sql.NullFloat64"
# Params contains any data you may want to pass to your templates. This is a
# good way to make templates reusable with different configuration values for
# different situations. The values in this field will be available in the
# .Params value for all templates.
[Params]
# mySpecalValue = "some value"`
mySpecialValue = "some value"`
// [[[end]]]
Empty file added cli/testdata/enum.tpl
Empty file.
Empty file added cli/testdata/schema.tpl
Empty file.
Empty file added cli/testdata/table.tpl
Empty file.
13 changes: 0 additions & 13 deletions run/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,6 @@ type Config struct {
// for different situations. The values in this field will be available in
// the .Params value for all templates.
Params map[string]interface{}

// OutputDir is the directory relative to the project root (where the
// gnorm.toml file is located) in which all the generated files are written
// to.
OutputDir string

// StaticDir is the directory relative to the project root (where the
// gnorm.toml file is located) in which all static files , which are
// intended to be copied to the OutputDir are found.
//
// The directory structure is preserved when copying the files to the
// OutputDir
StaticDir string
}

// OutputTarget contains a template that generates a filename to write to, and
Expand Down
Loading

0 comments on commit 4b0d4dc

Please sign in to comment.