Skip to content

Commit

Permalink
ALVS update complete
Browse files Browse the repository at this point in the history
  • Loading branch information
craigedmunds committed Feb 11, 2025
1 parent 6f2f0f3 commit a53b103
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 49 deletions.
7 changes: 5 additions & 2 deletions Btms.Backend.Cli/Features/GenerateModels/CSharpFileBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Btms.Backend.Cli.Features.GenerateModels.ClassMaps;
using Btms.Backend.Cli.Features.GenerateModels.DescriptorModel;
using Btms.Common.Extensions;
using RazorLight;

namespace Btms.Backend.Cli.Features.GenerateModels;
Expand Down Expand Up @@ -102,16 +103,18 @@ private static void ApplySourceClassMapOverrides(ClassDescriptor @class)
{
if (propertyMap.NoAttributes)
{
propertyDescriptor.SourceAttributes.Clear();
propertyDescriptor.NoAttributes = true;
propertyDescriptor.InternalAttributes.Clear();
}
else
{
propertyDescriptor.SourceAttributes.AddRange(propertyMap.SourceAttributes);
propertyDescriptor.SourceAttributes = propertyMap.SourceAttributes;
propertyDescriptor.InternalAttributes.AddRange(propertyMap.InternalAttributes);
}
}

propertyDescriptor.SourceJsonPropertyName = propertyMap.SourceJsonPropertyName;
propertyDescriptor.InternalJsonPropertyName = propertyMap.InternalJsonPropertyName;
propertyDescriptor.ExcludedFromSource = propertyMap.ExcludedFromSource;
propertyDescriptor.ExcludedFromInternal = propertyMap.ExcludedFromInternal;

Expand Down
68 changes: 45 additions & 23 deletions Btms.Backend.Cli/Features/GenerateModels/ClassMaps/Bootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,31 @@ public static void RegisterAlvsClassMaps()
{
map.MapProperty("ArrivalDateTime")
.IsDateTime(DatetimeType.Epoch)
.SetInternalName("ArrivesAt");

map.MapProperty("MasterUCR").SetName("MasterUcr");
map.MapProperty("SubmitterTURN").SetName("SubmitterTurn");
map.MapProperty("DeclarationUCR").SetName("DeclarationUcr");
.SetInternalName("arrivesAt");

map.MapProperty("MasterUCR")
.SetName("masterUcr")
.SetSourceJsonPropertyName("masterUCR");

map.MapProperty("SubmitterTURN")
.SetName("submitterTurn")
.SetSourceJsonPropertyName("submitterTURN");

map.MapProperty("DeclarationUCR")
.SetName("declarationUcr")
.SetSourceJsonPropertyName("declarationUCR");

});

GeneratorClassMap.RegisterClassMap("Items", map =>
{
map.MapProperty("Documents");
map.MapProperty("Checks");
map.MapProperty("Document")
.SetSourceJsonPropertyName("documents")
.SetInternalJsonPropertyName("documents");

map.MapProperty("Check")
.SetSourceJsonPropertyName("checks")
.SetInternalJsonPropertyName("checks");
});

GeneratorClassMap.RegisterClassMap("Check", map =>
Expand All @@ -48,13 +62,21 @@ public static void RegisterAlvsClassMaps()
IpaffsDescriptorBuilder.ClassNamePrefix));


map.MapProperty("MasterUCR").SetName("MasterUcr");
map.MapProperty("SubmitterTURN").SetName("SubmitterTurn");
map.MapProperty("DeclarationUCR").SetName("DeclarationUcr");
map.MapProperty("MasterUCR")
.SetName("masterUcr")
.SetSourceJsonPropertyName("masterUCR");

map.MapProperty("SubmitterTURN")
.SetName("submitterTurn")
.SetSourceJsonPropertyName("submitterTURN");

map.MapProperty("DeclarationUCR")
.SetName("declarationUcr")
.SetSourceJsonPropertyName("declarationUCR");
});

GeneratorClassMap.RegisterClassMap("ServiceHeader",
map => { map.MapProperty("ServiceCallTimestamp").IsDateTime(DatetimeType.Epoch).SetInternalName("ServiceCalled"); });
map => { map.MapProperty("ServiceCallTimestamp").IsDateTime(DatetimeType.Epoch).SetInternalName("serviceCalled"); });

GeneratorClassMap.RegisterClassMap("AlvsClearanceRequest",
map => { map.SetClassName("AlvsClearanceRequest", "CdsClearanceRequest"); });
Expand All @@ -63,14 +85,14 @@ public static void RegisterAlvsClassMaps()
{
map.SetClassName("AlvsClearanceRequestPost", "CdsClearanceRequestPost");
map.MapProperty("AlvsClearanceRequest").SetType("AlvsClearanceRequest", "CdsClearanceRequest");
map.MapProperty("sendingDate").SetInternalName("SentOn").IsDateTime();
map.MapProperty("sendingDate").SetInternalName("sentOn").IsDateTime();
});

