Skip to content

Generalize where* functions #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ Within those templates, the object emitted by docker-gen will have [this structu
* *`split $string $sep`*: Splits `$string` into a slice of substrings delimited by `$sep`. Alias for [`strings.Split`](http://golang.org/pkg/strings/#Split)
* *`trimPrefix $prefix $string`*: If `$prefix` is a prefix of `$string`, return `$string` with `$prefix` trimmed from the beginning. Otherwise, return `$string` unchanged.
* *`trimSuffix $suffix $string`*: If `$suffix` is a suffix of `$string`, return `$string` with `$suffix` trimmed from the end. Otherwise, return `$string` unchanged.
* *`where $containers $fieldPath $value`*: Filters an array of `RuntimeContainer` instances based on the values of a field path expression `$fieldPath`. A field path expression is a dot-delimited list of map keys or struct member names specifying the path from container to a nested value, which must be a string. Returns an array of containers having that value.
* *`whereExist $containers $fieldPath`*: Like `where`, but returns only containers where `$fieldPath` exists (is not nil).
* *`whereNotExist $containers $fieldPath`*: Like `where`, but returns only containers where `$fieldPath` does not exist (is nil).
* *`whereAny $containers $fieldPath $sep $values`*: Like `where`, but the string value specified by `$fieldPath` is first split by `$sep` into a list of strings. The comparison value is a string slice with possible matches. Returns containers which OR intersect these values.
* *`whereAll $containers $fieldPath $sep $values`*: Like `whereAny`, except all `$values` must exist in the `$fieldPath`.
* *`where $items $fieldPath $value`*: Filters an array or slice based on the values of a field path expression `$fieldPath`. A field path expression is a dot-delimited list of map keys or struct member names specifying the path from container to a nested value. Returns an array of items having that value.
* *`whereExist $items $fieldPath`*: Like `where`, but returns only items where `$fieldPath` exists (is not nil).
* *`whereNotExist $items $fieldPath`*: Like `where`, but returns only items where `$fieldPath` does not exist (is nil).
* *`whereAny $items $fieldPath $sep $values`*: Like `where`, but the string value specified by `$fieldPath` is first split by `$sep` into a list of strings. The comparison value is a string slice with possible matches. Returns items which OR intersect these values.
* *`whereAll $items $fieldPath $sep $values`*: Like `whereAny`, except all `$values` must exist in the `$fieldPath`.

===

Expand Down
2 changes: 1 addition & 1 deletion reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func deepGet(item interface{}, path string) interface{} {
return deepGet(mapValue.Interface(), strings.Join(parts[1:], "."))
}
default:
log.Printf("can't group by %s\n", path)
log.Printf("can't group by %s (value %v, kind %s)\n", path, itemValue, itemValue.Kind())
}
return nil
}
Expand Down
93 changes: 48 additions & 45 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,71 +66,74 @@ func groupByKeys(entries []*RuntimeContainer, key string) []string {
return ret
}

// selects entries based on key
func where(entries []*RuntimeContainer, key string, cmp string) []*RuntimeContainer {
selection := []*RuntimeContainer{}
for _, v := range entries {
value := deepGet(*v, key)
if value == cmp {
// Generalized where function
func generalizedWhere(funcName string, entries interface{}, key string, test func(interface{}) bool) (interface{}, error) {
entriesVal := reflect.ValueOf(entries)

switch entriesVal.Kind() {
case reflect.Array, reflect.Slice:
break
default:
return nil, fmt.Errorf("Must pass an array or slice to '%s'; received %v", funcName, entries)
}

selection := make([]interface{}, 0)
for i := 0; i < entriesVal.Len(); i++ {
v := reflect.Indirect(entriesVal.Index(i)).Interface()

value := deepGet(v, key)
if test(value) {
selection = append(selection, v)
}
}
return selection

return selection, nil
}

// selects entries based on key
func where(entries interface{}, key string, cmp interface{}) (interface{}, error) {
return generalizedWhere("where", entries, key, func(value interface{}) bool {
return reflect.DeepEqual(value, cmp)
})
}

// selects entries where a key exists
func whereExist(entries []*RuntimeContainer, key string) []*RuntimeContainer {
selection := []*RuntimeContainer{}
for _, v := range entries {
value := deepGet(*v, key)
if value != nil {
selection = append(selection, v)
}
}
return selection
func whereExist(entries interface{}, key string) (interface{}, error) {
return generalizedWhere("whereExist", entries, key, func(value interface{}) bool {
return value != nil
})
}

// selects entries where a key does not exist
func whereNotExist(entries []*RuntimeContainer, key string) []*RuntimeContainer {
selection := []*RuntimeContainer{}
for _, v := range entries {
value := deepGet(*v, key)
if value == nil {
selection = append(selection, v)
}
}
return selection
func whereNotExist(entries interface{}, key string) (interface{}, error) {
return generalizedWhere("whereNotExist", entries, key, func(value interface{}) bool {
return value == nil
})
}

// selects entries based on key. Assumes key is delimited and breaks it apart before comparing
func whereAny(entries []*RuntimeContainer, key, sep string, cmp []string) []*RuntimeContainer {
selection := []*RuntimeContainer{}
for _, v := range entries {
value := deepGet(*v, key)
if value != nil {
func whereAny(entries interface{}, key, sep string, cmp []string) (interface{}, error) {
return generalizedWhere("whereAny", entries, key, func(value interface{}) bool {
if value == nil {
return false
} else {
items := strings.Split(value.(string), sep)
if len(intersect(cmp, items)) > 0 {
selection = append(selection, v)
}
return len(intersect(cmp, items)) > 0
}
}
return selection
})
}

// selects entries based on key. Assumes key is delimited and breaks it apart before comparing
func whereAll(entries []*RuntimeContainer, key, sep string, cmp []string) []*RuntimeContainer {
selection := []*RuntimeContainer{}
func whereAll(entries interface{}, key, sep string, cmp []string) (interface{}, error) {
req_count := len(cmp)
for _, v := range entries {
value := deepGet(*v, key)
if value != nil {
return generalizedWhere("whereAll", entries, key, func(value interface{}) bool {
if value == nil {
return false
} else {
items := strings.Split(value.(string), sep)
if len(intersect(cmp, items)) == req_count {
selection = append(selection, v)
}
return len(intersect(cmp, items)) == req_count
}
}
return selection
})
}

// hasPrefix returns whether a given string is a prefix of another string
Expand Down
Loading