kwalify_to_json_schema 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9e7de38a589bad1cb9d5850e28ed875a341e6024d9236f1633c212771814edc
4
- data.tar.gz: eff3ee76a633a828a43b584651754c7ac300b89cf4b90917c0625ea98cd11473
3
+ metadata.gz: 2c071ca240ae54308f08beacbb2ddf8ac8646662b435ffca2b20bb3959e09984
4
+ data.tar.gz: 8c0b65039cf2ee1ff6f96e44b9053da918a47e33db8f9479f3e845e51d5997e5
5
5
  SHA512:
6
- metadata.gz: c5c0d389205182ba2ac093f5cd120aaf6e3d202ab06da674f8d837d4641259d096597788dca2a5e19f29a65d921e1253f816b868056f419602a5b2acdb804eaf
7
- data.tar.gz: dd1ef9b5e4d27fd0e6d60eba4dd36b3adbb68517a9bac2ab1a28a1c40f9e419ce6fb56538713cd0279758fe7c91d12686c4ecfd858c90f32c6c239be922bd0dc
6
+ metadata.gz: 00dc73a490152a8a715a577496eb25c0c61fc31313afc5475e4ec5471ffcac928a4029feda22583a8ff43f2a6a79d352a221bb67e1b5912b099ec467107602f6
7
+ data.tar.gz: aca9041623512921a06a012c27d874733b4cda5fcc18e19d7c7035629f8b39ae2de563fd0358ae6e75bc23bf4dd4d0e3aef10a78add72de0521e5fe242f42ac1
@@ -25,11 +25,11 @@ module KwalifyToJsonSchema
25
25
 
26
26
  desc "convert KWALIFY_SCHEMA_FILE, RESULT_FILE",
27
27
  "Convert a Kwalify schema file to a JSON schema file. The result file extension will decide the format: .json or .yaml"
28
- option :issues_to_description,
29
- :type => :boolean,
30
- :default => false,
31
- :desc => "Will append any conversion issue to the schema description"
32
- option :custom_processing,
28
+ option(*Options.cli_option(Options::ID))
29
+ option(*Options.cli_option(Options::TITLE))
30
+ option(*Options.cli_option(Options::DESCRIPTION))
31
+ option(*Options.cli_option(Options::ISSUES_TO_DESCRIPTION))
32
+ option Options::CUSTOM_PROCESSING,
33
33
  :type => :string,
34
34
  :desc => <<~DESC
35
35
  Allows to provide a pre/post processing file on handled schemas.
@@ -38,10 +38,8 @@ module KwalifyToJsonSchema
38
38
  DESC
39
39
 
40
40
  def convert(kwalify_schema_file, result_file)
41
- opts = {
42
- Options::ISSUES_TO_DESCRIPTION => options[:issues_to_description],
43
- Options::CUSTOM_PROCESSING => custom_processing(options),
44
- }
41
+ opts = options.dup
42
+ opts[Options::CUSTOM_PROCESSING] = custom_processing(options)
45
43
  KwalifyToJsonSchema.convert_file(kwalify_schema_file, result_file, opts)
46
44
  end
47
45
 
@@ -49,10 +47,7 @@ module KwalifyToJsonSchema
49
47
 
50
48
  desc "convert_dir KWALIFY_SCHEMA_DIR, RESULT_DIR",
51
49
  "Convert all the Kwalify schema from a directory to a JSON schema"
52
- option :issues_to_description,
53
- :type => :boolean,
54
- :default => false,
55
- :desc => "Will append any conversion issue to the schema description"
50
+ option(*Options.cli_option(:issues_to_description))
56
51
  option :format,
57
52
  :type => :string,
58
53
  :enum => ["json", "yaml"],
@@ -63,7 +58,7 @@ module KwalifyToJsonSchema
63
58
  :default => false,
64
59
  :desc => "Process files recursively",
65
60
  :long_desc => ""
66
- option :custom_processing,
61
+ option Options::CUSTOM_PROCESSING,
67
62
  :type => :string,
68
63
  :desc => <<~DESC
69
64
  Allows to provide a pre/post processing file on handled schemas.
@@ -98,7 +93,7 @@ module KwalifyToJsonSchema
98
93
  begin
99
94
  processing_class = Object.const_get :CustomProcessing
100
95
  custom_processing = processing_class.new
101
- rescue NameError => e
96
+ rescue NameError
102
97
  raise "The 'CustomProcessing' module must be defined in #{pf}"
103
98
  end
104
99
  end
@@ -11,7 +11,7 @@ module KwalifyToJsonSchema
11
11
  #
12
12
  # File.write("json_schema.json", JSON.pretty_generate(json_schema))
13
13
  class Converter
14
- SCHEMA = "http://json-schema.org/draft-07/schema#"
14
+ SCHEMA = "http://json-schema.org/%s/schema#"
15
15
 
16
16
  # The options given used to initialized the converter
17
17
  attr_reader :options
@@ -19,13 +19,14 @@ module KwalifyToJsonSchema
19
19
  attr_reader :issues
20
20
 
21
21
  # Converter options:
22
- # | Name | Type | Default value| Description |
23
- # |-----------------------|--------|--------------|-----------------------------------------------------|
24
- # | :id | String | nil | The JSON schema identifier |
25
- # | :title | String | nil | The JSON schema title |
26
- # | :description | String | nil | The JSON schema description |
27
- # | :issues_to_description| Boolean| false | To append the issuses to the JSON schema description|
28
- # | :custom_processing | Object | nil | To customize the conversion |
22
+ # | Name | Type | Default value| Description |
23
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
24
+ # | :id | string | nil | The JSON schema identifier |
25
+ # | :title | string | nil | The JSON schema title |
26
+ # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
27
+ # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
28
+ # | :custom_processing | object | nil | To customize the conversion |
29
+ # | :schema_version | String | "draft-04" | JSON schema version. Changing this value only change the value of $schema field |
29
30
  # --
30
31
  def initialize(options_hash = {})
31
32
  @options = Options.new(options_hash)
@@ -46,6 +47,9 @@ module KwalifyToJsonSchema
46
47
  description << issues.map { |issue| "* #{issue}" }.join("\n")
47
48
  end
48
49
 
50
+ # Override description if given in option
51
+ json_schema["description"] = options.description if options.description
52
+
49
53
  postprocess(json_schema)
50
54
  end
51
55
 
@@ -53,10 +57,9 @@ module KwalifyToJsonSchema
53
57
 
54
58
  def root
55
59
  {
56
- "$schema" => SCHEMA,
60
+ "$schema" => SCHEMA % options.schema_version,
57
61
  "$id" => options.id,
58
62
  "title" => options.title,
59
- "description" => options.description,
60
63
  }.reject { |k, v| v.nil? }
61
64
  end
62
65
 
@@ -152,7 +155,7 @@ module KwalifyToJsonSchema
152
155
  end
153
156
  end
154
157
 
155
- new_issue "'unique' is not supported by JSON Schema" if kelem["unique"]
158
+ new_issue Limitations::UNIQUE_NOT_SUPPORTED if kelem["unique"]
156
159
 
157
160
  target
158
161
  end
@@ -173,6 +176,4 @@ module KwalifyToJsonSchema
173
176
  @issues << description
174
177
  end
175
178
  end
176
-
177
-
178
179
  end
@@ -0,0 +1,53 @@
1
+ module KwalifyToJsonSchema
2
+
3
+ # Convert a Kwalify schema file to JSON .schema.
4
+ # The destination file can be JSON or YAML.
5
+ # The file extension is used to select the format: .json or .yaml.
6
+ # Other extension will fallback to JSON.
7
+ # Converter options:
8
+ # | Name | Type | Default value| Description |
9
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
10
+ # | :id | string | nil | The JSON schema identifier |
11
+ # | :title | string | nil | The JSON schema title |
12
+ # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
13
+ # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
14
+ # | :custom_processing | object | nil | To customize the conversion |
15
+ # | :schema_version | String | "draft-04" | JSON schema version. Changing this value only change the value of $schema field |
16
+ # --
17
+ # @param source Path to Kwalify YAML schema
18
+ # @param dest Path to resulting JSON schema
19
+ def self.convert_file(source, dest, options = {})
20
+ # Get a converter
21
+ converter = Converter.new(options)
22
+ # Convert
23
+ converted = converter.exec(Serialization.deserialize_from_file(source))
24
+ # Serialize
25
+ Serialization.serialize_to_file(dest, converted)
26
+ end
27
+
28
+ # Convert a Kwalify schema string to JSON .schema.
29
+ # The source and destination strings can be JSON or YAML.
30
+ # Other extension will fallback to JSON.
31
+ # Converter options:
32
+ # | Name | Type | Default value| Description |
33
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
34
+ # | :id | string | nil | The JSON schema identifier |
35
+ # | :title | string | nil | The JSON schema title |
36
+ # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
37
+ # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
38
+ # | :custom_processing | object | nil | To customize the conversion |
39
+ # | :schema_version | String | "draft-04" | JSON schema version. Changing this value only change the value of $schema field |
40
+ # --
41
+ # @param kwalify_schema Kwalify schema as YAML or JSON
42
+ # @param source_format format of the source schema
43
+ # @param dest_format format of the destination schema
44
+ # @param options
45
+ def self.convert_string(kwalify_schema, source_format = "yaml", dest_format = "json", options = {})
46
+ # Get a converter
47
+ converter = Converter.new(options)
48
+ # Convert
49
+ converted = converter.exec(Serialization.deserialize_from_string(kwalify_schema, source_format))
50
+ # Serialize
51
+ Serialization.serialize_to_string(converted, dest_format)
52
+ end
53
+ end
@@ -1,8 +1,9 @@
1
1
  module KwalifyToJsonSchema
2
- # Enumeration of known implementation limitation
2
+ # Enumeration of known implementation limitations
3
3
  module Limitations
4
4
  DATE_TYPE_NOT_IMPLEMENTED = "Kwalify 'date' type is not supported and is ignored"
5
5
  TIME_TYPE_NOT_IMPLEMENTED = "Kwalify 'time' type is not supported and is ignored"
6
6
  TIMESTAMP_TYPE_NOT_IMPLEMENTED = "Kwalify 'timestamp' type is not supported and is ignored"
7
+ UNIQUE_NOT_SUPPORTED = "Kwalify 'unique' is not supported by JSON Schema and is ignored"
7
8
  end
8
9
  end
@@ -0,0 +1,87 @@
1
+ module KwalifyToJsonSchema
2
+ # The possible options for the conversion and the associated accessors
3
+ class Options
4
+ # Converter options:
5
+ # | Name | Type | Default value| Description |
6
+ # |-----------------------|--------|--------------|------------------------------------------------------------------------------------------|
7
+ # | :id | string | nil | The JSON schema identifier |
8
+ # | :title | string | nil | The JSON schema title |
9
+ # | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present|
10
+ # | :issues_to_description| boolean| false | To append the issuses to the JSON schema description |
11
+ # | :custom_processing | object | nil | To customize the conversion |
12
+ # | :schema_version | String | "draft-04" | JSON schema version. Changing this value only change the value of $schema field |
13
+ # --
14
+ DECLARATION = %q(
15
+ ID # The JSON schema identifier [string] (nil)
16
+ TITLE # The JSON schema title [string] (nil)
17
+ DESCRIPTION # The JSON schema description. If not given the Kwalify description will be used if present [string] (nil)
18
+ ISSUES_TO_DESCRIPTION # To append the issuses to the JSON schema description [boolean] (false)
19
+ CUSTOM_PROCESSING # To customize the conversion [object] (nil)
20
+ SCHEMA_VERSION # JSON schema version. Changing this value only change the value of $schema field[String] ("draft-04")
21
+ )
22
+
23
+ # The options as Hash
24
+ attr_reader :options_hash
25
+
26
+ def initialize(options_hash)
27
+ @options_hash = options_hash
28
+ end
29
+
30
+ # Parse options declaration text and give an array of Hash
31
+ def self.parse
32
+ DECLARATION.lines.map { |l|
33
+ next nil if l.strip.empty?
34
+
35
+ # Parse line
36
+ const_name, comment = l.split("#", 2).map(&:strip)
37
+ name = const_name.downcase.to_s
38
+ description = comment.split("[").first.strip
39
+ # Get type and default value
40
+ m = comment.match(/\[(.+)\].*\((.+)\)/)
41
+ type, default_value = m.captures
42
+ default_value = eval(default_value)
43
+
44
+ # Create read accessor
45
+ attr_reader_name = "#{name}#{type == "boolean" ? "?" : ""}"
46
+
47
+ # Array entry as Hash for the option
48
+ {
49
+ const_name: const_name,
50
+ const_name_full: "#{Options.name}::#{const_name}",
51
+ name: name.to_sym,
52
+ description: description,
53
+ type: type,
54
+ default_value: default_value,
55
+ attr_reader_name: attr_reader_name,
56
+ }
57
+ }.compact
58
+ end
59
+
60
+ # Same as :parse but give a Hash with the name as key
61
+ def self.parse_hash
62
+ parse.map { |e| [e[:name], e] }.to_h
63
+ end
64
+
65
+ # Setup the constants and methods for the options
66
+ # Example: ID will lead to get ID constant and :id method
67
+ def self.setup
68
+ parse.each { |o|
69
+ # Create constant
70
+ const_set o[:const_name], o[:name]
71
+
72
+ # Create read accessor
73
+ define_method(o[:attr_reader_name]) {
74
+ options_hash[o[:name]] || o[:default_value]
75
+ }
76
+ }
77
+ end
78
+
79
+ # Get description for option name
80
+ def self.cli_option(name)
81
+ o = parse_hash[name]
82
+ [o[:name], :type => o[:type].to_sym, :default => o[:default_value], :desc => o[:description]]
83
+ end
84
+
85
+ setup
86
+ end
87
+ end
@@ -25,7 +25,7 @@ module KwalifyToJsonSchema
25
25
 
