Skip to content

Commit

Permalink
Merge pull request #593 from nipunayf/fix-data-map-def
Browse files Browse the repository at this point in the history
Make minor improvements to the function definition forms
  • Loading branch information
nipunayf authored Feb 12, 2025
2 parents 27152ba + 3a138b9 commit 24a95af
Show file tree
Hide file tree
Showing 27 changed files with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ public void visit(FunctionDefinitionNode functionDefinitionNode) {
NodeKind.DATA_MAPPER_DEFINITION,
DataMapperDefinitionBuilder.PARAMETERS_LABEL,
DataMapperDefinitionBuilder.PARAMETERS_DOC,
false,
DataMapperDefinitionBuilder.RECORD_TYPE)
: new FunctionMetadata(
NodeKind.FUNCTION_DEFINITION,
FunctionDefinitionBuilder.PARAMETERS_LABEL,
FunctionDefinitionBuilder.PARAMETERS_DOC,
true,
null);
NodeBuilder nodeBuilder = NodeBuilder.getNodeFromKind(metadata.nodeKind)
.defaultModuleName(this.moduleInfo);
Expand All @@ -104,7 +106,8 @@ public void visit(FunctionDefinitionNode functionDefinitionNode) {
functionDefinitionNode.functionSignature().returnTypeDesc()
.map(type -> type.type().toSourceCode().strip())
.orElse(""),
metadata.returnTypeConstraint)
metadata.returnTypeConstraint,
metadata.returnTypeConstraint == null)
.nestedProperty();

// Set the function parameters
Expand Down Expand Up @@ -138,7 +141,8 @@ public void visit(FunctionDefinitionNode functionDefinitionNode) {
Property.PARAMETERS_KEY,
metadata.parametersLabel,
metadata.parametersDoc,
FunctionDefinitionBuilder.getParameterSchema());
FunctionDefinitionBuilder.getParameterSchema(),
metadata.optionalParameters);

// Build the definition node
this.node = gson.toJsonTree(nodeBuilder.build());
Expand All @@ -156,6 +160,7 @@ private record FunctionMetadata(
NodeKind nodeKind,
String parametersLabel,
String parametersDoc,
boolean optionalParameters,
String returnTypeConstraint) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public FormBuilder<T> type(String typeName, String label, boolean editable, Line
return this;
}

