Skip to content

Commit 3c4b6d2

Browse files
Merge pull request #93 from code0-tech/92-generics
Add new messages for generics
2 parents 9e245f9 + 0228dcd commit 3c4b6d2

File tree

7 files changed

+221
-52
lines changed

7 files changed

+221
-52
lines changed

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

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module Tucana
44
module Shared
55
UnexpectedRuleType = Class.new(Tucana::Error)
6+
UnexpectedType = Class.new(Tucana::Error)
67

78
DataTypeRule.class_eval do
89
def variant
@@ -54,25 +55,36 @@ def create(variant, config)
5455
end
5556

5657
def self.create(variant, config)
57-
self.new.create(variant, config)
58+
new.create(variant, config)
5859
end
5960
end
6061

6162
DataTypeContainsKeyRuleConfig.class_eval do
6263
def to_h
6364
{
6465
key: self.key,
65-
data_type_identifier: self.data_type_identifier,
66+
data_type_identifier: self.data_type_identifier.to_h,
6667
}
6768
end
69+
70+
def self.from_hash(config)
71+
new(
72+
key: config[:key],
73+
data_type_identifier: DataTypeIdentifier.from_hash(config[:data_type_identifier])
74+
)
75+
end
6876
end
6977

7078
DataTypeContainsTypeRuleConfig.class_eval do
7179
def to_h
7280
{
73-
data_type_identifier: self.data_type_identifier,
81+
data_type_identifier: self.data_type_identifier.to_h,
7482
}
7583
end
84+
85+
def self.from_hash(config)
86+
new(data_type_identifier: DataTypeIdentifier.from_hash(config[:data_type_identifier]))
87+
end
7688
end
7789

7890
DataTypeItemOfCollectionRuleConfig.class_eval do
@@ -111,6 +123,12 @@ def to_h
111123
input_types: self.input_types.map { |input_type| input_type.to_h }
112124
}
113125
end
126+
127+
def self.from_hash(hash)
128+
new(
129+
input_types: hash.fetch(:input_types).map { |input_type| DataTypeInputType.from_hash(input_type) }
130+
)
131+
end
114132
end
115133

116134
DataTypeInputTypesRuleConfig::DataTypeInputType.class_eval do
@@ -120,6 +138,13 @@ def to_h
120138
input_identifier: self.input_identifier,
121139
}
122140
end
141+
142+
def self.from_hash(config)
143+
new(
144+
input_identifier: config[:input_identifier],
145+
data_type_identifier: DataTypeIdentifier.from_hash(config[:data_type_identifier])
146+
)
147+
end
123148
end
124149

125150
DataTypeReturnTypeRuleConfig.class_eval do
@@ -128,6 +153,95 @@ def to_h
128153
data_type_identifier: self.data_type_identifier,
129154
}
130155
end
156+
157+
def self.from_hash(config)
158+
new(data_type_identifier: DataTypeIdentifier.from_hash(config[:data_type_identifier]))
159+
end
160+
end
161+
162+
DataTypeIdentifier.class_eval do
163+
def to_h
164+
{
165+
data_type_identifier: self.data_type_identifier,
166+
generic_type: self.generic_type ? self.generic_type.to_h : nil,
167+
generic_key: self.generic_key,
168+
}
169+
end
170+
171+
def from_hash(config)
172+
if config.keys.intersection([:data_type_identifier, :generic_type, :generic_key]).count > 1
173+
raise UnexpectedType, "Cannot have more than one type"
174+
end
175+
176+
if config.key?(:data_type_identifier)
177+
self.data_type_identifier = config
178+
elsif config.key?(:generic_type)
179+
self.generic_type = GenericType.new(config)
180+
elsif config.key?(:generic_key)
181+
self.generic_key = config
182+
else
183+
raise UnexpectedType, "Unknown type"
184+
end
185+
186+
self
187+
end
188+
189+
def self.from_hash(config)
190+
new.from_hash(config)
191+
end
192+
end
193+
194+
GenericType.class_eval do
195+
def to_h
196+
{
197+
data_type_identifier: self.data_type_identifier.to_h,
198+
generic_mapper: self.generic_mapper ? self.generic_mapper.to_h : nil,
199+
}
200+
end
201+
202+
def from_hash(config)
203+
if config.key?(:data_type_identifier)
204+
self.data_type_identifier = DataTypeIdentifier.from_hash(config.fetch(:data_type_identifier))
205+
end
206+
207+
if config.key?(:generic_mapper)
208+
self.generic_mapper = GenericMapper.from_hash(config.fetch(:generic_mapper))
209+
end
210+
211+
self
212+
end
213+
214+
def self.from_hash(config)
215+
new.from_hash(config)
216+
end
217+
end
218+
219+
GenericMapper.class_eval do
220+
def to_h
221+
{
222+
data_type_identifier: self.data_type_identifier.to_h,
223+
generic_key: self.generic_key,
224+
source: self.source,
225+
}
226+
end
227+
228+
def from_hash(config)
229+
if config.keys.intersection([:data_type_identifier, :generic_key]).count > 1
230+
raise UnexpectedType, "Cannot have more than one type"
231+
end
232+
233+
if config.key?(:data_type_identifier)
234+
self.data_type_identifier = DataTypeIdentifier.new(config.fetch(:data_type_identifier))
235+
elsif config.key?(:generic_key)
236+
self.generic_key = config.fetch(:generic_key)
237+
end
238+
239+
self.source = config[:source]
240+
end
241+
242+
def self.from_hash(config)
243+
new.from_hash(config)
244+
end
131245
end
132246
end
133247
end

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
describe "#create" do
77
context "with :contains_key variant" do
88
it "sets the contains_key field" do
9-
config = { key: "test_key", data_type_identifier: "test_type" }
9+
config = { key: "test_key", data_type_identifier: { data_type_identifier: "test_type" } }
1010
rule = described_class.create(:contains_key, config)
1111
expect(rule.contains_key).to be_a(Tucana::Shared::DataTypeContainsKeyRuleConfig)
1212
end
1313
end
1414

1515
context "with :contains_type variant" do
1616
it "sets the contains_type field" do
17-
config = { data_type_identifier: "test_type" }
17+
config = { data_type_identifier: { data_type_identifier: "test_type" } }
1818
rule = described_class.create(:contains_type, config)
1919
expect(rule.contains_type).to be_a(Tucana::Shared::DataTypeContainsTypeRuleConfig)
2020
end
@@ -46,15 +46,15 @@
4646

4747
context "with :input_types variant" do
4848
it "sets the input_types field" do
49-
config = { input_types: [{ data_type_identifier: "test_type", input_identifier: "test_input" }] }
49+
config = { input_types: [{ data_type_identifier: { data_type_identifier: "test_type" }, input_identifier: "test_input" }] }
5050
rule = described_class.create(:input_types, config)
5151
expect(rule.input_types).to be_a(Tucana::Shared::DataTypeInputTypesRuleConfig)
5252
end
5353
end
5454

5555
context "with :return_type variant" do
5656
it "sets the return_type field" do
57-
config = { data_type_identifier: "test_type" }
57+
config = { data_type_identifier: { data_type_identifier: "test_type" } }
5858
rule = described_class.create(:return_type, config)
5959
expect(rule.return_type).to be_a(Tucana::Shared::DataTypeReturnTypeRuleConfig)
6060
end

