json-schema-generator 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,10 @@
1
+ require 'json/schema_generator/statement_group'
2
+
1
3
  module JSON
2
4
  class SchemaGenerator
5
+ DRAFT3 = 'draft-03'
6
+ DRAFT4 = 'draft-04'
7
+
3
8
  class << self
4
9
  def generate name, data, version
5
10
  generator = JSON::SchemaGenerator.new name, version
@@ -10,92 +15,115 @@ module JSON
10
15
  def initialize name, version = 'draft3'
11
16
  @buffer = StringIO.new
12
17
  @name = name
13
- @version = 'draft-03' if version == 'draft3'
14
- @version = 'draft-04' if version == 'draft4'
18
+ @version = DRAFT3 if version == 'draft3'
19
+ @version = DRAFT4 if version == 'draft4'
15
20
  end
16
21
 
17
22
  def generate raw_data
18
- # Write header
19
- @buffer.puts "{"
20
- @buffer.puts "\"$schema\": \"http://json-schema.org/#{@version}/schema#\","
21
- @buffer.print '"description": "Generated from '
22
- @buffer.print @name
23
- @buffer.print " with shasum "
24
- @buffer.print Digest::SHA1.hexdigest raw_data
25
- @buffer.puts '",'
26
-
27
- #
28
23
  data = JSON.load(raw_data)
29
-
30
- @buffer.puts create_hash(data)
31
- close
24
+ statement_group = StatementGroup.new
25
+ statement_group.add "\"$schema\": \"http://json-schema.org/#{@version}/schema#\""
26
+ statement_group.add "\"description\": \"Generated from #{@name} with shasum #{Digest::SHA1.hexdigest raw_data}\""
27
+ create_hash(statement_group, data, detect_required(data))
28
+ @buffer.puts statement_group
32
29
  result
33
30
  end
34
31
 
35
32
  protected
36
33
 
37
- def close
38
- @buffer.puts "}"
39
- end
34
+ def create_primitive(statement_group, key, value, required_keys)
35
+ if required_keys.nil?
36
+ required = true
37
+ else
38
+ required = required_keys.include? key
39
+ end
40
40
 
41
- def create_values(key, value)
42
- buffer = StringIO.new
43
- buffer.puts "\"#{key}\": {"
44
- case value
45
- when NilClass
41
+ type = case value
46
42
  when TrueClass, FalseClass
47
- buffer.puts '"type": "boolean",'
48
- buffer.puts '"required": true'
49
-
43
+ "boolean"
50
44
  when String
51
- buffer.puts '"type": "string",'
52
- buffer.puts '"required": true'
53
-
45
+ "string"
54
46
  when Integer
55
- buffer.puts '"type": "integer",'
56
- buffer.puts '"required": true'
57
-
47
+ "integer"
58
48
  when Float
59
- buffer.puts '"type": "number",'
60
- buffer.puts '"required": true'
49
+ "number"
50
+ else
51
+ raise "Unknown Primitive Type for #{key}! #{value.class}"
52
+ end
53
+
54
+ statement_group.add "\"type\": \"#{type}\""
55
+ statement_group.add "\"required\": #{required}" if @version == DRAFT3
56
+ # statement_group.add "\"default\": #{value.inspect}"
57
+ end
58
+
59
+ def create_values(key, value, required_keys = nil, in_array = false)
60
+ statement_group = StatementGroup.new key
61
+ # buffer.puts "\"#{key}\": {"
62
+ case value
63
+ when NilClass
64
+ when TrueClass, FalseClass, String, Integer, Float
65
+ create_primitive(statement_group, key, value, required_keys)
61
66
  when Array
62
- buffer << create_array(value)
67
+ create_array(statement_group, value, detect_required(value))
63
68
  when Hash
64
- buffer << create_hash(value)
69
+ if in_array
70
+ create_hash(statement_group, value, required_keys)
71
+ else
72
+ create_hash(statement_group, value, detect_required(value))
73
+ end
65
74
  else
66
75
  raise "Unknown Type for #{key}! #{value.class}"
67
76
  end
68
- buffer.print "}"
69
- buffer.string
77
+ statement_group
70
78
  end
71
79
 
