-
Notifications
You must be signed in to change notification settings - Fork 758
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 new isEnum()
and getEnumSymbol()
APIs in UnionTypeSymbol
to distinguish and get enum-symbol in union-type descriptors
#41173
Closed
dulajdilshan
wants to merge
5
commits into
ballerina-platform:master
from
dulajdilshan:fix/enum-symbol
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ceee36a
Introduce isEnum and getEnumSymbol APIs
dulajdilshan bb912eb
Add tests
dulajdilshan 7a1e398
Remove unnecessary field
dulajdilshan b75fc71
Merge branch 'master' into fix/enum-symbol
dulajdilshan 41b4726
Improve tests and address a review suggestion
dulajdilshan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,10 +19,15 @@ | |||||
import io.ballerina.compiler.api.ModuleID; | ||||||
import io.ballerina.compiler.api.SymbolTransformer; | ||||||
import io.ballerina.compiler.api.SymbolVisitor; | ||||||
import io.ballerina.compiler.api.impl.SymbolFactory; | ||||||
import io.ballerina.compiler.api.symbols.EnumSymbol; | ||||||
import io.ballerina.compiler.api.symbols.TypeDescKind; | ||||||
import io.ballerina.compiler.api.symbols.TypeSymbol; | ||||||
import io.ballerina.compiler.api.symbols.UnionTypeSymbol; | ||||||
import org.ballerinalang.model.symbols.SymbolKind; | ||||||
import org.ballerinalang.model.types.TypeKind; | ||||||
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BEnumSymbol; | ||||||
import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; | ||||||
import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols; | ||||||
import org.wso2.ballerinalang.compiler.semantics.model.types.BFiniteType; | ||||||
import org.wso2.ballerinalang.compiler.semantics.model.types.BType; | ||||||
|
@@ -36,6 +41,7 @@ | |||||
import java.util.ArrayList; | ||||||
import java.util.Collections; | ||||||
import java.util.List; | ||||||
import java.util.Optional; | ||||||
import java.util.Set; | ||||||
import java.util.StringJoiner; | ||||||
import java.util.regex.Pattern; | ||||||
|
@@ -58,6 +64,7 @@ public class BallerinaUnionTypeSymbol extends AbstractTypeSymbol implements Unio | |||||
private List<TypeSymbol> memberTypes; | ||||||
private List<TypeSymbol> originalMemberTypes; | ||||||
private String signature; | ||||||
private EnumSymbol enumSymbol; | ||||||
|
||||||
public BallerinaUnionTypeSymbol(CompilerContext context, BUnionType unionType) { | ||||||
super(context, TypeDescKind.UNION, unionType); | ||||||
|
@@ -147,6 +154,32 @@ public String signature() { | |||||
return this.signature; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public boolean isEnum() { | ||||||
if (this.enumSymbol != null) { | ||||||
return true; | ||||||
} | ||||||
|
||||||
return this.getBType().tsymbol.getKind() == SymbolKind.ENUM; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Optional<EnumSymbol> getEnumSymbol() { | ||||||
if (this.enumSymbol != null) { | ||||||
return Optional.of(this.enumSymbol); | ||||||
} | ||||||
|
||||||
BTypeSymbol tsymbol = this.getBType().tsymbol; | ||||||
if (tsymbol.getKind() != SymbolKind.ENUM) { | ||||||
return Optional.empty(); | ||||||
} | ||||||
|
||||||
SymbolFactory symbolFactory = SymbolFactory.getInstance(this.context); | ||||||
this.enumSymbol = symbolFactory.createEnumSymbol((BEnumSymbol) tsymbol, tsymbol.getName().value);; | ||||||
|
||||||
return Optional.ofNullable(this.enumSymbol); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
At this point can the enumSymbol be null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for noticing! |
||||||
} | ||||||
|
||||||
@Override | ||||||
public void accept(SymbolVisitor visitor) { | ||||||
visitor.visit(this); | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is ideal. isEnum kinda makes sense, but why do we need to allow retrieving the enum symbol like this? In a way, it's like having a reference to the definition from the type symbol.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It's kinda exposing the information related to the enum symbol. Currently, we may not have such things, but having this may allow us to expose that information right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and this is not a way of referencing. For referencing we have a structure as TypeRefType
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant having
enumSymbol
inBallerinaUnionTypeSymbol
. This isn't quite right, IMO. The type doesn't need to how it was defined.I don't think that's the correct way. If someone wants enum symbol they can still get it from the model, right? I don't think the type symbol should expose it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah. What if we have introduced some attributes only related to enums and the ballerina extension developer needs to get them? We can't expose that only-enum information via the BallerinaUnionTypeSymbol right?
How can they get an enum symbol without the location?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give an example for this?
Can't they filter out from module symbols by name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can't. I can't think of such info anyway. We shouldn't mix up enums and types. An enum is just another way to define a union of string singletons. From a type symbol point of view, they are basically the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about annotation attachments? We shouldn't link them with the UnionTypeSymbol right?
Yeah, we can, but we need the name to filter out right? What if we need to get the enum that is in another module, and we have referenced an enum value (e.x: OPEN) and we need to know the other sibling values of the enum (which is referenced in new type?
Let's say there is a ballerina extension writer who needs to get only the sibling values of
OPEN
.Also, isn't it costly to use the filter when there are so many symbols?
Yes. This is true, but here we are just showing the relationship with the associated enum defined somewhere
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the same as
so we shouldn't have to special case anything for enums.
If you have the union type you have the name?
How does adding the enum symbol to the union type help with this? The union type corresponds to the enum, not the member?
It's just filtering based on a string? Anyway, the API being well-defined is more important IMO.