26
26
  # @return a Hash giving serialization/deserialization module and methods for a format (json/yaml)
27
27
  def self.serialization_for_format(format)
28
- serializer = { "json" => Json, "yaml" => Yaml }[format] || Json
28
+ { "json" => Json, "yaml" => Yaml }[format] || Json
29
29
  end
30
30
 
31
31
  class Language
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kwalify_to_json_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sylvain Gamot
@@ -80,28 +80,22 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 12.3.0
83
- description: Allows to convert Kwalify schemas to JSON schemas Draft 7
83
+ description: Allows to convert Kwalify schemas to JSON schemas
84
84
  email: ''
85
85
  executables:
86
86
  - kwalify_to_json_schema
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema.rb"
91
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/cli.rb"
92
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/converter.rb"
93
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/custom_processing.rb"
94
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/kwalify_to_json_schema.rb"
95
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/limitations.rb"
96
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/options.rb"
97
- - "/home/sylvain/kwalify_to_json_schema/lib/kwalify_to_json_schema/serialization.rb"
98
- - "/home/sylvain/kwalify_to_json_schema/test/custom_processing.rb"
99
- - "/home/sylvain/kwalify_to_json_schema/test/test_kwalify_to_json_schema.rb"
100
- - "/home/sylvain/kwalify_to_json_schema/tools/all.rb"
101
- - "/home/sylvain/kwalify_to_json_schema/tools/doc_template.rb"
102
- - "/home/sylvain/kwalify_to_json_schema/tools/limitations.rb"
103
- - "/home/sylvain/kwalify_to_json_schema/tools/options.rb"
104
90
  - bin/kwalify_to_json_schema
91
+ - lib/kwalify_to_json_schema.rb
92
+ - lib/kwalify_to_json_schema/cli.rb
93
+ - lib/kwalify_to_json_schema/converter.rb
94
+ - lib/kwalify_to_json_schema/custom_processing.rb
95
+ - lib/kwalify_to_json_schema/kwalify_to_json_schema.rb
96
+ - lib/kwalify_to_json_schema/limitations.rb
97
+ - lib/kwalify_to_json_schema/options.rb
98
+ - lib/kwalify_to_json_schema/serialization.rb
105
99
  homepage: https://github.com/s-ga/kwalify_to_json_schema
106
100
  licenses:
107
101
  - MIT
