Skip to content

Commit

Permalink
Merge pull request #363 from Inxton/330-new-feature-poco-property-of-…
Browse files Browse the repository at this point in the history
…datetime-has-different-default-value-than-plc

Date related type initialization in POCO objects now initialized to the min value of the target controller.
  • Loading branch information
PTKu authored Jan 23, 2025
2 parents 08d4470 + 6c71972 commit e93cb05
Show file tree
Hide file tree
Showing 193 changed files with 19,541 additions and 570 deletions.
25 changes: 11 additions & 14 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,30 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</GlobalPackageReference>

<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="Microsoft.AspNetCore.Components.Web" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="Microsoft.AspNetCore.Components.Web" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="System.Collections.Immutable" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="System.IO.Packaging" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="System.IO.Packaging" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="System.Text.Encodings.Web" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="System.Text.Json" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="System.Speech" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="System.Text.Json" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="System.Speech" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0" Condition="'$(TargetFramework)' == 'net9.0'" />

<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="Microsoft.AspNetCore.Components.Web" Version="8.0.10" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="Microsoft.AspNetCore.Components.Web" Version="8.0.10" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="System.Collections.Immutable" Version="8.0.0" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="System.IO.Packaging" Version="8.0.0" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="System.IO.Packaging" Version="8.0.0" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="System.Text.Encodings.Web" Version="8.0.0" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="System.Speech" Version="8.0.0" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="System.Speech" Version="8.0.0" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.0" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.0" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" Condition="'$(TargetFramework)' == 'net8.0'" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0" Condition="'$(TargetFramework)' == 'net8.0'" />

<PackageVersion Include="Cake.DocFx" Version="1.0.0" />
<PackageVersion Include="Octokit" Version="13.0.1" />
<PackageVersion Include="Octokit.Extensions" Version="1.0.7" />
Expand All @@ -48,7 +45,7 @@
<PackageVersion Include="NuGet.Packaging" Version="6.12.1" />
<PackageVersion Include="Polly" Version="8.5.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageVersion Include="Irony" Version="1.5.3" />
<PackageVersion Include="Irony.Interpreter" Version="1.5.3" />
<PackageVersion Include="CommandLineParser" Version="2.9.1" />
Expand All @@ -66,7 +63,7 @@
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageVersion Include="Siemens.Simatic.S7.Webserver.API" Version="2.2.26" />
<PackageVersion Include="nunit" Version="4.2.2" />
<PackageVersion Include="nunit" Version="4.3.2" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" />
Expand All @@ -76,6 +73,6 @@
<PackageVersion Include="MSTest.TestAdapter" Version="3.6.3" />
<PackageVersion Include="MSTest.TestFramework" Version="3.6.3" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="System.Interactive" Version="6.0.1" />
<PackageVersion Include="System.Interactive" Version="6.0.1" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public interface ICompilerOptions
bool IgnoreS7Pragmas { get; set; }

bool SkipDependencyCompilation { get; set; }

/// <summary>
/// Provides target platform moniker to instruct the compiler about target specific options.
/// </summary>
string TargetPlatfromMoniker { get; set; }
}
9 changes: 6 additions & 3 deletions src/AXSharp.compiler/src/AXSharp.Compiler/AXSharpConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ public string OutputProjectFolder
/// Gets or sets whether compiler should use $base for base types of a class.
/// </summary>
public bool UseBase { get; set; }

/// <inheritdoc />
public bool NoDependencyUpdate { get; set; }

/// <inheritdoc />
public bool IgnoreS7Pragmas { get; set; }
/// <inheritdoc />
public bool SkipDependencyCompilation { get; set; }

/// <inheritdoc />
public string TargetPlatfromMoniker { get; set; }

