From 5b20ff0b92fd7cbb9c0a1c2c99af2ccb4819d93e Mon Sep 17 00:00:00 2001 From: Nate Finch Date: Mon, 23 Oct 2017 11:58:29 -0400 Subject: [PATCH] add except method (#65) --- cli/gnorm.toml | 4 +++- cli/sample.go | 4 +++- run/data/data.go | 20 ++++++++++++++++++++ run/data/data_test.go | 13 ++++++++++++- site/content/cli/configuration.md | 4 +++- site/content/templates/data.md | 1 + 6 files changed, 42 insertions(+), 4 deletions(-) diff --git a/cli/gnorm.toml b/cli/gnorm.toml index f067122..f4b5f22 100644 --- a/cli/gnorm.toml +++ b/cli/gnorm.toml @@ -1,4 +1,6 @@ -# ConnStr is the connection string for the database. +# ConnStr is the connection string for the database. Any environment variables +# in this string will be expanded, so for example dbname=$MY_DDB will do the +# right thing. # MySQL example: # ConnStr = "root:admin@tcp/" # Postgres example: diff --git a/cli/sample.go b/cli/sample.go index 0c67c95..247ccf3 100644 --- a/cli/sample.go +++ b/cli/sample.go @@ -24,7 +24,9 @@ package cli // fmt.Println("`") // } // gocog]]] -const sample = `# ConnStr is the connection string for the database. +const sample = `# ConnStr is the connection string for the database. Any environment variables +# in this string will be expanded, so for example dbname=$MY_DDB will do the +# right thing. # MySQL example: # ConnStr = "root:admin@tcp/" # Postgres example: diff --git a/run/data/data.go b/run/data/data.go index a483529..af4d0f7 100644 --- a/run/data/data.go +++ b/run/data/data.go @@ -154,6 +154,26 @@ func (s Strings) Sprintf(format string) Strings { return ret } +// Except returns a copy of the Strings with the given values removed. +func (s Strings) Except(excludes []string) Strings { + ret := make(Strings, 0, len(s)) + for x := range s { + if !contains(excludes, s[x]) { + ret = append(ret, s[x]) + } + } + return ret +} + +func contains(list []string, s string) bool { + for x := range list { + if s == list[x] { + return true + } + } + return false +} + // Columns represents the ordered list of columns in a table. type Columns []*Column diff --git a/run/data/data_test.go b/run/data/data_test.go index e385a6d..e54fbc0 100644 --- a/run/data/data_test.go +++ b/run/data/data_test.go @@ -1,6 +1,9 @@ package data -import "testing" +import ( + "reflect" + "testing" +) func TestStringsSprintf(t *testing.T) { s := Strings{"one", "two", "three"} @@ -14,3 +17,11 @@ func TestStringsSprintf(t *testing.T) { } } } + +func TestStringsExcept(t *testing.T) { + vals := Strings{"one", "two", "three", "four"}.Except([]string{"ONE", "two", "three"}) + expected := Strings{"one", "four"} + if !reflect.DeepEqual(vals, expected) { + t.Fatalf("expected %q, but got %q", expected, vals) + } +} diff --git a/site/content/cli/configuration.md b/site/content/cli/configuration.md index 1386153..0ba39e9 100644 --- a/site/content/cli/configuration.md +++ b/site/content/cli/configuration.md @@ -35,7 +35,9 @@ func main() { } gocog}}} --> ``` -# ConnStr is the connection string for the database. +# ConnStr is the connection string for the database. Any environment variables +# in this string will be expanded, so for example dbname=$MY_DDB will do the +# right thing. # MySQL example: # ConnStr = "root:admin@tcp/" # Postgres example: diff --git a/site/content/templates/data.md b/site/content/templates/data.md index 060e833..75035ba 100644 --- a/site/content/templates/data.md +++ b/site/content/templates/data.md @@ -148,6 +148,7 @@ Strings is a list of string values with the following methods | Method | Arguments | Description | | --- | ---- | --- | | Sprintf | format (string) | Sprintf calls [fmt.Sprintf](https://golang.org/pkg/fmt/#Sprintf)(format, str) for every string in this value and returns the results as a new Strings value. +| Except | vals ([]string) | Except returns a Strings value with the given values removed from the list (if they existed). The check is case sensitive. ### Table