camille 0.5.8 → 0.5.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/Gemfile.lock +1 -1
- data/README.md +2 -0
- data/lib/camille/basic_type.rb +8 -0
- data/lib/camille/type_error.rb +4 -0
- data/lib/camille/types/intersection.rb +39 -0
- data/lib/camille/types/object.rb +11 -6
- data/lib/camille/types/union.rb +1 -1
- data/lib/camille/version.rb +1 -1
- data/lib/camille.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd8f26b87f7ddf2f3c951a19a24610abfe1bfac4f4da041568883cc371090895
|
4
|
+
data.tar.gz: bbecf051c37ec9da1eae0920a6ec42c09d159feef1f2be734beaef2871af7410
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53bb5fd170e71b21687dadba5ab1ce6c5a81bcb1a4ede09e280103cd81693fb8e9f41a3a15af630b2d48b62f150f29c1f222d8308fff2636135c5653d9ca3da3
|
7
|
+
data.tar.gz: 47c9621bfdeb542e625f2115eb96b7d1d14fdecfd5f6cefe62bd6f92be2703d7e3e7518e96df26af3c692fb5887ac08ebe26db38eaeae8a8cfb059eeb07de839
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,20 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 0.5.9
|
4
|
+
|
5
|
+
### Added
|
6
|
+
|
7
|
+
* Added intersection type
|
8
|
+
* Added #print to Camille::TypeError
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
|
12
|
+
* Typecheck will no longer remove fields from a hash if those fields are not presented in type schema
|
13
|
+
|
14
|
+
### Fixed
|
15
|
+
|
16
|
+
* Fixed the wrong literal generated by an array of unions
|
17
|
+
|
3
18
|
## 0.5.8
|
4
19
|
|
5
20
|
### Added
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -153,6 +153,8 @@ params(
|
|
153
153
|
}[]
|
154
154
|
# a union type is two types connected by '|'
|
155
155
|
union: Number | String,
|
156
|
+
# an intersection type is two types connected by '&'
|
157
|
+
intersection: { id: Number } & { name: String },
|
156
158
|
# a tuple type is several types put inside '[]'
|
157
159
|
tuple: [Number, String, Boolean],
|
158
160
|
# a field followed by '?' is optional, the same as in TypeScript
|
data/lib/camille/basic_type.rb
CHANGED
@@ -7,6 +7,10 @@ module Camille
|
|
7
7
|
Camille::Types::Union.new(self, other)
|
8
8
|
end
|
9
9
|
|
10
|
+
def & other
|
11
|
+
Camille::Types::Intersection.new(self, other)
|
12
|
+
end
|
13
|
+
|
10
14
|
def []
|
11
15
|
Camille::Types::Array.new(self)
|
12
16
|
end
|
@@ -24,6 +28,10 @@ module Camille
|
|
24
28
|
Camille::Type.instance(self) | other
|
25
29
|
end
|
26
30
|
|
31
|
+
def self.& other
|
32
|
+
Camille::Type.instance(self) & other
|
33
|
+
end
|
34
|
+
|
27
35
|
def self.[]
|
28
36
|
Camille::Type.instance(self)[]
|
29
37
|
end
|
data/lib/camille/type_error.rb
CHANGED
@@ -0,0 +1,39 @@
|
|
1
|
+
module Camille
|
2
|
+
module Types
|
3
|
+
class Intersection < Camille::BasicType
|
4
|
+
class ArgumentError < ::ArgumentError; end
|
5
|
+
|
6
|
+
attr_reader :left, :right
|
7
|
+
|
8
|
+
def initialize left, right
|
9
|
+
@left = Camille::Type.instance left
|
10
|
+
@right = Camille::Type.instance right
|
11
|
+
end
|
12
|
+
|
13
|
+
def transform_and_check value
|
14
|
+
left_error, left_transformed = @left.transform_and_check value
|
15
|
+
if left_error
|
16
|
+
error = Camille::TypeError.new(
|
17
|
+
'intersection.left' => left_error
|
18
|
+
)
|
19
|
+
[error, nil]
|
20
|
+
else
|
21
|
+
right_error, right_transformed = @right.transform_and_check left_transformed
|
22
|
+
if right_error
|
23
|
+
error = Camille::TypeError.new(
|
24
|
+
'intersection.right' => right_error
|
25
|
+
)
|
26
|
+
[error, nil]
|
27
|
+
else
|
28
|
+
[nil, right_transformed]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def literal
|
34
|
+
"(#{@left.literal} & #{@right.literal})"
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/camille/types/object.rb
CHANGED
@@ -11,12 +11,17 @@ module Camille
|
|
11
11
|
|
12
12
|
def transform_and_check value
|
13
13
|
if value.is_a? Hash
|
14
|
-
|
15
|
-
|
16
|
-
if @
|
17
|
-
|
14
|
+
keys = (@fields.keys + value.keys).uniq
|
15
|
+
transform_and_check_results = keys.map do |key|
|
16
|
+
if type = @fields[key]
|
17
|
+
error, transformed = type.transform_and_check(value[key])
|
18
|
+
if @optional_keys.include?(key) && !error && transformed.nil?
|
19
|
+
nil
|
20
|
+
else
|
21
|
+
[key, [error, transformed]]
|
22
|
+
end
|
18
23
|
else
|
19
|
-
[key,
|
24
|
+
[key, [nil, value[key]]]
|
20
25
|
end
|
21
26
|
end.compact
|
22
27
|
|
@@ -63,7 +68,7 @@ module Camille
|
|
63
68
|
def check_case_conversion_safe sym
|
64
69
|
str = sym.to_s
|
65
70
|
if str != str.camelize.underscore
|
66
|
-
raise ArgumentError.new("Only keys satisfying `key.to_s == key.to_s.
|
71
|
+
raise ArgumentError.new("Only keys satisfying `key.to_s == key.to_s.camelize.underscore` can be used.")
|
67
72
|
end
|
68
73
|
end
|
69
74
|
end
|
data/lib/camille/types/union.rb
CHANGED
data/lib/camille/version.rb
CHANGED
data/lib/camille.rb
CHANGED
@@ -13,6 +13,7 @@ require_relative "camille/types/object"
|
|
13
13
|
require_relative "camille/types/null"
|
14
14
|
require_relative "camille/types/undefined"
|
15
15
|
require_relative "camille/types/union"
|
16
|
+
require_relative "camille/types/intersection"
|
16
17
|
require_relative "camille/types/tuple"
|
17
18
|
require_relative "camille/types/any"
|
18
19
|
require_relative "camille/types/number_literal"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: camille
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 辻彩
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- lib/camille/types/array.rb
|
79
79
|
- lib/camille/types/boolean.rb
|
80
80
|
- lib/camille/types/boolean_literal.rb
|
81
|
+
- lib/camille/types/intersection.rb
|
81
82
|
- lib/camille/types/null.rb
|
82
83
|
- lib/camille/types/number.rb
|
83
84
|
- lib/camille/types/number_literal.rb
|