Skip to content

Commit

Permalink
Update lambda request and response to support type
Browse files Browse the repository at this point in the history
  • Loading branch information
cheungpat committed May 7, 2018
1 parent 30f9e51 commit bc81286
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<String, Object>() {{
put("key13a", new Date());
}});
}});

request.validate();
}

@Test(expected = InvalidParameterException.class)
public void testLambdaRequestIncompatibleValueWithMapValidation() throws Exception {
new LambdaRequest("test:op1", new HashMap<String, Object>() {{
put("key1", new Date());
}});
}

@Test(expected = InvalidParameterException.class)
public void testLambdaRequestIncompatibleNestedValueWithMapValidation() throws Exception {
new LambdaRequest("test:op1", new HashMap<String, Object>() {{
put("key1", new HashMap<String, Object>() {{
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());
Expand All @@ -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());
Expand Down
45 changes: 15 additions & 30 deletions skygear/src/main/java/io/skygear/skygear/LambdaRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,27 @@ 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.
*
* @param name the name
* @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<Object> argList = Arrays.asList(args);
this.data.put("args", new JSONArray(argList));
}
this(name, (Object)args);
}

/**
Expand All @@ -80,20 +78,7 @@ public LambdaRequest(String name, Object[] args) {
* @param args the args
*/
public LambdaRequest(String name, Map<String, Object> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T extends Object> 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()));
}
}
}

0 comments on commit bc81286

Please sign in to comment.