Skip to content

Commit

Permalink
Support to object type dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago Almeida committed Nov 6, 2023
1 parent 77a4c5e commit 2918975
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion samples/SampleModels/Models/NullableAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace SampleModels.Models
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class NullableAttribute: Attribute
public class NullableAttribute : Attribute
{
}
}
2 changes: 2 additions & 0 deletions samples/SampleModels/Models/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ public class Person
[UnknownObject]
[Optional]
public object ShouldBeUnknown { get; set; }

[Optional] public Dictionary<string, object> Pairs { get; set; }
}
}
2 changes: 1 addition & 1 deletion samples/SampleModels/Models/TypeUnions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace SampleModels.Models
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class TypeUnionAttribute: Attribute
public class TypeUnionAttribute : Attribute
{
public TypeUnionAttribute(params string[] args) { }
public TypeUnionAttribute(params double[] args) { }
Expand Down
1 change: 1 addition & 0 deletions samples/SampleModels/Typescript/Person.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export interface Person {
typeUnionDouble: 1.2 | 2.2;
shouldBeAny: any;
shouldBeUnknown?: unknown;
pairs?: Map<string, unknown>;
}
2 changes: 1 addition & 1 deletion src/Onbox.TypeSharp/Onbox.TypeSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<AssemblyName>TypeSharp</AssemblyName>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<PackageId>Onbox.TypeSharp</PackageId>
<Version>0.3.9</Version>
<Version>0.4.0</Version>
<Authors>Thiago Almeida</Authors>
<Company>Onbox</Company>
<Description>Command line app to convert CSharp data models into Typescript</Description>
Expand Down
3 changes: 3 additions & 0 deletions src/Onbox.TypeSharp/Services/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public string ConvertModel(Type type)
foreach (var prop in props)
{
var contextPropType = prop.PropertyType;

if (this.propertyUtils.ShouldImport(contextPropType) && contextPropType != type)
{
// Collection needs to be checked for its generic item type
Expand Down Expand Up @@ -224,6 +225,8 @@ public string ConvertModel(Type type)
}
classBodyBuilder.AppendLine("}");

importStatments = string.Join("\n", importStatments.Split("\n").Where(l => l.StartsWith("export { Object } from \"./Object\"") == false).Distinct());

var result = importStatments.Any() ? importStatments + Environment.NewLine + classBodyBuilder.ToString() : classBodyBuilder.ToString();
return result;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Onbox.TypeSharp/Services/TypeNamingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public string GetPropertyTypeName(Type type)
{
return "CustomEvent";
}
else if (this.typeUtils.IsCustomPropObjects(type))
{
return "Map<string, unknown>";
}
//else if (this.typeUtils.IsDictionary(type))
//{
// Implement Dictionaries in the future
//}
else if (this.typeUtils.IsCollection(type))
{
var arg = this.genericTypeUtils.GetGenericType(type);
Expand Down
18 changes: 18 additions & 0 deletions src/Onbox.TypeSharp/Services/TypeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,23 @@ internal bool IsUnion(Type type)

return false;
}

internal bool IsCustomPropObjects(Type type)
{
var startsWithLongFormat = type.FullName?.StartsWith("System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib");
var includesObjectFormat = type.FullName?.Contains("[System.Object");
if (startsWithLongFormat == true && includesObjectFormat == true)
{
return true;
}

var startsWithSimpleFormat = type.FullName?.StartsWith("System.Collections.Generic.Dictionary`2[System.String,System.Object]");
if (startsWithSimpleFormat == true)
{
return true;
}

return false;
}
}
}

0 comments on commit 2918975

Please sign in to comment.