Skip to content

Commit

Permalink
Add a dedicated "None" column kind for empty columns
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuel099 committed Aug 28, 2020
1 parent c2ea4b3 commit 374c0fa
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion boil/columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
// to query for each kind on the column type itself, see IsInfer
// as example.
const (
columnsInfer int = iota
columnsNone int = iota
columnsInfer
columnsWhitelist
columnsGreylist
columnsBlacklist
Expand All @@ -26,6 +27,19 @@ type Columns struct {
Cols []string
}

// None creates an empty column list.
func None() Columns {
return Columns{
Kind: columnsNone,
}
}

// IsNone checks to see if no columns should be inferred.
// This method is here simply to not have to export the columns types.
func (c Columns) IsNone() bool {
return c.Kind == columnsNone
}

// Infer is a placeholder that means there is no other list, simply
// infer the final list of columns for insert/update etc.
func Infer() Columns {
Expand Down Expand Up @@ -96,6 +110,10 @@ func (c Columns) IsGreylist() bool {
// Note that a default column's zero value is based on the Go type and does
// not take into account the default value in the database.
//
// None:
// insert: empty
// return: empty
//
// Infer:
// insert: columns-without-default + non-zero-default-columns
// return: columns-with-defaults - insert
Expand All @@ -113,6 +131,9 @@ func (c Columns) IsGreylist() bool {
// return: columns-with-defaults - insert
func (c Columns) InsertColumnSet(cols, defaults, noDefaults, nonZeroDefaults []string) ([]string, []string) {
switch c.Kind {
case columnsNone:
return nil, nil

case columnsInfer:
insert := make([]string, len(noDefaults))
copy(insert, noDefaults)
Expand Down Expand Up @@ -152,12 +173,15 @@ func (c Columns) InsertColumnSet(cols, defaults, noDefaults, nonZeroDefaults []s
// which isn't useful in an update since then you can't find the original
// record you were trying to update.
//
// None: empty
// Infer: all - pkey-columns
// whitelist: whitelist
// blacklist: all - pkeys - blacklist
// greylist: all - pkeys + greylist
func (c Columns) UpdateColumnSet(allColumns, pkeyCols []string) []string {
switch c.Kind {
case columnsNone:
return nil
case columnsInfer:
return strmangle.SetComplement(allColumns, pkeyCols)
case columnsWhitelist:
Expand Down

0 comments on commit 374c0fa

Please sign in to comment.