-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
Note that FwDataMiniLcmApi hasn't implemented the UpdateWritingSystem overload with (id, type, update), which is the (before, after) method is going to end up calling. So that still needs to happen.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using SystemTextJsonPatch.Operations; | ||
|
||
namespace MiniLcm.SyncHelpers; | ||
|
||
public static class SimpleStringDiff | ||
{ | ||
public static IEnumerable<Operation<T>> GetStringDiff<T>(string path, | ||
string before, | ||
string after) where T : class | ||
{ | ||
if (before == after) yield break; | ||
if (after is null) yield return new Operation<T>("remove", $"/{path}", null); | ||
else if (before is null) yield return new Operation<T>("add", $"/{path}", null); | ||
else yield return new Operation<T>("replace", $"/{path}", null, after); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using MiniLcm; | ||
using MiniLcm.Models; | ||
using MiniLcm.SyncHelpers; | ||
using SystemTextJsonPatch; | ||
|
||
public static class WritingSystemSync | ||
{ | ||
public static async Task<int> Sync(WritingSystem[] currentWritingSystems, | ||
WritingSystem[] previousWritingSystems, | ||
IMiniLcmApi api) | ||
{ | ||
return await DiffCollection.Diff(api, | ||
previousWritingSystems, | ||
currentWritingSystems, | ||
ws => ws.Id, | ||
async (api, currentWs) => | ||
{ | ||
await api.CreateWritingSystem(currentWs.Type, currentWs); | ||
return 1; | ||
}, | ||
async (api, previousWs) => | ||
Check warning on line 21 in backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs GitHub Actions / Build FwHeadless / publish-fw-headless
Check warning on line 21 in backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs GitHub Actions / Build FW Lite and run tests
Check warning on line 21 in backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs GitHub Actions / Publish FW Lite app for Linux
Check warning on line 21 in backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs GitHub Actions / Publish FW Lite app for Linux
Check warning on line 21 in backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs GitHub Actions / Publish FW Lite app for Mac
Check warning on line 21 in backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs GitHub Actions / Publish FW Lite app for Mac
Check warning on line 21 in backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs GitHub Actions / Publish FW Lite app for Windows
Check warning on line 21 in backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs GitHub Actions / Publish FW Lite app for Windows
|
||
{ | ||
// await api.DeleteWritingSystem(previousWs.Id); // Deleting writing systems is dangerous as it causes cascading data deletion. Needs careful thought. | ||
// TODO: should we throw an exception? | ||
return 0; | ||
}, | ||
async (api, previousWs, currentWs) => | ||
{ | ||
return await Sync(currentWs, previousWs, api); | ||
}); | ||
} | ||
|
||
public static async Task<int> Sync(WritingSystem afterWs, WritingSystem beforeWs, IMiniLcmApi api) | ||
{ | ||
var updateObjectInput = WritingSystemDiffToUpdate(beforeWs, afterWs); | ||
if (updateObjectInput is not null) await api.UpdateWritingSystem(afterWs.WsId, afterWs.Type, updateObjectInput); | ||
return updateObjectInput is null ? 0 : 1; | ||
} | ||
|
||
public static UpdateObjectInput<WritingSystem>? WritingSystemDiffToUpdate(WritingSystem previousWritingSystem, WritingSystem currentWritingSystem) | ||
{ | ||
JsonPatchDocument<WritingSystem> patchDocument = new(); | ||
patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff<WritingSystem>(nameof(WritingSystem.WsId), | ||
previousWritingSystem.WsId, | ||
currentWritingSystem.WsId)); | ||
patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff<WritingSystem>(nameof(WritingSystem.Name), | ||
previousWritingSystem.Name, | ||
currentWritingSystem.Name)); | ||
patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff<WritingSystem>(nameof(WritingSystem.Abbreviation), | ||
previousWritingSystem.Abbreviation, | ||
currentWritingSystem.Abbreviation)); | ||
patchDocument.Operations.AddRange(SimpleStringDiff.GetStringDiff<WritingSystem>(nameof(WritingSystem.Font), | ||
previousWritingSystem.Font, | ||
currentWritingSystem.Font)); | ||
// TODO: Exemplars, Order, and do we need DeletedAt? | ||
if (patchDocument.Operations.Count == 0) return null; | ||
return new UpdateObjectInput<WritingSystem>(patchDocument); | ||
} | ||
} |