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

Commit

Permalink
Finished Mixins rule
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Feb 9, 2015
1 parent a34820b commit 969f32e
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 49 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']
ruleList = ['mson-inheritance', 'mson-mixin']
rules = (require './rules/' + rule for rule in ruleList)

@dataStructures = {}
Expand Down
23 changes: 6 additions & 17 deletions src/rules/mson-inheritance.coffee
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
rule = require './rule'

module.exports =

# Variables
expanded: {}
dataStructures: {}

# Copy all member types from one data structure to another
#
# @param supertTypeName [String] The name of the super type data structure
# @param memberTypeSection [Object] Member Type Section to be copied into
copyMembers: (superTypeName, memberTypeSection) ->
return if not @dataStructures[superTypeName]

for section in @dataStructures[superTypeName].sections
if section['class'] is 'memberType'

for member in section.content
memberTypeSection.content.push member if member['class'] in ['property', 'mixin']

# Given a data structure, expand it's inheritance recursively
# Given a data structure, expand its inheritance recursively
#
# @param name [String] Name of the data structure
# @param dataStructure [Object] Data structure
Expand All @@ -31,7 +20,7 @@ module.exports =
return @expanded[superType] = true

# Expand the super type first
@expandInheritance superType, @dataStructures[superType.literal]
@expandInheritance superType.literal, @dataStructures[superType.literal]

# If super type is not an object or array or enum
superTypeBaseName = @dataStructures[superType.literal].typeDefinition.typeSpecification.name
Expand All @@ -42,7 +31,7 @@ module.exports =
content: []

memberTypeSection['class'] = 'memberType'
@copyMembers superType.literal, memberTypeSection
rule.copyMembers @dataStructures[superType.literal], memberTypeSection

dataStructure.sections.push memberTypeSection if memberTypeSection.content.length
return @expanded[name] = true
Expand All @@ -64,7 +53,7 @@ module.exports =

# Copy super-type and all the member types to sub type
dataStructure.typeDefinition.typeSpecification.name = superTypeBaseName
@copyMembers superType.literal, memberTypeSection
rule.copyMembers @dataStructures[superType.literal], memberTypeSection

# Push the created type section
dataStructure.sections.push memberTypeSection if push and memberTypeSection.content.length
Expand Down
6 changes: 5 additions & 1 deletion src/rules/mson-member-type-name.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module.exports =

dataStructures: (dataStructures) ->
# Variables
dataStructures: {}

init: (dataStructures) ->
@dataStructures = dataStructures
51 changes: 50 additions & 1 deletion src/rules/mson-mixin.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
rule = require './rule'

module.exports =

dataStructures: (dataStructures) ->
# Variables
expanded: {}
dataStructures: {}

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

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

# 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
14 changes: 14 additions & 0 deletions src/rules/rule.coffee
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
157 changes: 128 additions & 29 deletions test/fixtures/dataStructures.ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,49 @@
},
{
"content": {
"attributes": [],
"typeSpecification": {
"name": {
"literal": "Legacy Timestamps",
"variable": false
},
"nestedTypes": []
}
"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": "mixin"
"class": "property"
}
]
}
Expand Down Expand Up @@ -305,16 +338,49 @@
},
{
"content": {
"attributes": [],
"typeSpecification": {
"name": {
"literal": "Legacy Timestamps",
"variable": false
},
"nestedTypes": []
}
"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": "mixin"
"class": "property"
}
]
}
Expand All @@ -339,7 +405,6 @@
"content": "A clone of Coupon Base to be used for testing\n\n"
},
{
"class": "memberType",
"content": [
{
"content": {
Expand Down Expand Up @@ -439,18 +504,52 @@
},
{
"content": {
"attributes": [],
"typeSpecification": {
"name": {
"literal": "Legacy Timestamps",
"variable": false
},
"nestedTypes": []
}
"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": "mixin"
"class": "property"
}
]
],
"class": "memberType"
}
]
},
Expand Down

0 comments on commit 969f32e

Please sign in to comment.