diff --git a/netflix-sel/src/main/java/com/netflix/sel/type/SelArray.java b/netflix-sel/src/main/java/com/netflix/sel/type/SelArray.java index b53503b8..cec8ee1e 100644 --- a/netflix-sel/src/main/java/com/netflix/sel/type/SelArray.java +++ b/netflix-sel/src/main/java/com/netflix/sel/type/SelArray.java @@ -159,6 +159,29 @@ public SelLong field(SelString field) { throw new UnsupportedOperationException(type() + " DO NOT support accessing field: " + field); } + @Override + public SelType call(String methodName, SelType[] args) { + if ("contains".equals(methodName) && args.length == 1) { + return contains(args[0]); + } + throw new UnsupportedOperationException( + type() + + " DO NOT support calling method: " + + methodName + + " with args: " + + Arrays.toString(args)); + } + + private SelBoolean contains(SelType element) { + for (SelType item : val) { + SelBoolean isEqual = (SelBoolean) item.binaryOps(SelOp.EQUAL, element); + if (isEqual.booleanVal()) { + return SelBoolean.of(true); + } + } + return SelBoolean.of(false); + } + @Override public String toString() { return Arrays.toString(val); diff --git a/netflix-sel/src/test/java/com/netflix/sel/type/SelArrayTest.java b/netflix-sel/src/test/java/com/netflix/sel/type/SelArrayTest.java index aaf9e913..1df96e1c 100644 --- a/netflix-sel/src/test/java/com/netflix/sel/type/SelArrayTest.java +++ b/netflix-sel/src/test/java/com/netflix/sel/type/SelArrayTest.java @@ -83,4 +83,26 @@ public void field() { public void invalidField() { one.field(SelString.of("size")); } + + @Test + public void testContainsTrue() { + SelBoolean result = (SelBoolean) one.call("contains", new SelType[] {SelString.of("foo")}); + assertTrue(result.booleanVal()); + } + + @Test + public void testContainsFalse() { + SelBoolean result = (SelBoolean) one.call("contains", new SelType[] {SelString.of("baz")}); + assertFalse(result.booleanVal()); + } + + @Test(expected = UnsupportedOperationException.class) + public void testContainsInvalidArgs() { + one.call("contains", new SelType[] {}); + } + + @Test(expected = UnsupportedOperationException.class) + public void testInvalidMethod() { + one.call("invalidMethod", new SelType[] {SelString.of("test")}); + } }