Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce rest params and included record params to the json model #482

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
26013bd
Index included record params and rest params
LakshanWeerasinghe Nov 7, 2024
2380658
Get the infering type correctly
LakshanWeerasinghe Nov 7, 2024
703241f
Generate default values for included record params
LakshanWeerasinghe Nov 7, 2024
cee306b
Add default value gen util
LakshanWeerasinghe Nov 7, 2024
b643ae9
Remove old DefaultValueGen Util useages
LakshanWeerasinghe Nov 7, 2024
21f747d
Refactor code
LakshanWeerasinghe Nov 7, 2024
1edfce3
Update FunctionResult retrival funcs
LakshanWeerasinghe Nov 7, 2024
6075893
Re-generate the index
LakshanWeerasinghe Nov 7, 2024
b53244a
Update function calling templates
LakshanWeerasinghe Nov 7, 2024
98bc3d0
Update node template tests
LakshanWeerasinghe Nov 7, 2024
c9c70eb
Add source builder for function calls
LakshanWeerasinghe Nov 7, 2024
eaff156
Function call to source impl
LakshanWeerasinghe Nov 7, 2024
c9786f4
Update source gen tests
LakshanWeerasinghe Nov 7, 2024
0b640d0
Fix failing model generator tests
LakshanWeerasinghe Nov 8, 2024
397ddae
Update new connection related tests
LakshanWeerasinghe Nov 8, 2024
c8c628d
Model gen for function call args
LakshanWeerasinghe Nov 8, 2024
6d7634f
Update model generator tests
LakshanWeerasinghe Nov 8, 2024
3c820c7
Fix build failures
LakshanWeerasinghe Nov 8, 2024
ac81835
Re-generate the index
LakshanWeerasinghe Nov 8, 2024
92c38e6
Failing model gen tests
LakshanWeerasinghe Nov 8, 2024
6960eca
Update property kinds
LakshanWeerasinghe Nov 8, 2024
f71be76
Fix failing source gen tests
LakshanWeerasinghe Nov 8, 2024
26c6455
Update node template tests
LakshanWeerasinghe Nov 8, 2024
87f34d4
Fix failing tests
LakshanWeerasinghe Nov 8, 2024
6883f21
Fix checkstyles failures
LakshanWeerasinghe Nov 8, 2024
e8c0b55
Fix spot bug failure
LakshanWeerasinghe Nov 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

package io.ballerina.flowmodelgenerator.core.db;

import io.ballerina.compiler.api.symbols.ParameterKind;
import io.ballerina.flowmodelgenerator.core.db.model.Function;
import io.ballerina.flowmodelgenerator.core.db.model.FunctionResult;
import io.ballerina.flowmodelgenerator.core.db.model.Parameter;
import io.ballerina.flowmodelgenerator.core.db.model.ParameterResult;

import java.io.IOException;
Expand All @@ -33,6 +33,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -224,7 +225,6 @@ public List<FunctionResult> searchFunctions(Map<String, String> queryMap, Functi
rs.getString("resource_path"),
Function.Kind.valueOf(rs.getString("kind")),
rs.getInt("return_error")

);
functionResults.add(functionResult);
}
Expand Down Expand Up @@ -385,6 +385,7 @@ public List<ParameterResult> getFunctionParameters(int functionId) {
"p.name, " +
"p.type, " +
"p.kind, " +
"p.optional, " +
"p.default_value, " +
"p.description " +
"FROM Parameter p " +
Expand All @@ -399,9 +400,10 @@ public List<ParameterResult> getFunctionParameters(int functionId) {
rs.getInt("parameter_id"),
rs.getString("name"),
rs.getString("type"),
ParameterKind.valueOf(rs.getString("kind")),
Parameter.Kind.valueOf(rs.getString("kind")),
rs.getString("default_value"),
rs.getString("description")
rs.getString("description"),
rs.getInt("optional")
);
parameterResults.add(parameterResult);
}
Expand All @@ -412,6 +414,42 @@ public List<ParameterResult> getFunctionParameters(int functionId) {
}
}

