Skip to content

Commit c7678a7

Browse files
author
Nico Sammito
authored
Merge pull request #222 from code0-tech/feat/#219-#220-#221
Implementation of new signature pattern to use TS as type definition layer
2 parents f14b882 + d7b1cb0 commit c7678a7

7 files changed

Lines changed: 30 additions & 639 deletions

File tree

build/ruby/lib/tucana/shared/shared.data_type.rb

Lines changed: 0 additions & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,21 @@ def variant
1212

1313
def rule_config
1414
case variant
15-
when :contains_key
16-
self.contains_key
17-
when :contains_type
18-
self.contains_type
19-
when :item_of_collection
20-
self.item_of_collection
2115
when :number_range
2216
self.number_range
2317
when :regex
2418
self.regex
25-
when :input_types
26-
self.input_types
27-
when :return_type
28-
self.return_type
29-
when :parent_type
30-
self.parent_type
3119
else
3220
raise UnexpectedRuleType, "Unknown rule type #{variant}"
3321
end
3422
end
3523

3624
def create(variant, config)
3725
case variant
38-
when :contains_key
39-
self.contains_key = DataTypeContainsKeyRuleConfig.new(config)
40-
when :contains_type
41-
self.contains_type = DataTypeContainsTypeRuleConfig.new(config)
42-
when :item_of_collection
43-
self.item_of_collection = DataTypeItemOfCollectionRuleConfig.from_hash(config)
4426
when :number_range
4527
self.number_range = DataTypeNumberRangeRuleConfig.new(config)
4628
when :regex
4729
self.regex = DataTypeRegexRuleConfig.new(config)
48-
when :input_types
49-
self.input_types = DataTypeInputTypesRuleConfig.new(config)
50-
when :return_type
51-
self.return_type = DataTypeReturnTypeRuleConfig.new(config)
52-
when :parent_type
53-
self.parent_type = DataTypeParentTypeRuleConfig.from_hash(config)
5430
else
5531
raise UnexpectedRuleType, "Unknown rule type #{variant}"
5632
end
@@ -63,46 +39,6 @@ def self.create(variant, config)
6339
end
6440
end
6541

66-
DataTypeContainsKeyRuleConfig.class_eval do
67-
def to_h
68-
{
69-
key: self.key,
70-
data_type_identifier: self.data_type_identifier.to_h,
71-
}
72-
end
73-
74-
def self.from_hash(config)
75-
new(
76-
key: config[:key],
77-
data_type_identifier: DataTypeIdentifier.from_hash(config[:data_type_identifier])
78-
)
79-
end
80-
end
81-
82-
DataTypeContainsTypeRuleConfig.class_eval do
83-
def to_h
84-
{
85-
data_type_identifier: self.data_type_identifier.to_h,
86-
}
87-
end
88-
89-
def self.from_hash(config)
90-
new(data_type_identifier: DataTypeIdentifier.from_hash(config[:data_type_identifier]))
91-
end
92-
end
93-
94-
DataTypeItemOfCollectionRuleConfig.class_eval do
95-
def to_h
96-
{
97-
items: self.items.map { |item| item.to_ruby(true) },
98-
}
99-
end
100-
101-
def self.from_hash(hash)
102-
new(items: hash.fetch(:items).map { |item| Value.from_ruby(item) })
103-
end
104-
end
105-
10642
DataTypeNumberRangeRuleConfig.class_eval do
10743
def to_h
10844
{
@@ -120,154 +56,5 @@ def to_h
12056
}
12157
end
12258
end
123-
124-
DataTypeInputTypesRuleConfig.class_eval do
125-
def to_h
126-
{
127-
input_types: self.input_types.map { |input_type| input_type.to_h }
128-
}
129-
end
130-
131-
def self.from_hash(hash)
132-
new(
133-
input_types: hash.fetch(:input_types).map { |input_type| DataTypeInputType.from_hash(input_type) }
134-
)
135-
end
136-
end
137-
138-
DataTypeInputTypesRuleConfig::DataTypeInputType.class_eval do
139-
def to_h
140-
{
141-
data_type_identifier: self.data_type_identifier,
142-
input_identifier: self.input_identifier,
143-
}
144-
end
145-
146-
def self.from_hash(config)
147-
new(
148-
input_identifier: config[:input_identifier],
149-
data_type_identifier: DataTypeIdentifier.from_hash(config[:data_type_identifier])
150-
)
151-
end
152-
end
153-
154-
DataTypeReturnTypeRuleConfig.class_eval do
155-
def to_h
156-
{
157-
data_type_identifier: self.data_type_identifier,
158-
}
159-
end
160-
161-
def self.from_hash(config)
162-
new(data_type_identifier: DataTypeIdentifier.from_hash(config[:data_type_identifier]))
163-
end
164-
end
165-
166-
DataTypeParentTypeRuleConfig.class_eval do
167-
def to_h
168-
{
169-
parent_type: self.parent_type,
170-
}
171-
end
172-
173-
def self.from_hash(config)
174-
new(parent_type: DataTypeIdentifier.from_hash(config[:parent_type]))
175-
end
176-
end
177-
178-
DataTypeIdentifier.class_eval do
179-
def to_h
180-
if !self.data_type_identifier.empty?
181-
return { data_type_identifier: self.data_type_identifier }
182-
elsif !self.generic_type.nil?
183-
return { generic_type: self.generic_type.to_h }
184-
elsif !self.generic_key.nil?
185-
return { generic_key: self.generic_key }
186-
end
187-
end
188-
189-
def from_hash(config)
190-
if config.keys.intersection([:data_type_identifier, :generic_type, :generic_key]).count > 1
191-
raise UnexpectedType, "Cannot have more than one type"
192-
end
193-
194-
if config.key?(:data_type_identifier)
195-
self.data_type_identifier = config[:data_type_identifier]
196-
elsif config.key?(:generic_type)
197-
self.generic_type = GenericType.from_hash(config[:generic_type])
198-
elsif config.key?(:generic_key)
199-
self.generic_key = config[:generic_key]
200-
else
201-
raise UnexpectedType, "Unknown type"
202-
end
203-
204-
self
205-
end
206-
207-
def self.from_hash(config)
208-
new.from_hash(config)
209-
end
210-
end
211-
212-
GenericType.class_eval do
213-
def to_h
214-
{
215-
data_type_identifier: self.data_type_identifier,
216-
generic_mappers: self.generic_mappers.map(&:to_h),
217-
}
218-
end
219-
220-
def from_hash(config)
221-
if config.key?(:data_type_identifier)
222-
self.data_type_identifier = config.fetch(:data_type_identifier)
223-
end
224-
225-
if config.key?(:generic_mappers)
226-
self.generic_mappers.clear
227-
config[:generic_mappers].each do |mapper_config|
228-
self.generic_mappers << GenericMapper.from_hash(mapper_config)
229-
end
230-
end
231-
232-
self
233-
end
234-
235-
def self.from_hash(config)
236-
new.from_hash(config)
237-
end
238-
end
239-
240-
GenericMapper.class_eval do
241-
def to_h
242-
{
243-
target: self.target,
244-
source: self.source.map(&:to_h),
245-
generic_combinations: self.generic_combinations.map(&:to_h),
246-
}
247-
end
248-
249-
def from_hash(config)
250-
self.target = config[:target]
251-
if config.key?(:source)
252-
self.source.clear
253-
config[:source].each do |source|
254-
self.source << DataTypeIdentifier.from_hash(source)
255-
end
256-
end
257-
258-
if config.key?(:generic_combinations)
259-
self.generic_combinations.clear
260-
config[:generic_combinations].each do |combo|
261-
self.generic_combinations << GenericMapper::GenericCombinationStrategy.resolve(combo)
262-
end
263-
end
264-
265-
self
266-
end
267-
268-
def self.from_hash(config)
269-
new.from_hash(config)
270-
end
271-
end
27259
end
27360
end

build/ruby/spec/tucana/shared/shared.data_type_spec.rb

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -3,95 +3,7 @@
33
Tucana.load_protocol(:shared)
44

