Add get_name() method to simplify logic in field handling

This commit is contained in:
Nick Rolfe
2020-10-28 11:30:50 +00:00
parent 53de99e6af
commit 11152583d5
4 changed files with 17 additions and 42 deletions

View File

@@ -225,10 +225,7 @@ impl Visitor<'_> {
"too many values"
},
node.kind(),
match field.name.as_ref() {
Some(x) => x,
None => "child",
}
&field.get_name()
)
}
}
@@ -237,10 +234,7 @@ impl Visitor<'_> {
self.trap_output.push(TrapEntry::ChildOf(
node_type_name(&field.parent.kind, field.parent.named),
parent_id,
match &field.name {
Some(name) => name.to_owned(),
None => "child".to_owned(),
},
field.get_name(),
Index(*index),
*child_id,
));

View File

@@ -47,10 +47,7 @@ fn add_field(
field: &node_types::Field,
entries: &mut Vec<dbscheme::Entry>,
) {
let field_name = match &field.name {
None => "child".to_owned(),
Some(x) => x.to_owned(),
};
let field_name = field.get_name();
let parent_name = node_types::node_type_name(&field.parent.kind, field.parent.named);
match field.storage {
node_types::Storage::Table { .. } => {

View File

@@ -54,14 +54,7 @@ fn create_supertype_map(nodes: &[node_types::Entry]) -> SupertypeMap {
// This field can have one of several types. Since we create an ad-hoc
// QL union type to represent them, the class wrapping that union type
// will be a supertype of all its members.
let field_union_name = format!(
"{}_{}_type",
&node_name,
match &field.name {
Some(name) => &name,
None => "child",
}
);
let field_union_name = format!("{}_{}_type", &node_name, &field.get_name());
let field_union_name = node_types::escape_name(&field_union_name);
let supertype_name = dbscheme_name_to_class_name(&field_union_name);
for field_type in &field.types {
@@ -213,14 +206,7 @@ fn create_field_class(
} else {
// This field can have one of several types. The dbscheme contains a
// union type, so we create a QL class to wrap that.
let field_union_name = format!(
"{}_{}_type",
parent_name,
match &field.name {
Some(name) => &name,
None => "child",
}
);
let field_union_name = format!("{}_{}_type", parent_name, &field.get_name());
let field_union_name = node_types::escape_name(&field_union_name);
let class_name = dbscheme_name_to_class_name(&field_union_name);
classes.push(ql::Class {
@@ -388,17 +374,13 @@ fn create_field_getters(
field: &node_types::Field,
field_type: &str,
) -> (ql::Predicate, ql::Expression) {
let field_name = match &field.name {
Some(name) => &name,
None => "child",
};
match &field.storage {
node_types::Storage::Column => {
let result = (
ql::Predicate {
name: format!(
"get{}",
dbscheme_name_to_class_name(&node_types::escape_name(field_name))
dbscheme_name_to_class_name(&node_types::escape_name(&field.get_name()))
),
overridden: false,
return_type: Some(ql::Type::Normal(dbscheme_name_to_class_name(field_type))),
@@ -419,19 +401,12 @@ fn create_field_getters(
result
}
node_types::Storage::Table { index: _ } => {
let field_table_name = format!(
"{}_{}",
parent_name,
match &field.name {
Some(name) => &name,
None => "child",
}
);
let field_table_name = format!("{}_{}", parent_name, &field.get_name());
(
ql::Predicate {
name: format!(
"get{}",
dbscheme_name_to_class_name(&node_types::escape_name(field_name))
dbscheme_name_to_class_name(&node_types::escape_name(&field.get_name()))
),
overridden: false,
return_type: Some(ql::Type::Normal(dbscheme_name_to_class_name(field_type))),

View File

@@ -33,6 +33,15 @@ pub struct Field {
pub storage: Storage,
}
impl Field {
pub fn get_name(&self) -> String {
match &self.name {
Some(name) => name.clone(),
None => "child".to_owned(),
}
}
}
#[derive(Debug)]
pub enum Storage {
/// the field is stored as a column in the parent table