Skip to content

Commit

Permalink
feat(@angular-devkit/core): move findTypes in utility and export
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl authored and alexeagle committed Sep 6, 2018
1 parent 4a25d7c commit 0b84c1d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 50 deletions.
54 changes: 4 additions & 50 deletions packages/angular_devkit/core/src/json/schema/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,7 @@
*/
import { JsonObject, JsonValue, isJsonObject } from '../interface';
import { JsonPointer } from './interface';

const allTypes = ['string', 'integer', 'number', 'object', 'array', 'boolean', 'null'];

function findTypes(schema: JsonObject): Set<string> {
if (!schema) {
return new Set();
}

let potentials: Set<string>;
if (typeof schema.type === 'string') {
potentials = new Set([schema.type]);
} else if (Array.isArray(schema.type)) {
potentials = new Set(schema.type as string[]);
} else {
potentials = new Set(allTypes);
}

if (isJsonObject(schema.not)) {
const notTypes = findTypes(schema.not);
potentials = new Set([...potentials].filter(p => !notTypes.has(p)));
}

if (Array.isArray(schema.allOf)) {
for (const sub of schema.allOf) {
const types = findTypes(sub as JsonObject);
potentials = new Set([...potentials].filter(p => types.has(p)));
}
}

if (Array.isArray(schema.oneOf)) {
let options = new Set<string>();
for (const sub of schema.oneOf) {
const types = findTypes(sub as JsonObject);
options = new Set([...options, ...types]);
}
potentials = new Set([...potentials].filter(p => options.has(p)));
}

if (Array.isArray(schema.anyOf)) {
let options = new Set<string>();
for (const sub of schema.anyOf) {
const types = findTypes(sub as JsonObject);
options = new Set([...options, ...types]);
}
potentials = new Set([...potentials].filter(p => options.has(p)));
}

return potentials;
}
import { getTypesOfSchema } from './utility';

export function addUndefinedDefaults(
value: JsonValue,
Expand All @@ -66,7 +18,7 @@ export function addUndefinedDefaults(
return value;
}

const types = findTypes(schema);
const types = getTypesOfSchema(schema);
if (types.size === 0) {
return value;
}
Expand Down Expand Up @@ -110,6 +62,8 @@ export function addUndefinedDefaults(
for (const propName of Object.getOwnPropertyNames(schema.properties)) {
if (propName in newValue) {
continue;
} else if (propName == '$schema') {
continue;
}

// TODO: Does not currently handle more complex schemas (oneOf/anyOf/etc.)
Expand Down
67 changes: 67 additions & 0 deletions packages/angular_devkit/core/src/json/schema/utility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { JsonObject, isJsonObject } from '../interface';


const allTypes = ['string', 'integer', 'number', 'object', 'array', 'boolean', 'null'];

export function getTypesOfSchema(schema: JsonObject | true): Set<string> {
if (!schema) {
return new Set();
}
if (schema === true) {
return new Set(allTypes);
}

let potentials: Set<string>;
if (typeof schema.type === 'string') {
potentials = new Set([schema.type]);
} else if (Array.isArray(schema.type)) {
potentials = new Set(schema.type as string[]);
} else {
potentials = new Set(allTypes);
}

if (isJsonObject(schema.not)) {
const notTypes = getTypesOfSchema(schema.not);
potentials = new Set([...potentials].filter(p => !notTypes.has(p)));
}

if (Array.isArray(schema.allOf)) {
for (const sub of schema.allOf) {
const types = getTypesOfSchema(sub as JsonObject);
potentials = new Set([...potentials].filter(p => types.has(p)));
}
}

if (Array.isArray(schema.oneOf)) {
let options = new Set<string>();
for (const sub of schema.oneOf) {
const types = getTypesOfSchema(sub as JsonObject);
options = new Set([...options, ...types]);
}
potentials = new Set([...potentials].filter(p => options.has(p)));
}

if (Array.isArray(schema.anyOf)) {
let options = new Set<string>();
for (const sub of schema.anyOf) {
const types = getTypesOfSchema(sub as JsonObject);
options = new Set([...options, ...types]);
}
potentials = new Set([...potentials].filter(p => options.has(p)));
}

if (schema.properties) {
potentials.add('object');
} else if (schema.items) {
potentials.add('array');
}

return potentials;
}

0 comments on commit 0b84c1d

Please sign in to comment.