72
- def create_hash(data)
73
- buffer = StringIO.new
74
- buffer.puts '"type": "object",'
75
- buffer.puts '"required": true,'
76
- buffer.puts '"properties": {'
77
- items = data.collect do |k,v|
78
- create_values k,v
80
+ def create_hash(statement_group, data, required_keys)
81
+ # statement_group = StatementGroup.new
82
+ statement_group.add '"type": "object"'
83
+ statement_group.add '"required": true' if @version == DRAFT3
84
+ if @version == DRAFT4
85
+ required_keys ||= []
86
+ required_string = required_keys.map(&:inspect).join ', '
87
+ statement_group.add "\"required\": [#{required_string}]"
79
88
  end
80
- buffer << items.join(",\n")
81
- buffer.puts '}'
89
+ statement_group.add create_hash_properties data, required_keys
90
+ statement_group
91
+ end
82
92
 
83
- buffer.string
93
+ def create_hash_properties(data, required_keys)
94
+ statement_group = StatementGroup.new "properties"
95
+ data.collect do |k,v|
96
+ statement_group.add create_values k, v, required_keys
97
+ end
98
+ statement_group
84
99
  end
85
100
 
86
- def create_array(data)
87
- buffer = StringIO.new
88
- buffer.puts '"type": "array",'
89
- buffer.puts '"required": true,'
101
+ def create_array(statement_group, data, required_keys)
102
+ # statement_group = StatementGroup.new
103
+ statement_group.add '"type": "array"'
104
+ statement_group.add '"required": true' if @version == DRAFT3
90
105
  if data.size == 0
91
- buffer.puts '"minItems": 0,'
106
+ statement_group.add '"minItems": 0'
92
107
  else
93
- buffer.puts '"minItems": 1,'
108
+ statement_group.add '"minItems": 1'
94
109
  end
95
- buffer.puts '"uniqueItems": true,'
96
- buffer.puts create_values("items", data.first)
110
+ statement_group.add '"uniqueItems": true'
111
+ statement_group.add create_values("items", data.first, required_keys, true)
97
112
 
98
- buffer.string
113
+ statement_group
114
+ end
115
+
116
+ def detect_required(collection)
117
+ begin
118
+ required_keys = collection.map(&:keys).inject{|required,keys| required & keys }
119
+ rescue
120
+ if collection.respond_to? :keys
121
+ required_keys = collection.keys
122
+ else
123
+ required_keys = nil
124
+ end
125
+ end
126
+ required_keys
99
127
  end
100
128
 
101
129
  def result
@@ -0,0 +1,26 @@
1
+ module JSON
2
+ class SchemaGenerator
3
+ class StatementGroup
4
+ def initialize key = nil
5
+ @key = key
6
+ @statements = []
7
+ end
8
+
9
+ def add statement
10
+ @statements << statement
11
+ end
12
+
13
+ def to_s
14
+ buffer = StringIO.new
15
+ if @key.nil?
16
+ buffer.puts "{"
17
+ else
18
+ buffer.puts "\"#{@key}\": {"
19
+ end
20
+ buffer.puts @statements.join ', '
21
+ buffer.puts "}"
22
+ buffer.string
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  module JSON
2
2
  class SchemaGenerator
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -4,8 +4,9 @@ class JSON::SchemaGeneratorCLI
4
4
  end
5
5
 
6
6
  def execute!
7
- file = ARGV[0]
8
- schema = JSON.parse(JSON::SchemaGenerator.generate file, File.read(file), ARGV[1])
7
+ file = ARGV.shift
8
+ version = ARGV.shift || 'draft3'
9
+ schema = JSON.parse(JSON::SchemaGenerator.generate file, File.read(file), version)
9
10
  @stdout.puts JSON.pretty_generate schema
10
11
  @kernel.exit(0)
11
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-31 00:00:00.000000000 Z
12
+ date: 2013-11-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -126,6 +126,7 @@ files:
126
126
  - json-schema-generator.gemspec
127
127
  - lib/json-schema-generator.rb
128
128
  - lib/json/schema_generator.rb
129
+ - lib/json/schema_generator/statement_group.rb
129
130
  - lib/json/schema_generator/version.rb
130
131
  - lib/json/schema_generator_cli.rb
131
132
  homepage: ''