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

Generate code for enums in definition blocks #31

Merged
merged 3 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -524,13 +524,19 @@ public void generateAlgodIndexerObjects (JsonNode root) throws IOException {
null ? root.get("components").get("schemas") : root.get("definitions");
for (Map.Entry<String, JsonNode> cls : getSortedSchema(schemas).entrySet()) {
String desc = null;
if (cls.getValue().get("description") != null) {
desc = cls.getValue().get("description").asText();
}

TypeDef clsType = getType(cls.getValue(), true, cls.getKey(), false, false);
if (clsType.isOfType("enum")) {
publisher.publish(Events.ENUM_DEFINITION, clsType);
}

if (!hasProperties(cls.getValue())) {
// If it has no properties, no class is needed for this type.
continue;
}
if (cls.getValue().get("description") != null) {
desc = cls.getValue().get("description").asText();
}
String className = Tools.getCamelCase(cls.getKey(), true);
if (!filterList.isEmpty() && filterList.contains(className)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public void onEvent(Events event, TypeDef type) {
case BODY_CONTENT:
javaQueryWriter.addQueryProperty(type, false, false, true);
break;
case ENUM_DEFINITION:
this.storeEnumDefinition(type);
Copy link
Contributor

@winder winder May 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On closer inspection, I think we can add this check without changing the parser. This would go in the TypeDef event:

         case NEW_PROPERTY:
             javaModelWriter.newProperty(type);
+            if (type.enumValues != null && type.enumValues.size() > 0) {
+                 this.storeEnumDefinition(type);
+            }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will work. The reason we have to change the parser is because generateAlgodIndexerObjects is the only method which is looking at the definitions block, and the following code block ensure that objects in that block are skipped if they don't have properties defined, which enums do not have.

if (!hasProperties(cls.getValue())) {
    // If it has no properties, no class is needed for this type.
    continue;
}

So in that case we wouldn't ever enter the NEW_PROPERTY event block.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, as an aside, enums implicity defined in the Parameters section are omitted from the generated code (Format)

break;
default:
throw new RuntimeException("Unimplemented event for TypeDef! " + event);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/algorand/sdkutils/listeners/Publisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class Publisher {
public enum Events {
ALL,

// Store an enum definition--some languages will just treat enums as strings.
ENUM_DEFINITION,

// Define a model object.
NEW_MODEL,
NEW_PROPERTY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ public void onEvent(Publisher.Events event, TypeDef type) {
case BODY_CONTENT:
activeQuery.bodyParameters.add(type);
break;
case ENUM_DEFINITION:
// do nothing for enum definitions
break;
default:
logger.info("unhandled event (Events, TypeDef): {}", event);
}
Expand Down