Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 117 additions & 3 deletions build/ruby/lib/tucana/shared/shared.data_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Tucana
module Shared
UnexpectedRuleType = Class.new(Tucana::Error)
UnexpectedType = Class.new(Tucana::Error)

DataTypeRule.class_eval do
def variant
Expand Down Expand Up @@ -54,25 +55,36 @@ def create(variant, config)
end

def self.create(variant, config)
self.new.create(variant, config)
new.create(variant, config)
end
end

DataTypeContainsKeyRuleConfig.class_eval do
def to_h
{
key: self.key,
data_type_identifier: self.data_type_identifier,
data_type_identifier: self.data_type_identifier.to_h,
}
end

def self.from_hash(config)
new(
key: config[:key],
data_type_identifier: DataTypeIdentifier.from_hash(config[:data_type_identifier])
)
end
end

DataTypeContainsTypeRuleConfig.class_eval do
def to_h
{
data_type_identifier: self.data_type_identifier,
data_type_identifier: self.data_type_identifier.to_h,
}
end

def self.from_hash(config)
new(data_type_identifier: DataTypeIdentifier.from_hash(config[:data_type_identifier]))
end
end

DataTypeItemOfCollectionRuleConfig.class_eval do
Expand Down Expand Up @@ -111,6 +123,12 @@ def to_h
input_types: self.input_types.map { |input_type| input_type.to_h }
}
end

def self.from_hash(hash)
new(
input_types: hash.fetch(:input_types).map { |input_type| DataTypeInputType.from_hash(input_type) }
)
end
end

DataTypeInputTypesRuleConfig::DataTypeInputType.class_eval do
Expand All @@ -120,6 +138,13 @@ def to_h
input_identifier: self.input_identifier,
}
end

def self.from_hash(config)
new(
input_identifier: config[:input_identifier],
data_type_identifier: DataTypeIdentifier.from_hash(config[:data_type_identifier])
)
end
end

DataTypeReturnTypeRuleConfig.class_eval do
Expand All @@ -128,6 +153,95 @@ def to_h
data_type_identifier: self.data_type_identifier,
}
end

def self.from_hash(config)
new(data_type_identifier: DataTypeIdentifier.from_hash(config[:data_type_identifier]))
end
end

DataTypeIdentifier.class_eval do
def to_h
{
data_type_identifier: self.data_type_identifier,
generic_type: self.generic_type ? self.generic_type.to_h : nil,
generic_key: self.generic_key,
}
end

def from_hash(config)
if config.keys.intersection([:data_type_identifier, :generic_type, :generic_key]).count > 1
raise UnexpectedType, "Cannot have more than one type"
end

if config.key?(:data_type_identifier)
self.data_type_identifier = config
elsif config.key?(:generic_type)
self.generic_type = GenericType.new(config)
elsif config.key?(:generic_key)
self.generic_key = config
else
raise UnexpectedType, "Unknown type"
end

self
end

def self.from_hash(config)
new.from_hash(config)
end
end

GenericType.class_eval do
def to_h
{
data_type_identifier: self.data_type_identifier.to_h,
generic_mapper: self.generic_mapper ? self.generic_mapper.to_h : nil,
}
end

def from_hash(config)
if config.key?(:data_type_identifier)
self.data_type_identifier = DataTypeIdentifier.from_hash(config.fetch(:data_type_identifier))
end

if config.key?(:generic_mapper)
self.generic_mapper = GenericMapper.from_hash(config.fetch(:generic_mapper))
end

self
end

def self.from_hash(config)
new.from_hash(config)
end
end

GenericMapper.class_eval do
def to_h
{
data_type_identifier: self.data_type_identifier.to_h,
generic_key: self.generic_key,
source: self.source,
}
end

def from_hash(config)
if config.keys.intersection([:data_type_identifier, :generic_key]).count > 1
raise UnexpectedType, "Cannot have more than one type"
end

