use of org.wso2.ballerinalang.compiler.tree.BLangStruct in project ballerina by ballerina-lang.
the class DocumentationTest method testDocFunction.
@Test(description = "Test doc function.")
public void testDocFunction() {
CompileResult compileResult = BCompileUtil.compile("test-src/documentation/function.bal");
Assert.assertEquals(0, compileResult.getWarnCount());
PackageNode packageNode = compileResult.getAST();
List<BLangDocumentation> docNodes = ((BLangFunction) packageNode.getFunctions().get(0)).docAttachments;
BLangDocumentation dNode = docNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + "Gets a access parameter value (`true` or `false`) for a given key. " + "Please note that #foo will always be bigger than #bar.\n" + "Example:\n" + "``SymbolEnv pkgEnv = symbolEnter.packageEnvs.get(pkgNode.symbol);``\n");
Assert.assertEquals(dNode.getAttributes().size(), 2);
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "file");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " file path ``C:\\users\\OddThinking\\Documents\\My Source\\Widget\\foo.src``\n");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "accessMode");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " read or write mode\n");
docNodes = ((BLangStruct) packageNode.getStructs().get(0)).docAttachments;
dNode = docNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, " Documentation for File struct\n");
Assert.assertEquals(dNode.getAttributes().size(), 1);
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "path");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " struct `field path` documentation\n");
}
use of org.wso2.ballerinalang.compiler.tree.BLangStruct in project ballerina by ballerina-lang.
the class DocumentationTest method testDocConnectorFunction.
@Test(description = "Test doc connector/function.", enabled = false)
public void testDocConnectorFunction() {
CompileResult compileResult = BCompileUtil.compile("test-src/documentation/connector_function.bal");
Assert.assertEquals(0, compileResult.getWarnCount());
PackageNode packageNode = compileResult.getAST();
BLangConnector connector = (BLangConnector) packageNode.getConnectors().get(0);
List<BLangDocumentation> docNodes = connector.docAttachments;
BLangDocumentation dNode = docNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.getAttributes().size(), 2);
Assert.assertEquals(dNode.documentationText, "Test Connector\n");
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "url");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " url for endpoint\n");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "path");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " path for endpoint\n");
dNode = connector.getActions().get(0).docAttachments.get(0);
Assert.assertEquals(dNode.getAttributes().size(), 1);
Assert.assertEquals(dNode.documentationText, "Test Connector action testAction ");
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "s");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " which represent successful or not");
dNode = connector.getActions().get(1).docAttachments.get(0);
Assert.assertEquals(dNode.documentationText, "Test Connector action testSend ");
Assert.assertEquals(dNode.getAttributes().size(), 2);
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "ep");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " which represent successful or not ");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "s");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " which represent successful or not");
docNodes = ((BLangFunction) packageNode.getFunctions().get(0)).docAttachments;
dNode = docNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + "Gets a access parameter value (`true` or `false`) for a given key. " + "Please note that #foo will always be bigger than #bar.\n" + "Example:\n" + "``SymbolEnv pkgEnv = symbolEnter.packageEnvs.get(pkgNode.symbol);``\n");
Assert.assertEquals(dNode.getAttributes().size(), 3);
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "file");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " file path ``C:\\users\\OddThinking\\Documents\\My Source\\Widget\\foo.src``\n");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "accessMode");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " read or write mode\n");
Assert.assertEquals(dNode.getAttributes().get(2).documentationField.getValue(), "successful");
Assert.assertEquals(dNode.getAttributes().get(2).documentationText, " boolean `true` or `false`\n");
docNodes = ((BLangStruct) packageNode.getStructs().get(0)).docAttachments;
dNode = docNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, " Documentation for File struct\n");
Assert.assertEquals(dNode.getAttributes().size(), 1);
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "path");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " struct `field path` documentation\n");
}
use of org.wso2.ballerinalang.compiler.tree.BLangStruct in project ballerina by ballerina-lang.
the class DocumentationTest method testDeprecated.
@Test(description = "Test doc deprecated.", enabled = false)
public void testDeprecated() {
CompileResult compileResult = BCompileUtil.compile("test-src/documentation/deprecated.bal");
Assert.assertEquals(compileResult.getWarnCount(), 0);
PackageNode packageNode = compileResult.getAST();
List<BLangDeprecatedNode> dNodes = ((BLangFunction) packageNode.getFunctions().get(0)).deprecatedAttachments;
BLangDeprecatedNode dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This function is deprecated use `openFile(string accessMode){}` instead.\n");
dNodes = ((BLangStruct) packageNode.getStructs().get(0)).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This Struct is deprecated use `File2` instead.\n");
dNodes = ((BLangEnum) packageNode.getEnums().get(0)).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This Enum is deprecated use `Enum2` instead.\n");
dNodes = ((BLangEnum) packageNode.getEnums().get(0)).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This Enum is deprecated use `Enum2` instead.\n");
dNodes = ((BLangVariable) packageNode.getGlobalVariables().get(0)).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "use ```const string testConst = " + "\"TestConstantDocumentation\";``` instead");
dNodes = ((BLangConnector) packageNode.getConnectors().get(0)).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This Connector is deprecated use `Connector(string url2){}` instead.\n");
dNodes = ((BLangConnector) packageNode.getConnectors().get(0)).getActions().get(0).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This action is deprecated use `Connector.test(string url2){}` instead.\n" + " ");
dNodes = ((BLangService) packageNode.getServices().get(0)).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " This Service is deprecated use `PizzaHutService{}` instead.\n");
dNodes = ((BLangService) packageNode.getServices().get(0)).getResources().get(0).deprecatedAttachments;
dNode = dNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "This Resource is deprecated use `PizzaHutService.orderFromPizza()` instead.");
}
use of org.wso2.ballerinalang.compiler.tree.BLangStruct in project ballerina by ballerina-lang.
the class DocumentationTest method testMultiple.
@Test(description = "Test doc multiple.")
public void testMultiple() {
CompileResult compileResult = BCompileUtil.compile("test-src/documentation/multiple.bal");
Assert.assertEquals(0, compileResult.getWarnCount());
PackageNode packageNode = compileResult.getAST();
List<BLangDocumentation> docNodes = ((BLangStruct) packageNode.getStructs().get(0)).docAttachments;
BLangDocumentation dNode = docNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, " Documentation for Tst struct\n");
Assert.assertEquals(dNode.getAttributes().size(), 3);
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "a");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " annotation `field a` documentation\n");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "b");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " annotation `field b` documentation\n");
Assert.assertEquals(dNode.getAttributes().get(2).documentationField.getValue(), "c");
Assert.assertEquals(dNode.getAttributes().get(2).documentationText, " annotation `field c` documentation");
docNodes = ((BLangEnum) packageNode.getEnums().get(0)).docAttachments;
dNode = docNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, " Documentation for state enum\n");
Assert.assertEquals(dNode.getAttributes().size(), 2);
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "foo");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " enum `field foo` documentation\n");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "bar");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " enum `field bar` documentation");
docNodes = ((BLangTransformer) packageNode.getTransformers().get(0)).docAttachments;
dNode = docNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "\n" + " Transformer Foo Person -> Employee\n" + " ");
Assert.assertEquals(dNode.getAttributes().size(), 3);
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "p");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText, " input struct Person source used for transformation\n ");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "e");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText, " output struct Employee struct which Person transformed to\n ");
Assert.assertEquals(dNode.getAttributes().get(2).documentationField.getValue(), "defaultAddress");
Assert.assertEquals(dNode.getAttributes().get(2).documentationText, " address which serves Eg: `POSTCODE 112`\n");
BLangService service = (BLangService) packageNode.getServices().get(0);
docNodes = service.docAttachments;
dNode = docNodes.get(0);
Assert.assertNotNull(dNode);
Assert.assertEquals(dNode.documentationText, "PizzaService HTTP Service");
/*
// Commented due to https://github.com/ballerina-lang/ballerina/issues/5586 issue
dNode = service.getResources().get(0).docAttachments.get(0);
Assert.assertEquals(dNode.getAttributes().size(), 2);
Assert.assertEquals(dNode.documentationText, "Check orderPizza resource. ");
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "conn");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText,
" HTTP connection. ");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "req");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText,
" In request.");
dNode = service.getResources().get(1).docAttachments.get(0);
Assert.assertEquals(dNode.documentationText, "Check status resource. ");
Assert.assertEquals(dNode.getAttributes().size(), 2);
Assert.assertEquals(dNode.getAttributes().get(0).documentationField.getValue(), "conn");
Assert.assertEquals(dNode.getAttributes().get(0).documentationText,
" HTTP connection. ");
Assert.assertEquals(dNode.getAttributes().get(1).documentationField.getValue(), "req");
Assert.assertEquals(dNode.getAttributes().get(1).documentationText,
" In request.");*/
}
use of org.wso2.ballerinalang.compiler.tree.BLangStruct in project ballerina by ballerina-lang.
the class CommonUtil method generateJSON.
/**
* Generate json representation for the given node.
*
* @param node Node to get the json representation
* @param anonStructs Map of anonymous structs
* @return {@link JsonElement} Json Representation of the node
*/
public static JsonElement generateJSON(Node node, Map<String, Node> anonStructs) {
if (node == null) {
return JsonNull.INSTANCE;
}
Set<Method> methods = ClassUtils.getAllInterfaces(node.getClass()).stream().flatMap(aClass -> Arrays.stream(aClass.getMethods())).collect(Collectors.toSet());
JsonObject nodeJson = new JsonObject();
JsonArray wsJsonArray = new JsonArray();
Set<Whitespace> ws = node.getWS();
if (ws != null && !ws.isEmpty()) {
for (Whitespace whitespace : ws) {
JsonObject wsJson = new JsonObject();
wsJson.addProperty("ws", whitespace.getWs());
wsJson.addProperty("i", whitespace.getIndex());
wsJson.addProperty("text", whitespace.getPrevious());
wsJson.addProperty("static", whitespace.isStatic());
wsJsonArray.add(wsJson);
}
nodeJson.add("ws", wsJsonArray);
}
org.ballerinalang.util.diagnostic.Diagnostic.DiagnosticPosition position = node.getPosition();
if (position != null) {
JsonObject positionJson = new JsonObject();
positionJson.addProperty("startColumn", position.getStartColumn());
positionJson.addProperty("startLine", position.getStartLine());
positionJson.addProperty("endColumn", position.getEndColumn());
positionJson.addProperty("endLine", position.getEndLine());
nodeJson.add("position", positionJson);
}
JsonArray type = getType(node);
if (type != null) {
nodeJson.add(SYMBOL_TYPE, type);
}
if (node.getKind() == NodeKind.INVOCATION) {
assert node instanceof BLangInvocation : node.getClass();
BLangInvocation invocation = (BLangInvocation) node;
if (invocation.symbol != null && invocation.symbol.kind != null) {
nodeJson.addProperty(INVOCATION_TYPE, invocation.symbol.kind.toString());
}
}
for (Method m : methods) {
String name = m.getName();
if (name.equals("getWS") || name.equals("getPosition")) {
continue;
}
String jsonName;
if (name.startsWith("get")) {
jsonName = toJsonName(name, 3);
} else if (name.startsWith("is")) {
jsonName = toJsonName(name, 2);
} else {
continue;
}
Object prop = null;
try {
prop = m.invoke(node);
} catch (IllegalAccessException | InvocationTargetException e) {
logger.error("Error while serializing source to JSON: [" + e.getMessage() + "]");
}
/* Literal class - This class is escaped in backend to address cases like "ss\"" and 8.0 and null */
if (node.getKind() == NodeKind.LITERAL && "value".equals(jsonName)) {
if (prop instanceof String) {
nodeJson.addProperty(jsonName, '"' + StringEscapeUtils.escapeJava((String) prop) + '"');
nodeJson.addProperty(UNESCAPED_VALUE, String.valueOf(prop));
} else {
nodeJson.addProperty(jsonName, String.valueOf(prop));
}
continue;
}
if (node.getKind() == NodeKind.USER_DEFINED_TYPE && jsonName.equals("typeName")) {
IdentifierNode typeNode = (IdentifierNode) prop;
Node structNode;
if (typeNode.getValue().startsWith("$anonStruct$") && (structNode = anonStructs.remove(typeNode.getValue())) != null) {
JsonObject anonStruct = generateJSON(structNode, anonStructs).getAsJsonObject();
anonStruct.addProperty("anonStruct", true);
nodeJson.add("anonStruct", anonStruct);
continue;
}
}
if (prop instanceof List && jsonName.equals("types")) {
// Currently we don't need any Symbols for the UI. So skipping for now.
continue;
}
/* Node classes */
if (prop instanceof Node) {
nodeJson.add(jsonName, generateJSON((Node) prop, anonStructs));
} else if (prop instanceof List) {
List listProp = (List) prop;
JsonArray listPropJson = new JsonArray();
nodeJson.add(jsonName, listPropJson);
for (Object listPropItem : listProp) {
if (listPropItem instanceof Node) {
/* Remove top level anon func and struct */
if (node.getKind() == NodeKind.COMPILATION_UNIT) {
if (listPropItem instanceof BLangStruct && ((BLangStruct) listPropItem).isAnonymous) {
anonStructs.put(((BLangStruct) listPropItem).getName().getValue(), ((BLangStruct) listPropItem));
continue;
}
if (listPropItem instanceof BLangFunction && (((BLangFunction) listPropItem)).name.value.startsWith("$lambda$")) {
continue;
}
}
listPropJson.add(generateJSON((Node) listPropItem, anonStructs));
} else {
logger.debug("Can't serialize " + jsonName + ", has a an array of " + listPropItem);
}
}
/* Runtime model classes */
} else if (prop instanceof Set && jsonName.equals("flags")) {
Set flags = (Set) prop;
for (Flag flag : Flag.values()) {
nodeJson.addProperty(StringUtils.lowerCase(flag.toString()), flags.contains(flag));
}
} else if (prop instanceof Set) {
// TODO : limit this else if to getInputs getOutputs of transform.
Set vars = (Set) prop;
JsonArray listVarJson = new JsonArray();
nodeJson.add(jsonName, listVarJson);
for (Object obj : vars) {
listVarJson.add(obj.toString());
}
} else if (prop instanceof NodeKind) {
String kindName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, prop.toString());
nodeJson.addProperty(jsonName, kindName);
} else if (prop instanceof OperatorKind) {
nodeJson.addProperty(jsonName, prop.toString());
/* Generic classes */
} else if (prop instanceof String) {
nodeJson.addProperty(jsonName, (String) prop);
} else if (prop instanceof Number) {
nodeJson.addProperty(jsonName, (Number) prop);
} else if (prop instanceof Boolean) {
nodeJson.addProperty(jsonName, (Boolean) prop);
} else if (prop instanceof Enum) {
nodeJson.addProperty(jsonName, StringUtils.lowerCase(((Enum) prop).name()));
} else if (prop != null) {
nodeJson.addProperty(jsonName, prop.toString());
String message = "Node " + node.getClass().getSimpleName() + " contains unknown type prop: " + jsonName + " of type " + prop.getClass();
logger.error(message);
}
}
return nodeJson;
}
Aggregations