【修复】修复cfg rust json生成代码,正确处理多态类型
parent
c29c00bbe3
commit
e69cf2a764
|
|
@ -9,22 +9,9 @@ namespace Luban.Job.Common.TypeVisitors
|
|||
public override string DoAccept(TType type)
|
||||
{
|
||||
if (type.IsNullable)
|
||||
{
|
||||
if (type.IsBean)
|
||||
{
|
||||
return $"std::option::Option<std::rc::Rc<{type.Apply(RustTypeUnderlyingNameVisitor.Ins)}>>";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"std::option::Option<{type.Apply(RustTypeUnderlyingNameVisitor.Ins)}>";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (type.IsBean)
|
||||
{
|
||||
return $"std::rc::Rc<{type.Apply(RustTypeUnderlyingNameVisitor.Ins)}>";
|
||||
}
|
||||
else
|
||||
{
|
||||
return type.Apply(RustTypeUnderlyingNameVisitor.Ins);
|
||||
|
|
@ -32,4 +19,3 @@ namespace Luban.Job.Common.TypeVisitors
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
* {{x.comment}}
|
||||
*/
|
||||
{{~end~}}
|
||||
{{~if !x.is_abstract_type~}}
|
||||
#[allow(non_camel_case_types)]
|
||||
pub struct {{name}} {
|
||||
{{~for field in hierarchy_export_fields~}}
|
||||
|
|
@ -20,12 +21,36 @@ pub {{field.rust_style_name}}: {{rust_define_type field.ctype}},
|
|||
|
||||
impl {{name}} {
|
||||
#[allow(dead_code)]
|
||||
pub fn new(__js: &json::JsonValue) -> Result<std::rc::Rc<{{name}}>, LoadError> {
|
||||
pub fn new(__js: &json::JsonValue) -> Result<{{name}}, LoadError> {
|
||||
let __b = {{name}} {
|
||||
{{~for field in hierarchy_export_fields~}}
|
||||
{{field.rust_style_name}}: {{rust_json_constructor ('__js["' + field.name + '"]') field.ctype}},
|
||||
{{~end~}}
|
||||
};
|
||||
Ok(std::rc::Rc::new(__b))
|
||||
Ok(__b)
|
||||
}
|
||||
}
|
||||
{{~else~}}
|
||||
#[allow(non_camel_case_types)]
|
||||
pub enum {{name}} {
|
||||
{{~for child in x.hierarchy_not_abstract_children~}}
|
||||
{{child.name}}(Box<{{child.rust_full_name}}>),
|
||||
{{~end~}}
|
||||
}
|
||||
|
||||
impl {{name}} {
|
||||
#[allow(dead_code)]
|
||||
pub fn new(__js: &json::JsonValue) -> Result<{{name}}, LoadError> {
|
||||
let __b = match __js["__type__"].as_str() {
|
||||
Some(type_name) => match type_name {
|
||||
{{~for child in x.hierarchy_not_abstract_children~}}
|
||||
"{{child.name}}" => {{name}}::{{child.name}}(Box::new({{child.rust_full_name + '::new(&__js)?'}})),
|
||||
{{~end~}}
|
||||
_ => return Err(LoadError{})
|
||||
},
|
||||
None => return Err(LoadError{})
|
||||
};
|
||||
Ok(__b)
|
||||
}
|
||||
}
|
||||
{{~end~}}
|
||||
|
|
|
|||
|
|
@ -11,15 +11,15 @@
|
|||
#[allow(non_camel_case_types)]
|
||||
pub struct {{name}} {
|
||||
{{~if x.is_map_table ~}}
|
||||
data_list: Vec<{{rust_define_type value_type}}>,
|
||||
data_map: std::collections::HashMap<{{rust_define_type key_type}}, {{rust_define_type value_type}}>,
|
||||
data_list: Vec<std::rc::Rc<{{rust_define_type value_type}}>>,
|
||||
data_map: std::collections::HashMap<{{rust_define_type key_type}}, std::rc::Rc<{{rust_define_type value_type}}>>,
|
||||
{{~else~}}
|
||||
data: {{rust_define_type value_type}},
|
||||
data: {{rust_class_name value_type}},
|
||||
{{~end~}}
|
||||
}
|
||||
|
||||
impl {{name}}{
|
||||
pub fn new(__js: &json::JsonValue) -> Result<std::rc::Rc<{{name}}>, LoadError> {
|
||||
pub fn new(__js: &json::JsonValue) -> Result<{{name}}, LoadError> {
|
||||
{{~if x.is_map_table ~}}
|
||||
if !__js.is_array() {
|
||||
return Err(LoadError{});
|
||||
|
|
@ -30,22 +30,30 @@ impl {{name}}{
|
|||
};
|
||||
|
||||
for __e in __js.members() {
|
||||
let __v = match {{rust_class_name value_type}}::new(__e) {
|
||||
let __v = std::rc::Rc::new(match {{rust_class_name value_type}}::new(__e) {
|
||||
Ok(x) => x,
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
});
|
||||
let __v2 = std::rc::Rc::clone(&__v);
|
||||
t.data_list.push(__v);
|
||||
{{~if !value_type.bean.is_abstract_type~}}
|
||||
t.data_map.insert(__v2.{{x.index_field.rust_style_name}}.clone(), __v2);
|
||||
{{~else~}}
|
||||
match &*__v2 {
|
||||
{{~for child in value_type.bean.hierarchy_not_abstract_children~}}
|
||||
{{rust_class_name value_type}}::{{child.name}}(__w__) => t.data_map.insert(__w__.{{x.index_field.rust_style_name}}.clone(), __v2),
|
||||
{{~end~}}
|
||||
};
|
||||
{{~end~}}
|
||||
}
|
||||
Ok(std::rc::Rc::new(t))
|
||||
Ok(t)
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
pub fn get_data_map(self:&{{name}}) -> &std::collections::HashMap<{{rust_define_type key_type}}, {{rust_define_type value_type}}> { &self.data_map }
|
||||
pub fn get_data_map(self:&{{name}}) -> &std::collections::HashMap<{{rust_define_type key_type}}, std::rc::Rc<{{rust_define_type value_type}}>> { &self.data_map }
|
||||
#[allow(dead_code)]
|
||||
pub fn get_data_list(self:&{{name}}) -> &Vec<{{rust_define_type value_type}}> { &self.data_list }
|
||||
pub fn get_data_list(self:&{{name}}) -> &Vec<std::rc::Rc<{{rust_define_type value_type}}>> { &self.data_list }
|
||||
#[allow(dead_code)]
|
||||
pub fn get(self:&{{name}}, key: {{rust_define_type key_type}}) -> std::option::Option<&{{rust_define_type value_type}}> { self.data_map.get(&key) }
|
||||
pub fn get(self:&{{name}}, key: {{rust_define_type key_type}}) -> std::option::Option<&std::rc::Rc<{{rust_define_type value_type}}>> { self.data_map.get(&key) }
|
||||
{{~else~}}
|
||||
if !__js.is_array() || __js.len() != 1 {
|
||||
return Err(LoadError{});
|
||||
|
|
@ -57,7 +65,7 @@ impl {{name}}{
|
|||
let t = {{name}} {
|
||||
data: __v,
|
||||
};
|
||||
Ok(std::rc::Rc::new(t))
|
||||
Ok(t)
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
pub fn get_data(self:&{{name}}) -> &{{rust_define_type value_type}} { &self.data }
|
||||
|
|
|
|||
|
|
@ -15,18 +15,18 @@ pub struct {{name}} {
|
|||
* {{table.comment}}
|
||||
*/
|
||||
{{~end~}}
|
||||
pub {{string.downcase table.name}}: std::rc::Rc<{{table.rust_full_name}}>,
|
||||
pub {{string.downcase table.name}}: {{table.rust_full_name}},
|
||||
{{~end~}}
|
||||
}
|
||||
|
||||
impl {{name}} {
|
||||
#[allow(dead_code)]
|
||||
pub fn new(loader: JsonLoader) -> std::result::Result<std::rc::Rc<Tables>, LoadError> {
|
||||
pub fn new(loader: JsonLoader) -> std::result::Result<Tables, LoadError> {
|
||||
let tables = Tables {
|
||||
{{~for table in tables ~}}
|
||||
{{string.downcase table.name}}: {{table.rust_full_name}}::new(&loader("{{table.output_data_file}}")?)?,
|
||||
{{~end~}}
|
||||
};
|
||||
return Ok(std::rc::Rc::new(tables));
|
||||
return Ok(tables);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue