use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.
the class Generator method createDocForNode.
/**
* Create documentation for annotations.
* @param annotationNode ballerina annotation node.
* @return documentation for annotation.
*/
public static AnnotationDoc createDocForNode(BLangAnnotation annotationNode) {
String annotationName = annotationNode.getName().getValue();
List<Variable> attributes = new ArrayList<>();
// Iterate through the attributes of the annotation
if (annotationNode.getAttributes().size() > 0) {
for (BLangAnnotAttribute annotAttribute : annotationNode.getAttributes()) {
String dataType = getTypeName(annotAttribute.getTypeNode());
String desc = annotFieldAnnotation(annotationNode, annotAttribute);
Variable variable = new Variable(annotAttribute.getName().value, dataType, desc);
attributes.add(variable);
}
}
return new AnnotationDoc(annotationName, description(annotationNode), new ArrayList<>(), attributes);
}
use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.
the class Generator method createDocForNode.
/**
* Create documentation for structs.
* @param structNode ballerina struct node.
* @return documentation for structs.
*/
public static StructDoc createDocForNode(BLangStruct structNode) {
String structName = structNode.getName().value;
// Check if its an anonymous struct
if (structName.contains(ANONYMOUS_STRUCT)) {
structName = "Anonymous Struct";
}
List<Field> fields = new ArrayList<>();
// Iterate through the struct fields
if (structNode.getFields().size() > 0) {
for (BLangVariable param : structNode.getFields()) {
String dataType = type(param);
String desc = fieldAnnotation(structNode, param);
String defaultValue = "";
if (null != param.getInitialExpression()) {
defaultValue = param.getInitialExpression().toString();
}
Field variable = new Field(param.getName().value, dataType, desc, defaultValue);
fields.add(variable);
}
}
return new StructDoc(structName, description(structNode), new ArrayList<>(), fields);
}
use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.
the class Generator method createDocForNode.
/**
* Create documentation for actions.
* @param actionNode ballerina action node.
* @return documentation for actions.
*/
public static ActionDoc createDocForNode(BLangAction actionNode) {
String actionName = actionNode.getName().value;
List<Variable> parameters = new ArrayList<>();
List<Variable> returnParams = new ArrayList<>();
// Iterate through the parameters
if (actionNode.getParameters().size() > 0) {
for (BLangVariable param : actionNode.getParameters()) {
String dataType = type(param);
String desc = paramAnnotation(actionNode, param);
Variable variable = new Variable(param.getName().value, dataType, desc);
parameters.add(variable);
}
}
// Iterate through the return types
if (actionNode.getReturnParameters().size() > 0) {
for (int i = 0; i < actionNode.getReturnParameters().size(); i++) {
BLangVariable returnParam = actionNode.getReturnParameters().get(i);
String dataType = type(returnParam);
String desc = returnParamAnnotation(actionNode, i);
Variable variable = new Variable(returnParam.getName().value, dataType, desc);
returnParams.add(variable);
}
}
return new ActionDoc(actionName, description(actionNode), new ArrayList<>(), parameters, returnParams);
}
use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.
the class SwaggerConverterUtils method getAlias.
/**
* Gets the alias for a given package from a bLang file root node.
* @param topCompilationUnit The root node.
* @param packageName The package name.
* @return The alias.
*/
private static String getAlias(BLangCompilationUnit topCompilationUnit, String packageName) {
for (TopLevelNode topLevelNode : topCompilationUnit.getTopLevelNodes()) {
if (topLevelNode instanceof BLangImportPackage) {
BLangImportPackage importPackage = (BLangImportPackage) topLevelNode;
String packagePath = importPackage.getPackageName().stream().map(BLangIdentifier::getValue).collect(Collectors.joining("."));
if (packageName.equals(packagePath)) {
return importPackage.getAlias().getValue();
}
}
}
return null;
}
use of org.wso2.charon3.core.utils.codeutils.Node in project ballerina by ballerina-lang.
the class ResourceContextHolder method buildContext.
public static ResourceContextHolder buildContext(ResourceNode resource) throws CodeGeneratorException {
ResourceContextHolder context = new ResourceContextHolder();
context.name = resource.getName().getValue();
context.parameters = new ArrayList<>();
for (VariableNode node : resource.getParameters()) {
ParameterContextHolder parameter = ParameterContextHolder.buildContext(node);
if (parameter != null) {
context.parameters.add(parameter);
}
}
// Iterate through all resource level annotations and find out resource configuration information
AnnotationAttachmentNode ann = GeneratorUtils.getAnnotationFromList(GeneratorConstants.RES_CONFIG_ANNOTATION, GeneratorConstants.HTTP_PKG_ALIAS, resource.getAnnotationAttachments());
if (ann == null) {
throw new CodeGeneratorException("Incomplete resource configuration found");
}
BLangRecordLiteral bLiteral = ((BLangRecordLiteral) ((BLangAnnotationAttachment) ann).getExpression());
List<BLangRecordLiteral.BLangRecordKeyValue> list = bLiteral.getKeyValuePairs();
Map<String, String[]> attrs = GeneratorUtils.getKeyValuePairAsMap(list);
// We don't expect multiple http methods to be supported by single action
// We only consider first content type for a single resource
context.method = attrs.get(GeneratorConstants.ATTR_METHODS) != null ? attrs.get(GeneratorConstants.ATTR_METHODS)[0] : null;
context.contentType = attrs.get(GeneratorConstants.ATTR_CONSUMES) != null ? attrs.get(GeneratorConstants.ATTR_CONSUMES)[0] : null;
String path = attrs.get(GeneratorConstants.ATTR_PATH) != null ? attrs.get(GeneratorConstants.ATTR_PATH)[0] : null;
context.path = context.getTemplatePath(path);
return context;
}
Aggregations