if config.key?(:data_type_identifier)
self.data_type_identifier = DataTypeIdentifier.new(config.fetch(:data_type_identifier))
elsif config.key?(:generic_key)
self.generic_key = config.fetch(:generic_key)
end

self.source = config[:source]
end

def self.from_hash(config)
new.from_hash(config)
end
end
end
end
8 changes: 4 additions & 4 deletions build/ruby/spec/tucana/shared/shared.data_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
describe "#create" do
context "with :contains_key variant" do
it "sets the contains_key field" do
config = { key: "test_key", data_type_identifier: "test_type" }
config = { key: "test_key", data_type_identifier: { data_type_identifier: "test_type" } }
rule = described_class.create(:contains_key, config)
expect(rule.contains_key).to be_a(Tucana::Shared::DataTypeContainsKeyRuleConfig)
end
end

context "with :contains_type variant" do
it "sets the contains_type field" do
config = { data_type_identifier: "test_type" }
config = { data_type_identifier: { data_type_identifier: "test_type" } }
rule = described_class.create(:contains_type, config)
expect(rule.contains_type).to be_a(Tucana::Shared::DataTypeContainsTypeRuleConfig)
end
Expand Down Expand Up @@ -46,15 +46,15 @@

context "with :input_types variant" do
it "sets the input_types field" do
config = { input_types: [{ data_type_identifier: "test_type", input_identifier: "test_input" }] }
config = { input_types: [{ data_type_identifier: { data_type_identifier: "test_type" }, input_identifier: "test_input" }] }
rule = described_class.create(:input_types, config)
expect(rule.input_types).to be_a(Tucana::Shared::DataTypeInputTypesRuleConfig)
end
end

