This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from apiaryio/pksunkara/start-expansion
Finished expanding MSON
- Loading branch information
Showing
9 changed files
with
3,281 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
"dependencies": { | ||
"coffee-script": "~1.7.1", | ||
"yargs": "~1.3.3", | ||
"protagonist-experimental": "0.18.4", | ||
"protagonist-experimental": "0.18.6", | ||
"boutique": "git+ssh://[email protected]:apiaryio/boutique.git" | ||
}, | ||
"devDependencies": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
rule = require './rule' | ||
|
||
module.exports = | ||
|
||
# Variables | ||
expanded: {} | ||
dataStructures: {} | ||
|
||
# Expand dataStructure element | ||
dataStructure: (element) -> | ||
superType = element.typeDefinition.name | ||
typeName = element.name | ||
|
||
if not typeName | ||
typeName = | ||
literal: '' | ||
|
||
@expandInheritance typeName.literal, element | ||
delete @expanded[''] | ||
|
||
# Given a data structure, expand its inheritance recursively | ||
# | ||
# @param name [String] Name of the data structure | ||
# @param dataStructure [Object] Data structure | ||
expandInheritance: (name, dataStructure) -> | ||
return if @expanded[name] | ||
|
||
# Check for inheritance | ||
superType = dataStructure.typeDefinition.typeSpecification.name | ||
|
||
if superType is null or typeof superType isnt 'object' or not superType?.literal | ||
return @expanded[superType] = true | ||
|
||
# Expand the super type first | ||
@expandInheritance superType.literal, @dataStructures[superType.literal] | ||
|
||
# If super type is not an object or array or enum | ||
superTypeBaseName = @dataStructures[superType.literal].typeDefinition.typeSpecification.name | ||
|
||
if superTypeBaseName not in ['object', 'array', 'value'] | ||
dataStructure.typeDefinition.typeSpecification.name = superTypeBaseName | ||
memberTypeSection = | ||
content: [] | ||
|
||
memberTypeSection['class'] = 'memberType' | ||
rule.copyMembers @dataStructures[superType.literal], memberTypeSection | ||
|
||
dataStructure.sections.push memberTypeSection if memberTypeSection.content.length | ||
return @expanded[name] = true | ||
|
||
# Find member type section of the current data structure | ||
memberTypeSection = null | ||
push = false | ||
|
||
for section in dataStructure.sections | ||
memberTypeSection = section if section['class'] is 'memberType' | ||
|
||
# If no member type sections, create one | ||
if not memberTypeSection | ||
memberTypeSection = | ||
content: [] | ||
|
||
memberTypeSection['class'] = 'memberType' | ||
push = true | ||
|
||
# Copy super-type and all the member types to sub type | ||
rule.copyMembers @dataStructures[superType.literal], memberTypeSection | ||
dataStructure.typeDefinition.typeSpecification = | ||
name: superTypeBaseName | ||
nestedTypes: @dataStructures[superType.literal].typeDefinition.typeSpecification.nestedTypes | ||
|
||
# Push the created type section | ||
dataStructure.sections.push memberTypeSection if push and memberTypeSection.content.length | ||
|
||
# Denote this type as expanded | ||
@expanded[name] = true | ||
|
||
init: (dataStructures) -> | ||
@expanded = {} | ||
@dataStructures = dataStructures | ||
|
||
# Initiate flags | ||
for name, dataStructure of @dataStructures | ||
@expanded[name] = false | ||
|
||
# Actual expansion | ||
for name, dataStructure of @dataStructures | ||
@expandInheritance name, dataStructure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
rule = require './rule' | ||
|
||
module.exports = | ||
|
||
# Variables | ||
expanded: {} | ||
dataStructures: {} | ||
|
||
# Expand dataStructure element | ||
dataStructure: (element) -> | ||
superType = element.typeDefinition.name | ||
typeName = element.name | ||
|
||
if not typeName | ||
typeName = | ||
literal: '' | ||
|
||
@expandMember typeName.literal, element | ||
delete @expanded[''] | ||
|
||
# Given a list of elements, recursively expand member type name contained | ||
# in a group of elements inside the initial group of elements | ||
# | ||
# @param elements [Object] List of elements either from type section or a member type | ||
diveIntoElements: (elements) -> | ||
for member in elements | ||
switch member['class'] | ||
|
||
when 'property', 'value' | ||
superType = member.content.valueDefinition.typeDefinition.typeSpecification.name | ||
|
||
# If super type is a valid symbol | ||
if typeof superType is 'object' and superType?.literal | ||
@expandMember superType.literal, @dataStructures[superType.literal] | ||
|
||
superTypeBaseName = @dataStructures[superType.literal].typeDefinition.typeSpecification.name | ||
member.content.valueDefinition.typeDefinition.typeSpecification.name = superTypeBaseName | ||
|
||
# If super type is not an object or array or enum | ||
if superTypeBaseName in ['object', 'array', 'value'] | ||
memberTypeSection = | ||
content: [] | ||
|
||
memberTypeSection['class'] = 'memberType' | ||
rule.copyMembers @dataStructures[superType.literal], memberTypeSection | ||
member.content.sections.push memberTypeSection if memberTypeSection.content.length | ||
|
||
when 'oneOf', 'group' | ||
@diveIntoElements member.content | ||
|
||
# Given a data structure, expand its member type recusrively | ||
# | ||
# @param name [String] Name of the data structure | ||
# @param dataStructure [Object] Data structure | ||
expandMember: (name, dataStructure) -> | ||
return if @expanded[name] | ||
|
||
# Check for member type name | ||
for section in dataStructure.sections | ||
if section['class'] is 'memberType' | ||
|
||
@diveIntoElements section.content | ||
|
||
# Denote this type as expanded | ||
@expanded[name] = true | ||
|
||
init: (dataStructures) -> | ||
@expanded = {} | ||
@dataStructures = dataStructures | ||
|
||
# Initiate flags | ||
for name, dataStructure of @dataStructures | ||
@expanded[name] = false | ||
|
||
# Actual expansion | ||
for name, dataStructure of @dataStructures | ||
@expandMember name, dataStructure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
rule = require './rule' | ||
|
||
module.exports = | ||
|
||
# Variables | ||
expanded: {} | ||
dataStructures: {} | ||
|
||
# Expand dataStructure element | ||
dataStructure: (element) -> | ||
superType = element.typeDefinition.name | ||
typeName = element.name | ||
|
||
if not typeName | ||
typeName = | ||
literal: '' | ||
|
||
@expandMixin typeName.literal, element | ||
delete @expanded[''] | ||
|
||
# Given a list of elements, recursively expand mixins contained | ||
# in a group of elements inside the initial group of elements | ||
# | ||
# @param elements [Object] List of elements either from type section or a member type | ||
# @param sectionOrMember [Object] Type section or a member type | ||
diveIntoElements: (elements, sectionOrMember) -> | ||
for member in elements | ||
switch member['class'] | ||
|
||
when 'mixin' | ||
superType = member.content.typeSpecification.name | ||
|
||
# Expand the super type first | ||
@expandMixin superType.literal, @dataStructures[superType.literal] | ||
rule.copyMembers @dataStructures[superType.literal], sectionOrMember | ||
|
||
when 'oneOf', 'group' | ||
memberType = | ||
content: [] | ||
|
||
# Recursively dive into the elements | ||
@diveIntoElements member.content, memberType | ||
|
||
# Replace the original member with out new member | ||
member.content = memberType.content | ||
sectionOrMember.content.push member | ||
|
||
else | ||
sectionOrMember.content.push member | ||
|
||
# Given a data structure, expand its mixins recusrively | ||
# | ||
# @param name [String] Name of the data structure | ||
# @param dataStructure [Object] Data structure | ||
expandMixin: (name, dataStructure) -> | ||
return if @expanded[name] | ||
|
||
# Check for mixin | ||
for section in dataStructure.sections | ||
if section['class'] is 'memberType' | ||
|
||
# New content for the section | ||
memberTypeSection = | ||
content: [] | ||
|
||
@diveIntoElements section.content, memberTypeSection | ||
|
||
# Replace section content with the new content | ||
section.content = memberTypeSection.content | ||
|
||
# Denote this type as expanded | ||
@expanded[name] = true | ||
|
||
init: (dataStructures) -> | ||
@expanded = {} | ||
@dataStructures = dataStructures | ||
|
||
# Initiate flags | ||
for name, dataStructure of @dataStructures | ||
@expanded[name] = false | ||
|
||
# Actual expansion | ||
for name, dataStructure of @dataStructures | ||
@expandMixin name, dataStructure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = | ||
|
||
# Copy all member types from one data structure to another | ||
# | ||
# @param dataStructure [Object] The super type data structure | ||
# @param memberTypeSection [Object] Member Type Section to be copied into | ||
copyMembers: (dataStructure, memberTypeSection) -> | ||
return if not dataStructure | ||
|
||
for section in dataStructure.sections | ||
if section['class'] is 'memberType' | ||
|
||
for member in section.content | ||
memberTypeSection.content.push member |
Oops, something went wrong.