2016-03-16 08:04:26 +01:00
|
|
|
package boil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2016-05-02 08:34:25 +02:00
|
|
|
|
2016-08-09 09:59:30 +02:00
|
|
|
"github.com/vattle/sqlboiler/strmangle"
|
2016-03-16 08:04:26 +01:00
|
|
|
)
|
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
// SetComplement subtracts the elements in b from a
|
|
|
|
func SetComplement(a []string, b []string) []string {
|
|
|
|
c := make([]string, 0, len(a))
|
|
|
|
|
|
|
|
for _, aVal := range a {
|
|
|
|
found := false
|
|
|
|
for _, bVal := range b {
|
|
|
|
if aVal == bVal {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
c = append(c, aVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIntersect returns the elements that are common in a and b
|
|
|
|
func SetIntersect(a []string, b []string) []string {
|
|
|
|
c := make([]string, 0, len(a))
|
|
|
|
|
|
|
|
for _, aVal := range a {
|
|
|
|
found := false
|
|
|
|
for _, bVal := range b {
|
|
|
|
if aVal == bVal {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
c = append(c, aVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-07-15 12:14:47 +02:00
|
|
|
// SetMerge will return a merged slice without duplicates
|
|
|
|
func SetMerge(a []string, b []string) []string {
|
|
|
|
var x, merged []string
|
|
|
|
|
|
|
|
x = append(x, a...)
|
|
|
|
x = append(x, b...)
|
|
|
|
|
|
|
|
check := map[string]bool{}
|
|
|
|
for _, v := range x {
|
|
|
|
if check[v] == true {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
merged = append(merged, v)
|
|
|
|
check[v] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return merged
|
|
|
|
}
|
|
|
|
|
2016-05-02 08:34:25 +02:00
|
|
|
// NonZeroDefaultSet returns the fields included in the
|
|
|
|
// defaults slice that are non zero values
|
|
|
|
func NonZeroDefaultSet(defaults []string, obj interface{}) []string {
|
|
|
|
c := make([]string, 0, len(defaults))
|
|
|
|
|
2016-05-17 12:00:56 +02:00
|
|
|
val := reflect.Indirect(reflect.ValueOf(obj))
|
2016-05-02 08:34:25 +02:00
|
|
|
|
|
|
|
for _, d := range defaults {
|
|
|
|
fieldName := strmangle.TitleCase(d)
|
|
|
|
field := val.FieldByName(fieldName)
|
|
|
|
if !field.IsValid() {
|
|
|
|
panic(fmt.Sprintf("Could not find field name %s in type %T", fieldName, obj))
|
|
|
|
}
|
|
|
|
|
|
|
|
zero := reflect.Zero(field.Type())
|
|
|
|
if !reflect.DeepEqual(zero.Interface(), field.Interface()) {
|
|
|
|
c = append(c, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-06-07 09:08:00 +02:00
|
|
|
// SortByKeys returns a new ordered slice based on the keys ordering
|
2016-06-07 08:21:00 +02:00
|
|
|
func SortByKeys(keys []string, strs []string) []string {
|
|
|
|
c := make([]string, len(strs))
|
|
|
|
|
|
|
|
index := 0
|
|
|
|
Outer:
|
|
|
|
for _, v := range keys {
|
|
|
|
for _, k := range strs {
|
|
|
|
if v == k {
|
|
|
|
c[index] = v
|
|
|
|
index++
|
|
|
|
|
|
|
|
if index > len(strs)-1 {
|
|
|
|
break Outer
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|