private List <DType> CreateBeanFields(DefBean bean, ExcelStream stream, DefAssembly ass) { var list = new List <DType>(); foreach (DefField f in bean.HierarchyFields) { try { string sep = f.ActualSep; if (string.IsNullOrWhiteSpace(sep)) { list.Add(f.CType.Apply(this, f.Remapper, stream, ass)); } else { list.Add(f.CType.Apply(this, f.Remapper, new ExcelStream(stream.ReadCell(), sep), ass)); } } catch (Exception e) { throw new Exception($"读取结构:{bean.FullName} 字段:{f.Name} 出错 ==> {e.Message}", e); } } return(list); }
private bool IsBaseDefineEqual(DefBean a, DefBean b) { if (!a.Name.Equals(b.Name)) { return(false); } if (!a.Namespace.Equals(b.Namespace)) { return(false); } if (a.Parent != b.Parent) { return(false); } if (a.IsValueType != b.IsValueType) { return(false); } if (a.Alias != b.Alias) { return(false); } if (a.IsMultiRow != b.IsMultiRow) { return(false); } if (a.Sep != b.Sep) { return(false); } return(true); }
public override string Render(DefBean b) { var template = t_beanRender ??= Template.Parse(@" {{x.cpp_namespace_begin}} {{ name = x.name parent_def_type = x.parent_def_type export_fields = x.export_fields hierarchy_export_fields = x.hierarchy_export_fields }} class {{name}} : public {{if parent_def_type}} {{parent_def_type.cpp_full_name}} {{else}} bright::CfgBean {{end}} { public: static bool deserialize{{name}}(ByteBuf& _buf, {{name}}*& _out); {{name}}() { } {{~if !hierarchy_export_fields.empty?~}} {{name}}({{- for field in hierarchy_export_fields }}{{cpp_define_type field.ctype}} {{field.name}}{{if !for.last}},{{end}} {{end}}) {{-if parent_def_type-}} : {{parent_def_type.cpp_full_name}}({{ for field in parent_def_type.hierarchy_export_fields }}{{field.name}}{{if !for.last}}, {{end}}{{end}}) {{-end-}} { {{~ for field in export_fields ~}} this->{{field.cpp_style_name}} = {{field.name}}; {{~end~}} } {{~end~}} virtual ~{{name}}() {} bool deserialize(ByteBuf& _buf); {{~ for field in export_fields ~}} {{cpp_define_type field.ctype}} {{field.cpp_style_name}}; {{~end~}} {{if !x.is_abstract_type}} static constexpr int ID = {{x.id}}; int getTypeId() const { return ID; } {{end}} }; {{x.cpp_namespace_end}} "); var result = template.RenderCode(b); return(result); }
public string Render(DefBean b) { var template = StringTemplateManager.Ins.GetTemplate("db/cs_async/bean"); var result = template.RenderCode(b); return(result); }
private List <DType> CreateBeanFields(DefBean bean, ExcelStream stream) { var list = new List <DType>(); foreach (DefField f in bean.HierarchyFields) { try { //string sep = f.Tags.TryGetValue("tag", out var s) ? s : null; //if (string.IsNullOrWhiteSpace(sep)) //{ list.Add(f.CType.Apply(this, stream)); //} //else //{ // list.Add(f.CType.Apply(this, new ExcelStream(stream.ReadCell(), sep))); //} } catch (DataCreateException dce) { dce.Push(bean, f); throw; } catch (Exception e) { var dce = new DataCreateException(e, stream.LastReadDataInfo); dce.Push(bean, f); throw dce; } } return(list); }
public string Render(DefBean b) { var template = t_beanRender ??= Template.Parse(@" #pragma once #include ""CoreMinimal.h"" #include ""UCfgObj.h"" {{ue_bp_includes}} #include ""{{ue_bp_header_file_name_without_suffix}}.generated.h"" UCLASS(BlueprintType) class X6PROTO_API {{ue_bp_full_name}} : public {{if parent_def_type}} {{parent_def_type.ue_bp_full_name}} {{else}} UCfgObj {{end}} { GENERATED_BODY() public: {{- for field in export_fields }} UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (DisplayName = ""{{field.name}}"")) {{field.ctype.ue_bp_cpp_define_type}} {{field.name}}; {{-end}} }; "); var result = template.Render(b); return(result); }
public override string Render(DefBean b) { var template = StringTemplateManager.Ins.GetTemplate($"config/{RenderTemplateDir}/bean"); var result = template.RenderCode(b); return(result); }
public override string Render(DefBean b) { var template = t_beanRender ??= Template.Parse(@" {{ name = x.py_full_name is_abstract_type = x.is_abstract_type parent_def_type = x.parent_def_type export_fields = x.export_fields hierarchy_export_fields = x.hierarchy_export_fields }} class {{name}} {{if parent_def_type}}({{parent_def_type.py_full_name}}){{else if is_abstract_type}}(metaclass=abc.ABCMeta){{end}}: {{~if x.is_abstract_type~}} _childrenTypes = None @staticmethod def fromJson(_json_): childrenTypes = {{name}}._childrenTypes if not childrenTypes: childrenTypes = {{name}}._childrenTypes = { {{~ for child in x.hierarchy_not_abstract_children~}} '{{child.name}}': {{child.py_full_name}}, {{~end~}} } type = _json_['__type__'] if type != None: child = {{name}}._childrenTypes.get(type) if child != None: return child(_json_) else: raise Exception() else: return None {{~end~}} def __init__(self, _json_): {{~if parent_def_type~}} {{parent_def_type.py_full_name}}.__init__(self, _json_) {{~end~}} {{~ for field in export_fields ~}} {{~if !field.ctype.is_nullable~}} if _json_['{{field.name}}'] == None: raise Exception() {{~end~}} {{py3_deserialize ('self.' + field.py_style_name) ('_json_[""' + field.name + '""]') field.ctype}} {{~end~}} {{~if export_fields.empty?}} pass {{~end~}} "); var result = template.RenderCode(b); return(result); }
public static string GetImplTypeName(DefBean implType, DefBean baseType) { if (implType.Namespace == baseType.Namespace) { return(implType.Name); } else { return(implType.FullName); } }
void Walk(DefBean type, Dictionary <string, DefTypeBase> types) { if (types.TryAdd(type.FullName, type)) { foreach (var f in type.Fields) { f.CType.Apply(this, types); } if (type.Children != null) { foreach (DefBean c in type.Children) { Walk(c, types); } } } }
public void Collect(DefBean bean, HashSet <DefTypeBase> x) { if (x.Add(bean)) { foreach (var f in bean.Fields) { f.CType.Apply(this, x); } if (bean.IsAbstractType) { foreach (DefBean c in bean.HierarchyNotAbstractChildren) { Collect(c, x); } } } }
public static DefBean GetImplTypeByNameOrAlias(DefBean bean, string subType) { if (string.IsNullOrEmpty(subType)) { throw new Exception($"module:'{bean.Namespace}' 多态数据type不能为空"); } DefBean defType = bean.HierarchyNotAbstractChildren.Cast <DefBean>().Where(c => c.Alias == subType || c.Name == subType || c.FullName == subType).FirstOrDefault(); if (defType == null) { throw new Exception($"module:'{bean.Namespace}' type:'{subType}' 不是合法类型"); } if (defType.IsAbstractType) { throw new Exception($"module:'{bean.Namespace}' type:'{subType}' 是抽象类. 不能创建实例"); } return(defType); }
//public static ExcelStream SepIfNeed(TType type, ExcelStream stream) //{ // string sep = DataUtil.GetSep(type); // if (!string.IsNullOrEmpty(sep)) // { // return new ExcelStream(stream.ReadCell(), sep); // } // else // { // return stream; // } //} public DType Accept(TBean type, ExcelStream x) { var originBean = (DefBean)type.Bean; if (!string.IsNullOrEmpty(originBean.Sep)) { x = new ExcelStream(x.ReadCell(), originBean.Sep); } else { x = TrySep(type, x); } if (originBean.IsAbstractType) { string subType = x.Read().ToString(); if (subType.ToLower().Trim() == DefBean.BEAN_NULL_STR) { if (!type.IsNullable) { throw new InvalidExcelDataException($"type:{type.Bean.FullName}不是可空类型. 不能为空"); } return(null); } DefBean implType = DataUtil.GetImplTypeByNameOrAlias(originBean, subType); return(new DBean(type, implType, CreateBeanFields(implType, x))); } else { if (type.IsNullable) { string subType = x.Read().ToString().Trim(); if (subType == DefBean.BEAN_NULL_STR) { return(null); } else if (subType != DefBean.BEAN_NOT_NULL_STR && subType != originBean.Name) { throw new Exception($"type:'{type.Bean.FullName}' 可空标识:'{subType}' 不合法(只能为{DefBean.BEAN_NOT_NULL_STR}或{DefBean.BEAN_NULL_STR}或{originBean.Name})"); } } return(new DBean(type, originBean, CreateBeanFields(originBean, x))); } }
public override string Render(DefBean b) { var template = t_beanRender ??= Template.Parse(@" #pragma once #include ""CoreMinimal.h"" #include ""FCfgObj.h"" {{editor_cpp_includes}} namespace editor { {{cpp_namespace_begin}} struct X6PROTOEDITOR_API {{ue_fname}} : public {{if parent_def_type}} {{parent_def_type.ue_fname}}{{else}}FCfgObj{{end}} { {{~for field in fields ~}} {{field.ctype.editor_ue_cpp_define_type}} {{field.name}}; {{~end~}} {{~if !is_abstract_type~}} bool Load(FJsonObject* _json) override; bool Save(FJsonObject*& result) override; {{~end~}} static bool Create(FJsonObject* _json, {{ue_fname}}*& result); }; {{cpp_namespace_end}} } "); var result = template.Render(b); return(result); }
public DType Accept(TBean type, Sheet.NamedRow row, bool multirow, bool nullable) { var originBean = (DefBean)type.Bean; if (originBean.IsAbstractType) { string subType = row.GetColumn(DefBean.TYPE_NAME_KEY, null).Read().ToString().Trim(); if (subType.ToLower() == "null") { return(new DBean(originBean, null, null)); } string fullType = TypeUtil.MakeFullName(originBean.Namespace, subType); DefBean implType = (DefBean)originBean.GetNotAbstractChildType(subType); if (implType == null) { throw new Exception($"type:{fullType} 不是 bean 类型"); } return(new DBean(originBean, implType, CreateBeanFields(implType, row))); } else { return(new DBean(originBean, originBean, CreateBeanFields(originBean, row))); } }
public DType Accept(TBean type, object converter, ExcelStream x, DefAssembly ass) { var originBean = (DefBean)type.Bean; if (originBean.IsAbstractType) { string subType = x.Read().ToString(); if (subType.ToLower().Trim() == "null") { return(new DBean(originBean, null, null)); } string fullType = TypeUtil.MakeFullName(originBean.Namespace, subType); DefBean implType = (DefBean)originBean.GetNotAbstractChildType(subType); if (implType == null) { throw new Exception($"type:{fullType} 不是bean类型"); } return(new DBean(originBean, implType, CreateBeanFields(implType, x, ass))); } else { return(new DBean(originBean, originBean, CreateBeanFields(originBean, x, ass))); } }
private List <DType> CreateBeanFields(DefBean bean, Sheet.NamedRow row) { var list = new List <DType>(); foreach (DefField f in bean.HierarchyFields) { string fname = f.Name; Sheet.Title title = row.GetTitle(fname); if (title == null) { throw new Exception($"bean:{bean.FullName} 缺失 列:{fname},请检查是否写错或者遗漏"); } // 多级标题 if (title.SubTitles.Count > 0) { try { if (f.IsMultiRow) { list.Add(f.CType.Apply(this, row.GetSubTitleNamedRowOfMultiRows(fname), f.RawIsMultiRow, f.IsNullable)); } else { list.Add(f.CType.Apply(this, row.GetSubTitleNamedRow(fname), f.RawIsMultiRow /* 肯定是 false */, f.IsNullable)); } } catch (Exception e) { throw new Exception($"读取结构:{bean.FullName} 字段:{fname} 读取 出错 ==> {e.Message}", e); } } else { string sep = f.ActualSep; if (string.IsNullOrWhiteSpace(sep) && IsContainerAndElementNotSepType(f.CType)) { sep = ";,"; } if (f.IsMultiRow) { try { list.Add(f.CType.Apply(MultiRowExcelDataCreator.Ins, row.GetColumnOfMultiRows(f.Name, sep), f.IsNullable, (DefAssembly)bean.AssemblyBase)); } catch (Exception e) { throw new Exception($"读取结构:{bean.FullName} 多行字段:{f.Name} 读取 出错 ==> {e.Message}", e); } } else { ExcelStream stream = row.GetColumn(f.Name, sep); try { list.Add(f.CType.Apply(ExcelDataCreator.Ins, f.Remapper, stream, (DefAssembly)bean.AssemblyBase)); } catch (Exception e) { throw new Exception($"读取结构:{bean.FullName} 字段:{f.Name} 位置:{stream.CurrentExcelPosition} 出错 ==> {e.Message}", e); } } } } return(list); }
public override string Render(DefBean b) { var template = t_beanRender ??= Template.Parse(@" using Bright.Serialization; using System.Collections.Generic; {{ name = x.name parent_def_type = x.parent_def_type export_fields = x.export_fields hierarchy_export_fields = x.hierarchy_export_fields }} namespace {{x.namespace_with_top_module}} { public {{x.cs_class_modifier}} partial class {{name}} : {{if parent_def_type}} {{x.parent}} {{else}} Bright.Config.BeanBase {{end}} { public {{name}}(ByteBuf _buf) {{if parent_def_type}} : base(_buf) {{end}} { {{~ for field in export_fields ~}} {{cs_deserialize '_buf' field.cs_style_name field.ctype}} {{~if field.index_field~}} foreach(var _v in {{field.cs_style_name}}) { {{field.cs_style_name}}_Index.Add(_v.{{field.index_field.cs_style_name}}, _v); } {{~end~}} {{~end~}} } public {{name}}({{- for field in hierarchy_export_fields }}{{cs_define_type field.ctype}} {{field.name}}{{if !for.last}},{{end}} {{end}}) {{if parent_def_type}} : base({{- for field in parent_def_type.hierarchy_export_fields }}{{field.name}}{{if !for.last}},{{end}}{{end}}) {{end}} { {{~ for field in export_fields ~}} this.{{field.cs_style_name}} = {{field.name}}; {{~if field.index_field~}} foreach(var _v in {{field.cs_style_name}}) { {{field.cs_style_name}}_Index.Add(_v.{{field.index_field.cs_style_name}}, _v); } {{~end~}} {{~end~}} } public static {{name}} Deserialize{{name}}(ByteBuf _buf) { {{if x.is_abstract_type}} switch (_buf.ReadInt()) { case 0 : return null; {{- for child in x.hierarchy_not_abstract_children}} case {{child.full_name}}.ID: return new {{child.full_name}}(_buf); {{-end}} default: throw new SerializationException(); } {{else}} return new {{x.full_name}}(_buf); {{end}} } {{~ for field in export_fields ~}} public readonly {{cs_define_type field.ctype}} {{field.cs_style_name}}; {{~if field.index_field~}} public readonly Dictionary<{{cs_define_type field.index_field.ctype}}, {{cs_define_type field.ctype.element_type}}> {{field.cs_style_name}}_Index = new Dictionary<{{cs_define_type field.index_field.ctype}}, {{cs_define_type field.ctype.element_type}}>(); {{~end~}} {{~if field.gen_ref~}} public {{field.cs_ref_validator_define}} {{~end~}} {{~end~}} {{if !x.is_abstract_type}} public const int ID = {{x.id}}; public override int GetTypeId() => ID; {{end}} public {{x.cs_method_modifier}} void Resolve(Dictionary<string, object> _tables) { {{~if parent_def_type}}base.Resolve(_tables);{{end}} {{~ for field in export_fields ~}} {{~if field.gen_ref~}} {{cs_ref_validator_resolve field}} {{~else if field.has_recursive_ref~}} {{cs_recursive_resolve field '_tables'}} {{~end~}} {{~end~}} OnResolveFinish(_tables); } partial void OnResolveFinish(Dictionary<string, object> _tables); public override string ToString() { return ""{{full_name}}{ "" {{- for field in hierarchy_export_fields }} + ""{{field.cs_style_name}}:"" + {{cs_to_string field.cs_style_name field.ctype}} + "","" {{-end}} + ""}""; } } } "); var result = template.RenderCode(b); return(result); }
private string Render(DefBean b) { var template = t_beanRender ??= Template.Parse(@" {{ is_value_type = x.is_value_type is_abstract_type = x.is_abstract_type name = x.name full_name = x.full_name parent_def_type = x.parent_def_type parent = x.parent fields = x.fields hierarchy_fields = x.hierarchy_fields }} using Bright.Serialization; namespace {{x.namespace_with_top_module}} { public {{if is_value_type}}struct{{else}}{{x.cs_class_modifier}} class{{end}} {{name}} : {{if parent_def_type}} {{parent}} {{else}} Bright.Serialization.BeanBase {{end}} { {{~if !is_value_type~}} public {{name}}() { } {{~end~}} public {{name}}(Bright.Common.NotNullInitialization _) {{if parent_def_type}} : base(_) {{end}} { {{~ for field in fields ~}} {{~if cs_need_init field.ctype~}} {{cs_init_field_ctor_value field.cs_style_name field.ctype}} {{~else if is_value_type~}} {{field.cs_style_name}} = default; {{~end~}} {{~end~}} } public static void Serialize{{name}}(ByteBuf _buf, {{name}} x) { {{~if is_abstract_type~}} if (x != null) { _buf.WriteInt(x.GetTypeId()); x.Serialize(_buf); } else { _buf.WriteInt(0); } {{~else~}} x.Serialize(_buf); {{~end~}} } public static {{name}} Deserialize{{name}}(ByteBuf _buf) { {{~if is_abstract_type~}} {{full_name}} x; switch (_buf.ReadInt()) { case 0 : return null; {{~ for child in x.hierarchy_not_abstract_children~}} case {{child.full_name}}.ID: x = new {{child.full_name}}(); break; {{~end~}} default: throw new SerializationException(); } x.Deserialize(_buf); {{~else~}} var x = new {{full_name}}(); x.Deserialize(_buf); {{~end~}} return x; } {{~ for field in fields ~}} public {{cs_define_type field.ctype}} {{field.cs_style_name}}; {{~end~}} {{~if !is_abstract_type~}} public const int ID = {{x.id}}; public override int GetTypeId() => ID; public override void Serialize(ByteBuf _buf) { {{~ for field in hierarchy_fields ~}} {{cs_serialize '_buf' field.cs_style_name field.ctype}} {{~end~}} } public override void Deserialize(ByteBuf _buf) { {{~ for field in hierarchy_fields ~}} {{cs_deserialize '_buf' field.cs_style_name field.ctype}} {{~end~}} } public override string ToString() { return ""{{full_name}}{ "" {{~ for field in hierarchy_fields ~}} + ""{{field.cs_style_name}}:"" + {{cs_to_string field.cs_style_name field.ctype}} + "","" {{~end~}} + ""}""; } {{~end~}} } } "); var result = template.RenderCode(b); return(result); }
public abstract string Render(DefBean b);
public override string Render(DefBean b) { var template = t_beanRender ??= Template.Parse(@" package {{x.namespace_with_top_module}}; import bright.serialization.*; {{ name = x.name parent_def_type = x.parent_def_type export_fields = x.export_fields hierarchy_export_fields = x.hierarchy_export_fields }} public {{x.java_class_modifier}} class {{name}} extends {{if parent_def_type}} {{x.parent_def_type.full_name_with_top_module}} {{else}} bright.serialization.AbstractBean {{end}} { public {{name}}(ByteBuf _buf) { {{~if parent_def_type~}} super(_buf); {{~end~}} {{~ for field in export_fields ~}} {{java_deserialize '_buf' field.java_style_name field.ctype}} {{~if field.index_field~}} for({{java_box_define_type field.ctype.element_type}} _v : {{field.java_style_name}}) { {{field.java_style_name}}_Index.put(_v.{{field.index_field.java_style_name}}, _v); } {{~end~}} {{~end~}} } public {{name}}({{- for field in hierarchy_export_fields }}{{java_define_type field.ctype}} {{field.name}}{{if !for.last}},{{end}} {{end}}) { {{~if parent_def_type~}} super({{ for field in parent_def_type.hierarchy_export_fields }}{{field.name}}{{if !for.last}}, {{end}}{{end}}); {{~end~}} {{~ for field in export_fields ~}} this.{{field.java_style_name}} = {{field.name}}; {{~if field.index_field~}} for({{java_box_define_type field.ctype.element_type}} _v : {{field.java_style_name}}) { {{field.java_style_name}}_Index.put(_v.{{field.index_field.java_style_name}}, _v); } {{~end~}} {{~end~}} } public static {{name}} deserialize{{name}}(ByteBuf _buf) { {{if x.is_abstract_type}} switch (_buf.readInt()) { case 0 : return null; {{- for child in x.hierarchy_not_abstract_children}} case {{child.full_name_with_top_module}}.ID: return new {{child.full_name_with_top_module}}(_buf); {{-end}} default: throw new SerializationException(); } {{else}} return new {{name}}(_buf); {{end}} } {{~ for field in export_fields ~}} public final {{java_define_type field.ctype}} {{field.java_style_name}}; {{~if field.index_field~}} public final java.util.HashMap<{{java_box_define_type field.index_field.ctype}}, {{java_box_define_type field.ctype.element_type}}> {{field.java_style_name}}_Index = new java.util.HashMap<>(); {{~end~}} {{~if field.gen_ref~}} public {{field.java_ref_validator_define}} {{~end~}} {{~end~}} {{if !x.is_abstract_type}} public static final int ID = {{x.id}}; @Override public int getTypeId() { return ID; } {{end}} @Override public void serialize(ByteBuf os) { throw new UnsupportedOperationException(); } @Override public void deserialize(ByteBuf os) { throw new UnsupportedOperationException(); } public void resolve(java.util.HashMap<String, Object> _tables) { {{~if parent_def_type}}super.resolve(_tables);{{end}} {{~ for field in export_fields ~}} {{~if field.gen_ref~}} {{java_ref_validator_resolve field}} {{~else if field.has_recursive_ref~}} {{java_recursive_resolve field '_tables'}} {{~end~}} {{~end~}} } @Override public String toString() { return ""{{full_name}}{ "" {{- for field in hierarchy_export_fields }} + ""{{field.java_style_name}}:"" + {{java_to_string field.java_style_name field.ctype}} + "","" {{-end}} + ""}""; } } "); var result = template.RenderCode(b); return(result); }
public string Render(DefBean b) { return("// bean"); }
protected abstract string Render(DefBean b);
public string Render(DefBean b) { var template = t_beanRender ??= Template.Parse(@" using Bright.Serialization; namespace {{namespace}} { public {{cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{parent}} {{else}} ISerializable {{if is_abstract_type}}, ITypeId {{end}} {{end}} { public {{name}}() {{if parent_def_type}} : base() {{end}} { } public {{name}}(Bright.Common.NotNullInitialization _) {{if parent_def_type}} : base(_) {{end}} { {{- for field in fields }} {{if field.ctype.need_init}}{{field.proto_cs_init_field}} {{end}} {{-end}} } {{if is_abstract_type}} public static void Serialize{{name}}(ByteBuf _buf, {{name}} x) { if (x == null) { _buf.WriteInt(0); return; } _buf.WriteInt(x.GetTypeId()); x.Serialize(_buf); } public static {{name}} Deserialize{{name}}(ByteBuf _buf) { {{name}} x; switch (_buf.ReadInt()) { case 0 : return null; {{- for child in hierarchy_not_abstract_children}} case {{child.full_name}}.ID: x = new {{child.full_name}}(false); break; {{-end}} default: throw new SerializationException(); } x.Deserialize(_buf); return x; } {{end}} {{- for field in fields }} public {{field.ctype.cs_define_type}} {{field.cs_style_name}}; {{-end}} {{if !parent_def_type && is_abstract_type}} public abstract int GetTypeId(); {{end}} {{if parent_def_type && !is_abstract_type}} public const int ID = {{id}}; public override int GetTypeId() { return ID; } {{end}} public {{cs_method_modifer}} void Serialize(ByteBuf _buf) { {{if parent_def_type}} base.Serialize(_buf); {{end}} {{- for field in fields }} {{field.cs_serialize}} {{-end}} } public {{cs_method_modifer}} void Deserialize(ByteBuf _buf) { {{if parent_def_type}} base.Deserialize(_buf); {{end}} {{- for field in fields }} {{field.cs_deserialize}} {{-end}} } public override string ToString() { return ""{{full_name}}{ "" {{- for field in hierarchy_fields }} + ""{{field.cs_style_name}}:"" + {{field.proto_cs_to_string}} + "","" {{-end}} + ""}""; } } } "); var result = template.Render(b); return(result); }
public DBean(TBean defType, DefBean implType, List <DType> fields) { this.TType = defType; this.ImplType = implType; this.Fields = fields; }
public string RenderForwardDefine(DefBean b) { return($"{b.CppNamespaceBegin} class {b.Name}; {b.CppNamespaceEnd} "); }
public string Render(DefBean b) { string package = "cfg"; var template = t_beanRender ??= Template.Parse(@" {{- go_full_name = x.go_full_name parent_def_type = x.parent_def_type is_abstract_type = x.is_abstract_type export_fields = x.export_fields hierarchy_not_abstract_children = x.hierarchy_not_abstract_children -}} package {{package}} import ""bright/serialization"" {{x.go_import}} type {{go_full_name}} struct { {{if parent_def_type}}{{parent_def_type.go_full_name}}{{end}} {{- for field in export_fields }} {{field.cs_style_name}} {{go_define_type field.ctype}} {{-end}} } {{if !is_abstract_type}} func ({{go_full_name}}) GetTypeId() int { return {{x.id}} } {{end}} func New{{go_full_name}}(_buf *serialization.ByteBuf) (_v *{{go_full_name}}, err error) { _v = &{{go_full_name}}{} {{if parent_def_type}} var _p *{{parent_def_type.go_full_name}} if _p, err = New{{parent_def_type.go_full_name}}(_buf) ; err != nil { return } _v.{{parent_def_type.go_full_name}} = *_p {{end}} {{- for field in export_fields }} {{go_deserialize_field field '_buf'}} {{-end}} return } {{if is_abstract_type}} func NewChild{{go_full_name}}(_buf *serialization.ByteBuf) (_v interface{}, err error) { var id int32 if id, err = _buf.ReadInt() ; err != nil { return } switch id { case 0 : return nil, nil {{- for child in hierarchy_not_abstract_children}} case {{child.id}}: return New{{child.go_full_name}}(_buf); {{-end}} } return } {{end}} "); var result = template.RenderCode(b, new Dictionary <string, object>() { ["package"] = package }); return(result); }
public bool Compare(DefBean a, DefBean b, Dictionary <DefTypeBase, bool> ctx, HashSet <DefTypeBase> inWalk) { bool setupNotEqual() { ctx.Add(a, false); return(false); } bool IsValidatorEquals(List <Validator> vs1, List <Validator> vs2) { if (vs1.Count != vs2.Count) { return(false); } for (int i = 0; i < vs1.Count; i++) { var v1 = vs1[i]; var v2 = vs2[i]; if (v1.Type != v2.Type || v1.Rule != v2.Rule) { return(false); } } return(true); } if (ctx.TryGetValue(a, out var e)) { return(e); } if (inWalk.Contains(a)) { return(true); } if (!IsBaseDefineEqual(a, b)) { return(setupNotEqual()); } inWalk.Add(a); try { if (a.Fields.Count != b.Fields.Count) { return(setupNotEqual()); } for (int i = 0; i < a.Fields.Count; i++) { var f1 = (DefField)a.Fields[i]; var f2 = (DefField)b.Fields[i]; if (f1.Name != f2.Name || f1.NeedExport != f2.NeedExport || f1.Index != f2.Index || f1.Sep != f2.Sep || f1.ResourceTag != f2.ResourceTag || f1.IsMultiRow != f2.IsMultiRow || f1.CType.IsNullable != f2.CType.IsNullable || f1.CType.GetType() != f2.CType.GetType() || !IsValidatorEquals(f1.RawDefine.Validators, f2.RawDefine.Validators) || !IsValidatorEquals(f1.RawDefine.KeyValidators, f2.RawDefine.KeyValidators) || !IsValidatorEquals(f1.RawDefine.ValueValidators, f2.RawDefine.ValueValidators) ) { return(setupNotEqual()); } if (!f1.CType.Apply(this, f2.CType, ctx, inWalk)) { return(setupNotEqual()); } } var parentType = (DefBean)a.ParentDefType; if (parentType != null && !Compare(parentType, (DefBean)b.ParentDefType, ctx, inWalk)) { return(setupNotEqual()); } if (a.Children == null) { if (b.Children != null) { return(setupNotEqual()); } } else { if (b.Children == null || a.Children.Count != b.Children.Count) { return(setupNotEqual()); } else { int index = 0; foreach (var c in a.Children) { if (!Compare((DefBean)c, (DefBean)b.Children[index++], ctx, inWalk)) { return(setupNotEqual()); } } } } ctx.Add(a, true); return(true); } finally { //inWalk.Remove(a); } }
public string Render(DefBean b) { var template = t_beanRender ??= Template.Parse(@" {{ name = x.name full_name = x.full_name parent_def_type = x.parent_def_type fields = x.fields hierarchy_fields = x.hierarchy_fields is_abstract_type = x.is_abstract_type }} using Bright.Serialization; namespace {{x.namespace_with_top_module}} { public {{x.cs_class_modifier}} class {{name}} : {{if parent_def_type}} {{x.parent}} {{else}} Bright.Transaction.TxnBeanBase {{end}} { {{~ for field in fields~}} {{if is_abstract_type}}protected{{else}}private{{end}} {{db_cs_define_type field.ctype}} {{field.internal_name}}; {{~end}} public {{name}}() { {{~ for field in fields~}} {{if cs_need_init field.ctype}}{{db_cs_init_field field.internal_name field.log_type field.ctype }} {{end}} {{~end~}} } {{~ for field in fields~}} {{~if has_setter field.ctype~}} private sealed class {{field.log_type}} : Bright.Transaction.FieldLogger<{{name}}, {{db_cs_define_type field.ctype}}> { public {{field.log_type}}({{name}} self, {{db_cs_define_type field.ctype}} value) : base(self, value) { } public override long FieldId => host._objectId_ + {{field.id}}; public override void Commit() { this.host.{{field.internal_name}} = this.Value; } public override void WriteBlob(ByteBuf _buf) { _buf.WriteInt(FieldTag.{{tag_name field.ctype}}); {{cs_write_blob '_buf' 'this.Value' field.ctype}} } } public {{db_cs_define_type field.ctype}} {{field.cs_style_name}} { get { if (this.InitedObjectId) { var txn = Bright.Transaction.TransactionContext.AsyncLocalCtx; if (txn == null) return {{field.internal_name}}; var log = ({{field.log_type}})txn.GetField(_objectId_ + {{field.id}}); return log != null ? log.Value : {{field.internal_name}}; } else { return {{field.internal_name}}; } } set { {{~if db_field_cannot_null~}} if (value == null) throw new ArgumentNullException(); {{~end~}} if (this.InitedObjectId) { var txn = Bright.Transaction.TransactionContext.AsyncLocalCtx; txn.PutField(_objectId_ + {{field.id}}, new {{field.log_type}}(this, value)); {{~if field.ctype.need_set_children_root}} value?.InitRoot(GetRoot()); {{end}} } else { {{field.internal_name}} = value; } } } {{~else~}} {{~if field.ctype.is_collection~}} private class {{field.log_type}} : {{db_cs_define_type field.ctype}}.Log { private readonly {{name}} host; public {{field.log_type}}({{name}} host, {{cs_immutable_type field.ctype}} value) : base(value) { this.host = host; } public override long FieldId => host._objectId_ + {{field.id}}; public override Bright.Transaction.TxnBeanBase Host => host; public override void Commit() { Commit(host.{{field.internal_name}}); } public override void WriteBlob(ByteBuf _buf) { _buf.WriteInt(FieldTag.{{tag_name field.ctype}}); {{cs_write_blob '_buf' 'this.Value' field.ctype}} } } {{~end~}} public {{db_cs_define_type field.ctype}} {{field.cs_style_name}} => {{field.internal_name}}; {{~end~}} {{~end~}} {{~if is_abstract_type~}} public static void Serialize{{name}}(ByteBuf _buf, {{name}} x) { if (x == null) { _buf.WriteInt(0); return; } _buf.WriteInt(x.GetTypeId()); x.Serialize(_buf); } public static {{name}} Deserialize{{name}}(ByteBuf _buf) { {{name}} x; switch (_buf.ReadInt()) { case 0 : return null; {{~ for child in x.hierarchy_not_abstract_children~}} case {{child.full_name}}.ID: x = new {{child.full_name}}(); break; {{~end~}} default: throw new SerializationException(); } x.Deserialize(_buf); return x; } {{~else~}} public override void Serialize(ByteBuf _buf) { _buf.WriteLong(_objectId_); {{~ for field in hierarchy_fields~}} { _buf.WriteInt(FieldTag.{{tag_name field.ctype}} | ({{field.id}} << FieldTag.TAG_SHIFT)); {{db_cs_compatible_serialize '_buf' field.internal_name field.ctype}} } {{~end}} } public override void Deserialize(ByteBuf _buf) { _objectId_ = _buf.ReadLong(); while(_buf.NotEmpty) { int _tag_ = _buf.ReadInt(); switch (_tag_) { {{~ for field in hierarchy_fields~}} case FieldTag.{{tag_name field.ctype}} | ({{field.id}} << FieldTag.TAG_SHIFT) : { {{db_cs_compatible_deserialize '_buf' field.internal_name field.ctype}} break; } {{~end~}} default: { _buf.SkipUnknownField(_tag_); break; } } } } public const int ID = {{x.id}}; public override int GetTypeId() => ID; {{~end~}} protected override void InitChildrenRoot(Bright.Storage.TKey root) { {{~ for field in hierarchy_fields~}} {{if need_set_children_root field.ctype}}{{field.internal_name}}?.InitRoot(root);{{end}} {{~end}} } public override string ToString() { return ""{{full_name}}{ "" {{~ for field in hierarchy_fields~}} + ""{{field.cs_style_name}}:"" + {{cs_to_string field.cs_style_name field.ctype}} + "","" {{~end~}} + ""}""; } } } "); var result = template.RenderCode(b); return(result); }
protected override string Render(DefBean b) { throw new System.NotImplementedException(); }