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.
(list interface{}, element interface{})
| 915 | // return (true, false) if element was not found. |
| 916 | // return (true, true) if element was found. |
| 917 | func containsElement(list interface{}, element interface{}) (ok, found bool) { |
| 918 | listValue := reflect.ValueOf(list) |
| 919 | listType := reflect.TypeOf(list) |
| 920 | if listType == nil { |
| 921 | return false, false |
| 922 | } |
| 923 | listKind := listType.Kind() |
| 924 | defer func() { |
| 925 | if e := recover(); e != nil { |
| 926 | ok = false |
| 927 | found = false |
| 928 | } |
| 929 | }() |
| 930 | |
| 931 | if listKind == reflect.String { |
| 932 | elementValue := reflect.ValueOf(element) |
| 933 | return true, strings.Contains(listValue.String(), elementValue.String()) |
| 934 | } |
| 935 | |
| 936 | if listKind == reflect.Map { |
| 937 | mapKeys := listValue.MapKeys() |
| 938 | for i := 0; i < len(mapKeys); i++ { |
| 939 | if ObjectsAreEqual(mapKeys[i].Interface(), element) { |
| 940 | return true, true |
| 941 | } |
| 942 | } |
| 943 | return true, false |
| 944 | } |
| 945 | |
| 946 | for i := 0; i < listValue.Len(); i++ { |
| 947 | if ObjectsAreEqual(listValue.Index(i).Interface(), element) { |
| 948 | return true, true |
| 949 | } |
| 950 | } |
| 951 | return true, false |
| 952 | } |
| 953 | |
| 954 | // Contains asserts that the specified string, list(array, slice...) or map contains the |
| 955 | // specified substring or element. |