Skip to content

Commit

Permalink
Adding builtin type conversion and client name fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekctek committed Mar 1, 2023
1 parent 59b959c commit aa3a2b6
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 304 deletions.
21 changes: 0 additions & 21 deletions src/ClientGenerator/RoslynSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ private static CompilationUnitSyntax PostProcessSourceCode(


// Moves all namespaces to usings from the Type declarations
//SetUsings(ref compilationUnit);
BuildSourceWithShorthands(ref compilationUnit);

var namespaceRemover = new NamespacePrefixRemover(modelNamespace);
compilationUnit = (CompilationUnitSyntax)namespaceRemover.Visit(compilationUnit)!;
Expand All @@ -107,24 +105,5 @@ private static CompilationUnitSyntax PostProcessSourceCode(
return compilationUnit;
}

private static void BuildSourceWithShorthands(ref CompilationUnitSyntax compilationUnit)
{
// TODO
//private static Dictionary<Type, string> systemTypeShorthands = new Dictionary<Type, string>
//{
// { typeof(string), "string" },
// { typeof(byte), "byte" },
// { typeof(ushort), "ushort" },
// { typeof(uint), "uint" },
// { typeof(ulong), "ulong" },
// { typeof(sbyte), "sbyte" },
// { typeof(short), "short" },
// { typeof(int), "int" },
// { typeof(long), "long" },
// { typeof(float), "float" },
// { typeof(double), "double" },
// { typeof(bool), "bool" }
//};
}
}
}
22 changes: 22 additions & 0 deletions src/ClientGenerator/SyntaxRewriters/NamespacePrefixRemover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ private NameSyntax StripNamespace(NameSyntax node)
}
case QualifiedNameSyntax q:
{
string? builtInType = q.ToString() switch
{
"System.String" => "string",
"System.Byte" => "byte",
"System.UInt16" => "ushort",
"System.UInt32" => "uint",
"System.UInt64" => "ulong",
"System.Int16" => "short",
"System.Int32" => "int",
"System.Int64" => "long",
"System.Single" => "float",
"System.Double" => "double",
"System.Boolean" => "bool",
"System.Decimal" => "decimal",
"System.Char" => "char",
"System.Object" => "object",
_ => null,
};
if (builtInType != null)
{
return SyntaxFactory.IdentifierName(builtInType);
}
NameSyntax @namespace = q.Left;
NameSyntax type = this.StripNamespace(q.Right);

Expand Down
4 changes: 2 additions & 2 deletions src/ClientGenerator/Tool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public static async Task Main(string[] args)

private static void WriteClient(ClientSyntax result, string outputDirectory)
{
Console.WriteLine($"Writing client file ./Client.cs...");
WriteFile(null, "Client", result.ClientFile);
Console.WriteLine($"Writing client file ./{result.Name}.cs...");
WriteFile(null, result.Name, result.ClientFile);


foreach ((string name, CompilationUnitSyntax sourceCode) in result.TypeFiles)
Expand Down
35 changes: 15 additions & 20 deletions test/Common.Tests/Generators/__snapshots__/Dex.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ using Test.Models;
using System.Threading.Tasks;
using System.Collections.Generic;
using EdjCase.ICP.Agent.Responses;
using System;

namespace Test.Models
{
Expand Down Expand Up @@ -72,11 +71,11 @@ namespace Test.Models
return reply.ToObjects<List<Balance>>();
}

public async System.Threading.Tasks.Task<List<Byte>> GetDepositAddress()
public async System.Threading.Tasks.Task<List<byte>> GetDepositAddress()
{
CandidArg arg = CandidArg.FromCandid();
CandidArg reply = await this.Agent.CallAndWaitAsync(this.CanisterId, "getDepositAddress", arg);
return reply.ToObjects<List<Byte>>();
return reply.ToObjects<List<byte>>();
}

public async System.Threading.Tasks.Task<OptionalValue<Order>> GetOrder(OrderId arg0)
Expand All @@ -93,11 +92,11 @@ namespace Test.Models
return reply.ToObjects<List<Order>>();
}

public async System.Threading.Tasks.Task<String> GetSymbol(Token arg0)
public async System.Threading.Tasks.Task<string> GetSymbol(Token arg0)
{
CandidArg arg = CandidArg.FromCandid(CandidTypedValue.FromObject(arg0));
CandidArg reply = await this.Agent.CallAndWaitAsync(this.CanisterId, "getSymbol", arg);
return reply.ToObjects<String>();
return reply.ToObjects<string>();
}

public async System.Threading.Tasks.Task<OrderPlacementReceipt> PlaceOrder(Token arg0, UnboundedUInt arg1, Token arg2, UnboundedUInt arg3)
Expand Down Expand Up @@ -130,8 +129,8 @@ using Token = EdjCase.ICP.Candid.Models.Principal;
using OrderId = System.UInt32;
using EdjCase.ICP.Candid.Mapping;
using Test.Models;
using System;
using EdjCase.ICP.Candid.Models;
using System;

namespace Test.Models
{
Expand All @@ -144,7 +143,7 @@ namespace Test.Models
[VariantValueProperty()]
public System.Object? Value { get; set; }

public WithdrawReceipt(WithdrawReceiptTag tag, Object? value)
public WithdrawReceipt(WithdrawReceiptTag tag, object? value)
{
this.Tag = tag;
this.Value = value;
Expand Down Expand Up @@ -202,7 +201,6 @@ using Token = EdjCase.ICP.Candid.Models.Principal;
using OrderId = System.UInt32;
using EdjCase.ICP.Candid.Mapping;
using Test.Models;
using System;

namespace Test.Models
{
Expand All @@ -215,7 +213,7 @@ namespace Test.Models
[VariantValueProperty()]
public System.Object? Value { get; set; }

public WithdrawErr(WithdrawErrTag tag, Object? value)
public WithdrawErr(WithdrawErrTag tag, object? value)
{
this.Tag = tag;
this.Value = value;
Expand Down Expand Up @@ -251,8 +249,8 @@ using Token = EdjCase.ICP.Candid.Models.Principal;
using OrderId = System.UInt32;
using EdjCase.ICP.Candid.Mapping;
using Test.Models;
using System;
using EdjCase.ICP.Candid.Models;
using System;

namespace Test.Models
{
Expand All @@ -265,7 +263,7 @@ namespace Test.Models
[VariantValueProperty()]
public System.Object? Value { get; set; }

public OrderPlacementReceipt(OrderPlacementReceiptTag tag, Object? value)
public OrderPlacementReceipt(OrderPlacementReceiptTag tag, object? value)
{
this.Tag = tag;
this.Value = value;
Expand Down Expand Up @@ -323,7 +321,6 @@ using Token = EdjCase.ICP.Candid.Models.Principal;
using OrderId = System.UInt32;
using EdjCase.ICP.Candid.Mapping;
using Test.Models;
using System;

namespace Test.Models
{
Expand All @@ -336,7 +333,7 @@ namespace Test.Models
[VariantValueProperty()]
public System.Object? Value { get; set; }

public OrderPlacementErr(OrderPlacementErrTag tag, Object? value)
public OrderPlacementErr(OrderPlacementErrTag tag, object? value)
{
this.Tag = tag;
this.Value = value;
Expand Down Expand Up @@ -417,8 +414,8 @@ using Token = EdjCase.ICP.Candid.Models.Principal;
using OrderId = System.UInt32;
using EdjCase.ICP.Candid.Mapping;
using Test.Models;
using System;
using EdjCase.ICP.Candid.Models;
using System;

namespace Test.Models
{
Expand All @@ -431,7 +428,7 @@ namespace Test.Models
[VariantValueProperty()]
public System.Object? Value { get; set; }

public DepositReceipt(DepositReceiptTag tag, Object? value)
public DepositReceipt(DepositReceiptTag tag, object? value)
{
this.Tag = tag;
this.Value = value;
Expand Down Expand Up @@ -489,7 +486,6 @@ using Token = EdjCase.ICP.Candid.Models.Principal;
using OrderId = System.UInt32;
using EdjCase.ICP.Candid.Mapping;
using Test.Models;
using System;

namespace Test.Models
{
Expand All @@ -502,7 +498,7 @@ namespace Test.Models
[VariantValueProperty()]
public System.Object? Value { get; set; }

public DepositErr(DepositErrTag tag, Object? value)
public DepositErr(DepositErrTag tag, object? value)
{
this.Tag = tag;
this.Value = value;
Expand Down Expand Up @@ -551,7 +547,7 @@ namespace Test.Models
[VariantValueProperty()]
public System.Object? Value { get; set; }

public CancelOrderReceipt(CancelOrderReceiptTag tag, Object? value)
public CancelOrderReceipt(CancelOrderReceiptTag tag, object? value)
{
this.Tag = tag;
this.Value = value;
Expand Down Expand Up @@ -609,7 +605,6 @@ using Token = EdjCase.ICP.Candid.Models.Principal;
using OrderId = System.UInt32;
using EdjCase.ICP.Candid.Mapping;
using Test.Models;
using System;

namespace Test.Models
{
Expand All @@ -622,7 +617,7 @@ namespace Test.Models
[VariantValueProperty()]
public System.Object? Value { get; set; }

public CancelOrderErr(CancelOrderErrTag tag, Object? value)
public CancelOrderErr(CancelOrderErrTag tag, object? value)
{
this.Tag = tag;
this.Value = value;
Expand Down
Loading

0 comments on commit aa3a2b6

Please sign in to comment.