Skip to content

Commit

Permalink
Fix adding members to record
Browse files Browse the repository at this point in the history
  • Loading branch information
dulajdilshan committed Dec 10, 2024
1 parent ab52daf commit 240bfef
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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<String, Symbol> symbolMap) {
// Record
switch (typeSymbol.typeKind()) {
Expand Down Expand Up @@ -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

Expand All @@ -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> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
},
{
Expand Down Expand Up @@ -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": []
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
}

0 comments on commit 240bfef

Please sign in to comment.