Skip to content

Commit

Permalink
feat: possible to refer type in another schemata
Browse files Browse the repository at this point in the history
This is only possible if the referent is parsed before the referrer (i.e. if A refer to B, B has to be parsed first). This may be fixed in the future
  • Loading branch information
mikaello committed Jun 11, 2021
1 parent 9e10337 commit 3b3dc94
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
22 changes: 19 additions & 3 deletions public/js/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,28 @@ AvroDoc.Schema = function (avrodoc, shared_types, schema_json, filename) {
if (primitive_types.includes(name)) {
return decorate({ type: name });
}
var type = named_types[qualifiedName(name, namespace)];

const qualifiedNameStr = qualifiedName(name, namespace);
const type = named_types[qualifiedNameStr];
if (type) {
return type;
} else {
throw "Unknown type name " + JSON.stringify(name) + " at " + path;
} else if (hasOwnPropertyS(shared_types, qualifiedNameStr)) {
const sharedType = shared_types[qualifiedNameStr].find(
(sharedSchema) => sharedSchema.qualified_name === name
);

if (sharedType) {
return sharedType;
} else {
// TODO: Should also support arbitrary ordering of Avro schemas (currently
// this only works if the schema to be referred is parsed first)
throw `Shared schema ${qualifiedNameStr} does not have type ${JSON.stringify(
name
)}, referred to at ${path}`;
}
}

throw "Unknown type name " + JSON.stringify(name) + " at " + path;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions schemata/reference_referent.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "ToBeReferred",
"type": "record",
"namespace": "com.example_referent",
"doc": "A simple object provided as a building block",
"fields": [
{
"name": "value",
"type": "boolean",
"doc": "The value of the object to be referenced"
},
{
"name": "history",
"type": "string",
"doc": "A history field detailing random information about the object"
}
]
}
18 changes: 18 additions & 0 deletions schemata/reference_referrer.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "TheReferrer",
"doc": "A complex object referencing object from another schemata",
"namespace": "com.example_referrer",
"type": "record",
"fields": [
{
"name": "name",
"type": "string",
"doc": "the name of the referrer object"
},
{
"name": "externalObject",
"type": "com.example_referent.ToBeReferred",
"doc": "An object from another schemata"
}
]
}

0 comments on commit 3b3dc94

Please sign in to comment.