Change Enforced to Validated

This commit is contained in:
Patrick O'brien 2016-08-03 14:22:32 +10:00
parent c7c0fe5c0d
commit afedc92224
8 changed files with 35 additions and 35 deletions

View file

@ -11,13 +11,13 @@ import (
// Column holds information about a database column. // Column holds information about a database column.
// Types are Go types, converted by TranslateColumnType. // Types are Go types, converted by TranslateColumnType.
type Column struct { type Column struct {
Name string Name string
Type string Type string
DBType string DBType string
Default string Default string
Nullable bool Nullable bool
Unique bool Unique bool
Enforced bool Validated bool
} }
// ColumnNames of the columns. // 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 // 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 { func FilterColumnsBySimpleDefault(columns []Column) []Column {
var cols []Column var cols []Column
for _, c := range columns { 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) cols = append(cols, c)
} }
} }
@ -92,12 +92,12 @@ func FilterColumnsByAutoIncrement(autos bool, columns []Column) []Column {
return cols return cols
} }
// FilterColumnsByEnforced generates the list of enforced columns // FilterColumnsByValidated generates the list of validated columns
func FilterColumnsByEnforced(columns []Column) []Column { func FilterColumnsByValidated(columns []Column) []Column {
var cols []Column var cols []Column
for _, c := range columns { for _, c := range columns {
if c.Enforced == true { if c.Validated == true {
cols = append(cols, c) cols = append(cols, c)
} }
} }

View file

@ -17,8 +17,8 @@ type PostgresDriver struct {
dbConn *sql.DB dbConn *sql.DB
} }
// enforcedTypes are types that cannot be zero values in the database. // validatedTypes are types that cannot be zero values in the database.
var enforcedTypes = []string{"uuid"} var validatedTypes = []string{"uuid"}
// NewPostgresDriver takes the database connection details as parameters and // NewPostgresDriver takes the database connection details as parameters and
// returns a pointer to a PostgresDriver object. Note that it is required to // 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{ column := bdb.Column{
Name: colName, Name: colName,
DBType: colType, DBType: colType,
Default: colDefault, Default: colDefault,
Nullable: nullable == "YES", Nullable: nullable == "YES",
Unique: unique, Unique: unique,
Enforced: isEnforced(colType), Validated: isValidated(colType),
} }
columns = append(columns, column) columns = append(columns, column)
} }
@ -298,9 +298,9 @@ func (p *PostgresDriver) TranslateColumnType(c bdb.Column) bdb.Column {
return c return c
} }
// isEnforced checks if the database type is in the enforcedTypes list. // isValidated checks if the database type is in the validatedTypes list.
func isEnforced(typ string) bool { func isValidated(typ string) bool {
for _, v := range enforcedTypes { for _, v := range validatedTypes {
if v == typ { if v == typ {
return true return true
} }

View file

@ -202,7 +202,7 @@ func RandomizeStruct(str interface{}, colTypes map[string]string, includeInvalid
return nil 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 // Check if it's pointer
value := reflect.ValueOf(obj) value := reflect.ValueOf(obj)
kind := value.Kind() kind := value.Kind()
@ -224,7 +224,7 @@ func RandomizeEnforcedStruct(obj interface{}, enforcedCols []string, colTypes ma
for i := 0; i < nFields; i++ { for i := 0; i < nFields; i++ {
fieldVal := value.Field(i) fieldVal := value.Field(i)
fieldTyp := typ.Field(i) fieldTyp := typ.Field(i)
for _, v := range enforcedCols { for _, v := range validatedCols {
if strmangle.TitleCase(v) == fieldTyp.Name { if strmangle.TitleCase(v) == fieldTyp.Name {
if err := randomizeField(fieldVal, colTypes[fieldTyp.Name], false); err != nil { if err := randomizeField(fieldVal, colTypes[fieldTyp.Name], false); err != nil {
return err return err

View file

@ -191,7 +191,7 @@ func TestRandomizeStruct(t *testing.T) {
} }
} }
func TestRandomizeEnforcedStruct(t *testing.T) { func TestRandomizeValidatedStruct(t *testing.T) {
t.Parallel() t.Parallel()
var testStruct = struct { var testStruct = struct {
@ -201,7 +201,7 @@ func TestRandomizeEnforcedStruct(t *testing.T) {
UUID2 string UUID2 string
}{} }{}
enforcedCols := []string{ validatedCols := []string{
"uuid1", "uuid1",
"uuid2", "uuid2",
} }
@ -212,7 +212,7 @@ func TestRandomizeEnforcedStruct(t *testing.T) {
"UUID2": "uuid", "UUID2": "uuid",
} }
err := RandomizeEnforcedStruct(&testStruct, enforcedCols, fieldTypes) err := RandomizeValidatedStruct(&testStruct, validatedCols, fieldTypes)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -222,6 +222,6 @@ func TestRandomizeEnforcedStruct(t *testing.T) {
} }
if testStruct.UUID1 == "" || testStruct.UUID2 == "" { 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)
} }
} }

View file

@ -168,7 +168,7 @@ var templateFunctions = template.FuncMap{
"filterColumnsByDefault": bdb.FilterColumnsByDefault, "filterColumnsByDefault": bdb.FilterColumnsByDefault,
"filterColumnsBySimpleDefault": bdb.FilterColumnsBySimpleDefault, "filterColumnsBySimpleDefault": bdb.FilterColumnsBySimpleDefault,
"filterColumnsByAutoIncrement": bdb.FilterColumnsByAutoIncrement, "filterColumnsByAutoIncrement": bdb.FilterColumnsByAutoIncrement,
"filterColumnsByEnforced": bdb.FilterColumnsByEnforced, "filterColumnsByValidated": bdb.FilterColumnsByValidated,
"autoIncPrimaryKey": bdb.AutoIncPrimaryKey, "autoIncPrimaryKey": bdb.AutoIncPrimaryKey,
"sqlColDefinitions": bdb.SQLColDefinitions, "sqlColDefinitions": bdb.SQLColDefinitions,
"sqlColDefStrings": bdb.SQLColDefStrings, "sqlColDefStrings": bdb.SQLColDefStrings,

View file

@ -5,7 +5,7 @@ var (
{{$varNameSingular}}ColumnsWithoutDefault = []string{{"{"}}{{.Table.Columns | filterColumnsByDefault false | columnNames | stringMap .StringFuncs.quoteWrap | join ","}}{{"}"}} {{$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}}ColumnsWithDefault = []string{{"{"}}{{.Table.Columns | filterColumnsByDefault true | columnNames | stringMap .StringFuncs.quoteWrap | join ","}}{{"}"}}
{{$varNameSingular}}ColumnsWithSimpleDefault = []string{{"{"}}{{.Table.Columns | filterColumnsBySimpleDefault | 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}}PrimaryKeyColumns = []string{{"{"}}{{.Table.PKey.Columns | stringMap .StringFuncs.quoteWrap | join ", "}}{{"}"}}
{{$varNameSingular}}AutoIncrementColumns = []string{{"{"}}{{.Table.Columns | filterColumnsByAutoIncrement true | columnNames | 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 -}}" {{$varNameSingular}}AutoIncPrimaryKey = "{{- with autoIncPrimaryKey .Table.Columns .Table.PKey -}}{{.Name}}{{- end -}}"

View file

@ -37,7 +37,7 @@ func Test{{$tableNamePlural}}Insert(t *testing.T) {
{{$varNamePlural}}DeleteAllRows(t) {{$varNamePlural}}DeleteAllRows(t)
item := &{{$tableNameSingular}}{} item := &{{$tableNameSingular}}{}
boil.RandomizeEnforcedStruct(item, {{$varNameSingular}}ColumnsEnforced, {{$varNameSingular}}DBTypes) boil.RandomizeValidatedStruct(item, {{$varNameSingular}}ColumnsValidated, {{$varNameSingular}}DBTypes)
if err = item.InsertG(); err != nil { if err = item.InsertG(); err != nil {
t.Errorf("Unable to insert zero-value item {{$tableNameSingular}}:\n%#v\nErr: %s", item, err) 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 ", "}}{{"}"}} regularCols := []string{{"{"}}{{.Table.Columns | filterColumnsByAutoIncrement false | filterColumnsByDefault false | columnNames | stringMap $parent.StringFuncs.quoteWrap | join ", "}}{{"}"}}
// Remove the enforced columns, they can never be zero values // Remove the validated columns, they can never be zero values
regularCols = boil.SetComplement(regularCols, {{$varNameSingular}}ColumnsEnforced) regularCols = boil.SetComplement(regularCols, {{$varNameSingular}}ColumnsValidated)
// Ensure the non-defaultvalue columns and non-autoincrement columns are stored correctly as zero or null values. // Ensure the non-defaultvalue columns and non-autoincrement columns are stored correctly as zero or null values.
for _, c := range regularCols { for _, c := range regularCols {

View file

@ -7,7 +7,7 @@ func Test{{$tableNamePlural}}Update(t *testing.T) {
var err error var err error
item := {{$tableNameSingular}}{} item := {{$tableNameSingular}}{}
boil.RandomizeEnforcedStruct(&item, {{$varNameSingular}}ColumnsEnforced, {{$varNameSingular}}DBTypes) boil.RandomizeValidatedStruct(&item, {{$varNameSingular}}ColumnsValidated, {{$varNameSingular}}DBTypes)
if err = item.InsertG(); err != nil { if err = item.InsertG(); err != nil {
t.Errorf("Unable to insert zero-value item {{$tableNameSingular}}:\n%#v\nErr: %s", item, err) t.Errorf("Unable to insert zero-value item {{$tableNameSingular}}:\n%#v\nErr: %s", item, err)
} }