@@ -1,51 +0,0 @@
1
- module KwalifyToJsonSchema
2
-
3
- # Convert a Kwalify schema file to JSON .schema.
4
- # The destination file can be JSON or YAML.
5
- # The file extension is used to select the format: .json or .yaml.
6
- # Other extension will fallback to JSON.
7
- # Converter options:
8
- # | Name | Type | Default value| Description |
9
- # |-----------------------|--------|--------------|-----------------------------------------------------|
10
- # | :id | String | nil | The JSON schema identifier |
11
- # | :title | String | nil | The JSON schema title |
12
- # | :description | String | nil | The JSON schema description |
13
- # | :issues_to_description| Boolean| false | To append the issuses to the JSON schema description|
14
- # | :custom_processing | Object | nil | To customize the conversion |
15
- # --
16
- # @param source Path to Kwalify YAML schema
17
- # @param dest Path to resulting JSON schema
18
- def self.convert_file(source, dest, options = {})
19
- # Get a converter
20
- converter = Converter.new(options)
21
- # Convert
22
- converted = converter.exec(Serialization.deserialize_from_file(source))
23
- # Serialize
24
- Serialization.serialize_to_file(dest, converted)
25
- end
26
-
27
- # Convert a Kwalify schema string to JSON .schema.
28
- # The source and destination strings can be JSON or YAML.
29
- # Other extension will fallback to JSON.
30
- # Converter options:
31
- # | Name | Type | Default value| Description |
32
- # |-----------------------|--------|--------------|-----------------------------------------------------|
33
- # | :id | String | nil | The JSON schema identifier |
34
- # | :title | String | nil | The JSON schema title |
35
- # | :description | String | nil | The JSON schema description |
36
- # | :issues_to_description| Boolean| false | To append the issuses to the JSON schema description|
37
- # | :custom_processing | Object | nil | To customize the conversion |
38
- # --
39
- # @param kwalify_schema Kwalify schema as YAML or JSON
40
- # @param source_format format of the source schema
41
- # @param dest_format format of the destination schema
42
- # @param options
43
- def self.convert_string(kwalify_schema, source_format = "yaml", dest_format = "json", options = {})
44
- # Get a converter
45
- converter = Converter.new(options)
46
- # Convert
47
- converted = converter.exec(Serialization.deserialize_from_string(kwalify_schema, source_format))
48
- # Serialize
49
- Serialization.serialize_to_string(converted, dest_format)
50
- end
51
- end
@@ -1,74 +0,0 @@
1
- module KwalifyToJsonSchema
2
- # The possible options for the conversion and the associated accessors
3
- class Options
4
- # Converter options:
5
- # | Name | Type | Default value| Description |
6
- # |-----------------------|--------|--------------|-----------------------------------------------------|
7
- # | :id | String | nil | The JSON schema identifier |
8
- # | :title | String | nil | The JSON schema title |
9
- # | :description | String | nil | The JSON schema description |
10
- # | :issues_to_description| Boolean| false | To append the issuses to the JSON schema description|
11
- # | :custom_processing | Object | nil | To customize the conversion |
12
- # --
13
- DECLARATION = %q(
14
- ID # The JSON schema identifier [String] (nil)
15
- TITLE # The JSON schema title [String] (nil)
16
- DESCRIPTION # The JSON schema description [String] (nil)
17
- ISSUES_TO_DESCRIPTION # To append the issuses to the JSON schema description [Boolean] (false)
18
- CUSTOM_PROCESSING # To customize the conversion [Object] (nil)
19
- )
20
-
21
- # The options as Hash
22
- attr_reader :options_hash
23
-
24
- def initialize(options_hash)
25
- @options_hash = options_hash
26
- end
27
-
28
- # Parse options declaration text and give an array of Hash
29
- def self.parse
30
- DECLARATION.lines.map { |l|
31
- next nil if l.strip.empty?
32
-
33
- # Parse line
34
- const_name, comment = l.split("#", 2).map(&:strip)
35
- name = const_name.downcase.to_s
36
- description = comment.split("[").first.strip
37
- # Get type and default value
38
- m = comment.match(/\[(.+)\].*\((.+)\)/)
39
- type, default_value = m.captures
40
- default_value = eval(default_value)
41
-
42
- # Create read accessor
43
- attr_reader_name = "#{name}#{type == "Boolean" ? "?" : ""}"
44
-
45
- # Array entry as Hash for the option
46
- {
47
- const_name: const_name,
48
- const_name_full: "#{Options.name}::#{const_name}",
49
- name: name,
50
- description: description,
51
- type: type,
52
- default_value: default_value,
53
- attr_reader_name: attr_reader_name,
54
- }
55
- }.compact
56
- end
57
-
58
- # Setup the constants and methods for the options
59
- # Example: ID will lead to get ID constant and :id method
60
- def self.setup
61
- parse.each { |o|
62
- # Create constant
63
- const_set o[:const_name], o[:name]
64
-
65
- # Create read accessor
66
- define_method(o[:attr_reader_name]) {
67
- options_hash[o[:name]] || o[:default_value]
68
- }
69
- }
70
- end
71
-
72
- setup
73
- end
74
- end
@@ -1,14 +0,0 @@
1
- # Customization of Kwalify to JSON schema
2
- class CustomProcessing
3
- def preprocess(kwalify_schema)
4
- # Remove and keep the wrapping name
5
- head = kwalify_schema.first
6
- @name = head.first
7
- head.last
8
- end
9
-
10
- def postprocess(json_schema)
11
- # Restore the wrapping name
12
- { @name => json_schema }
13
- end
14
- end
@@ -1,75 +0,0 @@
1
- require "minitest/autorun"
2
- require "json-schema"
3
- require_relative "../lib/kwalify_to_json_schema"
4
-
5
- module KwalifyToJsonSchema
6
- class Test < Minitest::Test
7
- @@debug = false
8
- @@tmpdir = Dir.mktmpdir
9
-
10
- [
11
- { test_group: "conversion", cli_options: [] },
12
- { test_group: "custom_processing", cli_options: ["--custom-processing", File.join(__dir__, "custom_processing.rb")] },
13
- ].each { |entry|
14
- test_group = entry[:test_group]
15
- cli_options = entry[:cli_options]
16
-
17
- # Create a test method for every Kwalify schema
18
- Dir.glob(File.join(__dir__, test_group, "kwalify", "*.yaml")).each { |source|
19
- test_file_base = File.basename(source, File.extname(source))
20
- test_name_base = test_file_base.gsub("#", "_")
21
- expected_formats = %w(json yaml)
22
-
23
- # Define a method for the test JSON output
24
- define_method("test_#{test_group}_#{test_name_base}_output".to_sym) {
25
- formats_done = 0
26
- expected_formats.map { |expected_format|
27
- output_file = test_file_base + ".#{expected_format}"
28
- expected = File.join(File.join(__dir__, test_group, "json_schema", expected_format, output_file))
29
-
30
- next unless File.exist?(expected)
31
- formats_done += 1
32
-
33
- ser = KwalifyToJsonSchema::Serialization::serialization_for_format(expected_format)
34
- dest = File.join(@@tmpdir, output_file)
35
-
36
- args = ["convert", source, dest]
37
- # Add issues to description if filename include "#issues_to_description"
38
- args << "--issues_to_description" if output_file.include?("#issues_to_description")
39
- args.concat cli_options
40
-
41
- # Convert
42
- # KwalifyToJsonSchema.convert_file(source, dest, options)
43
- KwalifyToJsonSchema::Cli.start(args)
44
-
45
- # Validate schema
46
- validate_json_schema_file(dest)
47
-
48
- if @@debug
49
- puts test_name_base
50
- puts ser.normalize(File.read(dest))
51
- end
52
- # Compare to expected result
53
- assert_equal(
54
- ser.normalize(File.read(expected)),
55
- ser.normalize(File.read(dest))
56
- )
57
- }
58
-
59
- skip "None of the expected #{expected_formats.join(", ")} result for test #{test_name_base} was found" if formats_done == 0
60
- }
61
- }
62
- }
63
-
64
- def validate_json_schema_file(schema_file)
65
- schema = KwalifyToJsonSchema::Serialization::deserialize_from_file(schema_file)
66
- validate_json_schema(schema)
67
- end
68
-
69
- def validate_json_schema(schema)
70
- # FIXME draft7 is not available in current json-schema gem
71
- metaschema = JSON::Validator.validator_for_name("draft4").metaschema
72
- JSON::Validator.validate!(metaschema, schema)
73
- end
74
- end
75
- end
@@ -1,4 +0,0 @@
1
- require_relative "../lib/kwalify_to_json_schema"
2
- require_relative "doc_template"
3
- require_relative "limitations"
4
- require_relative "options"
@@ -1,13 +0,0 @@
1
- require "erb"
2
-
3
- module DocTemplate
4
- def self.render(template_file, dest_file)
5
- template = ERB.new(File.read(template_file), nil, "-")
6
- File.write(dest_file, template.result(get_binding(template_file)))
7
- end
8
-
9
- def self.get_binding(template_file)
10
- template_dir = File.dirname template_file
11
- binding
12
- end
13
- end
@@ -1,19 +0,0 @@
1
- require_relative "../lib/kwalify_to_json_schema/limitations"
2
-
3
- # Gives implementation limitations
4
- module Limitations
5
-
6
- # @return list of limitation as array of strings
7
- def self.list
8
- KwalifyToJsonSchema::Limitations.constants.map { |cst|
9
- KwalifyToJsonSchema::Limitations.const_get(cst)
10
- }
11
- end
12
-
13
- # @return limitation as markdown text
14
- def self.markdown
15
- list.map { |l|
16
- "* #{l}"
17
- }.join("\n")
18
- end
19
- end
@@ -1,96 +0,0 @@
1
- require "yaml"
2
- require_relative "../lib/kwalify_to_json_schema/options"
3
-
4
- # Gives implementation limitations
5
- module Options
6
-
7
- # @return list of limitation as array of strings
8
- def self.list
9
- KwalifyToJsonSchema::Options.parse
10
- end
11
-
12
- # @return limitation as markdown text
13
- def self.ascii_table(formatting = ["%s"] * 4)
14
- header = ["Name", "Type", "Default value", "Description"]
15
-
16
- nb_cols = header.length
17
-
18
- table = [header] +
19
- [[""] * nb_cols] +
20
- list.map { |o|
21
- [
22
- formatting[0] % o[:name].to_sym.inspect,
23
- formatting[1] % o[:type],
24
- formatting[2] % o[:default_value].inspect,
25
- formatting[3] % o[:description],
26
- ]
27
- }
28
- nb_rows = table.length
29
-
30
- cols_max_length = (0..nb_cols - 1).map { |c|
31
- (0..nb_rows - 1).map { |r|
32
- cell = table[r][c]
33
- cell.length
34
- }.max
35
- }
36
-
37
- table.map.each_with_index { |row, r|
38
- row.map.each_with_index { |cell, c|
39
- max_length = cols_max_length[c]
40
- if r == 1
41
- "|-" + cell + ("-" * (max_length - cell.length))
42
- else
43
- "| " + cell + (" " * (max_length - cell.length))
44
- end
45
- }.join + "|"
46
- }.join "\n"
47
- end
48
-
49
- def self.markdown
50
- ascii_table [
51
- "`%s`",
52
- "`%s`",
53
- "`%s`",
54
- "_%s_",
55
- ]
56
- end
57
-
58
- def self.inject_as_code_comment(file)
59
- new_lines = []
60
- state = :init
61
- count = 0
62
-
63
- options_start = "Converter options:"
64
- options_stop = "--"
65
-
66
- File.read(file).each_line { |line|
67
- if line.strip.start_with? "#"
68
- content = line.strip[1..-1].strip
69
- case state
70
- when :init
71
- new_lines << line
72
- if content == options_start
73
- count += 1
74
- state = :in_options
75
- padding = line.index("#")
76
- new_lines.concat(ascii_table.lines.map { |l| "#{" " * padding}# #{l.chomp}\n" })
77
- end
78
- when :in_options
79
- if content.start_with? options_stop
80
- new_lines << line
81
- state = :init
82
- end
83
- end
84
- else
85
- state = :error unless state == :init
86
- new_lines << line
87
- end
88
- }
89
-
90
- if state == :error
91
- puts "Missing '#{options_stop}' delimiter after '#{options_start}' in file://#{file}"
92
- else
93
- File.write(file, new_lines.join) if count > 0
94
- end
95
- end
96
- end