-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from shipengqi/feat/group-type
feat(parameter): add dependson for the parameter group
- Loading branch information
Showing
18 changed files
with
824 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package helpers | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
// Contains asserts that the specified string, list(array, slice...) or map contains the | ||
// specified substring or element. | ||
// | ||
// helpers.Contains("Hello World", "World") | ||
// helpers.Contains(["Hello", "World"], "World") | ||
// helpers.Contains({"Hello": "World"}, "Hello") | ||
func Contains(s, contains interface{}) bool { | ||
ok, found := containsElement(s, contains) | ||
if !ok { | ||
return false | ||
} | ||
|
||
return found | ||
} | ||
|
||
// NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the | ||
// specified substring or element. | ||
// | ||
// helpers.NotContains("Hello World", "Earth") | ||
// helpers.NotContains(["Hello", "World"], "Earth") | ||
// helpers.NotContains({"Hello": "World"}, "Earth") | ||
func NotContains(s, contains interface{}) bool { | ||
ok, found := containsElement(s, contains) | ||
if !ok { | ||
return false | ||
} | ||
|
||
return !found | ||
} | ||
|
||
// containsElement try loop over the list check if the list includes the element. | ||
// return (false, false) if impossible. | ||
// return (true, false) if element was not found. | ||
// return (true, true) if element was found. | ||
func containsElement(list interface{}, element interface{}) (ok, found bool) { | ||
|
||
listValue := reflect.ValueOf(list) | ||
listType := reflect.TypeOf(list) | ||
if listType == nil { | ||
return false, false | ||
} | ||
listKind := listType.Kind() | ||
defer func() { | ||
if e := recover(); e != nil { | ||
ok = false | ||
found = false | ||
} | ||
}() | ||
|
||
if listKind == reflect.String { | ||
elementValue := reflect.ValueOf(element) | ||
return true, strings.Contains(listValue.String(), elementValue.String()) | ||
} | ||
|
||
if listKind == reflect.Map { | ||
mapKeys := listValue.MapKeys() | ||
for i := 0; i < len(mapKeys); i++ { | ||
if ObjectsAreEqual(mapKeys[i].Interface(), element) { | ||
return true, true | ||
} | ||
} | ||
return true, false | ||
} | ||
|
||
for i := 0; i < listValue.Len(); i++ { | ||
if ObjectsAreEqual(listValue.Index(i).Interface(), element) { | ||
return true, true | ||
} | ||
} | ||
return true, false | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package helpers | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestContainsNotContains(t *testing.T) { | ||
|
||
type A struct { | ||
Name, Value string | ||
} | ||
list := []string{"Foo", "Bar"} | ||
|
||
complexList := []*A{ | ||
{"b", "c"}, | ||
{"d", "e"}, | ||
{"g", "h"}, | ||
{"j", "k"}, | ||
} | ||
simpleMap := map[interface{}]interface{}{"Foo": "Bar"} | ||
var zeroMap map[interface{}]interface{} | ||
|
||
cases := []struct { | ||
expected interface{} | ||
actual interface{} | ||
result bool | ||
}{ | ||
{"Hello World", "Hello", true}, | ||
{"Hello World", "Salut", false}, | ||
{list, "Bar", true}, | ||
{list, "Salut", false}, | ||
{complexList, &A{"g", "h"}, true}, | ||
{complexList, &A{"g", "e"}, false}, | ||
{simpleMap, "Foo", true}, | ||
{simpleMap, "Bar", false}, | ||
{zeroMap, "Bar", false}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(fmt.Sprintf("Contains(%#v, %#v)", c.expected, c.actual), func(t *testing.T) { | ||
res := Contains(c.expected, c.actual) | ||
|
||
if res != c.result { | ||
if res { | ||
t.Errorf("Contains(%#v, %#v) should return true:\n\t%#v contains %#v", c.expected, c.actual, c.expected, c.actual) | ||
} else { | ||
t.Errorf("Contains(%#v, %#v) should return false:\n\t%#v does not contain %#v", c.expected, c.actual, c.expected, c.actual) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(fmt.Sprintf("NotContains(%#v, %#v)", c.expected, c.actual), func(t *testing.T) { | ||
res := NotContains(c.expected, c.actual) | ||
|
||
// NotContains should be inverse of Contains. If it's not, something is wrong | ||
if res == Contains(c.expected, c.actual) { | ||
if res { | ||
t.Errorf("NotContains(%#v, %#v) should return true:\n\t%#v does not contains %#v", c.expected, c.actual, c.expected, c.actual) | ||
} else { | ||
t.Errorf("NotContains(%#v, %#v) should return false:\n\t%#v contains %#v", c.expected, c.actual, c.expected, c.actual) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package helpers | ||
|
||
import "reflect" | ||
|
||
// Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either | ||
// a slice or a channel with len == 0. | ||
// | ||
// helpers.Empty(obj) | ||
func Empty(object interface{}) bool { | ||
return isEmpty(object) | ||
} | ||
|
||
// NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either | ||
// a slice or a channel with len == 0. | ||
// | ||
// if helpers.NotEmpty(obj) { | ||
// helpers.Equal("two", obj[1]) | ||
// } | ||
func NotEmpty(object interface{}) bool { | ||
return !isEmpty(object) | ||
} | ||
|
||
// isEmpty gets whether the specified object is considered empty or not. | ||
func isEmpty(object interface{}) bool { | ||
|
||
// get nil case out of the way | ||
if object == nil { | ||
return true | ||
} | ||
|
||
objValue := reflect.ValueOf(object) | ||
|
||
switch objValue.Kind() { | ||
// collection types are empty when they have no element | ||
case reflect.Chan, reflect.Map, reflect.Slice: | ||
return objValue.Len() == 0 | ||
// pointers are empty if nil or if the value they point to is empty | ||
case reflect.Ptr: | ||
if objValue.IsNil() { | ||
return true | ||
} | ||
deref := objValue.Elem().Interface() | ||
return isEmpty(deref) | ||
// for all other types, compare against the zero value | ||
// array types are empty when they match their zero-initialized state | ||
default: | ||
zero := reflect.Zero(objValue.Type()) | ||
return reflect.DeepEqual(object, zero.Interface()) | ||
} | ||
} |
Oops, something went wrong.