camille 0.5.10 → 0.5.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/Gemfile.lock +1 -1
- data/README.md +11 -0
- data/lib/camille/type.rb +9 -0
- data/lib/camille/type_error.rb +8 -2
- data/lib/camille/types/object.rb +8 -5
- data/lib/camille/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 321511ba40eb05246be9d567a74ac7c753287088b22e282207d6affe5cd2c7b6
|
4
|
+
data.tar.gz: 4f7bec2b9a36f8dec419131cbbd1a98e3d4735f20155320b2419e6e2fe6600e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 676055618b2b681f821b3a2d0fa87bf0f60f52e0416aad2dbdbc51eb4186bd19c699f4a121265cba56d5a1128f241f1e9f3bd32a9d1779dca58cc902b888fd71
|
7
|
+
data.tar.gz: e6545da7d1c98881b0fac58802b16c2a50ad2447039b9802d3617caf5f89d3ce68e67aaa2871f338b636fc463fb813f21ef1a040d13f5f43e70bf9ee890e2b29
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,22 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 0.5.12
|
4
|
+
|
5
|
+
### Added
|
6
|
+
|
7
|
+
* Added `.test` to `Camille::Type`
|
8
|
+
* Added error details to `Camille::TypeError#inspect` and `#to_s`
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
|
12
|
+
* Removed `Camille::TypeError#print` since details are now in `#inspect`
|
13
|
+
|
14
|
+
## 0.5.11
|
15
|
+
|
16
|
+
### Changed
|
17
|
+
|
18
|
+
* Optional fields are now preserved in generated TypeScript as having `?` suffix
|
19
|
+
|
3
20
|
## 0.5.10
|
4
21
|
|
5
22
|
### Added
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -128,6 +128,17 @@ Each custom type is considered a type alias in TypeScript. And `alias_of` define
|
|
128
128
|
type Product = {id: number, name: string}
|
129
129
|
```
|
130
130
|
|
131
|
+
You can perform a type check on a value using `test`, which might be handy in testing:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
error = Camille::Types::Product.test(hash)
|
135
|
+
if error.nil?
|
136
|
+
# the hash is accepted by Camille::Types::Product type
|
137
|
+
else
|
138
|
+
p error
|
139
|
+
end
|
140
|
+
```
|
141
|
+
|
131
142
|
### Available syntax for types
|
132
143
|
|
133
144
|
Camille supports most of the type syntax in TypeScript. Below is a list of types that you can use in type and schema definition.
|
data/lib/camille/type.rb
CHANGED
@@ -26,6 +26,15 @@ module Camille
|
|
26
26
|
@underlying.transform_and_check transformed
|
27
27
|
end
|
28
28
|
|
29
|
+
def test value
|
30
|
+
error, _ = transform_and_check value
|
31
|
+
error
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.test value
|
35
|
+
new.test value
|
36
|
+
end
|
37
|
+
|
29
38
|
def self.klass_name
|
30
39
|
name.gsub(/^Camille::Types::/, '')
|
31
40
|
end
|
data/lib/camille/type_error.rb
CHANGED
@@ -18,8 +18,14 @@ module Camille
|
|
18
18
|
!!@message
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
22
|
-
|
21
|
+
def inspect
|
22
|
+
string_io = StringIO.new
|
23
|
+
Camille::TypeErrorPrinter.new(self).print string_io
|
24
|
+
"#<Camille::TypeError\n#{string_io.string}>"
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
inspect
|
23
29
|
end
|
24
30
|
end
|
25
31
|
end
|
data/lib/camille/types/object.rb
CHANGED
@@ -14,11 +14,10 @@ module Camille
|
|
14
14
|
keys = (@fields.keys + value.keys).uniq
|
15
15
|
transform_and_check_results = keys.map do |key|
|
16
16
|
if type = @fields[key]
|
17
|
-
|
18
|
-
if @optional_keys.include?(key) && !error && transformed.nil?
|
17
|
+
if @optional_keys.include?(key) && value[key].nil?
|
19
18
|
nil
|
20
19
|
else
|
21
|
-
[key, [
|
20
|
+
[key, type.transform_and_check(value[key])]
|
22
21
|
end
|
23
22
|
else
|
24
23
|
[key, [nil, value[key]]]
|
@@ -43,7 +42,7 @@ module Camille
|
|
43
42
|
end
|
44
43
|
|
45
44
|
def literal
|
46
|
-
"{#{@fields.map{|k,v| "#{
|
45
|
+
"{#{@fields.map{|k,v| "#{literal_key k}: #{v.literal}"}.join(', ')}}"
|
47
46
|
end
|
48
47
|
|
49
48
|
private
|
@@ -54,7 +53,7 @@ module Camille
|
|
54
53
|
if key.end_with?('?')
|
55
54
|
new_key = remove_question_mark(key)
|
56
55
|
@optional_keys << new_key
|
57
|
-
[new_key, type
|
56
|
+
[new_key, type]
|
58
57
|
else
|
59
58
|
[key, type]
|
60
59
|
end
|
@@ -65,6 +64,10 @@ module Camille
|
|
65
64
|
sym.to_s.gsub(/\?$/, '').to_sym
|
66
65
|
end
|
67
66
|
|
67
|
+
def literal_key key
|
68
|
+
"#{ActiveSupport::Inflector.camelize key.to_s, false}#{@optional_keys.include?(key) ? '?' : ''}"
|
69
|
+
end
|
70
|
+
|
68
71
|
def check_case_conversion_safe sym
|
69
72
|
str = sym.to_s
|
70
73
|
if str != str.camelize.underscore
|
data/lib/camille/version.rb
CHANGED
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.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 辻彩
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-06-
|
11
|
+
date: 2023-06-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|