finitio 0.8.0 → 0.9.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 467a7c5d8dd70fe9b5de2154217a5594182150d6e9c9996ec85c861980c02ecc
4
- data.tar.gz: cadb1b56c039b7a88456f803733d9c0da9f7d879393674de0fc067c2fb71679b
3
+ metadata.gz: fb6a6dde74e450b0569a6f568d2f6e847a6f646b23cd815a3442e1a85dbdaedf
4
+ data.tar.gz: 26fd2339057cc212d2594d2d52ab66b5ae1ad6dc7db0294faa941f148406ac4a
5
5
  SHA512:
6
- metadata.gz: 4606b1f8de1df5a7712f8c6546252683928066a18e5e2ed91a96af2e68be552f66e3ccea26d8b6760e9ac5c073fffa416f06ae0ca8001bbac5ca4f2532469f1e
7
- data.tar.gz: 1d9436c52479abab152422d471b559724e72ea7d933ec8538cf2877e1fa14bebccfc6219dcf662c76758d167653cf47fe10b0332ee2cc118b6e18229826d5eed
6
+ metadata.gz: f2e4bc1270b3169f3a9692473525693c90e5771c42fc01e3fdce89da676b1e6867d977fb9f29cc2603adcbdc15920dec9dde6133ccd96f50638e12614cf22eed
7
+ data.tar.gz: acd957a3ea361e715a17d27bb25baef4687f2ed6252a1289a8e4c660e8151c4ad2813488d9a8fbccda189903b95be5da99983b0f0a922e3590f316dd656c7371
@@ -1,3 +1,9 @@
1
+ # 0.9.0 - 2020/12/16
2
+
3
+ * Add Type#to_json_schema that converts Finitio types to JSON schema
4
+ representations. This first implementation skips all constraints on sub types,
5
+ though. You need to explicitly require 'finitio/json_schema' to use it.
6
+
1
7
  # 0.8.0 - 2019/10/21
2
8
 
3
9
  * Add `Type#unconstrained` that returns a super type with all user specific
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- finitio (0.8.0)
4
+ finitio (0.9.0)
5
5
  citrus (>= 2.4, < 4.0)
6
6
 
7
7
  GEM
@@ -67,4 +67,4 @@ DEPENDENCIES
67
67
  rspec (~> 3.0)
68
68
 
69
69
  BUNDLED WITH
