From afedc922247ba50184c4d5de28d65fb59853acad Mon Sep 17 00:00:00 2001 From: Patrick O'brien Date: Wed, 3 Aug 2016 14:22:32 +1000 Subject: [PATCH] Change Enforced to Validated --- bdb/column.go | 24 ++++++++++++------------ bdb/drivers/postgres.go | 22 +++++++++++----------- boil/testing.go | 4 ++-- boil/testing_test.go | 8 ++++---- templates.go | 2 +- templates/01_types.tpl | 2 +- templates_test/insert.tpl | 6 +++--- templates_test/update.tpl | 2 +- 8 files changed, 35 insertions(+), 35 deletions(-) diff --git a/bdb/column.go b/bdb/column.go index 9ec2d92..b80b8d1 100644 --- a/bdb/column.go +++ b/bdb/column.go @@ -11,13 +11,13 @@ import ( // Column holds information about a database column. // Types are Go types, converted by TranslateColumnType. type Column struct { - Name string - Type string - DBType string - Default string - Nullable bool - Unique bool - Enforced bool + Name string + Type string + DBType string + Default string + Nullable bool + Unique bool + Validated bool } // ColumnNames of the columns. @@ -65,12 +65,12 @@ func FilterColumnsByDefault(defaults bool, columns []Column) []Column { } // FilterColumnsBySimpleDefault generates a list of columns that have simple default values -// A simple default value is one without a function call and a non-enforced type +// A simple default value is one without a function call and a non-validated type func FilterColumnsBySimpleDefault(columns []Column) []Column { var cols []Column for _, c := range columns { - if len(c.Default) != 0 && !strings.ContainsAny(c.Default, "()") && !c.Enforced { + if len(c.Default) != 0 && !strings.ContainsAny(c.Default, "()") && !c.Validated { cols = append(cols, c) } } @@ -92,12 +92,12 @@ func FilterColumnsByAutoIncrement(autos bool, columns []Column) []Column { return cols } -// FilterColumnsByEnforced generates the list of enforced columns -func FilterColumnsByEnforced(columns []Column) []Column { +// FilterColumnsByValidated generates the list of validated columns +func FilterColumnsByValidated(columns []Column) []Column { var cols []Column for _, c := range columns { - if c.Enforced == true { + if c.Validated == true { cols = append(cols, c) } } diff --git a/bdb/drivers/postgres.go b/bdb/drivers/postgres.go index 9717fc6..04e2557 100644 --- a/bdb/drivers/postgres.go +++ b/bdb/drivers/postgres.go @@ -17,8 +17,8 @@ type PostgresDriver struct { dbConn *sql.DB } -// enforcedTypes are types that cannot be zero values in the database. -var enforcedTypes = []string{"uuid"} +// validatedTypes are types that cannot be zero values in the database. +var validatedTypes = []string{"uuid"} // NewPostgresDriver takes the database connection details as parameters and // returns a pointer to a PostgresDriver object. Note that it is required to @@ -140,12 +140,12 @@ func (p *PostgresDriver) Columns(tableName string) ([]bdb.Column, error) { } column := bdb.Column{ - Name: colName, - DBType: colType, - Default: colDefault, - Nullable: nullable == "YES", - Unique: unique, - Enforced: isEnforced(colType), + Name: colName, + DBType: colType, + Default: colDefault, + Nullable: nullable == "YES", + Unique: unique, + Validated: isValidated(colType), } columns = append(columns, column) } @@ -298,9 +298,9 @@ func (p *PostgresDriver) TranslateColumnType(c bdb.Column) bdb.Column { return c } -// isEnforced checks if the database type is in the enforcedTypes list. -func isEnforced(typ string) bool { - for _, v := range enforcedTypes { +// isValidated checks if the database type is in the validatedTypes list. +func isValidated(typ string) bool { + for _, v := range validatedTypes { if v == typ { return true } diff --git a/boil/testing.go b/boil/testing.go index e2b02a3..223cb0f 100644 --- a/boil/testing.go +++ b/boil/testing.go @@ -202,7 +202,7 @@ func RandomizeStruct(str interface{}, colTypes map[string]string, includeInvalid return nil } -func RandomizeEnforcedStruct(obj interface{}, enforcedCols []string, colTypes map[string]string) error { +func RandomizeValidatedStruct(obj interface{}, validatedCols []string, colTypes map[string]string) error { // Check if it's pointer value := reflect.ValueOf(obj) kind := value.Kind() @@ -224,7 +224,7 @@ func RandomizeEnforcedStruct(obj interface{}, enforcedCols []string, colTypes ma for i := 0; i < nFields; i++ { fieldVal := value.Field(i) fieldTyp := typ.Field(i) - for _, v := range enforcedCols { + for _, v := range validatedCols { if strmangle.TitleCase(v) == fieldTyp.Name { if err := randomizeField(fieldVal, colTypes[fieldTyp.Name], false); err != nil { return err diff --git a/boil/testing_test.go b/boil/testing_test.go index 177025c..d36f94b 100644 --- a/boil/testing_test.go +++ b/boil/testing_test.go @@ -191,7 +191,7 @@ func TestRandomizeStruct(t *testing.T) { } } -func TestRandomizeEnforcedStruct(t *testing.T) { +func TestRandomizeValidatedStruct(t *testing.T) { t.Parallel() var testStruct = struct { @@ -201,7 +201,7 @@ func TestRandomizeEnforcedStruct(t *testing.T) { UUID2 string }{} - enforcedCols := []string{ + validatedCols := []string{ "uuid1", "uuid2", } @@ -212,7 +212,7 @@ func TestRandomizeEnforcedStruct(t *testing.T) { "UUID2": "uuid", } - err := RandomizeEnforcedStruct(&testStruct, enforcedCols, fieldTypes) + err := RandomizeValidatedStruct(&testStruct, validatedCols, fieldTypes) if err != nil { t.Fatal(err) } @@ -222,6 +222,6 @@ func TestRandomizeEnforcedStruct(t *testing.T) { } if testStruct.UUID1 == "" || testStruct.UUID2 == "" { - t.Errorf("the enforced values should be set: %#v", testStruct) + t.Errorf("the validated values should be set: %#v", testStruct) } } diff --git a/templates.go b/templates.go index ca7e8ad..f266236 100644 --- a/templates.go +++ b/templates.go @@ -168,7 +168,7 @@ var templateFunctions = template.FuncMap{ "filterColumnsByDefault": bdb.FilterColumnsByDefault, "filterColumnsBySimpleDefault": bdb.FilterColumnsBySimpleDefault, "filterColumnsByAutoIncrement": bdb.FilterColumnsByAutoIncrement, - "filterColumnsByEnforced": bdb.FilterColumnsByEnforced, + "filterColumnsByValidated": bdb.FilterColumnsByValidated, "autoIncPrimaryKey": bdb.AutoIncPrimaryKey, "sqlColDefinitions": bdb.SQLColDefinitions, "sqlColDefStrings": bdb.SQLColDefStrings, diff --git a/templates/01_types.tpl b/templates/01_types.tpl index 66d9117..3e2ab2e 100644 --- a/templates/01_types.tpl +++ b/templates/01_types.tpl @@ -5,7 +5,7 @@ var ( {{$varNameSingular}}ColumnsWithoutDefault = []string{{"{"}}{{.Table.Columns | filterColumnsByDefault false | columnNames | stringMap .StringFuncs.quoteWrap | join ","}}{{"}"}} {{$varNameSingular}}ColumnsWithDefault = []string{{"{"}}{{.Table.Columns | filterColumnsByDefault true | columnNames | stringMap .StringFuncs.quoteWrap | join ","}}{{"}"}} {{$varNameSingular}}ColumnsWithSimpleDefault = []string{{"{"}}{{.Table.Columns | filterColumnsBySimpleDefault | columnNames | stringMap .StringFuncs.quoteWrap | join ", "}}{{"}"}} - {{$varNameSingular}}ColumnsEnforced = []string{{"{"}}{{.Table.Columns | filterColumnsByEnforced | columnNames | stringMap .StringFuncs.quoteWrap | join ", "}}{{"}"}} + {{$varNameSingular}}ColumnsValidated = []string{{"{"}}{{.Table.Columns | filterColumnsByValidated | columnNames | stringMap .StringFuncs.quoteWrap | join ", "}}{{"}"}} {{$varNameSingular}}PrimaryKeyColumns = []string{{"{"}}{{.Table.PKey.Columns | stringMap .StringFuncs.quoteWrap | join ", "}}{{"}"}} {{$varNameSingular}}AutoIncrementColumns = []string{{"{"}}{{.Table.Columns | filterColumnsByAutoIncrement true | columnNames | stringMap .StringFuncs.quoteWrap | join "," }}{{"}"}} {{$varNameSingular}}AutoIncPrimaryKey = "{{- with autoIncPrimaryKey .Table.Columns .Table.PKey -}}{{.Name}}{{- end -}}" diff --git a/templates_test/insert.tpl b/templates_test/insert.tpl index 71ec245..29bc2a6 100644 --- a/templates_test/insert.tpl +++ b/templates_test/insert.tpl @@ -37,7 +37,7 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) { {{$varNamePlural}}DeleteAllRows(t) item := &{{$tableNameSingular}}{} - boil.RandomizeEnforcedStruct(item, {{$varNameSingular}}ColumnsEnforced, {{$varNameSingular}}DBTypes) + boil.RandomizeValidatedStruct(item, {{$varNameSingular}}ColumnsValidated, {{$varNameSingular}}DBTypes) if err = item.InsertG(); err != nil { t.Errorf("Unable to insert zero-value item {{$tableNameSingular}}:\n%#v\nErr: %s", item, err) } @@ -68,8 +68,8 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) { regularCols := []string{{"{"}}{{.Table.Columns | filterColumnsByAutoIncrement false | filterColumnsByDefault false | columnNames | stringMap $parent.StringFuncs.quoteWrap | join ", "}}{{"}"}} - // Remove the enforced columns, they can never be zero values - regularCols = boil.SetComplement(regularCols, {{$varNameSingular}}ColumnsEnforced) + // Remove the validated columns, they can never be zero values + regularCols = boil.SetComplement(regularCols, {{$varNameSingular}}ColumnsValidated) // Ensure the non-defaultvalue columns and non-autoincrement columns are stored correctly as zero or null values. for _, c := range regularCols { diff --git a/templates_test/update.tpl b/templates_test/update.tpl index 90b00ba..ab881d2 100644 --- a/templates_test/update.tpl +++ b/templates_test/update.tpl @@ -7,7 +7,7 @@ func Test{{$tableNamePlural}}Update(t *testing.T) { var err error item := {{$tableNameSingular}}{} - boil.RandomizeEnforcedStruct(&item, {{$varNameSingular}}ColumnsEnforced, {{$varNameSingular}}DBTypes) + boil.RandomizeValidatedStruct(&item, {{$varNameSingular}}ColumnsValidated, {{$varNameSingular}}DBTypes) if err = item.InsertG(); err != nil { t.Errorf("Unable to insert zero-value item {{$tableNameSingular}}:\n%#v\nErr: %s", item, err) }