Move string set operations to strmangle

- Delete MakeDBName (unused)
- Rename HasElement to SetInclude
This commit is contained in:
Aaron L 2016-08-13 16:03:34 -07:00
commit ab88d8511d
16 changed files with 290 additions and 297 deletions

95
strmangle/sets.go Normal file
View file

@ -0,0 +1,95 @@
package strmangle
// SetInclude checks to see if the string is found in the string slice
func SetInclude(str string, slice []string) bool {
for _, s := range slice {
if str == s {
return true
}
}
return false
}
// 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
}
// 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
}
// SortByKeys returns a new ordered slice based on the keys ordering
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
}

170
strmangle/sets_test.go Normal file
View file

@ -0,0 +1,170 @@
package strmangle
import (
"reflect"
"testing"
)
func TestSetInclude(t *testing.T) {
t.Parallel()
elements := []string{"one", "two"}
if got := SetInclude("one", elements); !got {
t.Error("should have found element key")
}
if got := SetInclude("three", elements); got {
t.Error("should not have found element key")
}
}
func TestSetComplement(t *testing.T) {
t.Parallel()
tests := []struct {
A []string
B []string
C []string
}{
{
[]string{"thing1", "thing2", "thing3"},
[]string{"thing2", "otherthing", "stuff"},
[]string{"thing1", "thing3"},
},
{
[]string{},
[]string{"thing1", "thing2"},
[]string{},
},
{
[]string{"thing1", "thing2"},
[]string{},
[]string{"thing1", "thing2"},
},
{
[]string{"thing1", "thing2"},
[]string{"thing1", "thing2"},
[]string{},
},
}
for i, test := range tests {
c := SetComplement(test.A, test.B)
if !reflect.DeepEqual(test.C, c) {
t.Errorf("[%d] mismatch:\nWant: %#v\nGot: %#v", i, test.C, c)
}
}
}
func TestSetIntersect(t *testing.T) {
t.Parallel()
tests := []struct {
A []string
B []string
C []string
}{
{
[]string{"thing1", "thing2", "thing3"},
[]string{"thing2", "otherthing", "stuff"},
[]string{"thing2"},
},
{
[]string{},
[]string{"thing1", "thing2"},
[]string{},
},
{
[]string{"thing1", "thing2"},
[]string{},
[]string{},
},
{
[]string{"thing1", "thing2"},
[]string{"thing1", "thing2"},
[]string{"thing1", "thing2"},
},
}
for i, test := range tests {
c := SetIntersect(test.A, test.B)
if !reflect.DeepEqual(test.C, c) {
t.Errorf("[%d] mismatch:\nWant: %#v\nGot: %#v", i, test.C, c)
}
}
}
func TestSetMerge(t *testing.T) {
t.Parallel()
tests := []struct {
A []string
B []string
C []string
}{
{
[]string{"thing1", "thing2", "thing3"},
[]string{"thing1", "thing3", "thing4"},
[]string{"thing1", "thing2", "thing3", "thing4"},
},
{
[]string{},
[]string{"thing1", "thing2"},
[]string{"thing1", "thing2"},
},
{
[]string{"thing1", "thing2"},
[]string{},
[]string{"thing1", "thing2"},
},
{
[]string{"thing1", "thing2", "thing3"},
[]string{"thing1", "thing2", "thing3"},
[]string{"thing1", "thing2", "thing3"},
},
{
[]string{"thing1", "thing2"},
[]string{"thing3", "thing4"},
[]string{"thing1", "thing2", "thing3", "thing4"},
},
}
for i, test := range tests {
m := SetMerge(test.A, test.B)
if !reflect.DeepEqual(test.C, m) {
t.Errorf("[%d] mismatch:\nWant: %#v\nGot: %#v", i, test.C, m)
}
}
}
func TestSortByKeys(t *testing.T) {
t.Parallel()
tests := []struct {
Keys []string
Strs []string
Ret []string
}{
{
[]string{"id", "name", "thing", "stuff"},
[]string{"thing", "stuff", "name", "id"},
[]string{"id", "name", "thing", "stuff"},
},
{
[]string{"id", "name", "thing", "stuff"},
[]string{"id", "name", "thing", "stuff"},
[]string{"id", "name", "thing", "stuff"},
},
{
[]string{"id", "name", "thing", "stuff"},
[]string{"stuff", "thing"},
[]string{"thing", "stuff"},
},
}
for i, test := range tests {
z := SortByKeys(test.Keys, test.Strs)
if !reflect.DeepEqual(test.Ret, z) {
t.Errorf("[%d] mismatch:\nWant: %#v\nGot: %#v", i, test.Ret, z)
}
}
}

View file

@ -1,6 +1,6 @@
// Package strmangle is used exclusively by the templates in sqlboiler.
// There are many helper functions to deal with bdb.* values as well
// as string manipulation. Because it is focused on pipelining inside templates
// Package strmangle is a collection of string manipulation functions.
// Primarily used by boil and templates for code generation.
// Because it is focused on pipelining inside templates
// you will see some odd parameter ordering.
package strmangle
@ -216,24 +216,6 @@ func StringMap(modifier func(string) string, strs []string) []string {
return ret
}
// MakeDBName takes a table name in the format of "table_name" and a
// column name in the format of "column_name" and returns a name used in the
// `db:""` component of an object in the format of "table_name_column_name"
func MakeDBName(tableName, colName string) string {
return fmt.Sprintf("%s_%s", tableName, colName)
}
// HasElement checks to see if the string is found in the string slice
func HasElement(str string, slice []string) bool {
for _, s := range slice {
if str == s {
return true
}
}
return false
}
// PrefixStringSlice with the given str.
func PrefixStringSlice(str string, strs []string) []string {
ret := make([]string, len(strs))

View file

@ -234,26 +234,6 @@ func TestStringMap(t *testing.T) {
}
}
func TestMakeDBName(t *testing.T) {
t.Parallel()
if out := MakeDBName("a", "b"); out != "a_b" {
t.Error("Out was wrong:", out)
}
}
func TestHasElement(t *testing.T) {
t.Parallel()
elements := []string{"one", "two"}
if got := HasElement("one", elements); !got {
t.Error("should have found element key")
}
if got := HasElement("three", elements); got {
t.Error("should not have found element key")
}
}
func TestPrefixStringSlice(t *testing.T) {
t.Parallel()
@ -262,6 +242,7 @@ func TestPrefixStringSlice(t *testing.T) {
t.Error("wrong output:", got)
}
}
func TestWhereClause(t *testing.T) {
t.Parallel()