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

Create proper canonical schema for logical types #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions canonical.go
Expand Up @@ -90,10 +90,24 @@ func pcfObject(jsonMap map[string]interface{}, parentNamespace string, typeLooku

for k, v := range jsonMap {

// Reduce primitive schemas to their simple form.
if len(jsonMap) == 1 && k == "type" {
if t, ok := v.(string); ok {
return "\"" + t + "\"", nil
for k, v := range jsonMap {

if k == "type" {
// Reduce primitive schemas to their simple form.
if len(jsonMap) == 1 {
if t, ok := v.(string); ok {
return "\"" + t + "\"", nil
}
} else {
// Reduce logical types that annotate Avro primitive types
if k == "type" {
if _, ok := jsonMap["logicalType"]; ok {
if annotatedType, ok := jsonMap["type"]; ok {
return "\"" + annotatedType.(string) + "\"", nil
}
}
}
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions canonical_test.go
Expand Up @@ -184,6 +184,20 @@ func TestCanonicalSchema(t *testing.T) {
Canonical: `{"name":"foo","type":"fixed","size":15}`,
},

// [LOGICAL TYPES] Simplify logical types that annotate primitive types
{
Schema: `{"type":"long","logicalType":"timestamp-millis"}`,
Canonical: `"long"`,
},
{
Schema: `{"type":"fixed", "logicalType":"custom-md5", "size":16, "name":"md5"}`,
Canonical: `{"name":"md5","type":"fixed","size":16}`,
},
{
Schema: `{"name":"logicalRecord", "type":"record", "logicalType":"logicalRecord", "fields":[{"name":"value", "type":"long"}]}`,
Canonical: `{"name":"logicalRecord","type":"record","fields":[{"name":"value","type":"long"}]}`,
},

// [STRINGS] For all JSON string literals in the schema text, replace
// any escaped characters (e.g., \uXXXX escapes) with their UTF-8
// equivalents.
Expand Down