superstudio 0.8.2102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,127 @@
1
+ module Superstudio
2
+ module SchemaInterpreter
3
+
4
+ # TODO: Refactor this, it's only being used in the generator and it's out of step with SchemaInternalDefiner.set_human_to_internal_mappings
5
+ def create_template(expected_mappings)
6
+ fork_nodes = expected_mappings.uniq { |i| i[:path] }
7
+ max_depth = expected_mappings.max_by { |x| x[:depth] }[:depth]
8
+
9
+ @template_bodies = {}
10
+ mappings = set_human_to_internal_mappings(expected_mappings)
11
+
12
+ while max_depth != 0
13
+ objects_at_depth = fork_nodes.select { |j| j[:depth] == max_depth}
14
+ at_depth_hash = {}
15
+
16
+ objects_at_depth.each do |object|
17
+ object_path_string = object[:path].join("_B_")
18
+ object_properties = expected_mappings.select { |mappings| mappings[:path] == object[:path] }
19
+ object_string = "{"
20
+
21
+ if @type_3_paths.include?(object[:path]) || @type_5_paths.include?(object[:path])
22
+ object_properties.each do |property, index|
23
+ expected_hash_key = "#{object_path_string}_A_#{property[:name]}"
24
+ object_string << "\"#{property[:name]}\":{#{expected_hash_key}},"
25
+ end
26
+ else
27
+ object_properties.each do |property, index|
28
+ expected_hash_key = "#{object_path_string}_P_#{property[:name]}"
29
+ object_string << "\"#{property[:name]}\":{#{expected_hash_key}},"
30
+ end
31
+ end
32
+ object_string = object_string.chomp(",")
33
+ object_string << "}"
34
+
35
+ @template_bodies[object[:path]] = object_string
36
+ end
37
+
38
+ max_depth = max_depth - 1
39
+ end
40
+
41
+ end
42
+ # TODO: Refactor this, it's only being used in the generator and it's out of step with SchemaInternalDefiner.set_human_to_internal_mappings
43
+
44
+
45
+
46
+ def interpret_json_schema(json_hash, depth = 0, path_array = [], expected_mappings = [], node_name = 'root')
47
+ level_keys = json_hash.keys
48
+ level_keys.each do |key|
49
+
50
+ if key == "type"
51
+ if ["integer", "boolean", "number", "string"].include?(json_hash[key])
52
+ return {depth: depth, path: path_array, name: node_name, node_type: json_hash[key]}
53
+ end
54
+ end
55
+
56
+ if key == "uniqueItems"
57
+ # if this is set to true, record the path so that we can match the type 3s against it to know where to check for uniqueness
58
+ # default is false
59
+ if json_hash[key].to_s == "true"
60
+ new_path_array = path_array.dup
61
+ new_path_array << node_name
62
+ @unique_threes_paths << new_path_array
63
+ end
64
+ end
65
+
66
+ # byebug
67
+ if json_hash[key].is_a?(Hash)
68
+ new_path_array = path_array.dup
69
+ new_depth = depth
70
+ # byebug
71
+ if node_name != "properties" && node_name != "items" && node_name != "dependencies"
72
+ new_path_array << node_name
73
+ new_depth = new_depth + 1
74
+ # we need something in here for viewed and real depths
75
+ if json_hash[key]["type"] != "array" && key != "items"
76
+ @type_2_paths << new_path_array
77
+ # @type_2_indicator_names << node_name
78
+ end
79
+ end
80
+ # if node_name == "properties" && json_hash[key]["type"] == "array"
81
+ # # return { depth: new_depth, path: new_path_array << key, name: node_name, node_type: "array" }
82
+ # # expected_mappings << interpret_json_schema(json_hash[key], new_depth + 1, new_path_array << key, [], key)
83
+ # alt_path = new_path_array.dup
84
+ # alt_path << key
85
+ # expected_mappings << { depth: new_depth + 1, path: alt_path, name: node_name, node_type: "array" }
86
+
87
+ # end
88
+ expected_mappings << interpret_json_schema(json_hash[key], new_depth, new_path_array, [], key)
89
+ # end
90
+ end
91
+
92
+ if key == "items"
93
+ if json_hash[key].is_a?(Array)
94
+ # Something like: { "type": "array", "items": [ { "type": "number" }, { "type": "string" } ] }
95
+ # What gets passed in has to be a string in exactly the format the user wants it in
96
+ # This is a type 5 path
97
+ new_depth = depth + 1
98
+ new_path_array = path_array.dup
99
+ new_path_array << node_name
100
+ @type_5_paths << new_path_array
101
+
102
+ return { depth: depth, path: new_path_array, name: node_name, node_type: "custom_array" }
103
+ else
104
+ # detect if this is an array of objects or values
105
+ # if path_array.last != "root"
106
+ # expected_mappings << { depth: depth, path: path_array, name: path_array.last, node_type: "array" }
107
+ # end
108
+ if json_hash[key]["type"].downcase == "object"
109
+ # Something like: { "type": "array", "items": { "type": "object", "properties" : { <stuff...> }}}
110
+ # don't think we need the new path, look at later
111
+ new_depth = new_depth + 1
112
+ @type_4_paths << new_path_array
113
+ # @type_4_indicator_names << node_name
114
+ else
115
+ # this is an array of values
116
+ # Something like: { "type": "array", "items": { "type: "number" } }
117
+
118
+ @type_3_paths << new_path_array
119
+ end
120
+ end
121
+ end
122
+
123
+ end
124
+ return expected_mappings.flatten
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,51 @@
1
+ module Superstudio
2
+ module SchemaReader
3
+ def schemas_directory
4
+ if ENV['JSON_SCHEMAS_DIRECTORY'].nil?
5
+ Rails.root.join('app', 'json_schemas')
6
+ else
7
+ Pathname.new(ENV['JSON_SCHEMAS_DIRECTORY'])
8
+ end
9
+ end
10
+
11
+ def schema_maps_directory
12
+ if ENV['JSON_SCHEMA_MAPS_DIRECTORY'].nil?
13
+ Rails.root.join('app', 'json_schemas')
14
+ else
15
+ Pathname.new(ENV['JSON_SCHEMA_MAPS_DIRECTORY'])
16
+ end
17
+ end
18
+
19
+ def parse_json_schema(file_name)
20
+ directory = schemas_directory
21
+ master_file = directory.join(file_name)
22
+ contents = File.read(master_file)
23
+ schema = JSON.parse(contents)
24
+ return schema
25
+ end
26
+
27
+ def replace_reference_keys(json_hash)
28
+ json_hash.each do |key, value|
29
+ if value.is_a?(Hash)
30
+ json_hash[key] = replace_reference_keys(value)
31
+ end
32
+ end
33
+
34
+ if json_hash.keys.include?("$ref")
35
+ # split on a hash character - the first piece is the file name
36
+ file_name = json_hash["$ref"].split('#')[0]
37
+ referenced_contents = parse_json_schema(file_name)
38
+ referenced_contents = replace_reference_keys(referenced_contents)
39
+ json_hash.delete("$ref")
40
+ json_hash = json_hash.merge(referenced_contents)
41
+ end
42
+
43
+ return json_hash
44
+ end
45
+
46
+ def read_schema(file_name)
47
+ json_hash = parse_json_schema(file_name)
48
+ json_hash = replace_reference_keys(json_hash)
49
+ end
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: superstudio
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.2102
5
+ platform: ruby
6
+ authors:
7
+ - David Dawson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email: its.dave.dawson@gmail.com
15
+ executables: []
16
+ extensions:
17
+ - ext/superstudio/extconf.rb
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - ext/superstudio/extconf.rb
23
+ - ext/superstudio/finalize.h
24
+ - ext/superstudio/fnv_64.c
25
+ - ext/superstudio/fnv_64.h
26
+ - ext/superstudio/hash_linked_list.c
27
+ - ext/superstudio/hash_linked_list.h
28
+ - ext/superstudio/json_builder.c
29
+ - ext/superstudio/json_builder.h
30
+ - ext/superstudio/json_object_array.c
31
+ - ext/superstudio/json_object_array.h
32
+ - ext/superstudio/json_single_object.c
33
+ - ext/superstudio/json_single_object.h
34
+ - ext/superstudio/json_value.c
35
+ - ext/superstudio/json_value.h
36
+ - ext/superstudio/json_value_array.c
37
+ - ext/superstudio/json_value_array.h
38
+ - ext/superstudio/jsonbroker.c
39
+ - ext/superstudio/jsonbroker.h
40
+ - ext/superstudio/ss_alloc.c
41
+ - ext/superstudio/ss_alloc.h
42
+ - ext/superstudio/superstudio.c
43
+ - ext/superstudio/superstudio.h
44
+ - lib/generators/superstudio/schema_generator.rb
45
+ - lib/generators/superstudio/schema_map_generator.rb
46
+ - lib/superstudio.rb
47
+ - lib/superstudio/schema_internal_definer.rb
48
+ - lib/superstudio/schema_interpreter.rb
49
+ - lib/superstudio/schema_reader.rb
50
+ - lib/superstudio/superstudio.so
51
+ homepage: https://github.com/DaveTD/superstudio
52
+ licenses:
53
+ - GPL-3.0
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.5.1
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: 'An alternative way of thinking about creating JSON output: life without
75
+ creating ruby objects.'
76
+ test_files: []