55
RSpec.describe Tucana::Shared::DefinitionDataTypeRule do
6-
describe '#from_hash' do
7-
context 'GenericType#from_hash' do
8-
it do
9-
generic_type = Tucana::Shared::GenericType.new.from_hash({
10-
data_type_identifier: 'SomeIdentifier',
11-
generic_mappers: [
12-
{
13-
source: [{ data_type_identifier: 'SomeType' }],
14-
target: 'T',
15-
generic_combinations: [:AND]
16-
}
17-
]
18-
})
19-
20-
expect(generic_type).not_to be_nil
21-
end
22-
end
23-
context 'GenericMapper#from_hash' do
24-
context 'when type is :map_to_generic_type' do
25-
it do
26-
generic_mapper = Tucana::Shared::GenericMapper.new.from_hash({
27-
source: [{ data_type_identifier: 'SomeType' }],
28-
target: 'T',
29-
generic_combinations: [:AND]
30-
})
31-
32-
expect(generic_mapper.to_s).to eq("<Tucana::Shared::GenericMapper: source: [<Tucana::Shared::DataTypeIdentifier: data_type_identifier: \"SomeType\">], target: \"T\", generic_combinations: [:AND]>")
33-
end
34-
end
35-
end
36-
end
376
describe '#create' do
38-
context 'DataTypeIdentifier#to_h' do
39-
context 'generic_key' do
40-
it do
41-
expect(Tucana::Shared::DataTypeIdentifier.new(generic_key: 'T').to_h)
42-
.to eq({ generic_key: 'T' })
43-
end
44-
end
45-
context 'identifier' do
46-
it do
47-
expect(Tucana::Shared::DataTypeIdentifier.new(data_type_identifier: 'MyType').to_h)
48-
.to eq({ data_type_identifier: 'MyType' })
49-
end
50-
end
51-
context 'generic_type' do
52-
it do
53-
generic_type = Tucana::Shared::GenericType.new(
54-
data_type_identifier: 'some_identifier',
55-
generic_mappers: []
56-
)
57-
58-
data_type_identifier = Tucana::Shared::DataTypeIdentifier.new(generic_type: generic_type)
59-
60-
expect(data_type_identifier.to_h)
61-
.to eq({
62-
generic_type: {
63-
data_type_identifier: 'some_identifier',
64-
generic_mappers: []
65-
}
66-
})
67-
end
68-
end
69-
end
70-
71-
context 'with :contains_key variant' do
72-
it 'sets the contains_key field' do
73-
config = { key: 'test_key', data_type_identifier: { data_type_identifier: 'test_type' } }
74-
rule = described_class.create(:contains_key, config)
75-
expect(rule.contains_key).to be_a(Tucana::Shared::DataTypeContainsKeyRuleConfig)
76-
end
77-
end
78-
79-
context 'with :contains_type variant' do
80-
it 'sets the contains_type field' do
81-
config = { data_type_identifier: { data_type_identifier: 'test_type' } }
82-
rule = described_class.create(:contains_type, config)
83-
expect(rule.contains_type).to be_a(Tucana::Shared::DataTypeContainsTypeRuleConfig)
84-
end
85-
end
86-
87-
context 'with :item_of_collection variant' do
88-
it 'sets the item_of_collection field' do
89-
config = { items: %w[item1 item2] }
90-
rule = described_class.create(:item_of_collection, config)
91-
expect(rule.item_of_collection).to be_a(Tucana::Shared::DataTypeItemOfCollectionRuleConfig)
92-
end
93-
end
94-
957
context 'with :number_range variant' do
968
it 'sets the number_range field' do
979
config = { from: 1, to: 10 }
@@ -108,31 +20,6 @@
10820
end
10921
end
11022

111-
context 'with :input_types variant' do
112-
it 'sets the input_types field' do
113-
config = { input_types: [{ data_type_identifier: { data_type_identifier: 'test_type' },
114-
input_identifier: 'test_input' }] }
115-
rule = described_class.create(:input_types, config)
116-
expect(rule.input_types).to be_a(Tucana::Shared::DataTypeInputTypesRuleConfig)
117-
end
118-
end
119-
120-
context 'with :return_type variant' do
121-
it 'sets the return_type field' do
122-
config = { data_type_identifier: { data_type_identifier: 'test_type' } }
123-
rule = described_class.create(:return_type, config)
124-
expect(rule.return_type).to be_a(Tucana::Shared::DataTypeReturnTypeRuleConfig)
125-
end
126-
end
127-
128-
context 'with :parent_type variant' do
129-
it 'sets the parent_type field' do
130-
config = { parent_type: { data_type_identifier: 'test_type' } }
131-
rule = described_class.create(:parent_type, config)
132-
expect(rule.parent_type).to be_a(Tucana::Shared::DataTypeParentTypeRuleConfig)
133-
end
134-
end
135-
13623
context 'with unknown variant' do
13724
it 'raises UnexpectedRuleType error' do
13825
expect do
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
pub mod path;
2-
pub mod rule;
32
pub mod value;

0 commit comments

Comments
 (0)