public FormBuilder<T> returnType(String value, String typeConstraint) {
public FormBuilder<T> returnType(String value, String typeConstraint, boolean optional) {
propertyBuilder
.metadata()
.label(Property.RETURN_TYPE_LABEL)
Expand All @@ -185,15 +185,15 @@ public FormBuilder<T> returnType(String value, String typeConstraint) {
.value(value == null ? "" : value)
.type(Property.ValueType.TYPE)
.typeConstraint(typeConstraint)
.optional(true)
.optional(optional)
.editable();

addProperty(Property.TYPE_KEY);
return this;
}

public FormBuilder<T> returnType(String value) {
return returnType(value, null);
return returnType(value, null, true);
}

public FormBuilder<T> dataVariable(TypedBindingPatternNode node, boolean implicit, Set<String> names) {
Expand Down Expand Up @@ -873,15 +873,16 @@ public FormBuilder<T> nestedProperty() {
}

public FormBuilder<T> endNestedProperty(Property.ValueType valueType, String key, String label, String doc,
Object typeConstraint) {
Object typeConstraint, boolean optional) {
propertyBuilder
.metadata()
.label(label)
.description(doc)
.stepOut()
.value(nodeProperties)
.typeConstraint(typeConstraint)
.type(valueType);
.type(valueType)
.optional(optional);
if (!nodePropertiesStack.isEmpty()) {
nodeProperties = nodePropertiesStack.pop();
}
Expand All @@ -890,7 +891,7 @@ public FormBuilder<T> endNestedProperty(Property.ValueType valueType, String key
}

public FormBuilder<T> endNestedProperty(Property.ValueType valueType, String key, String label, String doc) {
return endNestedProperty(valueType, key, label, doc, null);
return endNestedProperty(valueType, key, label, doc, null, false);
}

public final void addProperty(String key, Node node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public void setConcreteTemplateData(TemplateContext context) {
properties()
.functionNameTemplate("transform", context.getAllVisibleSymbolNames(),
DATA_MAPPER_NAME_LABEL, DATA_MAPPER_NAME_DOC)
.returnType(null, RECORD_TYPE)
.returnType(null, RECORD_TYPE, false)
.nestedProperty()
.endNestedProperty(Property.ValueType.REPEATABLE_PROPERTY, Property.PARAMETERS_KEY, PARAMETERS_LABEL,
PARAMETERS_DOC, FunctionDefinitionBuilder.getParameterSchema());
PARAMETERS_DOC, FunctionDefinitionBuilder.getParameterSchema(), false);
}

@Override
Expand Down Expand Up @@ -118,8 +118,7 @@ public Map<Path, List<TextEdit>> toSource(SourceBuilder sourceBuilder) {
String returnTypeString = returnType.get().value().toString();
sourceBuilder.token()
.keyword(SyntaxKind.RETURNS_KEYWORD)
.name(returnTypeString)
.keyword(SyntaxKind.RIGHT_DOUBLE_ARROW_TOKEN);
.name(returnTypeString);

// Generate text edits based on the line range. If a line range exists, update the signature of the existing
// function. Otherwise, create a new function definition in "data_mappings.bal".
Expand All @@ -142,6 +141,7 @@ public Map<Path, List<TextEdit>> toSource(SourceBuilder sourceBuilder) {
((RecordTypeSymbol) recordTypeSymbol).fieldDescriptors());
sourceBuilder
.token()
.keyword(SyntaxKind.RIGHT_DOUBLE_ARROW_TOKEN)
.openBrace()
.name(bodyText)
.closeBrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void setConcreteTemplateData(TemplateContext context) {
.returnType(null)
.nestedProperty()
.endNestedProperty(Property.ValueType.REPEATABLE_PROPERTY, Property.PARAMETERS_KEY, PARAMETERS_LABEL,
PARAMETERS_DOC, getParameterSchema());
PARAMETERS_DOC, getParameterSchema(), true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"valueType": "TYPE",
"valueTypeConstraint": "record",
"value": "Employee",
"optional": true,
"optional": false,
"editable": true,
"advanced": false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"valueType": "TYPE",
"valueTypeConstraint": "record",
"value": "Employee",
"optional": true,
"optional": false,
"editable": true,
"advanced": false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"valueType": "TYPE",
"valueTypeConstraint": "record",
"value": "Summary",
"optional": true,
"optional": false,
"editable": true,
"advanced": false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"valueType": "TYPE",
"valueTypeConstraint": "record",
"value": "Person",
"optional": true,
"optional": false,
"editable": true,
"advanced": false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"valueType": "TYPE",
"valueTypeConstraint": "record",
"value": "Employee",
"optional": true,
"optional": false,
"editable": true,
"advanced": false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"advanced": false
},
"value": {},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"advanced": false
},
"value": {},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"advanced": false
},
"value": {},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
"advanced": false
}
},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"advanced": false
}
},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"advanced": false
}
},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"advanced": false
}
},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
"advanced": false
}
},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
"advanced": false
}
},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"advanced": false
}
},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"advanced": false
},
"value": {},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
"advanced": false
}
},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"advanced": false
},
"value": {},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"valueType": "TYPE",
"valueTypeConstraint": "record",
"value": "",
"optional": true,
"optional": false,
"editable": true,
"advanced": false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"valueType": "TYPE",
"valueTypeConstraint": "record",
"value": "",
"optional": true,
"optional": false,
"editable": true,
"advanced": false
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"advanced": false
},
"value": {},
"optional": false,
"optional": true,
"editable": false,
"advanced": false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"character": 61
}
},
"newText": "function mapPersonToEmployee( Person person) returns Employee=> "
"newText": "function mapPersonToEmployee( Person person) returns Employee"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
"character": 86
}
},
"newText": "function transformToPerson( string name, string email, Address address) returns Person=> "
"newText": "function transformToPerson( string name, string email, Address address) returns Person"
}
]
}
Expand Down

0 comments on commit 24a95af

Please sign in to comment.