From 969f32ef9d7f252af425a77f94858c727a4a0986 Mon Sep 17 00:00:00 2001 From: Pavan Kumar Sunkara Date: Fri, 6 Feb 2015 11:52:23 +0530 Subject: [PATCH] Finished Mixins rule --- src/drafter.coffee | 2 +- src/rules/mson-inheritance.coffee | 23 +--- src/rules/mson-member-type-name.coffee | 6 +- src/rules/mson-mixin.coffee | 51 +++++++- src/rules/rule.coffee | 14 +++ test/fixtures/dataStructures.ast.json | 157 ++++++++++++++++++++----- 6 files changed, 204 insertions(+), 49 deletions(-) create mode 100644 src/rules/rule.coffee diff --git a/src/drafter.coffee b/src/drafter.coffee index 4afd3b7..5e4b296 100644 --- a/src/drafter.coffee +++ b/src/drafter.coffee @@ -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 = {} diff --git a/src/rules/mson-inheritance.coffee b/src/rules/mson-inheritance.coffee index 7fd5645..8a524a2 100644 --- a/src/rules/mson-inheritance.coffee +++ b/src/rules/mson-inheritance.coffee @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/rules/mson-member-type-name.coffee b/src/rules/mson-member-type-name.coffee index 8829f3f..23c1ba9 100644 --- a/src/rules/mson-member-type-name.coffee +++ b/src/rules/mson-member-type-name.coffee @@ -1,3 +1,7 @@ module.exports = - dataStructures: (dataStructures) -> + # Variables + dataStructures: {} + + init: (dataStructures) -> + @dataStructures = dataStructures diff --git a/src/rules/mson-mixin.coffee b/src/rules/mson-mixin.coffee index 8829f3f..ea1be3e 100644 --- a/src/rules/mson-mixin.coffee +++ b/src/rules/mson-mixin.coffee @@ -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 diff --git a/src/rules/rule.coffee b/src/rules/rule.coffee new file mode 100644 index 0000000..38d4b22 --- /dev/null +++ b/src/rules/rule.coffee @@ -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 diff --git a/test/fixtures/dataStructures.ast.json b/test/fixtures/dataStructures.ast.json index 76d6ac9..910534c 100644 --- a/test/fixtures/dataStructures.ast.json +++ b/test/fixtures/dataStructures.ast.json @@ -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" } ] } @@ -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" } ] } @@ -339,7 +405,6 @@ "content": "A clone of Coupon Base to be used for testing\n\n" }, { - "class": "memberType", "content": [ { "content": { @@ -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" } ] },