Module: Lacerda::Conversion
- Defined in:
- lib/lacerda/conversion.rb,
lib/lacerda/conversion/error.rb,
lib/lacerda/conversion/data_structure.rb,
lib/lacerda/conversion/apiary_to_json_schema.rb,
lib/lacerda/conversion/data_structure/member.rb,
lib/lacerda/conversion/data_structure/member/type.rb
Defined Under Namespace
Classes: ApiaryToJsonSchema, DataStructure, Error
Class Method Summary collapse
- .ast_parsing_annotation_messages(elements, type) ⇒ Object
-
.data_structures_from_blueprint_ast(filename) ⇒ Object
The structure is of an AST is normally something like parseResult - category # (meta => api) It seems there is always only 1 - category # (meta => dataStructures) It seems there is always only 1 - dataStructure # Bunch of data structures .
- .mson_to_ast_json(filename) ⇒ Object
- .mson_to_json_schema(options) ⇒ Object
- .mson_to_json_schema!(options) ⇒ Object
- .raise_parsing_errors(mson_file, elements) ⇒ Object
Class Method Details
.ast_parsing_annotation_messages(elements, type) ⇒ Object
146 147 148 149 150 151 152 |
# File 'lib/lacerda/conversion.rb', line 146 def self.(elements, type) elements.select do |element| element['element'] == 'annotation' && element['meta']['classes'].include?(type) end.map do |annotation| "#{type.capitalize} code #{annotation['attributes']['code']}: #{annotation['content']}" end end |
.data_structures_from_blueprint_ast(filename) ⇒ Object
The structure is of an AST is normally something like
parseResult
- category # (meta => api) It seems there is always only 1
- category # (meta => dataStructures) It seems there is always only 1
- dataStructure # Bunch of data structures
. . .
- dataStructure
. . .
- annotation # Bunch of annotations(errors/warnings
. . .
- annotation
. . .
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/lacerda/conversion.rb', line 121 def self.data_structures_from_blueprint_ast(filename) # The content of the ast parsing elements = parse_result_contents_from_ast_file(filename) raise_parsing_errors(filename, elements) # We keep the content of the categories only, they could be annotations otherwise result_categories = elements.select do |element| element['element'] == 'category' end.map { |category| category['content'] }.flatten # From these categories we keep the 'dataStructures' category contents. # If there could be other types, no idea ¯\_(ツ)_/¯ data_structures_categories_contents = result_categories.select do |result_category| result_category['meta']['classes'].include?('dataStructures') end.map { |data_structures_category| data_structures_category['content'] }.flatten # From the contents of 'dataStructures' categories we keep # the 'dataStructure' elements. If there could be other types, # no idea ¯\_(ツ)_/¯ data_structures_categories_contents.select do |data_structures_content| data_structures_content['element'] == 'dataStructure' end end |
.mson_to_ast_json(filename) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/lacerda/conversion.rb', line 154 def self.mson_to_ast_json(filename) input = filename output = filename.gsub(/\.\w+$/, '.blueprint-ast.json') # Add Data Structure section automatically mson = open(input).read unless mson[/^\#[ ]*data[ ]+structure/i] mson = "# Data Structures\n#{mson}" end result = LoungeLizard.parse(mson) File.open(output, 'w'){ |f| f.puts(result) } output end |
.mson_to_json_schema(options) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/lacerda/conversion.rb', line 10 def self.mson_to_json_schema() filename = .fetch(:filename) begin mson_to_json_schema!( filename: filename, keep_intermediary_files: .fetch(:keep_intermediary_files, false) ) puts "OK ".green + filename if .fetch(:verbose, true) true rescue puts "ERROR ".red + filename if .fetch(:verbose, true) false end end |
.mson_to_json_schema!(options) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lacerda/conversion.rb', line 25 def self.mson_to_json_schema!() filename = .fetch(:filename) # For now, we'll use the containing directory's name as a scope service_scope = File.dirname(filename).split(File::SEPARATOR).last.underscore # Parse MSON to an apiary blueprint AST # (see https://github.com/apiaryio/api-blueprint) ast_file = mson_to_ast_json(filename) # Pluck out Data structures from it data_structures = data_structures_from_blueprint_ast(ast_file) basename = File.basename(filename) file_type = basename[/(consume|publish)\.mson$/] if file_type.blank? raise Error, "Invalid filename #{basename}, can't tell if it's a publish or consume schema" end # Generate json schema from each contained data structure schema = { "$schema" => "http://json-schema.org/draft-04/schema#", "id" => file_type.gsub('.mson', '.schema.json'), "title" => service_scope, "definitions" => {}, "type" => "object", "properties" => {}, } # The json schema we're constructing contains every known # object type in the 'definitions'. So if we have definitions for # the objects User, Post and Tag, the schema will look like this: # # { # "$schema": "..." # # "definitions": { # "user": { "type": "object", "properties": { ... }} # "post": { "type": "object", "properties": { ... }} # "tag": { "type": "object", "properties": { ... }} # } # # "properties": { # "user": "#/definitions/user" # "post": "#/definitions/post" # "tag": "#/definitions/tag" # } # # } # # So when testing an object of type `user` against this schema, # we need to wrap it as: # # { # user: { # "your": "actual", # "data": "goes here" # } # } # data_structures.each do |data| id = data['content'].first['meta']['id'] json= DataStructure.new(id, data['content'], nil).to_json member = json.delete('title') schema['definitions'][member] = json schema['properties'][member] = {"$ref" => "#/definitions/#{member}"} end # Write it in a file outfile = filename.gsub(/\.\w+$/, '.schema.json') File.open(outfile, 'w'){ |f| f.puts JSON.pretty_generate(schema) } # Clean up FileUtils.rm_f(ast_file) unless .fetch(:keep_intermediary_files, false) true end |
.raise_parsing_errors(mson_file, elements) ⇒ Object
102 103 104 105 106 |
# File 'lib/lacerda/conversion.rb', line 102 def self.raise_parsing_errors(mson_file, elements) parsing_errors = ast_parsing_errors(elements) return if parsing_errors.empty? raise Error, parsing_errors.prepend("The following errors were found in #{mson_file}:").join("\n") end |