From 240bfef8e4265ed85bd67ee7dc9cce3ea4ba396b Mon Sep 17 00:00:00 2001 From: dulaj Date: Tue, 10 Dec 2024 10:23:11 +0530 Subject: [PATCH] Fix adding members to record --- .../flowmodelgenerator/core/TypesManager.java | 38 ++++++----- .../types_manager/config/get_all_types.json | 67 +++++++++++++++++++ .../types_manager/config/get_record_type.json | 30 +++++++++ 3 files changed, 118 insertions(+), 17 deletions(-) diff --git a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/TypesManager.java b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/TypesManager.java index c6ff59d08..5138b1e2f 100644 --- a/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/TypesManager.java +++ b/flow-model-generator/modules/flow-model-generator-core/src/main/java/io/ballerina/flowmodelgenerator/core/TypesManager.java @@ -108,6 +108,22 @@ public JsonElement getAllTypes() { return gson.toJsonTree(allTypes); } + public JsonElement getType(Document document, LinePosition linePosition) { + SemanticModel semanticModel = this.module.getCompilation().getSemanticModel(); + Optional symbol = semanticModel.symbol(document, linePosition); + if (symbol.isEmpty() || symbol.get().kind() != SymbolKind.TYPE_DEFINITION) { + return null; + } + + TypeDefinitionSymbol typeDef = (TypeDefinitionSymbol) symbol.get(); + // Only supports records so far. TODO: support unions, array, errors + if (typeDef.typeDescriptor().typeKind() == TypeDescKind.RECORD) { + return gson.toJsonTree(getRecordType(typeDef)); + } + + return null; + } + private void addMemberTypes(TypeSymbol typeSymbol, Map symbolMap) { // Record switch (typeSymbol.typeKind()) { @@ -239,13 +255,17 @@ private TypeData getRecordType(TypeDefinitionSymbol recTypeDefSymbol) { fieldMembers.put(name, memberBuilder .kind(Member.MemberKind.FIELD) - .ref(isForeignType(fieldTypeDesc) ? getTypeName(fieldTypeDesc) : fieldTypeDesc.signature()) + .type(fieldTypeDesc.typeKind() == TypeDescKind.TYPE_REFERENCE ? + getTypeName(fieldTypeDesc) : fieldTypeDesc.typeKind().getName()) + .ref(isForeignType(fieldTypeDesc) ? + getTypeName(fieldTypeDesc) : fieldTypeDesc.getName().orElse("")) .name(name) .docs(field.documentation().isEmpty() ? "" : field.documentation().get().description().orElse("")) // TODO: Add default value .build()); }); + typeDataBuilder.members(fieldMembers); // TODO: Add support for annotations @@ -269,20 +289,4 @@ private String getTypeName(Symbol symbol) { ModuleID moduleId = symbol.getModule().get().id(); return String.format("%s/%s:%s", moduleId.orgName(), moduleId.packageName(), name); } - - public JsonElement getType(Document document, LinePosition linePosition) { - SemanticModel semanticModel = this.module.getCompilation().getSemanticModel(); - Optional symbol = semanticModel.symbol(document, linePosition); - if (symbol.isEmpty() || symbol.get().kind() != SymbolKind.TYPE_DEFINITION) { - return null; - } - - TypeDefinitionSymbol typeDef = (TypeDefinitionSymbol) symbol.get(); - // Only supports records so far. TODO: support unions, array, errors - if (typeDef.typeDescriptor().typeKind() == TypeDescKind.RECORD) { - return gson.toJsonTree(getRecordType(typeDef)); - } - - return null; - } } diff --git a/flow-model-generator/modules/flow-model-generator-ls-extension/src/test/resources/types_manager/config/get_all_types.json b/flow-model-generator/modules/flow-model-generator-ls-extension/src/test/resources/types_manager/config/get_all_types.json index afe66b6c1..3fb865751 100644 --- a/flow-model-generator/modules/flow-model-generator-ls-extension/src/test/resources/types_manager/config/get_all_types.json +++ b/flow-model-generator/modules/flow-model-generator-ls-extension/src/test/resources/types_manager/config/get_all_types.json @@ -69,6 +69,43 @@ "advanced": true } }, + "members": { + "country": { + "kind": "FIELD", + "ref": "", + "type": "string", + "name": "country", + "docs": "" + }, + "city": { + "kind": "FIELD", + "ref": "", + "type": "string", + "name": "city", + "docs": "" + }, + "houseNo": { + "kind": "FIELD", + "ref": "", + "type": "string", + "name": "houseNo", + "docs": "" + }, + "line2": { + "kind": "FIELD", + "ref": "", + "type": "string", + "name": "line2", + "docs": "" + }, + "line1": { + "kind": "FIELD", + "ref": "", + "type": "string", + "name": "line1", + "docs": "" + } + }, "includes": [] }, { @@ -138,6 +175,36 @@ "advanced": true } }, + "members": { + "address": { + "kind": "FIELD", + "ref": "Address", + "type": "Address", + "name": "address", + "docs": "" + }, + "dob": { + "kind": "FIELD", + "ref": "ballerina/time:Utc", + "type": "ballerina/time:Utc", + "name": "dob", + "docs": "" + }, + "name": { + "kind": "FIELD", + "ref": "", + "type": "string", + "name": "name", + "docs": "" + }, + "id": { + "kind": "FIELD", + "ref": "", + "type": "int", + "name": "id", + "docs": "" + } + }, "includes": [] } ] diff --git a/flow-model-generator/modules/flow-model-generator-ls-extension/src/test/resources/types_manager/config/get_record_type.json b/flow-model-generator/modules/flow-model-generator-ls-extension/src/test/resources/types_manager/config/get_record_type.json index 3c6d29f91..f0337efcf 100644 --- a/flow-model-generator/modules/flow-model-generator-ls-extension/src/test/resources/types_manager/config/get_record_type.json +++ b/flow-model-generator/modules/flow-model-generator-ls-extension/src/test/resources/types_manager/config/get_record_type.json @@ -72,6 +72,36 @@ "advanced": true } }, + "members": { + "address": { + "kind": "FIELD", + "ref": "Address", + "type": "Address", + "name": "address", + "docs": "" + }, + "dob": { + "kind": "FIELD", + "ref": "ballerina/time:Utc", + "type": "ballerina/time:Utc", + "name": "dob", + "docs": "" + }, + "name": { + "kind": "FIELD", + "ref": "", + "type": "string", + "name": "name", + "docs": "" + }, + "id": { + "kind": "FIELD", + "ref": "", + "type": "int", + "name": "id", + "docs": "" + } + }, "includes": [] } }