import { schema } from '../lib' ; import { detectScrutinyTypes } from './scrutiny' ; export function massageSpec ( spec ) { detectScrutinyTypes ( spec ) ; replaceIncompleteTypes ( spec ) ; dropTypelessAttributes ( spec ) ; } /**
 * Fix incomplete type definitions in PropertyTypes
 *
 * Some user-defined types are defined to not have any properties, and not
 * be a collection of other types either. They have no definition at all.
 *
 * Add a property object type with empty properties.
 */ function replaceIncompleteTypes ( spec ) { for ( const [ name , definition ] of Object . entries ( spec . PropertyTypes ) ) { if ( ! schema . isRecordType ( definition ) && ! schema . isCollectionProperty ( definition ) && ! schema . isScalarProperty ( definition ) && ! schema . isPrimitiveProperty ( definition ) ) { console . log ( `[${ name }] Incomplete type, adding empty "Properties" field` ) ; ( definition as unknown as schema . RecordProperty ) . Properties = { } ; } } } /**
 * Drop Attributes specified with the different ResourceTypes that have
 * no type specified.
 */ function dropTypelessAttributes ( spec ) { const resourceTypes = spec . ResourceTypes ; Object . values ( resourceTypes ) . forEach ( ( resourceType ) => { const attributes = resourceType . Attributes ?? { } ; Object . keys ( attributes ) . forEach ( ( attrKey ) => { const attrVal = attributes [ attrKey ] ; if ( Object . keys ( attrVal ) . length === 0 ) { delete attributes [ attrKey ] ; } } ) ; } ) ; } param spec returns export function normalize ( spec ) { spec . ResourceTypes = normalizeSection ( spec . ResourceTypes ) ; if ( spec . PropertyTypes ) { spec . PropertyTypes = normalizeSection ( spec . PropertyTypes ) ; } return spec ; function normalizeSection < T > ( section ) { const result = { } ; for ( const key of Object . keys ( section ) . sort ( ) ) { result [ key ] = section [ key ] ; } return result ; } }