Skip to content

Commit

Permalink
Fix #106: convert optional collections to IDL
Browse files Browse the repository at this point in the history
  • Loading branch information
opwvhk committed Oct 16, 2023
1 parent 4bd54cb commit 9e25ca4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
21 changes: 12 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ intellij {
//version.set("2022.3.2")
//version.set("2022.3.3")
//version.set("2023.1")
//version.set("2023.2")
//version.set("2023.2.1")

// Note: without the java plugin tests will fail (so don't remove it even if the plugin does not need it)
plugins.set(listOf("com.intellij.java"))
Expand Down Expand Up @@ -121,6 +123,7 @@ tasks {
<ul data-version="213.5.3">
<li>Improved grammar (string literal syntax)</li>
<li>Implemented better symbols (adds symbol search)</li>
<li>Fix #106: converting to IDL now handles optional collections correctly
</ul>
<p>Version 213.5.2:</p>
<ul data-version="213.5.2">
Expand All @@ -133,7 +136,7 @@ tasks {
<ul data-version="213.5.1">
<li>Upgraded dependencies</li>
</ul>
""";
"""
changeLog += """
<p>Version 213.5.0:</p>
<ul data-version="213.5.0">
Expand All @@ -144,7 +147,7 @@ tasks {
</li>
<li>Added disposed check in menu actions (issue #73)</li>
</ul>
""";
"""
changeLog += """
<p>Version 213.4.3:</p>
<ul data-version="213.4.3">
Expand All @@ -166,7 +169,7 @@ tasks {
<li>Implement #42: Renaming a schema or field now adds an alias for the old name</li>
<li>Add new settings for #42, making the behaviour congigurable (by default, only fields receive an alias)</li>
</ul>
""";
"""
//changeLog += """
// <p>Version 213.3.1:</p>
// <ul data-version="213.3.1">
Expand All @@ -179,7 +182,7 @@ tasks {
// <li>Fix #37 (generating IDL can yield invalid names with Avro <= 1.11.0)</li>
// <li>Added error report submitter (submit crash reports directly to GitHub)</li>
// </ul>
// """;
// """
//changeLog += """
// <p>Version 213.2.1:</p>
// <ul data-version="213.2.1">
Expand All @@ -192,7 +195,7 @@ tasks {
// <li>Added support for Kotlin style nullable types (new in Avro 1.11.1)</li>
// <li>Added inspection for documentation comments to detect and apply fixes for improvements since Avro 1.11.1</li>
// </ul>
// """;
// """
//changeLog += """
// <p>Version 213.1.0:</p>
// <ul data-version="213.1.0">
Expand All @@ -202,7 +205,7 @@ tasks {
// <li>Improved brace handling</li>
// <li>Improved references to schemata in JSON (<code>.avsc</code>/<code>.avpr</code>)</li>
// </ul>
// """;
// """
//changeLog += """
// <p>Version 213.0.1:</p>
// <ul data-version="213.0.1">
Expand All @@ -217,7 +220,7 @@ tasks {
// <li>Improved quote & brace handling: now supports all "", {}, [] and <> pairs</li>
// <li>Using IntelliJ version 2021.3 to test</li>
// </ul>
// """;
// """
//changeLog += """
// <p>Version 203.1.2:</p>
// <ul data-version="203.1.2">
Expand All @@ -239,7 +242,7 @@ tasks {
// <li>Adjusted IDL parsing to allow dangling doc comments</li>
// <li>Added warnings for dangling doc comments (the Avro IDL compiler ignores these)</li>
// </ul>
// """;
// """
//changeLog += """
// <p>Version 203.0.3:</p>
// <ul data-version="203.0.3">
Expand All @@ -261,7 +264,7 @@ tasks {
// <li>Added refactoring actions to convert Avro IDL to and from Avro schemas and Avro protocols.</li>
// <li>Added file icon variant for dark mode</li>
// </ul>
// """;
// """
//changeLog += """
// <p>Version 0.2.1:</p>
// <ul data-version="0.2.1">
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/opwvhk/intellij/avro_idl/actions/IdlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public final class IdlUtils {
"idl", "import", "int", "local_timestamp_ms", "long", "map", "namespace", "null", "oneway", "protocol",
"record", "schema", "string", "throws",
"timestamp_ms", "time_ms", "true", "union", "uuid", "void");
private static final EnumSet<Schema.Type> NULLABLE_TYPES = EnumSet.complementOf(
EnumSet.of(Schema.Type.ARRAY, Schema.Type.MAP, Schema.Type.UNION));

static {
SCHEMA_FACTORY = getFieldValue(getField(Schema.class, "FACTORY"), null);
Expand Down Expand Up @@ -337,13 +339,13 @@ private static void writeFieldSchema(Schema schema, Writer writer, JsonGenerator
writer.append('>');
} else if (type == Schema.Type.UNION) {
// Note: unions cannot have properties
List<Schema> types = schema.getTypes();
if (schema.isNullable() && types.size() == 2) {
Schema nonNullSchema = !types.get(0).isNullable() ? types.get(0) : types.get(1);
writeFieldSchema(nonNullSchema, writer, jsonGen, alreadyDeclared, toDeclare, recordNameSpace);
Schema schemaForNullableSyntax = getNullableUnionType(schema);
if (schemaForNullableSyntax != null) {
writeFieldSchema(schemaForNullableSyntax, writer, jsonGen, alreadyDeclared, toDeclare, recordNameSpace);
writer.append('?');
} else {
writer.append("union{");
List<Schema> types = schema.getTypes();
Iterator<Schema> iterator = types.iterator();
if (iterator.hasNext()) {
writeFieldSchema(iterator.next(), writer, jsonGen, alreadyDeclared, toDeclare, recordNameSpace);
Expand Down Expand Up @@ -387,6 +389,23 @@ private static void writeFieldSchema(Schema schema, Writer writer, JsonGenerator
}
}

/**
* Get the type from a nullable 2-type union if that type is eligible for the '?'-syntax.
*
* @param unionSchema a union schema
* @return the non-null schema in a nullable 2-type union iff not a container
*/
private static Schema getNullableUnionType(Schema unionSchema) {
List<Schema> types = unionSchema.getTypes();
if (unionSchema.isNullable() && types.size() == 2) {
Schema nonNullSchema = !types.get(0).isNullable() ? types.get(0) : types.get(1);
if (NULLABLE_TYPES.contains(nonNullSchema.getType())) {
return nonNullSchema;
}
}
return null;
}

private static void writeSchemaAttributes(Schema schema, Writer writer, JsonGenerator jsonGen) throws IOException {
writeDocumentation(writer, " ", schema.getDoc());
writeJsonProperties(schema, writer, jsonGen, " ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ protocol HappyFlow {
@my-key("my-value") map<common.Flag> @order("DESCENDING") flags;
Counter mainCounter;
/** A list of counters. */
@my-key("my-value") array<Counter> otherCounters = [];
union{null, @my-key("my-value") array<Counter>} otherCounters = null;
Nonce nonce;
date my_date;
time_ms my_time;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ protocol HappyFlow {
@my-key("my-value") map<common.Flag> @order("DESCENDING") flags;
Counter mainCounter;
/** A list of counters. */
@my-key("my-value") array<Counter> otherCounters = [];
union{null, @my-key("my-value") array<Counter>} otherCounters = null;
Nonce nonce;
date my_date;
time_ms my_time;
Expand Down

0 comments on commit 9e25ca4

Please sign in to comment.