GeneratorClassMap.RegisterClassMap("AlvsClearanceRequestPostResult", map =>
{
map.SetClassName("AlvsClearanceRequestPostResult", "CdsClearanceRequestPostResult")
.NoInternalClass();
map.MapProperty("sendingDate").SetInternalName("SentOn").IsDateTime();
map.MapProperty("sendingDate").SetInternalName("sentOn").IsDateTime();
});
}

Expand Down Expand Up @@ -187,16 +209,16 @@ public static void RegisterIpaffsClassMaps()
GeneratorClassMap.RegisterClassMap("Decision",
map =>
{
map.MapProperty("decision").SetName("DecisionEnum");
map.MapProperty("decision").SetName("decisionEnum");
map.MapProperty("notAcceptableActionByDate").IsDate();
});

GeneratorClassMap.RegisterClassMap("ImportNotification", map =>
{
map.MapProperty("Id").SetName("IpaffsId");
map.MapProperty("Type").SetName("ImportNotificationType");
map.MapProperty("LastUpdated").SetName("LastUpdated", "UpdatedSource").IsDateTime();
map.MapProperty("RiskDecisionLockingTime").SetName("RiskDecisionLockedOn").IsDateTime();
map.MapProperty("Id").SetName("ipaffsId");
map.MapProperty("Type").SetName("importNotificationType");
map.MapProperty("LastUpdated").SetName("lastUpdated", "UpdatedSource").IsDateTime();
map.MapProperty("RiskDecisionLockingTime").SetName("riskDecisionLockedOn").IsDateTime();
});

GeneratorClassMap.RegisterClassMap("Purpose", map =>
Expand All @@ -206,8 +228,8 @@ public static void RegisterIpaffsClassMaps()


map.MapProperty("exitDate").IsDate();
map.MapProperty("FinalBIP").SetName("FinalBip");
map.MapProperty("ExitBIP").SetName("ExitBip");
map.MapProperty("FinalBIP").SetName("finalBip");
map.MapProperty("ExitBIP").SetName("exitBip");
});

GeneratorClassMap.RegisterClassMap("VeterinaryInformation",
Expand Down Expand Up @@ -243,7 +265,7 @@ public static void RegisterIpaffsClassMaps()
map => { map.MapProperty("dateOfIssue").IsDateTime().SetInternalName("issuedOn"); });

GeneratorClassMap.RegisterClassMap("JourneyRiskCategorisationResult",
map => { map.MapProperty("riskLevelDateTime").SetName("RiskLevelSetFor").IsDateTime(); });
map => { map.MapProperty("riskLevelDateTime").SetName("riskLevelSetFor").IsDateTime(); });