context "with :return_type variant" do
it "sets the return_type field" do
config = { data_type_identifier: "test_type" }
config = { data_type_identifier: { data_type_identifier: "test_type" } }
rule = described_class.create(:return_type, config)
expect(rule.return_type).to be_a(Tucana::Shared::DataTypeReturnTypeRuleConfig)
end
Expand Down
59 changes: 37 additions & 22 deletions build/rust/src/shared/helper/rule.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::shared::DataTypeIdentifier;
use crate::shared::data_type_input_types_rule_config::DataTypeInputType;
use crate::shared::{
DataTypeContainsKeyRuleConfig, DataTypeContainsTypeRuleConfig, DataTypeInputTypesRuleConfig,
Expand All @@ -14,20 +15,24 @@ impl RuleBuilder {
Self { rules: Vec::new() }
}

pub fn add_contains_key(mut self, key: String, data_type_identifier: String) -> Self {
pub fn add_contains_key(
mut self,
key: String,
data_type_identifier: DataTypeIdentifier,
) -> Self {
self.rules.push(DataTypeRule {
config: Some(Config::ContainsKey(DataTypeContainsKeyRuleConfig {
key,
data_type_identifier,
data_type_identifier: Some(data_type_identifier),
})),
});
self
}

pub fn add_contains_type(mut self, data_type_identifier: String) -> Self {
pub fn add_contains_type(mut self, data_type_identifier: DataTypeIdentifier) -> Self {
self.rules.push(DataTypeRule {
config: Some(Config::ContainsType(DataTypeContainsTypeRuleConfig {
data_type_identifier,
data_type_identifier: Some(data_type_identifier),
})),
});
self
Expand Down Expand Up @@ -69,10 +74,10 @@ impl RuleBuilder {
self
}

pub fn add_return_type(mut self, data_type_identifier: String) -> Self {
pub fn add_return_type(mut self, data_type_identifier: DataTypeIdentifier) -> Self {
self.rules.push(DataTypeRule {
config: Some(Config::ReturnType(DataTypeReturnTypeRuleConfig {
data_type_identifier,
data_type_identifier: Some(data_type_identifier),
})),
});
self
Expand All @@ -87,32 +92,40 @@ impl RuleBuilder {
mod tests {
use super::*;
use crate::shared::{
data_type_input_types_rule_config::DataTypeInputType, data_type_rule::Config,
helper::value::ToValue,
data_type_identifier::Type, data_type_input_types_rule_config::DataTypeInputType,
data_type_rule::Config, helper::value::ToValue,
};

fn to_data_type(str: &str) -> DataTypeIdentifier {
DataTypeIdentifier {
r#type: Some(Type::DataTypeIdentifier(str.into())),
}
}

#[test]
fn test_add_contains_key() {
let rules = RuleBuilder::new()
.add_contains_key("id".into(), "User".into())
.add_contains_key("id".into(), to_data_type("User"))
.build();

match &rules[0].config {
Some(Config::ContainsKey(cfg)) => {
assert_eq!(cfg.key, "id");
assert_eq!(cfg.data_type_identifier, "User");
assert_eq!(cfg.data_type_identifier, Some(to_data_type("User")));
}
_ => panic!("Expected ContainsKey config"),
}
}

#[test]
fn test_add_contains_type() {
let rules = RuleBuilder::new().add_contains_type("User".into()).build();
let rules = RuleBuilder::new()
.add_contains_type(to_data_type("User"))
.build();

match &rules[0].config {
Some(Config::ContainsType(cfg)) => {
assert_eq!(cfg.data_type_identifier, "User");
assert_eq!(cfg.data_type_identifier, Some(to_data_type("User")));
}
_ => panic!("Expected ContainsType config"),
}
Expand Down Expand Up @@ -163,11 +176,11 @@ mod tests {
fn test_add_input_types() {
let input_types = vec![
DataTypeInputType {
data_type_identifier: "Type1".into(),
data_type_identifier: Some(to_data_type("Type1")),
input_identifier: "input1".into(),
},
DataTypeInputType {
data_type_identifier: "Type2".into(),
data_type_identifier: Some(to_data_type("Type2")),
input_identifier: "input2".into(),
},
];
Expand All @@ -186,11 +199,13 @@ mod tests {

#[test]
fn test_add_return_type() {
let rules = RuleBuilder::new().add_return_type("Result".into()).build();
let rules = RuleBuilder::new()
.add_return_type(to_data_type("Result"))
.build();

match &rules[0].config {
Some(Config::ReturnType(cfg)) => {
assert_eq!(cfg.data_type_identifier, "Result");
assert_eq!(cfg.data_type_identifier, Some(to_data_type("Result")));
}
_ => panic!("Expected ReturnType config"),
}
Expand All @@ -199,23 +214,23 @@ mod tests {
#[test]
fn test_add_many_rules() {
let rules = RuleBuilder::new()
.add_contains_key("id".into(), "User".into())
.add_return_type("Result".into())
.add_contains_key("id".into(), to_data_type("User"))
.add_return_type(to_data_type("Result"))
.add_regex(r"^\d+$".into())
.add_contains_key("id".into(), "User".into())
.add_contains_key("id".into(), to_data_type("User"))
.build();

match &rules[0].config {
Some(Config::ContainsKey(cfg)) => {
assert_eq!(cfg.key, "id");
assert_eq!(cfg.data_type_identifier, "User");
assert_eq!(cfg.data_type_identifier, Some(to_data_type("User")));
}
_ => panic!("Expected ContainsKey config"),
}

match &rules[1].config {
Some(Config::ReturnType(cfg)) => {
assert_eq!(cfg.data_type_identifier, "Result");
assert_eq!(cfg.data_type_identifier, Some(to_data_type("Result")));
}
_ => panic!("Expected ReturnType config"),
}
Expand All @@ -230,7 +245,7 @@ mod tests {
match &rules[3].config {
Some(Config::ContainsKey(cfg)) => {
assert_eq!(cfg.key, "id");
assert_eq!(cfg.data_type_identifier, "User");
assert_eq!(cfg.data_type_identifier, Some(to_data_type("User")));
}
_ => panic!("Expected ContainsKey config"),
}
Expand Down
Loading