From 3e8bfb1d77c135b02b5f23c90960b734c73556ec Mon Sep 17 00:00:00 2001 From: walon Date: Sat, 14 Aug 2021 12:53:20 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=20cfg=20python3=20=E7=94=9F=E6=88=90=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=97=A0=E6=B3=95=E5=8A=A0=E8=BD=BD=E5=8F=AF?= =?UTF-8?q?=E7=A9=BA=E6=95=B0=E6=8D=AE=E7=9A=84bug=20=E3=80=90=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E3=80=91=E4=BF=AE=E5=A4=8D=20cfg=20python3=20?= =?UTF-8?q?=E7=94=9F=E6=88=90=E7=9A=84enum=E7=B1=BB=20=E6=9C=AA=E7=BB=A7?= =?UTF-8?q?=E6=89=BFenum.Enum=E5=AF=BC=E8=87=B4=E6=97=A0=E6=B3=95=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E6=9E=84=E9=80=A0=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/Defs/TTypeTemplateExtends.cs | 48 +++++++++++++++++-- .../Source/Generate/Python3CodeJsonRender.cs | 9 ++-- .../Source/Generate/PythonCodeRenderBase.cs | 2 +- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs index 800f6cc..8af03d2 100644 --- a/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs +++ b/src/Luban.Job.Cfg/Source/Defs/TTypeTemplateExtends.cs @@ -12,7 +12,7 @@ namespace Luban.Job.Cfg.Defs { 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 { @@ -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); + } } } } diff --git a/src/Luban.Job.Cfg/Source/Generate/Python3CodeJsonRender.cs b/src/Luban.Job.Cfg/Source/Generate/Python3CodeJsonRender.cs index 0b327cb..670e6de 100644 --- a/src/Luban.Job.Cfg/Source/Generate/Python3CodeJsonRender.cs +++ b/src/Luban.Job.Cfg/Source/Generate/Python3CodeJsonRender.cs @@ -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_) {{~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}} + {{py3_deserialize_field ('self.' + field.py_style_name) '_json_' field.name field.ctype}} {{~end~}} {{~if export_fields.empty?}} pass @@ -83,7 +80,7 @@ class {{name}}: self._dataList = [] for _json2_ in _json_: - {{py3_deserialize '_v' '_json2_' value_type}} + {{py3_deserialize_value '_v' '_json2_' value_type}} self._dataList.append(_v) self._dataMap[_v.{{x.index_field.py_style_name}}] = _v @@ -96,7 +93,7 @@ class {{name}}: def __init__(self, _json_): 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 diff --git a/src/Luban.Job.Cfg/Source/Generate/PythonCodeRenderBase.cs b/src/Luban.Job.Cfg/Source/Generate/PythonCodeRenderBase.cs index 1b32d0e..a876fd2 100644 --- a/src/Luban.Job.Cfg/Source/Generate/PythonCodeRenderBase.cs +++ b/src/Luban.Job.Cfg/Source/Generate/PythonCodeRenderBase.cs @@ -55,7 +55,7 @@ class {{x.py_full_name}}: {{comment}} ''' {{~end~}} -class {{py_full_name}}: +class {{py_full_name}}(Enum): {{~ for item in items ~}} {{~if item.comment != '' ~}} '''