Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
Dive into oneOf and groups when expanding mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Feb 9, 2015
1 parent 969f32e commit b4505ef
Show file tree
Hide file tree
Showing 5 changed files with 329 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/drafter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Drafter
protagonist.parse source, @config, (error, result) =>
callback error if error

ruleList = ['mson-inheritance', 'mson-mixin']
ruleList = ['mson-inheritance', 'mson-mixin', 'mson-member-type-name']
rules = (require './rules/' + rule for rule in ruleList)

@dataStructures = {}
Expand Down
22 changes: 22 additions & 0 deletions src/rules/mson-member-type-name.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
rule = require './rule'

module.exports =

# Variables
expanded: {}
dataStructures: {}

# 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]

# 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
41 changes: 30 additions & 11 deletions src/rules/mson-mixin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@ module.exports =
expanded: {}
dataStructures: {}

#
#
# @param elements [Object]
# @param sectionOrMember [Object]
diveIntoMember: (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
@diveIntoMember 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
Expand All @@ -21,17 +50,7 @@ module.exports =
memberTypeSection =
content: []

for member in section.content
if member['class'] is 'mixin'

# Expand the super type first
superType = member.content.typeSpecification.name
@expandMixin superType.literal, @dataStructures[superType.literal]

rule.copyMembers @dataStructures[superType.literal], memberTypeSection

else
memberTypeSection.content.push member
@diveIntoMember section.content, memberTypeSection

# Replace section content with the new content
section.content = memberTypeSection.content
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/dataStructures.apib
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ A clone of Coupon Base to be used for testing

## Timestamp Clone (Timestamp)
A clone of timestamp to be used for testing

## Subscription
+ id: 1000 (number)
+ plan: 'Startup' (string) - Subscription plan name
+ coupon (Coupon Base) - Coupon represting any existing discounts used by the customer during the subscription
+ One Of
+ Include Timestamps
+ Properties
+ modified_at (Timestamp)
+ Include (Legacy Timestamps)
267 changes: 266 additions & 1 deletion test/fixtures/dataStructures.ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,272 @@
"sections": [
{
"class": "blockDescription",
"content": "A clone of timestamp to be used for testing\n"
"content": "A clone of timestamp to be used for testing\n\n"
}
]
},
{
"element": "dataStructure",
"name": {
"literal": "Subscription",
"variable": false
},
"typeDefinition": {
"typeSpecification": {
"name": null,
"nestedTypes": []
},
"attributes": []
},
"sections": [
{
"class": "memberType",
"content": [
{
"content": {
"name": {
"literal": "id"
},
"description": "",
"valueDefinition": {
"values": [
{
"literal": "1000",
"variable": false
}
],
"typeDefinition": {
"typeSpecification": {
"name": "number",
"nestedTypes": []
},
"attributes": []
}
},
"sections": []
},
"class": "property"
},
{
"content": {
"name": {
"literal": "plan"
},
"description": "Subscription plan name",
"valueDefinition": {
"values": [
{
"literal": "'Startup'",
"variable": false
}
],
"typeDefinition": {
"typeSpecification": {
"name": "string",
"nestedTypes": []
},
"attributes": []
}
},
"sections": []
},
"class": "property"
},
{
"content": {
"name": {
"literal": "coupon"
},
"description": "Coupon represting any existing discounts used by the customer during the subscription",
"valueDefinition": {
"values": [],
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Coupon Base",
"variable": false
},
"nestedTypes": []
},
"attributes": []
}
},
"sections": []
},
"class": "property"
},
{
"content": [
{
"content": {
"name": {
"literal": "created"
},
"description": "",
"valueDefinition": {
"values": [],
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Timestamp",
"variable": false
},
"nestedTypes": []
},
"attributes": []
}
},
"sections": []
},
"class": "property"
},
{
"content": {
"name": {
"literal": "modified"
},
"description": "",
"valueDefinition": {
"values": [],
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Timestamp",
"variable": false
},
"nestedTypes": []
},
"attributes": []
}
},
"sections": []
},
"class": "property"
},
{
"content": {
"name": {
"literal": "created_at"
},
"description": "",
"valueDefinition": {
"values": [],
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Timestamp",
"variable": false
},
"nestedTypes": []
},
"attributes": []
}
},
"sections": []
},
"class": "property"
},
{
"content": {
"name": {
"literal": "updated_at"
},
"description": "",
"valueDefinition": {
"values": [],
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Timestamp",
"variable": false
},
"nestedTypes": []
},
"attributes": []
}
},
"sections": []
},
"class": "property"
},
{
"content": [
{
"content": {
"name": {
"literal": "modified_at"
},
"description": "",
"valueDefinition": {
"values": [],
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Timestamp",
"variable": false
},
"nestedTypes": []
},
"attributes": []
}
},
"sections": []
},
"class": "property"
},
{
"content": {
"name": {
"literal": "created_at"
},
"description": "",
"valueDefinition": {
"values": [],
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Timestamp",
"variable": false
},
"nestedTypes": []
},
"attributes": []
}
},
"sections": []
},
"class": "property"
},
{
"content": {
"name": {
"literal": "updated_at"
},
"description": "",
"valueDefinition": {
"values": [],
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Timestamp",
"variable": false
},
"nestedTypes": []
},
"attributes": []
}
},
"sections": []
},
"class": "property"
}
],
"class": "group"
}
],
"class": "oneOf"
}
]
}
]
}
Expand Down

0 comments on commit b4505ef

Please sign in to comment.