【修复】修复 cfg python3 生成的代码无法加载可空数据的bug

【修复】修复 cfg python3 生成的enum类 未继承enum.Enum导致无法正确构造的bug
main
walon 2021-08-14 12:53:20 +08:00
parent ef6524626e
commit 3e8bfb1d77
3 changed files with 47 additions and 12 deletions

View File

@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.Defs
{ {
if (type.IsNullable) if (type.IsNullable)
{ {
return $"{{ var _j = {bufName}.GetProperty(\"{jsonFieldName}\"); if (_j.ValueKind != JsonValueKind.Null) {{ {type.Apply(TypeVisitors.CsJsonDeserialize.Ins, "_j", fieldName)} }} else {{ {fieldName} = null; }} }}"; return $"{{ if ({bufName}.TryGetProperty(\"{jsonFieldName}\", out var _j) && _j.ValueKind != JsonValueKind.Null) {{ {type.Apply(TypeVisitors.CsJsonDeserialize.Ins, "_j", fieldName)} }} else {{ {fieldName} = null; }} }}";
} }
else else
{ {
@ -156,14 +156,52 @@ namespace Luban.Job.Cfg.Defs
} }
} }
public static string Py3Deserialize(string fieldName, string jsonFieldName, TType type) public static string Py3DeserializeValue(string fieldName, string jsonVarName, TType type)
{ {
return type.Apply(PyDeserializeVisitor.Py3Ins, $"{jsonFieldName}", fieldName); if (type.IsNullable)
{
return $"if {jsonVarName} != None: {type.Apply(PyUnderingDeserializeVisitor.Py3Ins, jsonVarName, fieldName)}";
}
else
{
return type.Apply(PyUnderingDeserializeVisitor.Py3Ins, jsonVarName, fieldName);
}
} }
public static string Py27Deserialize(string fieldName, string jsonFieldName, TType type) public static string Py27DeserializeValue(string fieldName, string jsonVarName, TType type)
{ {
return type.Apply(PyDeserializeVisitor.Py27Ins, $"{jsonFieldName}", fieldName); if (type.IsNullable)
{
return $"if {jsonVarName} != None: {type.Apply(PyUnderingDeserializeVisitor.Py27Ins, jsonVarName, fieldName)}";
}
else
{
return type.Apply(PyUnderingDeserializeVisitor.Py3Ins, jsonVarName, fieldName);
}
}
public static string Py3DeserializeField(string fieldName, string jsonVarName, string jsonFieldName, TType type)
{
if (type.IsNullable)
{
return $"if {jsonVarName}.get('{jsonFieldName}') != None: {type.Apply(PyUnderingDeserializeVisitor.Py3Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName)}";
}
else
{
return type.Apply(PyUnderingDeserializeVisitor.Py3Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName);
}
}
public static string Py27DeserializeField(string fieldName, string jsonVarName, string jsonFieldName, TType type)
{
if (type.IsNullable)
{
return $"if {jsonVarName}.get('{jsonFieldName}') != None: {type.Apply(PyUnderingDeserializeVisitor.Py3Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName)}";
}
else
{
return type.Apply(PyUnderingDeserializeVisitor.Py27Ins, $"{jsonVarName}['{jsonFieldName}']", fieldName);
}
} }
} }
} }

View File

@ -48,10 +48,7 @@ class {{name}} {{if parent_def_type}}({{parent_def_type.py_full_name}}){{else if
{{parent_def_type.py_full_name}}.__init__(self, _json_) {{parent_def_type.py_full_name}}.__init__(self, _json_)
{{~end~}} {{~end~}}
{{~ for field in export_fields ~}} {{~ for field in export_fields ~}}
{{~if !field.ctype.is_nullable~}} {{py3_deserialize_field ('self.' + field.py_style_name) '_json_' field.name field.ctype}}
if _json_['{{field.name}}'] == None: raise Exception()
{{~end~}}
{{py3_deserialize ('self.' + field.py_style_name) ('_json_[""' + field.name + '""]') field.ctype}}
{{~end~}} {{~end~}}
{{~if export_fields.empty?}} {{~if export_fields.empty?}}
pass pass
@ -83,7 +80,7 @@ class {{name}}:
self._dataList = [] self._dataList = []
for _json2_ in _json_: for _json2_ in _json_:
{{py3_deserialize '_v' '_json2_' value_type}} {{py3_deserialize_value '_v' '_json2_' value_type}}
self._dataList.append(_v) self._dataList.append(_v)
self._dataMap[_v.{{x.index_field.py_style_name}}] = _v self._dataMap[_v.{{x.index_field.py_style_name}}] = _v
@ -96,7 +93,7 @@ class {{name}}:
def __init__(self, _json_): def __init__(self, _json_):
if (len(_json_) != 1): raise Exception('table mode=one, but size != 1') if (len(_json_) != 1): raise Exception('table mode=one, but size != 1')
{{py3_deserialize 'self._data' '_json_[0]' value_type}} {{py3_deserialize_value 'self._data' '_json_[0]' value_type}}
def getData(self) : return self._data def getData(self) : return self._data

View File

@ -55,7 +55,7 @@ class {{x.py_full_name}}:
{{comment}} {{comment}}
''' '''
{{~end~}} {{~end~}}
class {{py_full_name}}: class {{py_full_name}}(Enum):
{{~ for item in items ~}} {{~ for item in items ~}}
{{~if item.comment != '' ~}} {{~if item.comment != '' ~}}
''' '''