Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DI-1132] Add Delete By Id Functionality #47

Merged
merged 17 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DataImport.Common.Tests/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
{
"PythonExecutableLocation": "..\\..\\..\\..\\.tools\\python\\python.exe"
}
2 changes: 1 addition & 1 deletion DataImport.Common/DataImport.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void WriteToCollection(DataReceivedEventArgs eventArgs, Collection<string> colle
{
using var process = Process.Start(startInfo);

var processName = process.ProcessName;
var processName = process.HasExited ? "Exited python process" : process.ProcessName;
_logger.LogInformation("External preprocess {ProcessName} started", processName);

process.OutputDataReceived += (_, e) => { WriteToCollection(e, outputLines); };
Expand Down
2 changes: 1 addition & 1 deletion DataImport.Models.Tests/DataMapExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void SetUp()
};

var dataMapSerializer = new DataMapSerializer(_dataMap);
_dataMap.Map = dataMapSerializer.Serialize(mappings);
_dataMap.Map = dataMapSerializer.Serialize(mappings, false);
}

[Test]
Expand Down
141 changes: 100 additions & 41 deletions DataImport.Models.Tests/DataMapSerializerTests.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion DataImport.Models/DataMapExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static string[] ReferencedColumns(this DataMapper[] mappings)
private static DataMapper[] Mappings(DataMap dataMap)
{
var dataMapSerializer = new DataMapSerializer(dataMap);
return dataMapSerializer.Deserialize(dataMap.Map);
return dataMapSerializer.Deserialize(dataMap.Map, false);
}

private static IEnumerable<string> Yield(this IEnumerable<DataMapper> mappings, Func<DataMapper, string> propertyAccessor)
Expand Down
49 changes: 42 additions & 7 deletions DataImport.Models/DataMapSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public DataMapSerializer(string resourcePath, ResourceMetadata[] resourceMetadat
_resourceMetadatas = resourceMetadatas;
}

public string Serialize(DataMapper[] mappings)
public string Serialize(DataMapper[] mappings, bool isDeleteByIdOperation)
{
return SerializeObject(_resourceMetadatas, mappings).ToString(Formatting.Indented);
return isDeleteByIdOperation
? SerializeObjectForDeleteById(mappings.Single()).ToString(Formatting.Indented)
: SerializeObject(_resourceMetadatas, mappings).ToString(Formatting.Indented);
}

public DataMapper[] Deserialize(string jsonMap)
public DataMapper[] Deserialize(string jsonMap, bool isDeleteByIdOperation)
{
JObject jobject;
try
Expand All @@ -60,12 +62,14 @@ public DataMapper[] Deserialize(string jsonMap)
, exception);
}

return Deserialize(jobject);
return Deserialize(jobject, isDeleteByIdOperation);
}

public DataMapper[] Deserialize(JObject jsonMap)
public DataMapper[] Deserialize(JObject jsonMap, bool isDeleteByIdOperation)
{
return DeserializeObject(_resourceMetadatas, jsonMap).ToArray();
return isDeleteByIdOperation
? DeserializeObjectForDeleteById(jsonMap).ToArray()
: DeserializeObject(_resourceMetadatas, jsonMap).ToArray();
}

private JObject SerializeObject(IReadOnlyList<ResourceMetadata> nodeMetadatas, IReadOnlyList<DataMapper> nodes)
Expand Down Expand Up @@ -125,6 +129,12 @@ private JObject SerializeObject(IReadOnlyList<ResourceMetadata> nodeMetadatas, I
return result;
}

private JObject SerializeObjectForDeleteById(DataMapper node)
{
var result = new JObject { new JProperty("Id", new JObject { new JProperty("Column", node.SourceColumn) }) };
return result;
}

private JArray SerializeArray(ResourceMetadata arrayItemMetadata, IReadOnlyList<DataMapper> nodes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simpat-adam
Can we have separate DataMapSerializer class for just for Delete operations ? This will make sure to have single response across the classes. If we add delete by natural key we can plugin one more class or over loads on this class !

{
var result = new JArray();
Expand Down Expand Up @@ -168,7 +178,7 @@ private List<DataMapper> DeserializeObject(IReadOnlyList<ResourceMetadata> nodeM

foreach (var node in nodes)
{
if (nodeMetadatas.All(x => x.Name != node.Name))
if (node.Name != "Id" && nodeMetadatas.All(x => x.Name != node.Name))
throw new InvalidOperationException(
$"Cannot deserialize mappings from JSON, because the key '{node.Name}' should not exist " +
$"according to the metadata for resource '{_resourcePath}'.");
Expand Down Expand Up @@ -213,6 +223,31 @@ private List<DataMapper> DeserializeObject(IReadOnlyList<ResourceMetadata> nodeM
return result;
}

private List<DataMapper> DeserializeObjectForDeleteById(JToken objectToken)
{
var jobject = objectToken as JObject;

if (jobject == null)
throw new InvalidOperationException(
"Cannot deserialize mappings from JSON, because an object literal was expected. " +
"Instead, found: " +
$"{objectToken.ToString(Formatting.Indented)}");

var result = new List<DataMapper>();

var nodes = jobject.Children().Cast<JProperty>().ToArray();

var node = nodes.Single(n => n.Name == "Id");

var propertyValue = node.Children().Single();

var sourceColumn = ((JObject) propertyValue).Children().Cast<JProperty>().Single().Value;

result.Add(new DataMapper() { Name = "Id", SourceColumn = DeserializeRawValue(sourceColumn) });

return result;
}

private List<DataMapper> DeserializeArray(ResourceMetadata arrayItemMetadata, JToken arrayToken)
{
var array = arrayToken as JArray;
Expand Down
2 changes: 2 additions & 0 deletions DataImport.Models/Datamap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ public DataMap()
public Script FileProcessorScript { get; set; }

public ICollection<DataMapAgent> DataMapAgents { get; set; }

public bool IsDeleteOperation { get; set; }
}
}
4 changes: 2 additions & 2 deletions DataImport.Models/MetadataValidationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ private static bool IsObjectCompatibleWithResource(this JObject jsonMap, Resourc
try
{
var serializer = new DataMapSerializer(resource);
var deserialized = serializer.Deserialize(jsonMap);
var deserialized = serializer.Deserialize(jsonMap, false);

if (level == MetadataCompatibilityLevel.Bootstrap && deserialized.ReferencedColumns().Any())
throw new Exception("Bootstrap JSON cannot include column references.");

serializer.Serialize(deserialized);
serializer.Serialize(deserialized, false);
exceptionMessage = null;
return true;
}
Expand Down
Loading
Loading