build/rust/src/shared/helper/rule.rs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::shared::DataTypeIdentifier;
12
use crate::shared::data_type_input_types_rule_config::DataTypeInputType;
23
use crate::shared::{
34
DataTypeContainsKeyRuleConfig, DataTypeContainsTypeRuleConfig, DataTypeInputTypesRuleConfig,
@@ -14,20 +15,24 @@ impl RuleBuilder {
1415
Self { rules: Vec::new() }
1516
}
1617

17-
pub fn add_contains_key(mut self, key: String, data_type_identifier: String) -> Self {
18+
pub fn add_contains_key(
19+
mut self,
20+
key: String,
21+
data_type_identifier: DataTypeIdentifier,
22+
) -> Self {
1823
self.rules.push(DataTypeRule {
1924
config: Some(Config::ContainsKey(DataTypeContainsKeyRuleConfig {
2025
key,
21-
data_type_identifier,
26+
data_type_identifier: Some(data_type_identifier),
2227
})),
2328
});
2429
self
2530
}
2631

27-
pub fn add_contains_type(mut self, data_type_identifier: String) -> Self {
32+
pub fn add_contains_type(mut self, data_type_identifier: DataTypeIdentifier) -> Self {
2833
self.rules.push(DataTypeRule {
2934
config: Some(Config::ContainsType(DataTypeContainsTypeRuleConfig {
30-
data_type_identifier,
35+
data_type_identifier: Some(data_type_identifier),
3136
})),
3237
});
3338
self
@@ -69,10 +74,10 @@ impl RuleBuilder {
6974
self
7075
}
7176

72-
pub fn add_return_type(mut self, data_type_identifier: String) -> Self {
77+
pub fn add_return_type(mut self, data_type_identifier: DataTypeIdentifier) -> Self {
7378
self.rules.push(DataTypeRule {
7479
config: Some(Config::ReturnType(DataTypeReturnTypeRuleConfig {
75-
data_type_identifier,
80+
data_type_identifier: Some(data_type_identifier),
7681
})),
7782
});
7883
self
@@ -87,32 +92,40 @@ impl RuleBuilder {
8792
mod tests {
8893
use super::*;
8994
use crate::shared::{
90-
data_type_input_types_rule_config::DataTypeInputType, data_type_rule::Config,
91-
helper::value::ToValue,
95+
data_type_identifier::Type, data_type_input_types_rule_config::DataTypeInputType,
96+
data_type_rule::Config, helper::value::ToValue,
9297
};
9398

99+
fn to_data_type(str: &str) -> DataTypeIdentifier {
100+
DataTypeIdentifier {
101+
r#type: Some(Type::DataTypeIdentifier(str.into())),
102+
}
103+
}
104+
94105
#[test]
95106
fn test_add_contains_key() {
96107
let rules = RuleBuilder::new()
97-
.add_contains_key("id".into(), "User".into())
108+
.add_contains_key("id".into(), to_data_type("User"))
98109
.build();
99110

100111
match &rules[0].config {
101112
Some(Config::ContainsKey(cfg)) => {
102113
assert_eq!(cfg.key, "id");
103-
assert_eq!(cfg.data_type_identifier, "User");
114+
assert_eq!(cfg.data_type_identifier, Some(to_data_type("User")));
104115
}
105116
_ => panic!("Expected ContainsKey config"),
106117
}
107118
}
108119

109120
#[test]
110121
fn test_add_contains_type() {
111-
let rules = RuleBuilder::new().add_contains_type("User".into()).build();
122+
let rules = RuleBuilder::new()
123+
.add_contains_type(to_data_type("User"))
124+
.build();
112125

113126
match &rules[0].config {
114127
Some(Config::ContainsType(cfg)) => {
115-
assert_eq!(cfg.data_type_identifier, "User");
128+
assert_eq!(cfg.data_type_identifier, Some(to_data_type("User")));
116129
}
117130
_ => panic!("Expected ContainsType config"),
118131
}
@@ -163,11 +176,11 @@ mod tests {
163176
fn test_add_input_types() {
164177
let input_types = vec![
165178
DataTypeInputType {
166-
data_type_identifier: "Type1".into(),
179+
data_type_identifier: Some(to_data_type("Type1")),
167180
input_identifier: "input1".into(),
168181
},
169182
DataTypeInputType {
170-
data_type_identifier: "Type2".into(),
183+
data_type_identifier: Some(to_data_type("Type2")),
171184
input_identifier: "input2".into(),
172185
},
173186
];
@@ -186,11 +199,13 @@ mod tests {
186199

187200
#[test]
188201
fn test_add_return_type() {
189-
let rules = RuleBuilder::new().add_return_type("Result".into()).build();
202+
let rules = RuleBuilder::new()
203+
.add_return_type(to_data_type("Result"))
204+
.build();
190205

191206
match &rules[0].config {
192207
Some(Config::ReturnType(cfg)) => {
193-
assert_eq!(cfg.data_type_identifier, "Result");
208+
assert_eq!(cfg.data_type_identifier, Some(to_data_type("Result")));
194209
}
195210
_ => panic!("Expected ReturnType config"),
196211
}
@@ -199,23 +214,23 @@ mod tests {
199214
#[test]
200215
fn test_add_many_rules() {
201216
let rules = RuleBuilder::new()
202-
.add_contains_key("id".into(), "User".into())
203-
.add_return_type("Result".into())
217+
.add_contains_key("id".into(), to_data_type("User"))
218+
.add_return_type(to_data_type("Result"))
204219
.add_regex(r"^\d+$".into())
205-
.add_contains_key("id".into(), "User".into())
220+
.add_contains_key("id".into(), to_data_type("User"))
206221
.build();
207222

208223
match &rules[0].config {
209224
Some(Config::ContainsKey(cfg)) => {
210225
assert_eq!(cfg.key, "id");
211-
assert_eq!(cfg.data_type_identifier, "User");
226+
assert_eq!(cfg.data_type_identifier, Some(to_data_type("User")));
212227
}
213228
_ => panic!("Expected ContainsKey config"),
214229
}
215230

216231
match &rules[1].config {
217232
Some(Config::ReturnType(cfg)) => {
218-
assert_eq!(cfg.data_type_identifier, "Result");
233+
assert_eq!(cfg.data_type_identifier, Some(to_data_type("Result")));
219234
}
220235
_ => panic!("Expected ReturnType config"),
221236
}
@@ -230,7 +245,7 @@ mod tests {
230245
match &rules[3].config {
231246
Some(Config::ContainsKey(cfg)) => {
232247
assert_eq!(cfg.key, "id");
233-
assert_eq!(cfg.data_type_identifier, "User");
248+
assert_eq!(cfg.data_type_identifier, Some(to_data_type("User")));
234249
}
235250
_ => panic!("Expected ContainsKey config"),
236251
}

0 commit comments

Comments
 (0)