diff --git a/skygear/src/androidTest/java/io/skygear/skygear/LambdaRequestUnitTest.java b/skygear/src/androidTest/java/io/skygear/skygear/LambdaRequestUnitTest.java index 6358f0c..8247f4c 100644 --- a/skygear/src/androidTest/java/io/skygear/skygear/LambdaRequestUnitTest.java +++ b/skygear/src/androidTest/java/io/skygear/skygear/LambdaRequestUnitTest.java @@ -97,17 +97,13 @@ public void testLambdaRequestCompatibleValueValidation() throws Exception { (short) 3, "3", jsonObject, - jsonArray + jsonArray, + new Date() }); request.validate(); } - @Test(expected = InvalidParameterException.class) - public void testLambdaRequestIncompatibleValueValidation() throws Exception { - new LambdaRequest("test:op1", new Object[]{new Date()}); - } - @Test public void testLambdaRequestAllowNullArgumentWithMap() throws Exception { LambdaRequest request = new LambdaRequest( @@ -146,29 +142,17 @@ public void testLambdaRequestCompatibleValueWithMapValidation() throws Exception put("key9", "3"); put("key10", jsonObject); put("key11", jsonArray); + put("key12", new Date()); + put("key13", new HashMap() {{ + put("key13a", new Date()); + }}); }}); request.validate(); } - @Test(expected = InvalidParameterException.class) - public void testLambdaRequestIncompatibleValueWithMapValidation() throws Exception { - new LambdaRequest("test:op1", new HashMap() {{ - put("key1", new Date()); - }}); - } - - @Test(expected = InvalidParameterException.class) - public void testLambdaRequestIncompatibleNestedValueWithMapValidation() throws Exception { - new LambdaRequest("test:op1", new HashMap() {{ - put("key1", new HashMap() {{ - put("key2", new Date()); - }}); - }}); - } - - @Test(expected = InvalidParameterException.class) - public void testLambdaRequestIncompatibleNestedValueWithMapValidation2() throws Exception { + @Test + public void testLambdaRequestCompatibleNestedValueWithMapValidation1() throws Exception { final JSONObject object = new JSONObject(); final JSONObject object2 = new JSONObject(); object2.put("key2", new Date()); @@ -179,8 +163,8 @@ public void testLambdaRequestIncompatibleNestedValueWithMapValidation2() throws } - @Test(expected = InvalidParameterException.class) - public void testLambdaRequestIncompatibleNestedValueWithMapValidation3() throws Exception { + @Test + public void testLambdaRequestCompatibleNestedValueWithMapValidation2() throws Exception { final JSONArray object = new JSONArray(); final JSONObject object2 = new JSONObject(); object2.put("key2", new Date()); diff --git a/skygear/src/main/java/io/skygear/skygear/LambdaRequest.java b/skygear/src/main/java/io/skygear/skygear/LambdaRequest.java index 2f7fc16..98ab601 100644 --- a/skygear/src/main/java/io/skygear/skygear/LambdaRequest.java +++ b/skygear/src/main/java/io/skygear/skygear/LambdaRequest.java @@ -48,6 +48,19 @@ public class LambdaRequest extends Request { Map.class )); + protected LambdaRequest(String name, Object args) { + super(name); + + try { + this.data = new HashMap<>(); + if (args != null) { + this.data.put("args", ValueSerializer.serialize(args)); + } + } catch (JSONException ex) { + throw new InvalidParameterException(ex.getMessage()); + } + } + /** * Instantiates a new Skygear Lambda Function Request. * @@ -55,22 +68,7 @@ public class LambdaRequest extends Request { * @param args the args */ public LambdaRequest(String name, Object[] args) { - super(name); - - this.data = new HashMap<>(); - - if (args != null) { - for (int idx = 0; idx < args.length; idx++) { - if (!this.isCompatibleArgument(args[idx])) { - throw new InvalidParameterException( - String.format("Argument at index %d is incompatible", idx) - ); - } - } - - List argList = Arrays.asList(args); - this.data.put("args", new JSONArray(argList)); - } + this(name, (Object)args); } /** @@ -80,20 +78,7 @@ public LambdaRequest(String name, Object[] args) { * @param args the args */ public LambdaRequest(String name, Map args) { - super(name); - - this.data = new HashMap<>(); - - if (args != null) { - for (String key : args.keySet()) { - if (!this.isCompatibleArgument(args.get(key))) { - throw new InvalidParameterException( - String.format("Argument at index %s is incompatible", key) - ); - } - } - this.data.put("args", new JSONObject(args)); - } + this(name, (Object)args); } private boolean isCompatibleArgument(Object[] array) { diff --git a/skygear/src/main/java/io/skygear/skygear/TypedLambdaResponseHandler.java b/skygear/src/main/java/io/skygear/skygear/TypedLambdaResponseHandler.java new file mode 100644 index 0000000..e1c07e7 --- /dev/null +++ b/skygear/src/main/java/io/skygear/skygear/TypedLambdaResponseHandler.java @@ -0,0 +1,48 @@ +/* + * Copyright 2017 Oursky Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package io.skygear.skygear; + +import org.json.JSONException; +import org.json.JSONObject; + +/** + * The Skygear Typed Lambda Response Handler. + */ +public abstract class TypedLambdaResponseHandler extends LambdaResponseHandler { + /** + * The success callback. + * + * @param result the result + */ + public abstract void onLambdaSuccess(T result); + + public void onLambdaSuccess(JSONObject result) { + this.onLambdaSuccess((T)result); + } + + @Override + public void onSuccess(JSONObject result) { + try { + this.onLambdaSuccess((T)ValueSerializer.deserialize(result)); + } catch (JSONException ex) { + this.onFail(new Error(ex.getMessage())); + } catch (ClassCastException ex) { + this.onFail(new Error(ex.getMessage())); + } + } +}