/// <summary>
/// Gets or sets name of the output project file.
Expand Down Expand Up @@ -157,5 +159,6 @@ private static void OverridesFromCli(ICompilerOptions fromConfig, ICompilerOptio
fromConfig.NoDependencyUpdate = newCompilerOptions.NoDependencyUpdate;
fromConfig.IgnoreS7Pragmas = newCompilerOptions.IgnoreS7Pragmas;
fromConfig.SkipDependencyCompilation = newCompilerOptions.SkipDependencyCompilation;
fromConfig.TargetPlatfromMoniker = newCompilerOptions.TargetPlatfromMoniker;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,30 @@ internal static class IecToClrConverter
private static readonly IDictionary<string, Type> NullabePrimitives = new Dictionary<string, Type>
{
{ "WSTRING", typeof(string) },
{ "STRING", typeof(string) },
{ "STRING", typeof(string) },
{ "DATE", typeof(DateOnly) },
{ "LDATE", typeof(DateOnly) },
{ "DATE_AND_TIME", typeof(DateTime) },
{ "LDATE_AND_TIME", typeof(DateTime) },
{ "DATE_TIME", typeof(DateTime) },
{ "DATE_TIME", typeof(DateTime) },
{ "TIME", typeof(TimeSpan) },
{ "LTIME", typeof(TimeSpan) },
{ "TIME_OF_DAY", typeof(TimeSpan) },
{ "LTIME_OF_DAY", typeof(TimeSpan) },
{ "TOD", typeof(TimeSpan) }
};


private static readonly IDictionary<string, Type> NullabeDateRelatedPrimitives = new Dictionary<string, Type>
{
{ "DATE", typeof(DateOnly) },
{ "LDATE", typeof(DateOnly) },
{ "DATE_AND_TIME", typeof(DateTime) },
{ "LDATE_AND_TIME", typeof(DateTime) },
{ "DATE_TIME", typeof(DateTime) },
};


public static bool IsNonNullablePrimitive(this IElementaryTypeSyntax type)
{
return NonNullabePrimitives.ContainsKey(type.TypeName);
Expand All @@ -66,12 +77,84 @@ public static bool IsNullablePrimitive(this IElementaryTypeSyntax type)
{
return NullabePrimitives.ContainsKey(type.TypeName);
}
public static bool IsNullableDateRelatedPrimitive(this IScalarTypeDeclaration type)
{
return NullabeDateRelatedPrimitives.ContainsKey(type.Type.Name);
}

public static bool IsNullablePrimitive(this IScalarTypeDeclaration type)
public static string CreateScalarInitializer(this IScalarTypeDeclaration scalar, string? targetPlatformMoniker)
{
return NullabePrimitives.ContainsKey(type.Name);
if (targetPlatformMoniker == null)
{
throw new ArgumentNullException(nameof(targetPlatformMoniker), "Target platform moniker cannot be null.");
}

if (!scalar.IsNullableDateRelatedPrimitive() && scalar.IsNullablePrimitive())
{
return $" = default({scalar.TransformType()});\n";
}

if (scalar.IsNullableDateRelatedPrimitive())
{

// We need to provide differrent default values for date
// related types based on target platform

switch (targetPlatformMoniker.ToLower())
{
case "ax":
return scalar.CreateDefaultValueForAx();
case "tia":
return scalar.CreateDefaultValueForTia();
}
}

return string.Empty;
}

private static string CreateDefaultValueForAx(this IScalarTypeDeclaration scalar)
{
switch (scalar.Name.Trim().ToUpper())
{
case "DATE":
return " = new DateOnly(1970, 1, 1);\n";
case "LDATE":
return " = new DateOnly(1970, 1, 1);\n";
case "DATE_AND_TIME":
return " = new DateTime(1970, 1, 1);\n";
case "LDATE_AND_TIME":
return " = new DateTime(1970, 1, 1);\n";
case "LDATE_TIME":
return " = new DateTime(1970, 1, 1);\n";
default:
return " = new();";
}
}

private static string CreateDefaultValueForTia(this IScalarTypeDeclaration scalar)
{
switch (scalar.Name.Trim().ToUpper())
{
case "DATE":
return " = new DateOnly(1990, 1, 1);\n";
case "LDATE":
return " = new DateOnly(1990, 1, 1);\n";
case "DATE_AND_TIME":
return " = new DateTime(1990, 1, 1);\n";
case "LDATE_AND_TIME":
return " = new DateTime(1990, 1, 1);\n";
case "LDATE_TIME":
return " = new DateTime(1990, 1, 1);\n";
default:
return " = new();";
}
}

private static bool IsNullablePrimitive(this IScalarTypeDeclaration type)
{
return NullabePrimitives.ContainsKey(type.Name);
}

public static string TransformType(this IElementaryTypeSyntax type)
{
var typeName = type.TypeName.ToUpperInvariant();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ public void CreateFieldDeclaration(IFieldDeclaration fieldDeclaration, IxNodeVis
break;
case IScalarTypeDeclaration scalar:
AddPropertyDeclaration(fieldDeclaration, visitor);
if (scalar.IsNullablePrimitive())
{
AddToSource($" = default({scalar.TransformType()});\n");
}
AddToSource(scalar.CreateScalarInitializer(this.Project?.CompilerOptions?.TargetPlatfromMoniker));
break;
case IReferenceTypeDeclaration d:
case IStructuredTypeDeclaration s:
Expand Down Expand Up @@ -308,14 +305,11 @@ public void CreateVariableDeclaration(IVariableDeclaration fieldDeclaration, IxN
break;
case IScalarTypeDeclaration scalar:
AddPropertyDeclaration(fieldDeclaration, visitor);
if (scalar.IsNullablePrimitive())
{
AddToSource($" = default({scalar.TransformType()});\n");
}
AddToSource(scalar.CreateScalarInitializer(this.Project?.CompilerOptions?.TargetPlatfromMoniker));
break;
case IReferenceTypeDeclaration d:
case IStructuredTypeDeclaration s:
AddPropertyDeclaration(fieldDeclaration, visitor);
AddPropertyDeclaration(fieldDeclaration, visitor);
AddToSource(" = new ");
fieldDeclaration.Type.Accept(visitor, this);
AddToSource("();");
Expand Down
4 changes: 4 additions & 0 deletions src/AXSharp.compiler/src/ixc/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@ internal class Options : ICompilerOptions
[Option('d', "skip-deps", Required = false, Default = false,
HelpText = "Instructs the compiler to skip dependencies compilation of referenced AX# project.")]
public bool SkipDependencyCompilation { get; set; }

[Option('t', "target-platform-moniker", Required = false, Default = "ax",
HelpText = "Instructs the compiler to adjust for target platform differences. Possible values 'ax', 'tia'")]
public string TargetPlatfromMoniker { get; set; }
}

4 changes: 4 additions & 0 deletions src/AXSharp.compiler/src/ixd/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ internal class Options : ICompilerOptions

public bool IgnoreS7Pragmas { get; set; }
public bool SkipDependencyCompilation { get; set; }

[Option('t', "target-platform-moniker", Required = false, Default = "ax",
HelpText = "Instructs the compiler to adjust for target platform differences. Possible values 'ax', 'tia'")]
public string TargetPlatfromMoniker { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/AXSharp.compiler/src/ixr/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ internal class Options : ICompilerOptions

public bool IgnoreS7Pragmas { get; set; }
public bool SkipDependencyCompilation { get; set; }

[Option('t', "target-platform-moniker", Required = false, Default = "ax",
HelpText = "Instructs the compiler to adjust for target platform differences. Possible values 'ax', 'tia'")]
public string TargetPlatfromMoniker { get; set; }
}
}
Loading

0 comments on commit e93cb05

Please sign in to comment.