Skip to content

Commit

Permalink
Merge pull request ballerina-platform#729 from maheeka/deserializer
Browse files Browse the repository at this point in the history
[Tool] Deserializer
  • Loading branch information
sumuditha-viraj authored Jan 12, 2017
2 parents 3f9b366 + c32dd6a commit f993088
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,9 @@ public void visit(ActionInvocationStmt actionInvocationStmt) {
public void visit(ReplyStmt replyStmt) {
StringBuffer buffer = new StringBuffer();
bufferStack.push(buffer);
buffer.append("reply ");
replyStmt.getReplyExpr().accept(this);
buffer.append(bufferStack.peek());
bufferStack.pop();
buffer.append(";");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.wso2.ballerina.core.semantics.SemanticAnalyzer;

import javax.ws.rs.Consumes;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -81,7 +82,7 @@ public Response getBallerinaJsonDataModelGivenContent(BFileContent content) {
try {
InputStream stream = new ByteArrayInputStream(content.getContent().getBytes(StandardCharsets.UTF_8));
String response = parseJsonDataModel(stream);
return Response.ok(response, MediaType.APPLICATION_JSON).build();
return Response.ok(response, MediaType.APPLICATION_JSON).header("Access-Control-Allow-Origin", '*').build();
} catch (IOException ex) {
logger.error("IOException occured while generating JSON data model for ballerina file", ex);
JsonObject entity = new JsonObject();
Expand All @@ -92,6 +93,19 @@ public Response getBallerinaJsonDataModelGivenContent(BFileContent content) {
}
}

@OPTIONS
@Path("/model/content")
@Consumes("application/json")
@Produces("application/json")
public Response options() {
return Response.ok()
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Credentials", "true")
.header("Access-Control-Allow-Methods", "POST, GET, PUT, UPDATE, DELETE, OPTIONS, HEAD")
.header("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With")
.build();
}

private String parseJsonDataModel(InputStream stream) throws IOException {

ANTLRInputStream antlrInputStream = new ANTLRInputStream(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,7 @@ public void visit(VariableDcl variableDcl) {
public void visit(BlockStmt blockStmt) {
if (blockStmt.getStatements() != null) {
for (Statement statement : blockStmt.getStatements()) {
if (isExprAsString) {
JsonObject jsonObject = new JsonObject();
statement.accept(exprVisitor);
jsonObject.addProperty(BLangJSONModelConstants.STATEMENT,
exprVisitor.getBuffer().toString());
tempJsonArrayRef.peek().add(jsonObject);
} else {
statement.accept(this);
}
statement.accept(this);
}
}
}
Expand Down Expand Up @@ -564,6 +556,11 @@ public void visit(ReplyStmt replyStmt) {
JsonObject replyStmtObj = new JsonObject();
replyStmtObj.addProperty(BLangJSONModelConstants.STATEMENT_TYPE,
BLangJSONModelConstants.REPLY_STATEMENT);
if (isExprAsString) {
replyStmt.accept(exprVisitor);
String stmtExpression = exprVisitor.getBuffer().toString();
replyStmtObj.addProperty(BLangJSONModelConstants.EXPRESSION, stmtExpression);
}
tempJsonArrayRef.push(new JsonArray());
replyStmt.getReplyExpr().accept(this);
replyStmtObj.add(BLangJSONModelConstants.CHILDREN, tempJsonArrayRef.peek());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,6 @@ public class BLangJSONModelConstants {

public static final String STATEMENT = "statement";

public static final String EXPRESSION = "expression";

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,29 @@ public class BLangJSONModelTest {

private MicroservicesRunner microservicesRunner;
//private HashMap<String, Package> packages = new HashMap<String, Package>();
private String exptdStrFunc = "{\"root\":[{\"type\":\"package\",\"package_name\":\"test.samples\"},{\"type\":\"import\"," +
"\"import_package_name\":\"twitter\",\"import_package_path\":\"ballerina.connectors.twitter\"},{\"type\":" +
"\"import\",\"import_package_name\":\"sf\",\"import_package_path\":\"ballerina.connectors.salesforce\"}," +
"{\"type\":\"import\",\"import_package_name\":\"samples\",\"import_package_path\":\"test.samples\"},{\"type\":" +
"\"service_definition\",\"service_name\":\"HelloService\",\"annotations\":[],\"children\":[{\"type\":" +
"\"resource_definition\",\"resource_name\":\"tweet\",\"annotations\":[{\"type\":\"annotation\",\"annotation_name\":" +
"\"GET\",\"annotation_value\":null,\"children\":[]},{\"type\":\"annotation\",\"annotation_name\":\"Path\"," +
"\"annotation_value\":\"/tweet\",\"children\":[]}],\"children\":[{\"type\":\"argument_declaration\",\"parameter_name\":\"" +
"m\",\"parameter_type\":\"message\",\"children\":[]},{\"statement\":\"reply m;\"}]}]},{\"type\":\"function_definition\"," +
"\"function_name\":\"test_int\",\"is_public_function\":false,\"annotations\":[],\"children\":[{\"type\":\"argument_declaration\"," +
"\"parameter_name\":\"a\",\"parameter_type\":\"int\",\"children\":[]},{\"type\":\"return_type\",\"children\":" +
"[{\"type\":\"type_name\",\"type_name\":\"int\"}]},{\"statement\":\"reply a + 2 ;\"}]}]}";
private String exptdStrFunc = "{\"root\":[{\"type\":\"package\",\"package_name\":\"test.samples\"}," +
"{\"type\":\"import\",\"import_package_name\":\"twitter\"," +
"\"import_package_path\":\"ballerina.connectors.twitter\"},{\"type\":\"import\"," +
"\"import_package_name\":\"sf\",\"import_package_path\":\"ballerina.connectors" +
".salesforce\"},{\"type\":\"import\",\"import_package_name\":\"samples\"," +
"\"import_package_path\":\"test.samples\"},{\"type\":\"service_definition\"," +
"\"service_name\":\"HelloService\",\"annotations\":[]," +
"\"children\":[{\"type\":\"resource_definition\",\"resource_name\":\"tweet\"," +
"\"annotations\":[{\"type\":\"annotation\",\"annotation_name\":\"GET\"," +
"\"annotation_value\":null,\"children\":[]},{\"type\":\"annotation\"," +
"\"annotation_name\":\"Path\",\"annotation_value\":\"/tweet\",\"children\":[]}]," +
"\"children\":[{\"type\":\"argument_declaration\",\"parameter_name\":\"m\"," +
"\"parameter_type\":\"message\",\"children\":[]},{\"type\":\"reply_statement\"," +
"\"expression\":\"m\",\"children\":[{\"type\":\"variable_reference_expression\"," +
"\"variable_reference_name\":\"m\"}]}]}]},{\"type\":\"function_definition\"," +
"\"function_name\":\"test_int\",\"is_public_function\":false,\"annotations\":[]," +
"\"children\":[{\"type\":\"argument_declaration\",\"parameter_name\":\"a\"," +
"\"parameter_type\":\"int\",\"children\":[]},{\"type\":\"return_type\"," +
"\"children\":[{\"type\":\"type_name\",\"type_name\":\"int\"}]}," +
"{\"type\":\"return_statement\",\"children\":[{\"type\":\"add_expression\"," +
"\"children\":[{\"type\":\"variable_reference_expression\"," +
"\"variable_reference_name\":\"a\"},{\"type\":\"basic_literal_expression\"," +
"\"basic_literal_type\":\"int\",\"basic_literal_value\":\"2\"}]}]}]}]}";

public static void main(String[] args) {
try {
Expand Down
8 changes: 8 additions & 0 deletions modules/editor/web/js/ballerina/ast/assignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,13 @@ define(['lodash', './statement'], function (_, Statement) {
return this._expression;
};

/**
* initialize from json
* @param jsonNode
*/
Assignment.prototype.initFromJson = function (jsonNode) {
this._expression = jsonNode.statement;
};

return Assignment;
});
104 changes: 55 additions & 49 deletions modules/editor/web/js/ballerina/ast/ballerina-ast-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,56 +589,62 @@ define(['./ballerina-ast-root', './service-definition', './function-definition',

BallerinaASTFactory.createFromJson = function (jsonNode) {
var node;
switch (jsonNode.type) {
case 'package':
node = BallerinaASTFactory.createPackageDefinition();
break;
case 'import':
node = BallerinaASTFactory.createImportDeclaration();
break;
case 'service_definition':
node = BallerinaASTFactory.createServiceDefinition();
break;
case 'function_definition':
node = BallerinaASTFactory.createFunctionDefinition();
break;
case 'connector_definition':
node = BallerinaASTFactory.createConnectorDefinition();
break;
case 'type_definition':
node = BallerinaASTFactory.createTypeDefinition();
break;
case 'resource_definition':
node = BallerinaASTFactory.createResourceDefinition();
break;
case 'connector_declaration':
node = BallerinaASTFactory.createConnectorDeclaration();
break;
case 'variable_declaration':
node = BallerinaASTFactory.createVariableDeclaration();
break;
case 'argument_declaration':
node = BallerinaASTFactory.createResourceArgument();
break;
case 'reply_statement':
node = BallerinaASTFactory.createReplyStatement();
break;
case 'return_statement':
node = BallerinaASTFactory.createReturnStatement();
break;
case 'return_type':
node = BallerinaASTFactory.createReturnType();
break;
case 'type_name':
node = BallerinaASTFactory.createTypeName();
break;
case 'function_invocation':
node = BallerinaASTFactory.createFunctionInvocationStatement();
break;
default:
throw "Unknown node definition for " + jsonNode.type;
var nodeType = jsonNode.type;

if (_.isUndefined(jsonNode.type)) {
var statement = jsonNode.statement;
node = BallerinaASTFactory.createAssignmentStatement();
} else {
switch (nodeType) {
case 'package':
node = BallerinaASTFactory.createPackageDefinition();
break;
case 'import':
node = BallerinaASTFactory.createImportDeclaration();
break;
case 'service_definition':
node = BallerinaASTFactory.createServiceDefinition();
break;
case 'function_definition':
node = BallerinaASTFactory.createFunctionDefinition();
break;
case 'connector_definition':
node = BallerinaASTFactory.createConnectorDefinition();
break;
case 'type_definition':
node = BallerinaASTFactory.createTypeDefinition();
break;
case 'resource_definition':
node = BallerinaASTFactory.createResourceDefinition();
break;
case 'connector_declaration':
node = BallerinaASTFactory.createConnectorDeclaration();
break;
case 'variable_declaration':
node = BallerinaASTFactory.createVariableDeclaration();
break;
case 'argument_declaration':
node = BallerinaASTFactory.createResourceArgument();
break;
case 'reply_statement':
node = BallerinaASTFactory.createReplyStatement();
break;
case 'return_statement':
node = BallerinaASTFactory.createReturnStatement();
break;
case 'return_type':
node = BallerinaASTFactory.createReturnType();
break;
case 'type_name':
node = BallerinaASTFactory.createTypeName();
break;
case 'function_invocation':
node = BallerinaASTFactory.createFunctionInvocationStatement();
break;
default:
throw "Unknown node definition for " + jsonNode.type;
}
}

node.initFromJson(jsonNode);
return node;
};
Expand Down
12 changes: 6 additions & 6 deletions modules/editor/web/js/ballerina/ast/function-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ define(['lodash', './callable-definition'],
/**
* Constructor for FunctionDefinition
* @param {Object} args - The arguments to create the FunctionDefinition
* @param {string} args.functionName [args.functionName=newFunction] - Function name
* @param {string} args.isPublic [args.isPublic=false] - Public or not of the function
* @param {string[]} args.annotations - Function annotations
* @param {string} [args.functionName=newFunction] - Function name
* @param {boolean} [args.isPublic=false] - Public or not of the function
* @param {string[]} [args.annotations] - Function annotations
* @constructor
*/
var FunctionDefinition = function (args) {
Expand Down Expand Up @@ -283,9 +283,9 @@ define(['lodash', './callable-definition'],
/**
* initialize FunctionDefinition from json object
* @param {Object} jsonNode to initialize from
* @param {string} jsonNode.function_name - Name of the function definition
* @param {string} jsonNode.annotations - Annotations of the function definition
* @param {string} jsonNode.is_public_function - Public or not of the function
* @param {string} [jsonNode.function_name] - Name of the function definition
* @param {string} [jsonNode.annotations] - Annotations of the function definition
* @param {boolean} [jsonNode.is_public_function] - Public or not of the function
*/
FunctionDefinition.prototype.initFromJson = function (jsonNode) {
this._functionName = jsonNode.function_name;
Expand Down
2 changes: 1 addition & 1 deletion modules/editor/web/js/ballerina/ast/reply-statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ define(['lodash', 'log', './statement'], function (_, log, Statement) {
* @param jsonNode
*/
ReplyStatement.prototype.initFromJson = function (jsonNode) {
//TODO : load properties from expression
this._message = jsonNode.expression;
};

return ReplyStatement;
Expand Down
12 changes: 6 additions & 6 deletions modules/editor/web/js/ballerina/ast/resource-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ define(['lodash', 'log', './node'],
/**
* Constructor for ResourceDefinition
* @param {Object} args - The arguments to create the ServiceDefinition
* @param {string} args.resourceName [args.resourceName=newResource] - Service name
* @param {string[]} args.annotations - Resource annotations
* @param {string} args.annotations.Method - Resource annotation for Method
* @param {string} args.annotations.Path - Resource annotation for Path
* @param {string} [args.resourceName=newResource] - Service name
* @param {string[]} [args.annotations] - Resource annotations
* @param {string} [args.annotations.Method] - Resource annotation for Method
* @param {string} [args.annotations.Path] - Resource annotation for Path
* @constructor
*/
var ResourceDefinition = function (args) {
Expand Down Expand Up @@ -242,8 +242,8 @@ define(['lodash', 'log', './node'],
/**
* initialize ResourceDefinition from json object
* @param {Object} jsonNode to initialize from
* @param {string} jsonNode.resource_name - Name of the resource definition
* @param {string} jsonNode.annotations - Annotations of the resource definition
* @param {string} [jsonNode.resource_name] - Name of the resource definition
* @param {string} [jsonNode.annotations] - Annotations of the resource definition
*/
ResourceDefinition.prototype.initFromJson = function (jsonNode) {
this._resourceName = jsonNode.resource_name;
Expand Down
10 changes: 5 additions & 5 deletions modules/editor/web/js/ballerina/ast/service-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ define(['lodash', './node'],
/**
* Constructor for ServiceDefinition
* @param {Object} args - The arguments to create the ServiceDefinition
* @param {string} args.serviceName [args.serviceName=newService] - Service name
* @param {string[]} args.annotations - Service annotations
* @param {string} args.annotations.BasePath - Service annotation for BasePath
* @param {string} [args.serviceName=newService] - Service name
* @param {string[]} [args.annotations] - Service annotations
* @param {string} [args.annotations.BasePath] - Service annotation for BasePath
* @constructor
*/
var ServiceDefinition = function (args) {
Expand Down Expand Up @@ -160,8 +160,8 @@ define(['lodash', './node'],
/**
* initialize ServiceDefinition from json object
* @param {Object} jsonNode to initialize from
* @param {string} jsonNode.service_name - Name of the service definition
* @param {string} jsonNode.annotations - Annotations of the function definition
* @param {string} [jsonNode.service_name] - Name of the service definition
* @param {string} [jsonNode.annotations] - Annotations of the function definition
*/
ServiceDefinition.prototype.initFromJson = function (jsonNode) {
this._serviceName = jsonNode.service_name;
Expand Down
Loading

0 comments on commit f993088

Please sign in to comment.