-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
129 additions
and
30 deletions.
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
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
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
43 changes: 43 additions & 0 deletions
43
avro/src/main/java/tools/jackson/dataformat/avro/schema/UUIDVisitor.java
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package tools.jackson.dataformat.avro.schema; | ||
|
||
import java.util.Set; | ||
|
||
import tools.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor; | ||
import tools.jackson.databind.jsonFormatVisitors.JsonValueFormat; | ||
|
||
import org.apache.avro.LogicalTypes; | ||
import org.apache.avro.Schema; | ||
|
||
/** | ||
* Visitor for {@link java.util.UUID} type. When it is created with logicalTypesEnabled enabled, | ||
* Avro schema is created with logical type uuid. | ||
* | ||
* @since 2.19 | ||
*/ | ||
public class UUIDVisitor extends JsonStringFormatVisitor.Base | ||
implements SchemaBuilder { | ||
protected boolean _logicalTypesEnabled = false; | ||
|
||
|
||
public UUIDVisitor(boolean logicalTypesEnabled) { | ||
_logicalTypesEnabled = logicalTypesEnabled; | ||
} | ||
|
||
@Override | ||
public void format(JsonValueFormat format) { | ||
// Ideally, we'd recognize UUIDs, Dates etc if need be, here... | ||
} | ||
|
||
@Override | ||
public void enumTypes(Set<String> enums) { | ||
// Do nothing | ||
} | ||
|
||
@Override | ||
public Schema builtAvroSchema() { | ||
// [dataformats-binary#179]: need special help with UUIDs, to coerce into Binary | ||
// (could actually be | ||
Schema schema = AvroSchemaHelper.createUUIDSchema(); | ||
return this._logicalTypesEnabled ? LogicalTypes.uuid().addToSchema(schema) : schema; | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
avro/src/test/java/tools/jackson/dataformat/avro/schema/UUIDVisitor_builtAvroSchemaTest.java
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package tools.jackson.dataformat.avro.schema; | ||
|
||
import org.junit.Test; | ||
|
||
import org.apache.avro.LogicalType; | ||
import org.apache.avro.Schema; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class UUIDVisitor_builtAvroSchemaTest { | ||
|
||
@Test | ||
public void testLogicalTypesDisabled() { | ||
// GIVEN | ||
boolean logicalTypesEnabled = false; | ||
UUIDVisitor uuidVisitor = new UUIDVisitor(logicalTypesEnabled); | ||
|
||
// WHEN | ||
Schema actualSchema = uuidVisitor.builtAvroSchema(); | ||
|
||
// THEN | ||
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.FIXED); | ||
assertThat(actualSchema.getFixedSize()).isEqualTo(16); | ||
assertThat(actualSchema.getName()).isEqualTo("UUID"); | ||
assertThat(actualSchema.getNamespace()).isEqualTo("java.util"); | ||
assertThat(actualSchema.getProp(LogicalType.LOGICAL_TYPE_PROP)).isNull(); | ||
} | ||
|
||
@Test | ||
public void testLogicalTypesEnabled() { | ||
// GIVEN | ||
boolean logicalTypesEnabled = true; | ||
UUIDVisitor uuidVisitor = new UUIDVisitor(logicalTypesEnabled); | ||
|
||
// WHEN | ||
Schema actualSchema = uuidVisitor.builtAvroSchema(); | ||
|
||
// THEN | ||
assertThat(actualSchema.getType()).isEqualTo(Schema.Type.FIXED); | ||
assertThat(actualSchema.getFixedSize()).isEqualTo(16); | ||
assertThat(actualSchema.getName()).isEqualTo("UUID"); | ||
assertThat(actualSchema.getNamespace()).isEqualTo("java.util"); | ||
assertThat(actualSchema.getProp(LogicalType.LOGICAL_TYPE_PROP)).isEqualTo("uuid"); | ||
} | ||
|
||
} |
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