Skip to content

Commit

Permalink
feat(markdown): generate type details
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Dec 10, 2019
1 parent fa34cf1 commit c9f19e1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/formatInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function gettype(schema) {
if (schema.type === undefined) {
return `undefined`;
}
if (schema.oneOf || schema.anyOf || schema.allOf || schema.not) {
if (!!(schema.oneOf || schema.anyOf || schema.allOf || schema.not)) {
return 'merged';
}
console.log('Unknown type', schema.type);
Expand Down
54 changes: 51 additions & 3 deletions lib/markdownBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ function build({ header, links = {} }) {
function type(property) {
const types = Array.isArray(property.type) ? property.type : [property.type];
const realtypes = flist(filter(types, type => type !== 'null' && type !== undefined));
if (size(realtypes) === 0) {
if (!!(property.allOf || property.anyOf || property.oneOf || property.not)) {
return text(i18n`Merged`);
} else if (size(realtypes) === 0) {
return text(i18n`Not specified`);
} else if (size(realtypes) === 1) {
const [realtype] = realtypes;
Expand Down Expand Up @@ -199,8 +201,53 @@ function build({ header, links = {} }) {
]);
}

function maketypefact(definition) {
return listItem([text(i18n`Type:`)]);
function maketypefact(definition, isarray = '') {
const alltypes = Array.isArray(definition.type) ? definition.type : [definition.type];
// filter out types that are null
const realtypes = alltypes.filter(type => type!=='null');
// can the type be `null`
const nullable = alltypes.filter(type => type === 'null').length > 0;
// is there only a single type or can there be multiple types
const singletype = realtypes.length <= 1;
const [ firsttype ] = realtypes;
// is `null` the only allowed value
const nulltype = nullable && realtypes.length === 0;

const array = firsttype === 'array';
const merged = !!(definition.allOf || definition.anyOf || definition.oneOf ||definition.not);

if (array && definition.items) {
console.log('an array!');
return maketypefact(definition.items, isarray + '[]');
}

const typefact = (() => {
if (nulltype) {
return [inlineCode('null' + isarray), text(i18n`, the value must be null`)]
} else if (singletype && firsttype) {
return [inlineCode(firsttype + isarray)];
} else if (!singletype) {
return [text(isarray ? i18n`an array of the following:` : i18n`any of the folllowing: `), ...flist(flat(realtypes.map((type, index) => {
return [inlineCode(type || i18n`not defined`), text(index === realtypes.length -1 ? '' : i18n` or `)];
})))]
} else if (merged) {
return [text(isarray ? `an array of merged types` : i18n`merged type`)];
}
console.log('unknown type', realtypes, singletype, merged, definition[s.pointer]);
return [text(i18n`unknown` + isarray)];
})();

const typelink = (() => {
if (definition.title) {
// if the type has a title, always create a link to the schema
return [text(' ('), link(`${definition[s.slug]}.md`, '', text(definition.title)), text(')')];
} else if (!singletype || firsttype === 'object' || merged) {
return [text(' ('), link(`${definition[s.slug]}.md`, '', text(i18n`Details`)), text(')')];
}
return [];
})();
const retval = listItem(paragraph([text(i18n`Type: `), ...typefact, ...typelink]));
return retval;
}

function makefactlist(name, definition, required = []) {
Expand Down Expand Up @@ -269,6 +316,7 @@ function build({ header, links = {} }) {
return [
heading(1, text(i18n`${schema.title} Properties`)),
makeproptable(schema.properties, schema.required, slugger),
...makeproplist(schema.properties, schema.required, 1)
];
}
return [];
Expand Down

0 comments on commit c9f19e1

Please sign in to comment.