70
- 2.0.1
70
+ 2.0.2
@@ -0,0 +1,16 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ class Error < Finitio::Error; end
4
+ end # module JsonSchema
5
+ end # module Finitio
6
+ require_relative 'json_schema/ad_type'
7
+ require_relative 'json_schema/any_type'
8
+ require_relative 'json_schema/alias_type'
9
+ require_relative 'json_schema/builtin_type'
10
+ require_relative 'json_schema/hash_based_type'
11
+ require_relative 'json_schema/rel_based_type'
12
+ require_relative 'json_schema/seq_type'
13
+ require_relative 'json_schema/set_type'
14
+ require_relative 'json_schema/struct_type'
15
+ require_relative 'json_schema/sub_type'
16
+ require_relative 'json_schema/union_type'
@@ -0,0 +1,11 @@
1
+ module Finitio
2
+ class AdType
3
+
4
+ def to_json_schema(*args, &bl)
5
+ {
6
+ anyOf: contracts.map{|c| c.infotype.to_json_schema(*args, &bl) }
7
+ }
8
+ end
9
+
10
+ end # class AdType
11
+ end # module Finitio
@@ -0,0 +1,9 @@
1
+ module Finitio
2
+ class AliasType
3
+
4
+ def to_json_schema(*args, &bl)
5
+ target.to_json_schema(*args, &bl)
6
+ end
7
+
8
+ end # class AliasType
9
+ end # module Finitio
@@ -0,0 +1,9 @@
1
+ module Finitio
2
+ class AnyType
3
+
4
+ def to_json_schema(*args, &bl)
5
+ {}
6
+ end
7
+
8
+ end # class AnyType
9
+ end # module Finitio
@@ -0,0 +1,27 @@
1
+ module Finitio
2
+ module JsonSchema
3
+
4
+ BUILTIN_MAPPING = {
5
+ NilClass => "null",
6
+ String => "string",
7
+ Integer => "integer",
8
+ Fixnum => "integer",
9
+ Bignum => "integer",
10
+ Float => "number",
11
+ Numeric => "number"
12
+ }
13
+
14
+ end
15
+ class BuiltinType
16
+
17
+ def to_json_schema(*args, &bl)
18
+ mapped = JsonSchema::BUILTIN_MAPPING[ruby_type]
19
+ if mapped
20
+ { type: mapped }
21
+ else
22
+ raise JsonSchema::Error, "Unable to map #{ruby_type} to json-schema"
23
+ end
24
+ end
25
+
26
+ end # class BuiltinType
27
+ end # module Finitio
@@ -0,0 +1,25 @@
1
+ module Finitio
2
+ module HashBasedType
3
+
4
+ def to_json_schema(*args, &bl)
5
+ base = {
6
+ type: "object"
7
+ }
8
+ unless heading.empty?
9
+ base[:properties] = heading.inject({}){|ps,a|
10
+ ps.merge(a.name => a.type.to_json_schema(*args, &bl))
11
+ }
12
+ end
13
+ unless (reqs = heading.select{|a| a.required? }).empty?
14
+ base[:required] = reqs.map{|a| a.name }
15
+ end
16
+ base[:additionalProperties] = if heading.allow_extra?
17
+ heading.allow_extra.to_json_schema(*args, &bl)
18
+ else
19
+ false
20
+ end
21
+ base
22
+ end
23
+
24
+ end # module HashBasedType
25
+ end # module UnionType
@@ -0,0 +1,13 @@
1
+ module Finitio
2
+ module RelBasedType
3
+
4
+ def to_json_schema(*args, &bl)
5
+ {
6
+ type: "array",
7
+ items: tuple_type.to_json_schema(*args, &bl),
8
+ uniqueItems: true
9
+ }
10
+ end
11
+
12
+ end # module RelBasedType
13
+ end # module Finitio
@@ -0,0 +1,12 @@
1
+ module Finitio
2
+ class SeqType
3
+
4
+ def to_json_schema(*args, &bl)
5
+ {
6
+ type: "array",
7
+ items: elm_type.to_json_schema(*args, &bl)
8
+ }
9
+ end
10
+
11
+ end # class SeqType
12
+ end # module Finitio
@@ -0,0 +1,13 @@
1
+ module Finitio
2
+ class SetType
3
+
4
+ def to_json_schema(*args, &bl)
5
+ {
6
+ type: "array",
7
+ items: elm_type.to_json_schema(*args, &bl),
8
+ uniqueItems: true
9
+ }
10
+ end
11
+
12
+ end # class SetType
13
+ end # module Finitio
@@ -0,0 +1,12 @@
1
+ module Finitio
2
+ class StructType
3
+
4
+ def to_json_schema(*args, &bl)
5
+ {
6
+ type: "array",
7
+ items: component_types.map{|c| c.to_json_schema(*args, &bl) }
8
+ }
9
+ end
10
+
11
+ end # class StructType
12
+ end # module Finitio
@@ -0,0 +1,10 @@
1
+ module Finitio
2
+ class SubType
3
+
4
+ def to_json_schema(*args, &bl)
5
+ # TODO: add support for constraints here...
6
+ super_type.to_json_schema(*args, &bl)
7
+ end
8
+
9
+ end # class SubType
10
+ end # module Finitio
@@ -0,0 +1,11 @@
1
+ module Finitio
2
+ class UnionType
3
+
4
+ def to_json_schema(*args, &bl)
5
+ {
6
+ anyOf: candidates.map{|c| c.to_json_schema(*args, &bl) }
7
+ }
8
+ end
9
+
10
+ end # class UnionType
11
+ end # module Finitio
@@ -2,7 +2,7 @@ module Finitio
2
2
  module Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 8
5
+ MINOR = 9
6
6
  TINY = 0
7
7
 
8
8
  def self.to_s
@@ -4,15 +4,15 @@ module Finitio
4
4
  describe Inference do
5
5
 
6
6
  let(:system) {
7
- Finitio.system <<-FIO
8
- @import finitio/data
7
+ Finitio.system <<~FIO
8
+ @import finitio/data
9
9
 
10
- Nil = .NilClass
11
- Boolean = .TrueClass|.FalseClass
12
- Integer = .Integer
13
- Date = .Date <iso8601> .String \\( s | Date.iso8601(s) )
14
- \\( d | d.iso8601 )
15
- String = .String
10
+ Nil = .NilClass
11
+ Boolean = .TrueClass|.FalseClass
12
+ Integer = .Integer
13
+ Date = .Date <iso8601> .String \\( s | Date.iso8601(s) )
14
+ \\( d | d.iso8601 )
15
+ String = .String
16
16
  FIO
17
17
  }
18
18
 
@@ -0,0 +1,20 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "AdType" do
4
+
5
+ let(:type) {
6
+ type = AdType.new(Color, [rgb_contract, hex_contract])
7
+ }
8
+
9
+ it 'works as expected' do
10
+ expect(type.to_json_schema).to eql({
11
+ anyOf: [
12
+ { type: "integer" },
13
+ { type: "string" }
14
+ ]
15
+ })
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "AliasType" do
4
+
5
+ let(:alias_type) {
6
+ AliasType.new(anyType, "X")
7
+ }
8
+
9
+ it 'works as expected' do
10
+ expect(alias_type.to_json_schema).to eql({})
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "AnyType" do
4
+
5
+ it 'works as expected' do
6
+ expect(anyType.to_json_schema).to eql({})
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,51 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "BuiltinType" do
4
+
5
+ let(:builtin_type) {
6
+ BuiltinType.new(ruby_type)
7
+ }
8
+
9
+ subject {
10
+ builtin_type.to_json_schema
11
+ }
12
+
13
+ context 'with NilClass' do
14
+ let(:ruby_type){ NilClass }
15
+
16
+ it 'works' do
17
+ expect(subject).to eql({ type: "null" })
18
+ end
19
+ end
20
+
21
+ context 'with String' do
22
+ let(:ruby_type){ String }
23
+
24
+ it 'works' do
25
+ expect(subject).to eql({ type: "string" })
26
+ end
27
+ end
28
+
29
+ [Fixnum, Bignum, Integer].each do |rt|
30
+ context "with #{rt}" do
31
+ let(:ruby_type){ rt }
32
+
33
+ it 'works' do
34
+ expect(subject).to eql({ type: "integer" })
35
+ end
36
+ end
37
+ end
38
+
39
+ [Float, Numeric].each do |rt|
40
+ context "with #{rt}" do
41
+ let(:ruby_type){ rt }
42
+
43
+ it 'works' do
44
+ expect(subject).to eql({ type: "number" })
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,58 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "MultiRelationType" do
4
+
5
+ context 'with a true allow extra and some optional attribute' do
6
+ let(:heading){
7
+ Heading.new([
8
+ Attribute.new(:a, anyType),
9
+ Attribute.new(:b, anyType, false),
10
+ ], allow_extra: true)
11
+ }
12
+
13
+ let(:multi_relation_type) {
14
+ MultiRelationType.new(heading)
15
+ }
16
+
17
+ it 'works as expected' do
18
+ expect(multi_relation_type.to_json_schema).to eql({
19
+ type: "array",
20
+ items: {
21
+ type: "object",
22
+ properties: {
23
+ a: {},
24
+ b: {}
25
+ },
26
+ required: [:a],
27
+ additionalProperties: {}
28
+ },
29
+ uniqueItems: true
30
+ })
31
+ end
32
+ end
33
+
34
+ context 'with a allow extra requiring Strings' do
35
+ let(:heading){
36
+ Heading.new([
37
+ ], allow_extra: BuiltinType.new(String))
38
+ }
39
+
40
+ let(:multi_relation_type) {
41
+ MultiRelationType.new(heading)
42
+ }
43
+
44
+ it 'works as expected' do
45
+ expect(multi_relation_type.to_json_schema).to eql({
46
+ type: "array",
47
+ items: {
48
+ type: "object",
49
+ additionalProperties: { type: "string" }
50
+ },
51
+ uniqueItems: true
52
+ })
53
+ end
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,50 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "MultiTupleType" do
4
+
5
+ context 'with a true allow extra and some optional attribute' do
6
+ let(:heading){
7
+ Heading.new([
8
+ Attribute.new(:a, anyType),
9
+ Attribute.new(:b, anyType, false),
10
+ ], allow_extra: true)
11
+ }
12
+
13
+ let(:multi_tuple_type) {
14
+ MultiTupleType.new(heading)
15
+ }
16
+
17
+ it 'works as expected' do
18
+ expect(multi_tuple_type.to_json_schema).to eql({
19
+ type: "object",
20
+ properties: {
21
+ a: {},
22
+ b: {}
23
+ },
24
+ required: [:a],
25
+ additionalProperties: {}
26
+ })
27
+ end
28
+ end
29
+
30
+ context 'with a allow extra requiring Strings' do
31
+ let(:heading){
32
+ Heading.new([
33
+ ], allow_extra: BuiltinType.new(String))
34
+ }
35
+
36
+ let(:multi_tuple_type) {
37
+ MultiTupleType.new(heading)
38
+ }
39
+
40
+ it 'works as expected' do
41
+ expect(multi_tuple_type.to_json_schema).to eql({
42
+ type: "object",
43
+ additionalProperties: { type: "string" }
44
+ })
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,30 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "RelationType" do
4
+
5
+ let(:heading){
6
+ Heading.new([Attribute.new(:a, anyType)])
7
+ }
8
+
9
+ let(:tuple_type) {
10
+ RelationType.new(heading)
11
+ }
12
+
13
+ it 'works as expected' do
14
+ expect(tuple_type.to_json_schema).to eql({
15
+ type: "array",
16
+ items: {
17
+ type: "object",
18
+ properties: {
19
+ a: {}
20
+ },
21
+ required: [:a],
22
+ additionalProperties: false
23
+ },
24
+ uniqueItems: true
25
+ })
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "SeqType" do
4
+
5
+ let(:type) {
6
+ SeqType.new(anyType)
7
+ }
8
+
9
+ it 'works as expected' do
10
+ expect(type.to_json_schema).to eql({
11
+ type: "array",
12
+ items: {}
13
+ })
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "SetType" do
4
+
5
+ let(:type) {
6
+ SetType.new(anyType)
7
+ }
8
+
9
+ it 'works as expected' do
10
+ expect(type.to_json_schema).to eql({
11
+ type: "array",
12
+ items: {},
13
+ uniqueItems: true
14
+ })
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "StructType" do
4
+
5
+ let(:type) {
6
+ StructType.new([anyType,anyType])
7
+ }
8
+
9
+ it 'works as expected' do
10
+ expect(type.to_json_schema).to eql({
11
+ type: "array",
12
+ items: [{},{}]
13
+ })
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "SubType" do
4
+
5
+ let(:type) {
6
+ byte
7
+ }
8
+
9
+ it 'skips constraints (for now)' do
10
+ expect(byte.to_json_schema).to eql({
11
+ type: "integer"
12
+ })
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "TupleType" do
4
+
5
+ let(:heading){
6
+ Heading.new([Attribute.new(:a, anyType)])
7
+ }
8
+
9
+ let(:tuple_type) {
10
+ TupleType.new(heading)
11
+ }
12
+
13
+ it 'works as expected' do
14
+ expect(tuple_type.to_json_schema).to eql({
15
+ type: "object",
16
+ properties: {
17
+ a: {}
18
+ },
19
+ required: [:a],
20
+ additionalProperties: false
21
+ })
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ module Finitio
2
+ module JsonSchema
3
+ describe "UnionType" do
4
+
5
+ let(:union_type) {
6
+ UnionType.new([anyType])
7
+ }
8
+
9
+ it 'works as expected' do
10
+ expect(union_type.to_json_schema).to eql({
11
+ anyOf: [{}]
12
+ })
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -3,6 +3,7 @@ require 'path'
3
3
  require 'finitio'
