Skip to content

Instantly share code, notes, and snippets.

@homestar9
Last active November 30, 2022 20:04
Show Gist options
  • Save homestar9/346110b21dd7af27213e1a5fcd294377 to your computer and use it in GitHub Desktop.
Save homestar9/346110b21dd7af27213e1a5fcd294377 to your computer and use it in GitHub Desktop.
/**
* Parses API Document $extend notations recursively
*
* @param APIDoc The struct representation of the API Document
* @param [XPath] The XPath to zoom the parsed document to during recursion
**/
public function parseDocumentInheritance( required any DocItem ){
if( isArray( DocItem ) ) {
for( var i = 1; i <= arrayLen( DocItem ); i++){
DocItem[ i ] = parseDocumentInheritance( DocItem[ i ] );
}
} else if( isStruct( DocItem ) ) {
// $extend is a special key which enables merging json structs
var compositionKeys = [ "$extend" ];
for( var composition in compositionKeys ){
// handle top-level extension
if(
structKeyExists( DocItem, composition ) &&
isArray( DocItem[ composition ] )
) {
return extendObject( DocItem[ composition ] );
}
for( var key in DocItem){
// If `DocItem[ key ]` is an instance of Parser, we need to flattin it to a CFML struct
if (
isStruct( DocItem[ key ] ) &&
findNoCase( "Parser", getMetaData( DocItem[ key ] ).name )
) {
DocItem[ key ] = DocItem[ key ].getNormalizedDocument();
}
if (
isStruct( DocItem[ key ] ) &&
structKeyExists( DocItem[ key ], composition ) &&
isArray( DocItem[ key ][ composition ] )
) {
DocItem[ key ] = extendObject( DocItem[ key ][ composition ] );
} else if( isStruct( DocItem[ key ] ) || isArray( DocItem[ key ] ) ){
DocItem[ key ] = parseDocumentInheritance( DocItem[ key ] );
}
}
}
}
return DocItem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment