Skip to content

Commit

Permalink
[NEW-FEATURE] Add swapping methods to ITwinObject interface (#127)
Browse files Browse the repository at this point in the history
* Create draft PR for #126

* [ixc] adds type agnostic methods for swapping data

* asp

---------

Co-authored-by: PTKu <PTKu@users.noreply.github.com>
  • Loading branch information
PTKu and PTKu authored Mar 20, 2023
1 parent d96a571 commit 7b33f4f
Show file tree
Hide file tree
Showing 50 changed files with 1,644 additions and 39 deletions.
4 changes: 4 additions & 0 deletions src/ix.blazor/tests/sandbox/ax-blazor-example/apax.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ targets:
- axunit-llvm
devDependencies:
"@ax/sdk": 3.0.2
scripts:
ixc:
- dotnet run --project ..\\..\\..\\..\\ix.compiler\\src\\ixc\\Ix.ixc.csproj --framework net7.0

1 change: 1 addition & 0 deletions src/ix.blazor/tests/sandbox/ax-blazor-example/ix/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using System.Threading.Tasks;
10 changes: 10 additions & 0 deletions src/ix.compiler/src/IX.Cs.Compiler/Helpers/CsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ public static string GetAttributeNameValue(this IDeclaration declaration, string
{
return declaration.GetPropertyValue("AttributeName", memberName);
}

public static string CreateGenericSwapperMethodToPlainer(string methodName)
{
return $"public object {methodName}(){{\n return this.{methodName}Async().Result;\n}}";
}

public static string CreateGenericSwapperMethodFromPlainer(string methodName, string pocoTypeName)
{
return $"public void {methodName}(object plain){{\n this.{methodName}Async(({pocoTypeName})plain).Wait();\n}}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,18 @@ internal void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration de
case IClassDeclaration classDeclaration:
//case IAnonymousTypeDeclaration anonymousTypeDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($" plain.{declaration.Name} = await {declaration.Name}.{MethodName}();");
AddToSource($" plain.{declaration.Name} = await {declaration.Name}.{MethodName}Async();");
break;
case IArrayTypeDeclaration arrayTypeDeclaration:
switch (arrayTypeDeclaration.ElementTypeAccess.Type)
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
//plain.ArrayOfDrives = ArrayOfDrives.Select(async p => await p.OnlineToPlainAsync()).Select(p => p.Result).ToArray();
AddToSource($"plain.{declaration.Name} = {declaration.Name}.Select(async p => await p.{MethodName}()).Select(p => p.Result).ToArray();");
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($"plain.{declaration.Name} = {declaration.Name}.Select(async p => await p.{MethodName}Async()).Select(p => p.Result).ToArray();");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
//plain.ArrayOfBytes = ArrayOfBytes.Select(p => p.LastValue).ToArray();

AddToSource($"plain.{declaration.Name} = {declaration.Name}.Select(p => p.LastValue).ToArray();");
//AddToSource(
// $"Ix.Connector.BuilderHelpers.Arrays.CopyOnlineToPlain<{IecToOnlinerConverter.TransformType(arrayTypeDeclaration.ElementTypeAccess.Type)},{IecToClrConverter.TransformType(arrayTypeDeclaration.ElementTypeAccess.Type)}>({declaration.Name}, plain.{declaration.Name});");
case IStringTypeDeclaration stringTypeDeclaration:
AddToSource($"plain.{declaration.Name} = {declaration.Name}.Select(p => p.LastValue).ToArray();");
break;
}
break;
Expand Down Expand Up @@ -121,7 +116,10 @@ public static CsOnlinerPlainerOnlineToPlainBuilder Create(IxNodeVisitor visitor,
ISourceBuilder sourceBuilder)
{
var builder = new CsOnlinerPlainerOnlineToPlainBuilder(sourceBuilder);
builder.AddToSource($"public async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}(){{\n");

builder.AddToSource(CsHelpers.CreateGenericSwapperMethodToPlainer(MethodName));

builder.AddToSource($"public async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}Async(){{\n");
builder.AddToSource($"Pocos.{semantics.FullyQualifiedName} plain = new Pocos.{semantics.FullyQualifiedName}();");
builder.AddToSource("await this.ReadAsync();");

Expand All @@ -136,13 +134,16 @@ public static CsOnlinerPlainerOnlineToPlainBuilder Create(IxNodeVisitor visitor,
ISourceBuilder sourceBuilder, bool isExtended)
{
var builder = new CsOnlinerPlainerOnlineToPlainBuilder(sourceBuilder);
builder.AddToSource($"public async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}(){{\n");

builder.AddToSource(CsHelpers.CreateGenericSwapperMethodToPlainer(MethodName));

builder.AddToSource($"public async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}Async(){{\n");
builder.AddToSource($"Pocos.{semantics.FullyQualifiedName} plain = new Pocos.{semantics.FullyQualifiedName}();");
builder.AddToSource("await this.ReadAsync();");

if (isExtended)
{
builder.AddToSource($"await base.{MethodName}(plain);");
builder.AddToSource($"await base.{MethodName}Async(plain);");
}

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected CsOnlinerPlainerOnlineToPlainProtectedBuilder(ISourceBuilder sourceBui
ISourceBuilder sourceBuilder)
{
var builder = new CsOnlinerPlainerOnlineToPlainProtectedBuilder(sourceBuilder);
builder.AddToSource($"protected async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}(Pocos.{semantics.FullyQualifiedName} plain){{\n");
builder.AddToSource($"protected async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}Async(Pocos.{semantics.FullyQualifiedName} plain){{\n");

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
builder.AddToSource($"return plain;");
Expand All @@ -45,12 +45,12 @@ protected CsOnlinerPlainerOnlineToPlainProtectedBuilder(ISourceBuilder sourceBui
ISourceBuilder sourceBuilder, bool isExtended)
{
var builder = new CsOnlinerPlainerOnlineToPlainProtectedBuilder(sourceBuilder);
builder.AddToSource($"protected async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}(Pocos.{semantics.FullyQualifiedName} plain){{\n");
builder.AddToSource($"protected async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}Async(Pocos.{semantics.FullyQualifiedName} plain){{\n");


if (isExtended)
{
builder.AddToSource($"await base.{MethodName}(plain);");
builder.AddToSource($"await base.{MethodName}Async(plain);");
}

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration dec
case IClassDeclaration classDeclaration:
//case IAnonymousTypeDeclaration anonymousTypeDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($" await this.{declaration.Name}.{MethodName}(plain.{declaration.Name});");
AddToSource($" await this.{declaration.Name}.{MethodName}Async(plain.{declaration.Name});");
break;
case IArrayTypeDeclaration arrayTypeDeclaration:

Expand All @@ -75,7 +75,7 @@ private void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration dec
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($"var _{declaration.Name}_i_FE8484DAB3 = 0;");
AddToSource($"{declaration.Name}.Select(p => p.{MethodName}(plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++])).ToArray();");
AddToSource($"{declaration.Name}.Select(p => p.{MethodName}Async(plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++])).ToArray();");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
Expand Down Expand Up @@ -121,7 +121,10 @@ public static CsOnlinerPlainerPlainToOnlineBuilder Create(IxNodeVisitor visitor,
ISourceBuilder sourceBuilder)
{
var builder = new CsOnlinerPlainerPlainToOnlineBuilder(sourceBuilder);
builder.AddToSource($"public async Task<IEnumerable<ITwinPrimitive>> {MethodName}(Pocos.{semantics.FullyQualifiedName} plain){{\n");

builder.AddToSource(CsHelpers.CreateGenericSwapperMethodFromPlainer(MethodName, $"Pocos.{semantics.FullyQualifiedName}"));

builder.AddToSource($"public async Task<IEnumerable<ITwinPrimitive>> {MethodName}Async(Pocos.{semantics.FullyQualifiedName} plain){{\n");

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));

Expand All @@ -135,12 +138,15 @@ public static CsOnlinerPlainerPlainToOnlineBuilder Create(IxNodeVisitor visitor,
ISourceBuilder sourceBuilder, bool isExtended)
{
var builder = new CsOnlinerPlainerPlainToOnlineBuilder(sourceBuilder);
builder.AddToSource($"public async Task<IEnumerable<ITwinPrimitive>> {MethodName}(Pocos.{semantics.FullyQualifiedName} plain){{\n");

builder.AddToSource(CsHelpers.CreateGenericSwapperMethodFromPlainer(MethodName, $"Pocos.{semantics.FullyQualifiedName}"));

builder.AddToSource($"public async Task<IEnumerable<ITwinPrimitive>> {MethodName}Async(Pocos.{semantics.FullyQualifiedName} plain){{\n");


if (isExtended)
{
builder.AddToSource($"await base.{MethodName}(plain);");
builder.AddToSource($"await base.{MethodName}Async(plain);");
}

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration dec
case IClassDeclaration classDeclaration:
//case IAnonymousTypeDeclaration anonymousTypeDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($" await this.{declaration.Name}.{MethodName}(plain.{declaration.Name});");
AddToSource($" await this.{declaration.Name}.{MethodName}Async(plain.{declaration.Name});");
break;
case IArrayTypeDeclaration arrayTypeDeclaration:

Expand All @@ -74,7 +74,7 @@ private void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration dec
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($"var _{declaration.Name}_i_FE8484DAB3 = 0;");
AddToSource($"{declaration.Name}.Select(p => p.{MethodName}(plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++])).ToArray();");
AddToSource($"{declaration.Name}.Select(p => p.{MethodName}Async(plain.{declaration.Name}[_{declaration.Name}_i_FE8484DAB3++])).ToArray();");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
Expand Down Expand Up @@ -120,7 +120,10 @@ public static CsOnlinerPlainerPlainToShadowBuilder Create(IxNodeVisitor visitor,
ISourceBuilder sourceBuilder)
{
var builder = new CsOnlinerPlainerPlainToShadowBuilder(sourceBuilder);
builder.AddToSource($"public async Task<IEnumerable<ITwinPrimitive>> {MethodName}(Pocos.{semantics.FullyQualifiedName} plain){{\n");

builder.AddToSource(CsHelpers.CreateGenericSwapperMethodFromPlainer(MethodName, $"Pocos.{semantics.FullyQualifiedName}"));

builder.AddToSource($"public async Task<IEnumerable<ITwinPrimitive>> {MethodName}Async(Pocos.{semantics.FullyQualifiedName} plain){{\n");

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));

Expand All @@ -133,12 +136,15 @@ public static CsOnlinerPlainerPlainToShadowBuilder Create(IxNodeVisitor visitor,
ISourceBuilder sourceBuilder, bool isExtended)
{
var builder = new CsOnlinerPlainerPlainToShadowBuilder(sourceBuilder);
builder.AddToSource($"public async Task<IEnumerable<ITwinPrimitive>> {MethodName}(Pocos.{semantics.FullyQualifiedName} plain){{\n");

builder.AddToSource(CsHelpers.CreateGenericSwapperMethodFromPlainer(MethodName, $"Pocos.{semantics.FullyQualifiedName}"));

builder.AddToSource($"public async Task<IEnumerable<ITwinPrimitive>> {MethodName}Async(Pocos.{semantics.FullyQualifiedName} plain){{\n");


if (isExtended)
{
builder.AddToSource($"await base.{MethodName}(plain);");
builder.AddToSource($"await base.{MethodName}Async(plain);");
}

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ internal void CreateAssignment(ITypeDeclaration typeDeclaration, IDeclaration de
case IClassDeclaration classDeclaration:
//case IAnonymousTypeDeclaration anonymousTypeDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($" plain.{declaration.Name} = await {declaration.Name}.{MethodName}();");
AddToSource($" plain.{declaration.Name} = await {declaration.Name}.{MethodName}Async();");
break;
case IArrayTypeDeclaration arrayTypeDeclaration:
switch (arrayTypeDeclaration.ElementTypeAccess.Type)
{
case IClassDeclaration classDeclaration:
case IStructuredTypeDeclaration structuredTypeDeclaration:
AddToSource($"plain.{declaration.Name} = {declaration.Name}.Select(async p => await p.{MethodName}()).Select(p => p.Result).ToArray();");
AddToSource($"plain.{declaration.Name} = {declaration.Name}.Select(async p => await p.{MethodName}Async()).Select(p => p.Result).ToArray();");
break;
case IScalarTypeDeclaration scalarTypeDeclaration:
case IStringTypeDeclaration stringTypeDeclaration:
Expand Down Expand Up @@ -117,7 +117,10 @@ public static CsOnlinerPlainerShadowToPlainBuilder Create(IxNodeVisitor visitor,
ISourceBuilder sourceBuilder)
{
var builder = new CsOnlinerPlainerShadowToPlainBuilder(sourceBuilder);
builder.AddToSource($"public async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}(){{\n");

builder.AddToSource(CsHelpers.CreateGenericSwapperMethodToPlainer(MethodName));

builder.AddToSource($"public async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}Async(){{\n");
builder.AddToSource($"Pocos.{semantics.FullyQualifiedName} plain = new Pocos.{semantics.FullyQualifiedName}();");

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
Expand All @@ -131,12 +134,15 @@ public static CsOnlinerPlainerShadowToPlainBuilder Create(IxNodeVisitor visitor,
ISourceBuilder sourceBuilder, bool isExtended)
{
var builder = new CsOnlinerPlainerShadowToPlainBuilder(sourceBuilder);
builder.AddToSource($"public async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}(){{\n");

builder.AddToSource(CsHelpers.CreateGenericSwapperMethodToPlainer(MethodName));

builder.AddToSource($"public async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}Async(){{\n");
builder.AddToSource($"Pocos.{semantics.FullyQualifiedName} plain = new Pocos.{semantics.FullyQualifiedName}();");

if (isExtended)
{
builder.AddToSource($"await base.{MethodName}(plain);");
builder.AddToSource($"await base.{MethodName}Async(plain);");
}

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected CsOnlinerPlainerShadowToPlainProtectedBuilder(ISourceBuilder sourceBui
ISourceBuilder sourceBuilder)
{
var builder = new CsOnlinerPlainerShadowToPlainProtectedBuilder(sourceBuilder);
builder.AddToSource($"protected async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}(Pocos.{semantics.FullyQualifiedName} plain){{\n");
builder.AddToSource($"protected async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}Async(Pocos.{semantics.FullyQualifiedName} plain){{\n");

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
builder.AddToSource($"return plain;");
Expand All @@ -43,12 +43,12 @@ protected CsOnlinerPlainerShadowToPlainProtectedBuilder(ISourceBuilder sourceBui
ISourceBuilder sourceBuilder, bool isExtended)
{
var builder = new CsOnlinerPlainerShadowToPlainProtectedBuilder(sourceBuilder);
builder.AddToSource($"protected async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}(Pocos.{semantics.FullyQualifiedName} plain){{\n");
builder.AddToSource($"protected async Task<Pocos.{semantics.FullyQualifiedName}> {MethodName}Async(Pocos.{semantics.FullyQualifiedName} plain){{\n");


if (isExtended)
{
builder.AddToSource($"await base.{MethodName}(plain);");
builder.AddToSource($"await base.{MethodName}Async(plain);");
}

semantics.Fields.ToList().ForEach(p => p.Accept(visitor, builder));
Expand Down
Loading

0 comments on commit 7b33f4f

Please sign in to comment.