GeneratorClassMap.RegisterClassMap("RiskAssessmentResult",
Expand Down Expand Up @@ -295,9 +317,9 @@ public static void RegisterIpaffsClassMaps()
map.MapProperty("importerLocalReferenceNumber").IsSensitive();
map.MapProperty("commodities").ExcludeFromInternal();
map.MapProperty("originalEstimatedDateTime").SetName("originalEstimatedOn").IsDateTime();
map.MapProperty("submissionDate").SetName("SubmittedOn").IsDateTime();
map.MapProperty("submissionDate").SetName("submittedOn").IsDateTime();
map.MapProperty("isGVMSRoute").SetName("isGvmsRoute");
map.MapProperty("portOfExitDate").IsDateTime().SetInternalName("ExitedPortOfOn");
map.MapProperty("portOfExitDate").IsDateTime().SetInternalName("exitedPortOfOn");

map.MapDateOnlyAndTimeOnlyToDateTimeProperty("arrivalDate", "arrivalTime", "arrivesAt");
map.MapDateOnlyAndTimeOnlyToDateTimeProperty("departureDate", "departureTime", "departedOn");
Expand Down
65 changes: 58 additions & 7 deletions Btms.Backend.Cli/Features/GenerateModels/ClassMaps/PropertyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ internal class PropertyMap(string name)
{
public string Name { get; set; } = name;

public string? SourceJsonPropertyName { get; set; }

public string? InternalJsonPropertyName { get; set; }

public string Type { get; set; } = null!;

public string InternalType { get; set; } = null!;
Expand All @@ -28,9 +32,9 @@ internal class PropertyMap(string name)

public bool TypeOverwritten { get; set; }

public List<string> SourceAttributes { get; set; } = new();
public List<string> SourceAttributes { get; set; } = [];

public List<string> InternalAttributes { get; set; } = new();
public List<string> InternalAttributes { get; set; } = [];

public bool AttributesOverwritten { get; set; }

Expand Down Expand Up @@ -59,14 +63,14 @@ public class MapperMap

public PropertyMap SetInternalType(string type)
{
InternalType = type ?? throw new ArgumentNullException("type");
InternalType = type ?? throw new ArgumentNullException(nameof(type));
InternalTypeOverwritten = true;
return this;
}

public PropertyMap SetType(string type, string? internalType = null)
{
Type = type ?? throw new ArgumentNullException("type");
Type = type ?? throw new ArgumentNullException(nameof(type));
InternalType = internalType ?? type;
InternalTypeOverwritten = internalType.HasValue();
TypeOverwritten = true;
Expand Down Expand Up @@ -97,25 +101,72 @@ public PropertyMap IsTime()

public PropertyMap SetName(string name, string? internalName = null)
{
if (!name.StartsWithLowercase())
{
throw new InvalidOperationException(
"Name must start with lowercase letter");
}

if (!internalName.StartsWithLowercase())
{
throw new InvalidOperationException(
"Internal name must start with lowercase letter");
}

SetSourceName(name);
SetInternalName(internalName ?? name);
return this;
}

public PropertyMap SetSourceName(string name)
{
OverriddenSourceName = name ?? throw new ArgumentNullException("name");
if (!name.StartsWithLowercase())
{
throw new InvalidOperationException(
"Name must start with lowercase letter");
}

OverriddenSourceName = name ?? throw new ArgumentNullException(nameof(name));
SourceNameOverwritten = true;
return this;
}

public PropertyMap SetInternalName(string name)
{
OverriddenInternalName = name ?? throw new ArgumentNullException("name");
if (!name.StartsWithLowercase())
{
throw new InvalidOperationException(
"Name must start with lowercase letter");
}
OverriddenInternalName = name ?? throw new ArgumentNullException(nameof(name));
InternalNameOverwritten = true;
return this;
}

public PropertyMap SetSourceJsonPropertyName(string name)
{
if (!name.StartsWithLowercase())
{
throw new InvalidOperationException(
"Name must start with lowercase letter");
}

SourceJsonPropertyName = name;
return this;
}

public PropertyMap SetInternalJsonPropertyName(string name)
{
if (!name.StartsWithLowercase())
{
throw new InvalidOperationException(
"Name must start with lowercase letter");
}

InternalJsonPropertyName = name;
return this;
}

public PropertyMap IsSensitive()
{
AddAttribute("[Btms.SensitiveData.SensitiveData]", Model.Source);
Expand Down Expand Up @@ -150,7 +201,7 @@ public PropertyMap AddAttribute(string attribute, Model model)
{
if (string.IsNullOrEmpty(attribute))
{
throw new ArgumentNullException("attribute");
throw new ArgumentNullException(nameof(attribute));
}

switch (model)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using Btms.Common.Extensions;
using Humanizer;

namespace Btms.Backend.Cli.Features.GenerateModels.DescriptorModel;
Expand Down Expand Up @@ -34,17 +35,24 @@ public PropertyDescriptor(string sourceName, string internalName, string type, s
Description = description?.Replace("\n", " ");
IsReferenceType = isReferenceType;
IsArray = isArray;
SourceAttributes = [$"[JsonPropertyName(\"{sourceName}\")]"];
InternalAttributes = ["[Attr]", $"[System.ComponentModel.Description(\"{Description}\")]", $"[JsonPropertyName(\"{sourceName}\")]"];
// SourceAttributes = []; // [$"[JsonPropertyName(\"{sourceName}\")]"];
// InternalAttributes = ["[Attr]", $"[System.ComponentModel.Description(\"{Description}\")]", $"[JsonPropertyName(\"{sourceName}\")]"];

if (type.EndsWith("Enum"))
{
InternalAttributes.Add(
"[MongoDB.Bson.Serialization.Attributes.BsonRepresentation(MongoDB.Bson.BsonType.String)]");
}
}

public bool NoAttributes { get; set; } = default;

public string SourceName { get; set; }

public string? SourceJsonPropertyName { get; set; }

public string? InternalJsonPropertyName { get; set; }

public string InternalName { get; set; }

public string Type { get; set; }
Expand All @@ -53,9 +61,9 @@ public PropertyDescriptor(string sourceName, string internalName, string type, s

public string? Description { get; set; }

public List<string> SourceAttributes { get; set; }
public List<string> SourceAttributes { get; set; } = [];

public List<string> InternalAttributes { get; set; }
public List<string> InternalAttributes { get; set; } = [];

public bool IsReferenceType { get; set; }

Expand Down Expand Up @@ -132,6 +140,32 @@ public string GetInternalPropertyName()
return n;
}

public string[] GetSourceAttributes()
{
if (NoAttributes) return [];

var defaultParams = new List<string>() { $"[JsonPropertyName(\"{SourceJsonPropertyName ?? SourceName}\")]" };
defaultParams.AddRange(SourceAttributes);

return defaultParams.ToArray();
}

public string[] GetInternalAttributes()
{
if (NoAttributes) return [];

var defaultParams = new List<string>() { "[Attr]", $"[JsonPropertyName(\"{InternalJsonPropertyName ?? InternalName}\")]" };

if (!string.IsNullOrEmpty(Description))
{
defaultParams.Add($"[System.ComponentModel.Description(\"{Description}\")]");
}

defaultParams.AddRange(InternalAttributes);

return defaultParams.ToArray();
}

public string GetInternalPropertyType()
{
return GetPropertyType(InternalType ?? Type);
Expand Down
Loading

0 comments on commit a53b103

Please sign in to comment.