Skip to content

Commit

Permalink
generate-reader.uc: handle non-toplevel schema references
Browse files Browse the repository at this point in the history
 - Handle schema references in object schema types
 - Derive type from referenced sub schema if type property is missing
 - Avoid outputting trailing white space

Signed-off-by: Jo-Philipp Wich <[email protected]>
  • Loading branch information
jow- authored and blogic committed May 15, 2021
1 parent 6ba5208 commit 9973f51
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions generate-reader.uc
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,13 @@ let GeneratorProto = {

print: function(path, fmt, ...args)
{
for (let _ in path)
print("\t");
if (length(fmt)) {
for (let _ in path)
print("\t");

printf(fmt, ...args);
}

printf(fmt, ...args);
print("\n");
},

Expand Down Expand Up @@ -322,14 +325,21 @@ let GeneratorProto = {

emit_spec_validation_function: function(path, verb, propertyName, valueSpec)
{
let functionName = to_method_name(verb, propertyName);
let functionName = to_method_name(verb, propertyName),
isRef = this.is_ref(valueSpec);

this.print(path, 'function %s(value) {', functionName);

this.emit_spec_validation_asserts([...path, propertyName], 'value', valueSpec);

this.print(path, '');

/* Derive type from referenced subschema if possible */
if (!exists(valueSpec, 'type') && isRef) {
let def = this.schema['$defs'][replace(isRef, '#/$defs/', '')];
valueSpec.type = def ? def.type : null;
}

switch (valueSpec.type) {
case 'array':
let itemSpec = valueSpec.items,
Expand Down Expand Up @@ -360,10 +370,6 @@ let GeneratorProto = {
break;

case 'object':
/* XXX: not yet */
if (exists(valueSpec, "$ref"))
return;

if (type(valueSpec.propertyNames) == "object") {
let keySpec = { type: 'string', ...(valueSpec.propertyNames) };

Expand All @@ -373,10 +379,20 @@ let GeneratorProto = {
this.print(path, '');
}

if (valueSpec.additionalProperties === true)
this.print(path, ' let obj = { ...value };\n');
else
this.print(path, ' let obj = {};\n');
if (exists(valueSpec, "$ref")) {
let fn = to_method_name('instantiate', replace(valueSpec['$ref'], '#/$defs/', ''));

if (valueSpec.additionalProperties === true)
this.print(path, ' let obj = { ...value, ...(%s(value)) };\n', fn);
else
this.print(path, ' let obj = %s(value);\n', fn);
}
else {
if (valueSpec.additionalProperties === true)
this.print(path, ' let obj = { ...value };\n');
else
this.print(path, ' let obj = {};\n');
}

if (type(valueSpec.properties) == "object") {
for (let objectPropertyName, propertySpec in valueSpec.properties) {
Expand Down

0 comments on commit 9973f51

Please sign in to comment.