public LinkedHashMap<String, ParameterResult> getFunctionParametersAsMap(int functionId) {
String sql = "SELECT " +
"p.parameter_id, " +
"p.name, " +
"p.type, " +
"p.kind, " +
"p.optional, " +
"p.default_value, " +
"p.description " +
"FROM Parameter p " +
"WHERE p.function_id = ?;";
try (Connection conn = DriverManager.getConnection(dbPath);
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, functionId);
ResultSet rs = stmt.executeQuery();
LinkedHashMap<String, ParameterResult> parameterResults = new LinkedHashMap<>();
while (rs.next()) {
String paramName = rs.getString("name");
ParameterResult parameterResult = new ParameterResult(
rs.getInt("parameter_id"),
paramName,
rs.getString("type"),
Parameter.Kind.valueOf(rs.getString("kind")),
rs.getString("default_value"),
rs.getString("description"),
rs.getInt("optional")
);
parameterResults.put(paramName, parameterResult);
}
return parameterResults;
} catch (SQLException e) {
Logger.getGlobal().severe("Error executing query: " + e.getMessage());
return new LinkedHashMap<>();
}
}

public List<FunctionResult> getConnectorActions(int connectorId) {
String sql = "SELECT " +
"f.function_id, " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ public record FunctionResult(
String version,
String resourcePath,
Function.Kind kind,
Integer returnError
) {
Integer returnError) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
public class Parameter {

public enum Kind {
REQUIRED, DEFAULTABLE, INCLUDED_RECORD, REST
REQUIRED,
DEFAULTABLE,
INCLUDED_RECORD,
REST_PARAMETER,
INCLUDED_FIELD,
PARAM_FOR_TYPE_INFER,
INCLUDED_RECORD_REST
}

@Id
Expand All @@ -44,6 +50,9 @@ public enum Kind {
@JoinColumn(name = "function_id")
private Function function;

@Column(name = "optional")
private Integer optional;

// Getters and setters
public Long getParameterId() {
return parameterId;
Expand Down Expand Up @@ -92,4 +101,12 @@ public Function getFunction() {
public void setFunction(Function function) {
this.function = function;
}

public Integer getOptional() {
return optional;
}

public void setOptional(Integer optional) {
this.optional = optional;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@

package io.ballerina.flowmodelgenerator.core.db.model;

import io.ballerina.compiler.api.symbols.ParameterKind;

public record ParameterResult(
int parameterId,
String name,
String type,
ParameterKind kind,
Parameter.Kind kind,
String defaultValue,
String description) {

public String getDefaultValue() {
//TODO: Remove included record params once #238 completed.
return (kind == ParameterKind.REQUIRED || kind == ParameterKind.INCLUDED_RECORD) ? defaultValue : "";
}
String description,
Integer optional) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@
* @param editable whether the property is not readonly
* @param advanced whether the property should be shown in the advanced tab
* @param diagnostics diagnostics of the property
* @param kind kind of the property
* @since 1.4.0
*/
public record Property(Metadata metadata, String valueType, Object valueTypeConstraint, Object value,
String placeholder, boolean optional, boolean editable, boolean advanced,
Diagnostics diagnostics) {
Diagnostics diagnostics, String kind) {

private static final Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();

Expand Down Expand Up @@ -150,7 +151,9 @@ public enum ValueType {
VIEW,
INCLUSION,
UNION,
FLAG
FLAG,
MAPPING_EXPRESSION_SET,
EXPRESSION_SET
}

public static ValueType valueTypeFrom(String s) {
Expand All @@ -170,6 +173,7 @@ public static class Builder<T> extends FacetedBuilder<T> implements DiagnosticHa
private boolean editable;
private boolean advanced;
private Object typeConstraint;
private String kind;
private Metadata.Builder<Builder<T>> metadataBuilder;
private Diagnostics.Builder<Builder<T>> diagnosticsBuilder;

Expand Down Expand Up @@ -213,6 +217,11 @@ public Builder<T> advanced(boolean advanced) {
return this;
}

public Builder<T> kind(String kind) {
this.kind = kind;
return this;
}

public Builder<T> editable() {
this.editable = true;
return this;
Expand Down Expand Up @@ -247,7 +256,7 @@ public Property build() {
Property property =
new Property(metadataBuilder == null ? null : metadataBuilder.build(), type, typeConstraint, value,
placeholder, optional, editable, advanced,
diagnosticsBuilder == null ? null : diagnosticsBuilder.build());
diagnosticsBuilder == null ? null : diagnosticsBuilder.build(), kind);
this.metadataBuilder = null;
this.type = null;
this.typeConstraint = null;
Expand All @@ -257,6 +266,7 @@ public Property build() {
this.editable = false;
this.advanced = false;
this.diagnosticsBuilder = null;
this.kind = null;
return property;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.flowmodelgenerator.core.CommonUtils;
import io.ballerina.flowmodelgenerator.core.db.model.Parameter;
import io.ballerina.projects.Document;
import io.ballerina.projects.Module;
import io.ballerina.projects.ModuleDescriptor;
Expand Down Expand Up @@ -268,30 +269,47 @@ public SourceBuilder functionParameters(FlowNode nodeTemplate, Set<String> ignor
Set<String> keys = new LinkedHashSet<>(properties != null ? properties.keySet() : Set.of());
keys.removeAll(ignoredProperties);

boolean hasEmptyParam = false;
boolean firstParamAdded = false;
for (String key : keys) {
Optional<Property> property = flowNode.getProperty(key);
if (property.isEmpty() || property.get().value() == null ||
(property.get().optional() && property.get().value().toString().isEmpty())) {
hasEmptyParam = true;
continue;
}

Property prop = property.get();
String kind = prop.kind();

if (firstParamAdded) {
tokenBuilder.keyword(SyntaxKind.COMMA_TOKEN);
if ((kind.equals(Parameter.Kind.REST_PARAMETER.name()))) {
if (hasRestParamValues(prop)) {
tokenBuilder.keyword(SyntaxKind.COMMA_TOKEN);
addRestParamValues(prop);
continue;
}
} else if (kind.equals(Parameter.Kind.INCLUDED_RECORD_REST.name())) {
if (hasRestParamValues(prop)) {
tokenBuilder.keyword(SyntaxKind.COMMA_TOKEN);
addIncludedRecordRestParamValues(prop);
continue;
}
} else {
tokenBuilder.keyword(SyntaxKind.COMMA_TOKEN);
}
} else {
firstParamAdded = true;
}

if (hasEmptyParam) {
tokenBuilder
.name(key)
.keyword(SyntaxKind.EQUAL_TOKEN);
hasEmptyParam = false;
if (kind.equals(Parameter.Kind.REQUIRED.name()) || kind.equals(Parameter.Kind.DEFAULTABLE.name())
|| kind.equals(Parameter.Kind.INCLUDED_RECORD.name())) {
tokenBuilder.expression(prop);
} else if (kind.equals(Parameter.Kind.INCLUDED_FIELD.name())) {
tokenBuilder.name(key).whiteSpace().keyword(SyntaxKind.EQUAL_TOKEN).expression(prop);
} else if (kind.equals(Parameter.Kind.REST_PARAMETER.name())) {
addRestParamValues(prop);
} else if (kind.equals(Parameter.Kind.INCLUDED_RECORD_REST.name())) {
addIncludedRecordRestParamValues(prop);
}

tokenBuilder.expression(property.get());
}

tokenBuilder
Expand All @@ -300,6 +318,37 @@ public SourceBuilder functionParameters(FlowNode nodeTemplate, Set<String> ignor
return this;
}

private boolean hasRestParamValues(Property prop) {
if (prop.value() instanceof List<?> values) {
return !values.isEmpty();
}
return false;
}

private void addRestParamValues(Property prop) {
if (prop.value() instanceof List<?>) {
List<String> values = (List<String>) prop.value();
if (!values.isEmpty()) {
tokenBuilder.expression(String.join(", ", values));
}
}
}

private void addIncludedRecordRestParamValues(Property prop) {
if (prop.value() instanceof List<?>) {
List<Map> values = (List<Map>) prop.value();
if (!values.isEmpty()) {
List<String> result = new ArrayList<>();
values.forEach(keyValuePair -> {
String key = (String) keyValuePair.keySet().iterator().next();
String value = (String) keyValuePair.values().iterator().next();
result.add(key + " = " + value);
});
tokenBuilder.expression(String.join(", ", result));
}
}
}

public SourceBuilder textEdit(boolean isExpression) {
return textEdit(isExpression, filePath, CommonUtils.toRange(flowNode.codedata().lineRange()));
}
Expand Down
Loading
Loading