4
4
  require 'finitio/syntax'
5
5
  require 'finitio/generation'
6
+ require 'finitio/json_schema'
6
7
 
7
8
  require 'coveralls'
8
9
  Coveralls.wear!
@@ -126,7 +127,7 @@ module SpecHelpers
126
127
  end
127
128
 
128
129
  def hex_contract
129
- @hex_contract ||= Finitio::Contract.new(floatType, Color.method(:hex), Finitio::IDENTITY, :hex)
130
+ @hex_contract ||= Finitio::Contract.new(stringType, Color.method(:hex), Finitio::IDENTITY, :hex)
130
131
  end
131
132
 
132
133
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finitio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-21 00:00:00.000000000 Z
11
+ date: 2020-12-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: citrus
@@ -65,6 +65,18 @@ files:
65
65
  - lib/finitio/generation/sub_type.rb
66
66
  - lib/finitio/generation/union_type.rb
67
67
  - lib/finitio/inference.rb
68
+ - lib/finitio/json_schema.rb
69
+ - lib/finitio/json_schema/ad_type.rb
70
+ - lib/finitio/json_schema/alias_type.rb
71
+ - lib/finitio/json_schema/any_type.rb
72
+ - lib/finitio/json_schema/builtin_type.rb
73
+ - lib/finitio/json_schema/hash_based_type.rb
74
+ - lib/finitio/json_schema/rel_based_type.rb
75
+ - lib/finitio/json_schema/seq_type.rb
76
+ - lib/finitio/json_schema/set_type.rb
77
+ - lib/finitio/json_schema/struct_type.rb
78
+ - lib/finitio/json_schema/sub_type.rb
79
+ - lib/finitio/json_schema/union_type.rb
68
80
  - lib/finitio/stdlib/finitio/data.fio
