Created
November 5, 2019 15:46
-
-
Save TehWardy/24c22e3131e6cf422b6d5f4be95f08c1 to your computer and use it in GitHub Desktop.
Eval Issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.DataAnnotations; | |
namespace Test_App | |
{ | |
public class GenericLineItem | |
{ | |
public string Type { get; set; } | |
public object SourceDataItem { get; set; } | |
public string ConversionMessage { get; set; } | |
public string Currency { get; set; } | |
public string TransactionNumber { get; set; } | |
public int LineNumber { get; set; } | |
[StringLength(200)] | |
public string Description { get; set; } | |
public decimal LinePrice { get; set; } | |
[StringLength(50)] | |
public string ProductCode { get; set; } | |
public decimal UnitPrice { get; set; } | |
public decimal Quantity { get; set; } | |
public decimal TaxRate { get; set; } | |
public decimal TaxFee { get; set; } | |
public decimal TaxableAmount { get; set; } | |
public decimal TaxAmount { get; set; } | |
public string TaxReference { get; set; } | |
public string SourceSystem { get; set; } | |
public string[] CompanyReferences { get; set; } | |
public string PaymentReference { get; set; } | |
public Dictionary<string, string> TransactionReferences { get; set; } | |
public Dictionary<string, DateTimeOffset> Dates { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System.Collections.Generic; | |
namespace Test_App | |
{ | |
public class MappingCollection | |
{ | |
public IDictionary<string, Dictionary<string, string>> Data { get; set; } | |
public string Get(string mappingName, string key) | |
{ | |
return Data[mappingName][key]; | |
} | |
public static MappingCollection FromDynamicObject(object source) | |
{ | |
return JsonConvert.DeserializeObject<MappingCollection>($"{{ \"Data\": {source.ToString()} }}"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using Z.Expressions; | |
namespace Test_App | |
{ | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
// parse the source data and process it normally | |
dynamic sourceData = JsonConvert.DeserializeObject<dynamic>(data); | |
var normalResult = FromObject(sourceData); | |
// ok now lets construct an expression parser and do that agian | |
var parser = new EvalContext(); | |
parser.RegisterAssembly(Assembly.GetExecutingAssembly()); | |
// the mapping collection here isn't need in this case, but it's part of the standard codebase | |
var func = parser.Compile<Func<dynamic, MappingCollection, object>>(code, "source", "mappings"); | |
var dynamicResult = func(sourceData, null); | |
Console.WriteLine("It worked!"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
static string code = @" | |
try | |
{ | |
return new GenericLineItem() | |
{ | |
ConversionMessage = ""success"", | |
SourceDataItem = source, | |
Currency = ((dynamic)source).Currency.Value, | |
LineNumber = int.Parse(((dynamic)source).TransactionLineNum.Value), | |
Description = ((dynamic)source).Comment.Value, | |
LinePrice = (decimal)((dynamic)source).TotalIncludingTax.Value, | |
UnitPrice = (decimal)((dynamic)source).TotalIncludingTax.Value, | |
Quantity = 1, | |
TaxFee = (decimal)((dynamic)source).TotalTax.Value, | |
TaxableAmount = (decimal)((dynamic)source).TotalBeforeTax.Value, | |
TaxAmount = (decimal)((dynamic)source).TotalTax.Value, | |
SourceSystem = ((dynamic)source).TransactionReferenceSystem.Value, | |
Type = ""Invoice|Credit"" + ((((dynamic)source).PaymentRef.Value != string.Empty) ? ""|Remittance"" : string.Empty), | |
TransactionReferences = new Dictionary<string, string>() | |
{ | |
{ ""InvoiceReference"", ""FCS|"" + ((dynamic)source).TransactionReference.Value }, | |
{ ""CreditReference"", ""FCS|"" + ((dynamic)source).TransactionReference.Value }, | |
{ ""RemittanceAdviceReference"", ""FCS|"" + ((dynamic)source).PaymentRef.Value } | |
}, | |
CompanyReferences = new string[4] { | |
(((dynamic)source).FunderReference.Value != string.Empty) ? ""Funder|FCS|"" + ((dynamic)source).FunderReference.Value : string.Empty, | |
(((dynamic)source).SupplierGlobalTaxReference.Value != string.Empty) ? ""Creditor|GlobalTax|"" + ((dynamic)source).SupplierGlobalTaxReference.Value : string.Empty, | |
""Creditor|FCS|"" + ((dynamic)source).SupplierReference.Value, ""Debtor|FCS|"" + ((dynamic)source).BuyerReference.Value | |
} | |
.Where(i => i != string.Empty) | |
.ToArray(), | |
Dates = new Dictionary<string, DateTimeOffset>() | |
{ | |
{""DueDate"", new DateTimeOffset(((dynamic)source).DueDate.Value) }, | |
{""TaxPoint"", new DateTimeOffset(((dynamic)source).TaxPoint.Value) }, | |
{""PaymentDate"", ((dynamic)source).ContainsKey(""PaymentDate"") ? DateTimeOffset.UtcNow.AddDays(60) : new DateTimeOffset(((dynamic)source).PaymentDate.Value) } | |
} | |
}; | |
} | |
catch (Exception ex) | |
{ | |
return new GenericLineItem() | |
{ | |
SourceDataItem = source, | |
ConversionMessage = ex.Message | |
}; | |
} | |
"; | |
static string data = @" | |
{ | |
""SourceFileName"": ""TT_CLX1_CI_C_20191014_013115.xml"", | |
""BuyerReferenceSystem"": ""FCS"", | |
""BuyerReference"": ""CLX1"", | |
""SupplierReferenceSystem"": ""FCS"", | |
""SupplierReference"": ""CLXS0002"", | |
""FunderReferenceSystem"": ""FCS"", | |
""FunderReference"": ""CLXF0001"", | |
""GlobalTaxReferenceSystem"": ""GlobalTax"", | |
""SupplierGlobalTaxReference"": ""GB56782222"", | |
""TransactionReferenceSystem"": ""FCS"", | |
""TransactionReference"": ""5100044691CLX12019"", | |
""SupplierTransactionReference"": ""9200030442"", | |
""Currency"": ""EUR"", | |
""TransactionLineNum"": ""001"", | |
""TotalBeforeTax"": 13232.8, | |
""TotalTax"": 2112.8, | |
""TotalIncludingTax"": 15345.6, | |
""TaxPoint"": ""2019-07-26T00:00:00Z"", | |
""DueDate"": ""2019-10-24T00:00:00Z"", | |
""DOC_TYPE"": ""RE"", | |
""PROFIT_CENTER"": ""0000001097"", | |
""Comment"": ""BAU-FIS-INEU-RD-HagenbachBAU-FIS-INEU-RD-HagenbachBAU-FIS-INEU-RD-HagenbachBAU-FIS-INEU-RD-Hagenbach"", | |
""PaymentRef"": ""2900001111CLX12019"", | |
""PaymentDate"": ""2019-10-11T00:00:00Z"" | |
} | |
"; | |
static GenericLineItem FromObject(object source) | |
{ | |
try | |
{ | |
return new GenericLineItem() | |
{ | |
ConversionMessage = "success", | |
SourceDataItem = source, | |
Currency = ((dynamic)source).Currency.Value, | |
LineNumber = int.Parse(((dynamic)source).TransactionLineNum.Value), | |
Description = ((dynamic)source).Comment.Value, | |
LinePrice = (decimal)((dynamic)source).TotalIncludingTax.Value, | |
UnitPrice = (decimal)((dynamic)source).TotalIncludingTax.Value, | |
Quantity = 1, | |
TaxFee = (decimal)((dynamic)source).TotalTax.Value, | |
TaxableAmount = (decimal)((dynamic)source).TotalBeforeTax.Value, | |
TaxAmount = (decimal)((dynamic)source).TotalTax.Value, | |
SourceSystem = ((dynamic)source).TransactionReferenceSystem.Value, | |
Type = "Invoice|Credit" + ((((dynamic)source).PaymentRef.Value != string.Empty) ? "|Remittance" : string.Empty), | |
TransactionReferences = new Dictionary<string, string>() | |
{ | |
{ "InvoiceReference", "FCS|" + ((dynamic)source).TransactionReference.Value }, | |
{ "CreditReference", "FCS|" + ((dynamic)source).TransactionReference.Value }, | |
{ "RemittanceAdviceReference", "FCS|" + ((dynamic)source).PaymentRef.Value } | |
}, | |
CompanyReferences = new string[4] { | |
(((dynamic)source).FunderReference.Value != string.Empty) ? "Funder|FCS|" + ((dynamic)source).FunderReference.Value : string.Empty, | |
(((dynamic)source).SupplierGlobalTaxReference.Value != string.Empty) ? "Creditor|GlobalTax|" + ((dynamic)source).SupplierGlobalTaxReference.Value : string.Empty, | |
"Creditor|FCS|" + ((dynamic)source).SupplierReference.Value, "Debtor|FCS|" + ((dynamic)source).BuyerReference.Value | |
} | |
.Where(i => i != string.Empty) | |
.ToArray(), | |
Dates = new Dictionary<string, DateTimeOffset>() | |
{ | |
{"DueDate", new DateTimeOffset(((dynamic)source).DueDate.Value) }, | |
{"TaxPoint", new DateTimeOffset(((dynamic)source).TaxPoint.Value) }, | |
{"PaymentDate", ((dynamic)source).ContainsKey("PaymentDate") ? DateTimeOffset.UtcNow.AddDays(60) : new DateTimeOffset(((dynamic)source).PaymentDate.Value) } | |
} | |
}; | |
} | |
catch (Exception ex) | |
{ | |
return new GenericLineItem() | |
{ | |
SourceDataItem = source, | |
ConversionMessage = ex.Message | |
}; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment