Skip to content

Commit

Permalink
[fix] 修复多维容器下生成typescript代码的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
pirunxi committed Jan 19, 2024
1 parent e898f2b commit df1486a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class TypescriptJsonTemplateExtension : ScriptObject
{
public static string Deserialize(string fieldName, string jsonVar, TType type)
{
return type.Apply(JsonDeserializeVisitor.Ins, jsonVar, fieldName);
return type.Apply(JsonDeserializeVisitor.Ins, jsonVar, fieldName, 0);
}
}
8 changes: 4 additions & 4 deletions src/Luban.Typescript/TypeVisitors/JsonDeserializeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

namespace Luban.Typescript.TypeVisitors;

public class JsonDeserializeVisitor : DecoratorFuncVisitor<string, string, string>
public class JsonDeserializeVisitor : DecoratorFuncVisitor<string, string, int, string>
{
public static JsonDeserializeVisitor Ins { get; } = new();

public override string DoAccept(TType type, string jsonFieldName, string fieldName)
public override string DoAccept(TType type, string jsonFieldName, string fieldName, int depth)
{
if (type.IsNullable)
{
return $"if({jsonFieldName} != undefined) {{ {type.Apply(JsonUnderlyingDeserializeVisitor.Ins, jsonFieldName, fieldName)} }} else {{ {fieldName} = undefined }}";
return $"if({jsonFieldName} != undefined) {{ {type.Apply(JsonUnderlyingDeserializeVisitor.Ins, jsonFieldName, fieldName, depth)} }} else {{ {fieldName} = undefined }}";
}
else
{
return type.Apply(JsonUnderlyingDeserializeVisitor.Ins, jsonFieldName, fieldName);
return type.Apply(JsonUnderlyingDeserializeVisitor.Ins, jsonFieldName, fieldName, depth);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,61 @@

namespace Luban.Typescript.TypeVisitors;

public class JsonUnderlyingDeserializeVisitor : ITypeFuncVisitor<string, string, string>
public class JsonUnderlyingDeserializeVisitor : ITypeFuncVisitor<string, string, int, string>
{
public static JsonUnderlyingDeserializeVisitor Ins { get; } = new JsonUnderlyingDeserializeVisitor();

public string Accept(TBool type, string jsonVarName, string fieldName)
public string Accept(TBool type, string jsonVarName, string fieldName, int depth)
{
return $"{fieldName} = {jsonVarName}";
}

public string Accept(TByte type, string jsonVarName, string fieldName)
public string Accept(TByte type, string jsonVarName, string fieldName, int depth)
{
return $"{fieldName} = {jsonVarName}";
}

public string Accept(TShort type, string jsonVarName, string fieldName)
public string Accept(TShort type, string jsonVarName, string fieldName, int depth)
{
return $"{fieldName} = {jsonVarName}";
}

public string Accept(TInt type, string jsonVarName, string fieldName)
public string Accept(TInt type, string jsonVarName, string fieldName, int depth)
{
return $"{fieldName} = {jsonVarName}";
}

public string Accept(TLong type, string jsonVarName, string fieldName)
public string Accept(TLong type, string jsonVarName, string fieldName, int depth)
{
return $"{fieldName} = {jsonVarName}";
}

public string Accept(TFloat type, string jsonVarName, string fieldName)
public string Accept(TFloat type, string jsonVarName, string fieldName, int depth)
{
return $"{fieldName} = {jsonVarName}";
}

public string Accept(TDouble type, string jsonVarName, string fieldName)
public string Accept(TDouble type, string jsonVarName, string fieldName, int depth)
{
return $"{fieldName} = {jsonVarName}";
}

public string Accept(TEnum type, string jsonVarName, string fieldName)
public string Accept(TEnum type, string jsonVarName, string fieldName, int depth)
{
return $"{fieldName} = {jsonVarName}";
}

public string Accept(TString type, string jsonVarName, string fieldName)
public string Accept(TString type, string jsonVarName, string fieldName, int depth)
{
return $"{fieldName} = {jsonVarName}";
}

public string Accept(TDateTime type, string jsonVarName, string fieldName)
public string Accept(TDateTime type, string jsonVarName, string fieldName, int depth)
{
return $"{fieldName} = {jsonVarName}";
}

public string Accept(TBean type, string jsonVarName, string fieldName)
public string Accept(TBean type, string jsonVarName, string fieldName, int depth)
{
if (type.DefBean.IsAbstractType)
{
Expand All @@ -71,30 +71,30 @@ public string Accept(TBean type, string jsonVarName, string fieldName)
}
}

public string Accept(TArray type, string jsonVarName, string fieldName)
public string Accept(TArray type, string jsonVarName, string fieldName, int depth)
{
return $"{{ {fieldName} = []; for(let _ele of {jsonVarName}) {{ let _e; {type.ElementType.Apply(this, "_ele", "_e")}; {fieldName}.push(_e);}}}}";
return $"{{ {fieldName} = []; for(let _ele{depth} of {jsonVarName}) {{ let _e{depth}; {type.ElementType.Apply(this, $"_ele{depth}", $"_e{depth}", depth + 1)}; {fieldName}.push(_e{depth});}}}}";
}

public string Accept(TList type, string jsonVarName, string fieldName)
public string Accept(TList type, string jsonVarName, string fieldName, int depth)
{
return $"{{ {fieldName} = []; for(let _ele of {jsonVarName}) {{ let _e; {type.ElementType.Apply(this, "_ele", "_e")}; {fieldName}.push(_e);}}}}";
return $"{{ {fieldName} = []; for(let _ele{depth} of {jsonVarName}) {{ let _e{depth}; {type.ElementType.Apply(this, $"_ele{depth}", $"_e{depth}", depth + 1)}; {fieldName}.push(_e{depth});}}}}";
}

public string Accept(TSet type, string jsonVarName, string fieldName)
public string Accept(TSet type, string jsonVarName, string fieldName, int depth)
{
if (type.Apply(SimpleJsonTypeVisitor.Ins))
{
return $"{fieldName} = {jsonVarName}";
}
else
{
return $"{{ {fieldName} = new {type.Apply(DeclaringTypeNameVisitor.Ins)}(); for(var _ele of {jsonVarName}) {{ let _e; {type.ElementType.Apply(this, "_ele", "_e")}; {fieldName}.add(_e);}}}}";
return $"{{ {fieldName} = new {type.Apply(DeclaringTypeNameVisitor.Ins)}(); for(var _ele{depth} of {jsonVarName}) {{ let _e{depth}; {type.ElementType.Apply(this, $"_ele{depth}", $"_e{depth}", depth + 1)}; {fieldName}.add(_e{depth});}}}}";
}
}

public string Accept(TMap type, string jsonVarName, string fieldName)
public string Accept(TMap type, string jsonVarName, string fieldName, int depth)
{
return $"{fieldName} = new {type.Apply(DeclaringTypeNameVisitor.Ins)}(); for(var _entry_ of {jsonVarName}) {{ let _k; {type.KeyType.Apply(this, "_entry_[0]", "_k")}; let _v; {type.ValueType.Apply(this, "_entry_[1]", "_v")}; {fieldName}.set(_k, _v); }}";
return $"{fieldName} = new {type.Apply(DeclaringTypeNameVisitor.Ins)}(); for(var _entry{depth}_ of {jsonVarName}) {{ let _k{depth}; {type.KeyType.Apply(this, $"_entry{depth}_[0]", $"_k{depth}", depth + 1)}; let _v{depth}; {type.ValueType.Apply(this, $"_entry{depth}_[1]", $"_v{depth}", depth + 1)}; {fieldName}.set(_k{depth}, _v{depth}); }}";
}
}

0 comments on commit df1486a

Please sign in to comment.