69
81
  - lib/finitio/support.rb
70
82
  - lib/finitio/support/attribute.rb
@@ -186,6 +198,19 @@ files:
186
198
  - spec/heading/test_suppremum.rb
187
199
  - spec/heading/test_to_name.rb
188
200
  - spec/inference/test_inference.rb
201
+ - spec/json_schema/test_ad_type.rb
202
+ - spec/json_schema/test_alias_type.rb
203
+ - spec/json_schema/test_any_type.rb
204
+ - spec/json_schema/test_builtin_type.rb
205
+ - spec/json_schema/test_multi_relation_type.rb
206
+ - spec/json_schema/test_multi_tuple_type.rb
207
+ - spec/json_schema/test_relation_type.rb
208
+ - spec/json_schema/test_seq_type.rb
209
+ - spec/json_schema/test_set_type.rb
210
+ - spec/json_schema/test_struct_type.rb
211
+ - spec/json_schema/test_sub_type.rb
212
+ - spec/json_schema/test_tuple_type.rb
213
+ - spec/json_schema/test_union_type.rb
189
214
  - spec/spec_helper.rb
190
215
  - spec/support/test_compare_attrs.rb
191
216
  - spec/support/test_proc_with_code.rb
@@ -350,13 +375,25 @@ required_rubygems_version: !ruby/object:Gem::Requirement
350
375
  - !ruby/object:Gem::Version
351
376
  version: '0'
352
377
  requirements: []
353
- rubyforge_project:
354
- rubygems_version: 2.7.6
378
+ rubygems_version: 3.1.2
355
379
  signing_key:
356
380
  specification_version: 4
357
381
  summary: Finitio - in Ruby
358
382
  test_files:
359
383
  - spec/inference/test_inference.rb
384
+ - spec/json_schema/test_union_type.rb
385
+ - spec/json_schema/test_alias_type.rb
386
+ - spec/json_schema/test_builtin_type.rb
387
+ - spec/json_schema/test_set_type.rb
388
+ - spec/json_schema/test_multi_tuple_type.rb
389
+ - spec/json_schema/test_sub_type.rb
390
+ - spec/json_schema/test_relation_type.rb
391
+ - spec/json_schema/test_multi_relation_type.rb
392
+ - spec/json_schema/test_any_type.rb
393
+ - spec/json_schema/test_ad_type.rb
394
+ - spec/json_schema/test_seq_type.rb
395
+ - spec/json_schema/test_tuple_type.rb
396
+ - spec/json_schema/test_struct_type.rb
360
397
  - spec/syntax/nodes/test_system.rb
361
398
  - spec/syntax/nodes/test_union_type.rb
362
399
  - spec/syntax/nodes/test_constraints.rb