uinit-type 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +0 -0
- data/LICENSE.txt +21 -0
- data/README.md +35 -0
- data/lib/uinit/type/array_of.rb +33 -0
- data/lib/uinit/type/base.rb +50 -0
- data/lib/uinit/type/check.rb +50 -0
- data/lib/uinit/type/composition.rb +115 -0
- data/lib/uinit/type/const.rb +40 -0
- data/lib/uinit/type/context.rb +84 -0
- data/lib/uinit/type/error.rb +41 -0
- data/lib/uinit/type/extensions.rb +9 -0
- data/lib/uinit/type/fn.rb +43 -0
- data/lib/uinit/type/generic.rb +19 -0
- data/lib/uinit/type/hash_of.rb +61 -0
- data/lib/uinit/type/impl.rb +63 -0
- data/lib/uinit/type/instance.rb +39 -0
- data/lib/uinit/type/operators.rb +15 -0
- data/lib/uinit/type/set_of.rb +35 -0
- data/lib/uinit/type/version.rb +7 -0
- data/lib/uinit/type.rb +33 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c8294bdb922588856e7af78dcd2820da93c237a00cd2db24e90133600610e8ae
|
4
|
+
data.tar.gz: 173370c0263e2b555b906947fabaa59956ec248b9ce022837b7869069239308e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0ca2c4d8a705f0e5ef4eb5034355c870b5bb8fc479ce5a652034eb8b956d2350693ad6eaaf59f5ef79fac997c72bfcc92292b7460fbc55f4bce257d82329061c
|
7
|
+
data.tar.gz: 9cb5b519afd44840f88246be9c1052f3ea37bccd2100b23ff9192ad2ef3f0ebf931783fa1bf6de1f698035d5ce1fe6e8a3b67e3a90875594f01a720c6a681797
|
data/CHANGELOG.md
ADDED
File without changes
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Kimoja
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Uinit::Type
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/uinit/type`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Development
|
24
|
+
|
25
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
26
|
+
|
27
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/uinit-type.
|
32
|
+
|
33
|
+
## License
|
34
|
+
|
35
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
class ArrayOf < Generic
|
6
|
+
def self.from?(array)
|
7
|
+
array.is_a?(Array)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.from(array)
|
11
|
+
new(array.first) if from?(array)
|
12
|
+
end
|
13
|
+
|
14
|
+
def is?(value)
|
15
|
+
return false unless value.is_a?(Array)
|
16
|
+
|
17
|
+
value.all? { |val| type.is?(val) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def check!(value, depth)
|
21
|
+
type_error!("#{value.inspect} must be an Array", depth) unless value.is_a?(Array)
|
22
|
+
|
23
|
+
value.each_with_index do |val, index|
|
24
|
+
type.check!(val, depth + 1)
|
25
|
+
rescue Error => e
|
26
|
+
trace!(e, "[#{index}]")
|
27
|
+
end
|
28
|
+
|
29
|
+
value
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
class Base
|
6
|
+
include Operators
|
7
|
+
|
8
|
+
def self.from?(type)
|
9
|
+
type.is_a?(Base)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.from(type)
|
13
|
+
type if from?(type)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.[](...)
|
17
|
+
new(...)
|
18
|
+
end
|
19
|
+
|
20
|
+
def type_error!(message, depth, trace = [])
|
21
|
+
raise Error.new(message.to_s, [self], depth, trace)
|
22
|
+
end
|
23
|
+
|
24
|
+
def trace!(error, trace)
|
25
|
+
error.trace(self, trace)
|
26
|
+
|
27
|
+
raise error
|
28
|
+
end
|
29
|
+
|
30
|
+
def inspect
|
31
|
+
"$#{self.class.name.split('::').last}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_s
|
35
|
+
inspect
|
36
|
+
end
|
37
|
+
|
38
|
+
def ==(other)
|
39
|
+
inspect == other.inspect
|
40
|
+
end
|
41
|
+
|
42
|
+
def is!(value)
|
43
|
+
check!(value, 0)
|
44
|
+
rescue Error => e
|
45
|
+
e.value = value
|
46
|
+
raise
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
class Check < Base
|
6
|
+
def self.from?(check)
|
7
|
+
check.is_a?(Proc)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.from(check)
|
11
|
+
new(check) if from?(check)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(check)
|
15
|
+
super()
|
16
|
+
|
17
|
+
raise ArgumentError, 'check must be a Proc' unless self.class.from?(check)
|
18
|
+
|
19
|
+
@check = check
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_reader :check
|
23
|
+
|
24
|
+
def is?(value)
|
25
|
+
res = check[value]
|
26
|
+
|
27
|
+
res == true || res.nil?
|
28
|
+
end
|
29
|
+
|
30
|
+
def check!(value, depth)
|
31
|
+
res = check[value]
|
32
|
+
|
33
|
+
return value if res == true || res.nil?
|
34
|
+
|
35
|
+
file, line = check.source_location
|
36
|
+
|
37
|
+
type_error!(
|
38
|
+
"#{value.inspect} does not respect rule specified in #{file}:#{line}",
|
39
|
+
depth
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
def inspect
|
44
|
+
file, line = check.source_location
|
45
|
+
|
46
|
+
"#{super}[#{file}:#{line}]"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
class Composition < Base
|
6
|
+
INTERSECTION = '&'
|
7
|
+
UNION = '|'
|
8
|
+
|
9
|
+
def initialize(operand, compositions = [])
|
10
|
+
super()
|
11
|
+
|
12
|
+
@operand = Type.from(operand)
|
13
|
+
@compositions = compositions
|
14
|
+
end
|
15
|
+
|
16
|
+
attr_reader :operand, :compositions
|
17
|
+
|
18
|
+
def &(other)
|
19
|
+
clone.add_intersection_composition(other)
|
20
|
+
end
|
21
|
+
|
22
|
+
def |(other)
|
23
|
+
clone.add_union_composition(other)
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_intersection_composition(operand)
|
27
|
+
add_composition(INTERSECTION, operand)
|
28
|
+
end
|
29
|
+
|
30
|
+
def add_union_composition(operand)
|
31
|
+
add_composition(UNION, operand)
|
32
|
+
end
|
33
|
+
|
34
|
+
def is?(value)
|
35
|
+
is = operand.is?(value)
|
36
|
+
|
37
|
+
compositions.each do |operator, operand|
|
38
|
+
break if is && operator == UNION
|
39
|
+
next if !is && operator == INTERSECTION
|
40
|
+
|
41
|
+
is = operand.is?(value)
|
42
|
+
end
|
43
|
+
|
44
|
+
is
|
45
|
+
end
|
46
|
+
|
47
|
+
# rubocop:disable Metrics/AbcSize
|
48
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
49
|
+
# rubocop:disable Metrics/PerceivedComplexity
|
50
|
+
def check!(value, depth)
|
51
|
+
errors = []
|
52
|
+
is = true
|
53
|
+
|
54
|
+
begin
|
55
|
+
operand.check!(value, depth + 1)
|
56
|
+
rescue Error => e
|
57
|
+
errors << e
|
58
|
+
is = false
|
59
|
+
end
|
60
|
+
|
61
|
+
compositions.each do |operator, operand|
|
62
|
+
break if is && operator == UNION
|
63
|
+
next if !is && operator == INTERSECTION
|
64
|
+
|
65
|
+
begin
|
66
|
+
operand.check!(value, depth + 1)
|
67
|
+
is = true
|
68
|
+
rescue Error => e
|
69
|
+
errors << e
|
70
|
+
is = false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
return value if is
|
75
|
+
|
76
|
+
type_error!(value, errors.max_by(&:depth))
|
77
|
+
end
|
78
|
+
# rubocop:enable Metrics/AbcSize
|
79
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
80
|
+
# rubocop:enable Metrics/PerceivedComplexity
|
81
|
+
|
82
|
+
def inspect
|
83
|
+
"(#{[operand, *compositions.flatten].join(' ')})"
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def add_composition(operator, operand)
|
89
|
+
compositions << [operator, Type.from(operand)]
|
90
|
+
|
91
|
+
self
|
92
|
+
end
|
93
|
+
|
94
|
+
def clone
|
95
|
+
Composition.new(operand, compositions.dup)
|
96
|
+
end
|
97
|
+
|
98
|
+
def type_error!(value, deepest_error)
|
99
|
+
error_message =
|
100
|
+
if deepest_error.types.first.is_a?(Composition)
|
101
|
+
deepest_error.error_message
|
102
|
+
else
|
103
|
+
"#{value.inspect}: No type composition matches\nDeepest error: #{deepest_error.error_message}"
|
104
|
+
end
|
105
|
+
|
106
|
+
raise Error.new(
|
107
|
+
error_message,
|
108
|
+
[self] + deepest_error.types.dup,
|
109
|
+
deepest_error.depth,
|
110
|
+
deepest_error.traces
|
111
|
+
)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
class Const < Base
|
6
|
+
def self.from?(_val)
|
7
|
+
true
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.from(val)
|
11
|
+
new(val) if from?(val)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(value)
|
15
|
+
super()
|
16
|
+
|
17
|
+
@value = value
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :value
|
21
|
+
|
22
|
+
def is?(value)
|
23
|
+
self.value == value
|
24
|
+
end
|
25
|
+
|
26
|
+
def check!(value, depth)
|
27
|
+
return value if is?(value)
|
28
|
+
|
29
|
+
type_error!(
|
30
|
+
"#{value.inspect} must be equal to #{self.value.inspect}",
|
31
|
+
depth
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
def inspect
|
36
|
+
"#{super}[#{value.inspect}]"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
module Context
|
6
|
+
Nil = Const[nil].freeze
|
7
|
+
Boolean = (Const[true] | Const[false]).freeze
|
8
|
+
Str = Instance[String].freeze
|
9
|
+
Int = Instance[Integer].freeze
|
10
|
+
Float = Instance[Float].freeze
|
11
|
+
Hsh = Instance[Hash].freeze
|
12
|
+
Arr = Instance[Array].freeze
|
13
|
+
|
14
|
+
def array_of(type)
|
15
|
+
ArrayOf.new(type)
|
16
|
+
end
|
17
|
+
|
18
|
+
def any_of(const, *consts)
|
19
|
+
consts.reduce(Const[const]) { |type, cst| type | Const[cst] }
|
20
|
+
end
|
21
|
+
|
22
|
+
def check(check = nil, &proc_check)
|
23
|
+
Check.new(check || proc_check)
|
24
|
+
end
|
25
|
+
|
26
|
+
def const(const)
|
27
|
+
Const.new(const)
|
28
|
+
end
|
29
|
+
|
30
|
+
def fn(arity = -1)
|
31
|
+
Fn.new(arity)
|
32
|
+
end
|
33
|
+
|
34
|
+
def hash_of(schema, strict: false)
|
35
|
+
HashOf.new(schema, strict:)
|
36
|
+
end
|
37
|
+
|
38
|
+
def impl(*, **)
|
39
|
+
Impl.new(*, **)
|
40
|
+
end
|
41
|
+
|
42
|
+
def instance(type)
|
43
|
+
Instance.new(type)
|
44
|
+
end
|
45
|
+
|
46
|
+
def set_of(type) # rubocop:disable Naming/AccessorMethodName
|
47
|
+
SetOf.new(type)
|
48
|
+
end
|
49
|
+
|
50
|
+
def nilable
|
51
|
+
Nil
|
52
|
+
end
|
53
|
+
alias null nilable
|
54
|
+
alias none nilable
|
55
|
+
|
56
|
+
def boolean
|
57
|
+
Boolean
|
58
|
+
end
|
59
|
+
alias bool boolean
|
60
|
+
|
61
|
+
def string
|
62
|
+
Str
|
63
|
+
end
|
64
|
+
alias str string
|
65
|
+
|
66
|
+
def integer
|
67
|
+
Int
|
68
|
+
end
|
69
|
+
alias int integer
|
70
|
+
|
71
|
+
def float
|
72
|
+
Float
|
73
|
+
end
|
74
|
+
|
75
|
+
def hash
|
76
|
+
Hsh
|
77
|
+
end
|
78
|
+
|
79
|
+
def array
|
80
|
+
Arr
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
class Error < TypeError
|
6
|
+
def initialize(error_message, types, depth, trace = [])
|
7
|
+
super()
|
8
|
+
|
9
|
+
@error_message = error_message
|
10
|
+
@types = types
|
11
|
+
@depth = depth
|
12
|
+
@traces = trace
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :error_message, :instance, :types, :depth, :traces
|
16
|
+
attr_accessor :value
|
17
|
+
|
18
|
+
def trace(type, trace)
|
19
|
+
types.unshift(type)
|
20
|
+
traces.unshift(trace)
|
21
|
+
end
|
22
|
+
|
23
|
+
def message
|
24
|
+
msg = error_message
|
25
|
+
|
26
|
+
msg =
|
27
|
+
if types.size == 1
|
28
|
+
"#{msg}\nType: #{types.first}"
|
29
|
+
else
|
30
|
+
"#{msg}\nTypes:\n #{types * "\n "}"
|
31
|
+
end
|
32
|
+
|
33
|
+
msg = "#{msg}\nTraces: #{traces.join}" unless traces.empty?
|
34
|
+
|
35
|
+
msg = "#{msg}\nValue: #{value.inspect}" if value
|
36
|
+
|
37
|
+
msg
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
class Fn < Base
|
6
|
+
# TODO: arguments
|
7
|
+
def initialize(arity = -1)
|
8
|
+
super()
|
9
|
+
|
10
|
+
raise ArgumentError, 'arity must be an Integer' unless arity.is_a?(Integer)
|
11
|
+
|
12
|
+
@arity = arity
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :arity
|
16
|
+
|
17
|
+
def is?(value)
|
18
|
+
return false unless value.is_a?(Proc) || value.is_a?(Method) || value.is_a?(UnboundMethod)
|
19
|
+
|
20
|
+
return true if arity == -1 || value.arity == -1
|
21
|
+
|
22
|
+
value.arity == arity
|
23
|
+
end
|
24
|
+
|
25
|
+
def check!(value, depth)
|
26
|
+
unless value.is_a?(Proc) || value.is_a?(Method) || value.is_a?(UnboundMethod)
|
27
|
+
type_error!("#{value.inspect} must be an Proc a Method or a UnboundMethod", depth)
|
28
|
+
end
|
29
|
+
|
30
|
+
return value if arity == -1 || value.arity == -1 || value.arity == arity
|
31
|
+
|
32
|
+
type_error!(
|
33
|
+
"#{value.inspect} does not respect expected arity #{arity}, defined arity #{value.arity}",
|
34
|
+
depth
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
def inspect
|
39
|
+
"#{super}[arity = #{arity}]"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
class Generic < Base
|
6
|
+
def initialize(type)
|
7
|
+
super()
|
8
|
+
|
9
|
+
@type = Type.from(type)
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :type
|
13
|
+
|
14
|
+
def inspect
|
15
|
+
"#{super}[#{type.inspect}]"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
class HashOf < Base
|
6
|
+
def self.from?(schema)
|
7
|
+
schema.is_a?(Hash)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.from(schema)
|
11
|
+
new(schema) if from?(schema)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(schema, strict: false)
|
15
|
+
super()
|
16
|
+
|
17
|
+
raise ArgumentError, 'schema must be a Hash' unless self.class.from?(schema)
|
18
|
+
|
19
|
+
@schema =
|
20
|
+
schema.transform_values do |type|
|
21
|
+
Type.from(type)
|
22
|
+
end
|
23
|
+
|
24
|
+
@strict = !!strict
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_reader :schema, :strict
|
28
|
+
|
29
|
+
def is?(value)
|
30
|
+
return false unless value.is_a?(Hash)
|
31
|
+
|
32
|
+
is =
|
33
|
+
schema.all? do |key, type|
|
34
|
+
type.is?(value[key])
|
35
|
+
end
|
36
|
+
|
37
|
+
return is unless strict
|
38
|
+
|
39
|
+
extra = value.keys - schema
|
40
|
+
|
41
|
+
extra.empty?
|
42
|
+
end
|
43
|
+
|
44
|
+
def check!(value, depth)
|
45
|
+
type_error!("#{value.inspect} must be a Hash", depth) unless value.is_a?(Hash)
|
46
|
+
|
47
|
+
schema.each do |key, type|
|
48
|
+
type.check!(value[key], depth + 1)
|
49
|
+
rescue Error => e
|
50
|
+
trace!(e, "[#{key.inspect}]")
|
51
|
+
end
|
52
|
+
|
53
|
+
value
|
54
|
+
end
|
55
|
+
|
56
|
+
def inspect
|
57
|
+
"#{super}[#{schema.inspect}]"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
class Impl < Base
|
6
|
+
# TODO: Allow to pass Fn
|
7
|
+
def initialize(*sym_interface, **interface)
|
8
|
+
super()
|
9
|
+
|
10
|
+
sym_interface.each do |meth|
|
11
|
+
next if meth.is_a?(Symbol)
|
12
|
+
|
13
|
+
raise ArgumentError, "#{meth.inspect} must be a Symbol"
|
14
|
+
end
|
15
|
+
|
16
|
+
interface.each do |(meth, fn)|
|
17
|
+
next if fn.nil? || fn.is_a?(Fn)
|
18
|
+
|
19
|
+
raise ArgumentError, "#{meth.inspect} must be Fn or Nil"
|
20
|
+
end
|
21
|
+
|
22
|
+
@interface =
|
23
|
+
sym_interface.each_with_object(interface.dup) do |meth, inter|
|
24
|
+
inter[meth] = nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_reader :interface
|
29
|
+
|
30
|
+
def is?(value)
|
31
|
+
interface.all? do |(meth, fn)|
|
32
|
+
is = value.respond_to?(meth)
|
33
|
+
|
34
|
+
next is if !is || fn.nil?
|
35
|
+
|
36
|
+
fn.is?(value.method(meth))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def check!(value, depth)
|
41
|
+
interface.each do |meth, fn|
|
42
|
+
is = value.respond_to?(meth)
|
43
|
+
|
44
|
+
type_error!("#{value.inspect} do not implement #{meth.inspect} method", depth) unless is
|
45
|
+
|
46
|
+
next if fn.nil?
|
47
|
+
|
48
|
+
begin
|
49
|
+
fn.check!(value.method(meth), depth + 1)
|
50
|
+
rescue Error => e
|
51
|
+
trace!(e, ".#{meth}()")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
value
|
56
|
+
end
|
57
|
+
|
58
|
+
def inspect
|
59
|
+
"#{super}[#{interface.inspect}]"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
class Instance < Base
|
6
|
+
def self.from?(class_module)
|
7
|
+
class_module.is_a?(::Class) || class_module.is_a?(::Module)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.from(class_module)
|
11
|
+
new(class_module) if from?(class_module)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(class_module)
|
15
|
+
super()
|
16
|
+
|
17
|
+
raise ArgumentError, 'class_module must be a Class or a Module' unless self.class.from?(class_module)
|
18
|
+
|
19
|
+
@class_module = class_module
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_reader :class_module
|
23
|
+
|
24
|
+
def is?(value)
|
25
|
+
value.is_a?(class_module)
|
26
|
+
end
|
27
|
+
|
28
|
+
def check!(value, depth)
|
29
|
+
return value if is?(value)
|
30
|
+
|
31
|
+
type_error!("#{value.inspect} must be an instance of #{class_module}", depth)
|
32
|
+
end
|
33
|
+
|
34
|
+
def inspect
|
35
|
+
"#{super}[#{class_module.inspect}]"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
module Operators
|
6
|
+
def &(other)
|
7
|
+
Composition[self].add_intersection_composition(other)
|
8
|
+
end
|
9
|
+
|
10
|
+
def |(other)
|
11
|
+
Composition[self].add_union_composition(other)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Uinit
|
4
|
+
module Type
|
5
|
+
class SetOf < Generic
|
6
|
+
def self.from?(set)
|
7
|
+
set.is_a?(::Set)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.from(set)
|
11
|
+
new(set.first) if from?(set)
|
12
|
+
end
|
13
|
+
|
14
|
+
def is?(value)
|
15
|
+
return false unless value.is_a?(::Set)
|
16
|
+
|
17
|
+
value.all? do |val|
|
18
|
+
type.is?(val)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def check!(value, depth)
|
23
|
+
type_error!("#{value.inspect} must be a Set", depth) unless value.is_a?(::Set)
|
24
|
+
|
25
|
+
value.each_with_index do |val, index|
|
26
|
+
type.check!(val, depth + 1)
|
27
|
+
rescue Error => e
|
28
|
+
trace!(e, "[#{index}]")
|
29
|
+
end
|
30
|
+
|
31
|
+
value
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/uinit/type.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'zeitwerk'
|
4
|
+
|
5
|
+
module Uinit; end
|
6
|
+
|
7
|
+
Zeitwerk::Loader.for_gem.tap do |loader|
|
8
|
+
loader.push_dir(__dir__, namespace: Uinit)
|
9
|
+
loader.setup
|
10
|
+
end
|
11
|
+
|
12
|
+
module Uinit
|
13
|
+
module Type
|
14
|
+
def self.is?(type, value)
|
15
|
+
from(type).is?(value)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.is!(type, value)
|
19
|
+
from(type).is?(value)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.from(arg)
|
23
|
+
klass =
|
24
|
+
[Base, Instance, ArrayOf, SetOf, HashOf, Check, Const].find do |type_class|
|
25
|
+
type_class.from?(arg)
|
26
|
+
end
|
27
|
+
|
28
|
+
klass.from(arg)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
require_relative 'type/extensions'
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uinit-type
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kimoja
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-05-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: zeitwerk
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Dynamic type checker
|
84
|
+
email:
|
85
|
+
- joakim.carrilho@cheerz.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- CHANGELOG.md
|
91
|
+
- LICENSE.txt
|
92
|
+
- README.md
|
93
|
+
- lib/uinit/type.rb
|
94
|
+
- lib/uinit/type/array_of.rb
|
95
|
+
- lib/uinit/type/base.rb
|
96
|
+
- lib/uinit/type/check.rb
|
97
|
+
- lib/uinit/type/composition.rb
|
98
|
+
- lib/uinit/type/const.rb
|
99
|
+
- lib/uinit/type/context.rb
|
100
|
+
- lib/uinit/type/error.rb
|
101
|
+
- lib/uinit/type/extensions.rb
|
102
|
+
- lib/uinit/type/fn.rb
|
103
|
+
- lib/uinit/type/generic.rb
|
104
|
+
- lib/uinit/type/hash_of.rb
|
105
|
+
- lib/uinit/type/impl.rb
|
106
|
+
- lib/uinit/type/instance.rb
|
107
|
+
- lib/uinit/type/operators.rb
|
108
|
+
- lib/uinit/type/set_of.rb
|
109
|
+
- lib/uinit/type/version.rb
|
110
|
+
homepage: https://github.com/Kimoja/uinit-type
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata:
|
114
|
+
homepage_uri: https://github.com/Kimoja/uinit-type
|
115
|
+
allowed_push_host: https://rubygems.org
|
116
|
+
source_code_uri: https://github.com/Kimoja/uinit-type
|
117
|
+
changelog_uri: https://github.com/Kimoja/uinit-type/blob/main/CHANGELOG.md
|
118
|
+
bug_tracker_uri: https://github.com/Kimoja/uinit-type/issues
|
119
|
+
rubygems_mfa_required: 'true'
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: 3.2.1
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubygems_version: 3.4.6
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Dynamic type checker
|
139
|
+
test_files: []
|