Skip to content

Commit

Permalink
AVRO-2254: to Idl module
Browse files Browse the repository at this point in the history
  • Loading branch information
clesaec committed Sep 18, 2023
1 parent 1bfa3d3 commit cd30eb9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -41,6 +42,8 @@ private SchemaResolver() {

private static final String UR_SCHEMA_NS = "org.apache.avro.compiler";

private static final AtomicInteger COUNTER = new AtomicInteger();

/**
* Create a schema to represent an "unresolved" schema. (used to represent a
* schema whose definition does not exist, yet).
Expand All @@ -49,8 +52,8 @@ private SchemaResolver() {
* @return an unresolved schema for the given name
*/
static Schema unresolvedSchema(final String name) {
Schema schema = Schema.createRecord(UR_SCHEMA_NAME, "unresolved schema", UR_SCHEMA_NS, false,
Collections.emptyList());
Schema schema = Schema.createRecord(UR_SCHEMA_NAME + '_' + COUNTER.getAndIncrement(), "unresolved schema",
UR_SCHEMA_NS, false, Collections.emptyList());
schema.addProp(UR_SCHEMA_ATTR, name);
return schema;
}
Expand All @@ -62,8 +65,8 @@ static Schema unresolvedSchema(final String name) {
* @return whether the schema is an unresolved schema
*/
static boolean isUnresolvedSchema(final Schema schema) {
return (schema.getType() == Schema.Type.RECORD && schema.getProp(UR_SCHEMA_ATTR) != null
&& UR_SCHEMA_NAME.equals(schema.getName()) && UR_SCHEMA_NS.equals(schema.getNamespace()));
return (schema.getType() == Schema.Type.RECORD && schema.getProp(UR_SCHEMA_ATTR) != null && schema.getName() != null
&& schema.getName().startsWith(UR_SCHEMA_NAME) && UR_SCHEMA_NS.equals(schema.getNamespace()));
}

/**
Expand Down
16 changes: 16 additions & 0 deletions lang/java/idl/src/test/idl/input/union.avdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@namespace("org.apache.avro.gen")
protocol UnionFwd {

record TestRecord {
union {SR1, SR2} unionField;
}

record SR1 {
string field;
}

record SR2 {
string field;
}

}
38 changes: 38 additions & 0 deletions lang/java/idl/src/test/idl/output/union.avpr
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"protocol": "UnionFwd",
"namespace": "org.apache.avro.gen",
"types": [
{
"type": "record",
"name": "TestRecord",
"fields": [
{
"name": "unionField",
"type": [
{
"type": "record",
"name": "SR1",
"fields": [
{
"name": "field",
"type": "string"
}
]
},
{
"type": "record",
"name": "SR2",
"fields": [
{
"name": "field",
"type": "string"
}
]
}
]
}
]
}
],
"messages": {}
}

0 comments on commit cd30eb9

Please sign in to comment.