Skip to content

Commit

Permalink
Switch API order to (before, after) everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
rmunn committed Dec 4, 2024
1 parent db8aff0 commit 98c43ed
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 54 deletions.
8 changes: 4 additions & 4 deletions backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update WritingSystem",
"Revert WritingSystem",
async () =>
{
await WritingSystemSync.Sync(after, before, this);
await WritingSystemSync.Sync(before, after, this);
});
return await GetWritingSystem(after.WsId, after.Type) ?? throw new NullReferenceException($"unable to find {after.Type} writing system with id {after.WsId}");
}
Expand Down Expand Up @@ -857,7 +857,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update Entry",
"Revert entry",
async () =>
{
await EntrySync.Sync(after, before, this);
await EntrySync.Sync(before, after, this);
});
return await GetEntry(after.Id) ?? throw new NullReferenceException("unable to find entry with id " + after.Id);
}
Expand Down Expand Up @@ -950,7 +950,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update Sense",
"Revert Sense",
async () =>
{
await SenseSync.Sync(entryId, after, before, this);
await SenseSync.Sync(entryId, before, after, this);
});
return await GetSense(entryId, after.Id) ?? throw new NullReferenceException("unable to find sense with id " + after.Id);
}
Expand Down Expand Up @@ -1048,7 +1048,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update Example Sentence",
"Revert Example Sentence",
async () =>
{
await ExampleSentenceSync.Sync(entryId, senseId, after, before, this);
await ExampleSentenceSync.Sync(entryId, senseId, before, after, this);
});
return await GetExampleSentence(entryId, senseId, after.Id) ?? throw new NullReferenceException("unable to find example sentence with id " + after.Id);
}
Expand Down
8 changes: 4 additions & 4 deletions backend/FwLite/FwLiteProjectSync.Tests/EntrySyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task CanSyncRandomEntries()
{
var createdEntry = await _fixture.CrdtApi.CreateEntry(await AutoFaker.EntryReadyForCreation(_fixture.CrdtApi));
var after = await AutoFaker.EntryReadyForCreation(_fixture.CrdtApi, entryId: createdEntry.Id);
await EntrySync.Sync(after, createdEntry, _fixture.CrdtApi);
await EntrySync.Sync(createdEntry, after, _fixture.CrdtApi);
var actual = await _fixture.CrdtApi.GetEntry(after.Id);
actual.Should().NotBeNull();
actual.Should().BeEquivalentTo(after, options => options);
Expand Down Expand Up @@ -53,7 +53,7 @@ public async Task CanChangeComplexFormVisSync_Components()
after.Components[0].ComponentEntryId = component2.Id;
after.Components[0].ComponentHeadword = component2.Headword();

await EntrySync.Sync(after, complexForm, _fixture.CrdtApi);
await EntrySync.Sync(complexForm, after, _fixture.CrdtApi);

var actual = await _fixture.CrdtApi.GetEntry(after.Id);
actual.Should().NotBeNull();
Expand Down Expand Up @@ -85,7 +85,7 @@ public async Task CanChangeComplexFormViaSync_ComplexForms()
after.ComplexForms[0].ComplexFormEntryId = complexForm2.Id;
after.ComplexForms[0].ComplexFormHeadword = complexForm2.Headword();

await EntrySync.Sync(after, component, _fixture.CrdtApi);
await EntrySync.Sync(component, after, _fixture.CrdtApi);

var actual = await _fixture.CrdtApi.GetEntry(after.Id);
actual.Should().NotBeNull();
Expand All @@ -99,7 +99,7 @@ public async Task CanChangeComplexFormTypeViaSync()
var entry = await _fixture.CrdtApi.CreateEntry(new() { LexemeForm = { { "en", "complexForm1" } } });
var after = (Entry) entry.Copy();
after.ComplexFormTypes = [complexFormType];
await EntrySync.Sync(after, entry, _fixture.CrdtApi);
await EntrySync.Sync(entry, after, _fixture.CrdtApi);

var actual = await _fixture.CrdtApi.GetEntry(after.Id);
actual.Should().NotBeNull();
Expand Down
20 changes: 10 additions & 10 deletions backend/FwLite/FwLiteProjectSync/CrdtFwdataProjectSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,26 @@ private async Task<SyncResult> Sync(IMiniLcmApi crdtApi, IMiniLcmApi fwdataApi,
}

var currentFwDataWritingSystems = await fwdataApi.GetWritingSystems();
var crdtChanges = await WritingSystemSync.Sync(currentFwDataWritingSystems, projectSnapshot.WritingSystems, crdtApi);
var fwdataChanges = await WritingSystemSync.Sync(await crdtApi.GetWritingSystems(), currentFwDataWritingSystems, fwdataApi);
var crdtChanges = await WritingSystemSync.Sync(projectSnapshot.WritingSystems, currentFwDataWritingSystems, crdtApi);
var fwdataChanges = await WritingSystemSync.Sync(currentFwDataWritingSystems, await crdtApi.GetWritingSystems(), fwdataApi);

var currentFwDataPartsOfSpeech = await fwdataApi.GetPartsOfSpeech().ToArrayAsync();
crdtChanges += await PartOfSpeechSync.Sync(currentFwDataPartsOfSpeech, projectSnapshot.PartsOfSpeech, crdtApi);
fwdataChanges += await PartOfSpeechSync.Sync(await crdtApi.GetPartsOfSpeech().ToArrayAsync(), currentFwDataPartsOfSpeech, fwdataApi);
crdtChanges += await PartOfSpeechSync.Sync(projectSnapshot.PartsOfSpeech, currentFwDataPartsOfSpeech, crdtApi);
fwdataChanges += await PartOfSpeechSync.Sync(currentFwDataPartsOfSpeech, await crdtApi.GetPartsOfSpeech().ToArrayAsync(), fwdataApi);

var currentFwDataSemanticDomains = await fwdataApi.GetSemanticDomains().ToArrayAsync();
crdtChanges += await SemanticDomainSync.Sync(currentFwDataSemanticDomains, projectSnapshot.SemanticDomains, crdtApi);
fwdataChanges += await SemanticDomainSync.Sync(await crdtApi.GetSemanticDomains().ToArrayAsync(), currentFwDataSemanticDomains, fwdataApi);
crdtChanges += await SemanticDomainSync.Sync(projectSnapshot.SemanticDomains, currentFwDataSemanticDomains, crdtApi);
fwdataChanges += await SemanticDomainSync.Sync(currentFwDataSemanticDomains, await crdtApi.GetSemanticDomains().ToArrayAsync(), fwdataApi);

var currentFwDataComplexFormTypes = await fwdataApi.GetComplexFormTypes().ToArrayAsync();
crdtChanges += await ComplexFormTypeSync.Sync(currentFwDataComplexFormTypes, projectSnapshot.ComplexFormTypes, crdtApi);
fwdataChanges += await ComplexFormTypeSync.Sync(await crdtApi.GetComplexFormTypes().ToArrayAsync(), currentFwDataComplexFormTypes, fwdataApi);
crdtChanges += await ComplexFormTypeSync.Sync(projectSnapshot.ComplexFormTypes, currentFwDataComplexFormTypes, crdtApi);
fwdataChanges += await ComplexFormTypeSync.Sync(currentFwDataComplexFormTypes, await crdtApi.GetComplexFormTypes().ToArrayAsync(), fwdataApi);

var currentFwDataEntries = await fwdataApi.GetAllEntries().ToArrayAsync();
crdtChanges += await EntrySync.Sync(currentFwDataEntries, projectSnapshot.Entries, crdtApi);
crdtChanges += await EntrySync.Sync(projectSnapshot.Entries, currentFwDataEntries, crdtApi);
LogDryRun(crdtApi, "crdt");

fwdataChanges += await EntrySync.Sync(await crdtApi.GetAllEntries().ToArrayAsync(), currentFwDataEntries, fwdataApi);
fwdataChanges += await EntrySync.Sync(currentFwDataEntries, await crdtApi.GetAllEntries().ToArrayAsync(), fwdataApi);
LogDryRun(fwdataApi, "fwdata");

//todo push crdt changes to lexbox
Expand Down
8 changes: 4 additions & 4 deletions backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task<WritingSystem> UpdateWritingSystem(WritingSystemId id, Writing

public async Task<WritingSystem> UpdateWritingSystem(WritingSystem before, WritingSystem after)
{
await WritingSystemSync.Sync(after, before, this);
await WritingSystemSync.Sync(before, after, this);
return await GetWritingSystem(after.WsId, after.Type) ?? throw new NullReferenceException("unable to find writing system with id " + after.WsId);
}

Expand Down Expand Up @@ -435,7 +435,7 @@ public async Task<Entry> UpdateEntry(Guid id,

public async Task<Entry> UpdateEntry(Entry before, Entry after)
{
await EntrySync.Sync(after, before, this);
await EntrySync.Sync(before, after, this);
return await GetEntry(after.Id) ?? throw new NullReferenceException("unable to find entry with id " + after.Id);
}

Expand Down Expand Up @@ -492,7 +492,7 @@ public async Task<Sense> UpdateSense(Guid entryId,

public async Task<Sense> UpdateSense(Guid entryId, Sense before, Sense after)
{
await SenseSync.Sync(entryId, after, before, this);
await SenseSync.Sync(entryId, before, after, this);
return await GetSense(entryId, after.Id) ?? throw new NullReferenceException("unable to find sense with id " + after.Id);
}

Expand Down Expand Up @@ -543,7 +543,7 @@ public async Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
ExampleSentence before,
ExampleSentence after)
{
await ExampleSentenceSync.Sync(entryId, senseId, after, before, this);
await ExampleSentenceSync.Sync(entryId, senseId, before, after, this);
return await GetExampleSentence(entryId, senseId, after.Id) ?? throw new NullReferenceException();
}

Expand Down
4 changes: 2 additions & 2 deletions backend/FwLite/MiniLcm/SyncHelpers/ComplexFormTypeSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace MiniLcm.SyncHelpers;

public static class ComplexFormTypeSync
{
public static async Task<int> Sync(ComplexFormType[] afterComplexFormTypes,
ComplexFormType[] beforeComplexFormTypes,
public static async Task<int> Sync(ComplexFormType[] beforeComplexFormTypes,
ComplexFormType[] afterComplexFormTypes,
IMiniLcmApi api)
{
return await DiffCollection.Diff(api,
Expand Down
24 changes: 12 additions & 12 deletions backend/FwLite/MiniLcm/SyncHelpers/EntrySync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace MiniLcm.SyncHelpers;

public static class EntrySync
{
public static async Task<int> Sync(Entry[] afterEntries,
Entry[] beforeEntries,
public static async Task<int> Sync(Entry[] beforeEntries,
Entry[] afterEntries,
IMiniLcmApi api)
{
Func<IMiniLcmApi, Entry, Task<Entry>> add = static async (api, afterEntry) =>
Expand All @@ -23,21 +23,21 @@ public static async Task<int> Sync(Entry[] afterEntries,
await api.DeleteEntry(beforeEntry.Id);
return 1;
};
Func<IMiniLcmApi, Entry, Entry, Task<int>> replace = static async (api, beforeEntry, afterEntry) => await Sync(afterEntry, beforeEntry, api);
Func<IMiniLcmApi, Entry, Entry, Task<int>> replace = static async (api, beforeEntry, afterEntry) => await Sync(beforeEntry, afterEntry, api);
return await DiffCollection.DiffAddThenUpdate(api, beforeEntries, afterEntries, entry => entry.Id, add, remove, replace);
}

public static async Task<int> Sync(Entry afterEntry, Entry beforeEntry, IMiniLcmApi api)
public static async Task<int> Sync(Entry beforeEntry, Entry afterEntry, IMiniLcmApi api)
{
try
{
var updateObjectInput = EntryDiffToUpdate(beforeEntry, afterEntry);
if (updateObjectInput is not null) await api.UpdateEntry(afterEntry.Id, updateObjectInput);
var changes = await SensesSync(afterEntry.Id, afterEntry.Senses, beforeEntry.Senses, api);
var changes = await SensesSync(afterEntry.Id, beforeEntry.Senses, afterEntry.Senses, api);

changes += await Sync(afterEntry.Components, beforeEntry.Components, api);
changes += await Sync(afterEntry.ComplexForms, beforeEntry.ComplexForms, api);
changes += await Sync(afterEntry.Id, afterEntry.ComplexFormTypes, beforeEntry.ComplexFormTypes, api);
changes += await Sync(beforeEntry.Components, afterEntry.Components, api);
changes += await Sync(beforeEntry.ComplexForms, afterEntry.ComplexForms, api);
changes += await Sync(afterEntry.Id, beforeEntry.ComplexFormTypes, afterEntry.ComplexFormTypes, api);
return changes + (updateObjectInput is null ? 0 : 1);
}
catch (Exception e)
Expand All @@ -47,8 +47,8 @@ public static async Task<int> Sync(Entry afterEntry, Entry beforeEntry, IMiniLcm
}

private static async Task<int> Sync(Guid entryId,
IList<ComplexFormType> afterComplexFormTypes,
IList<ComplexFormType> beforeComplexFormTypes,
IList<ComplexFormType> afterComplexFormTypes,
IMiniLcmApi api)
{
return await DiffCollection.Diff(api,
Expand All @@ -69,7 +69,7 @@ private static async Task<int> Sync(Guid entryId,
static (api, beforeComplexFormType, afterComplexFormType) => Task.FromResult(0));
}

private static async Task<int> Sync(IList<ComplexFormComponent> afterComponents, IList<ComplexFormComponent> beforeComponents, IMiniLcmApi api)
private static async Task<int> Sync(IList<ComplexFormComponent> beforeComponents, IList<ComplexFormComponent> afterComponents, IMiniLcmApi api)
{
return await DiffCollection.Diff(api,
beforeComponents,
Expand Down Expand Up @@ -110,8 +110,8 @@ static async (api, beforeComponent) =>
}

private static async Task<int> SensesSync(Guid entryId,
IList<Sense> afterSenses,
IList<Sense> beforeSenses,
IList<Sense> afterSenses,
IMiniLcmApi api)
{
Func<IMiniLcmApi, Sense, Task<int>> add = async (api, afterSense) =>
Expand All @@ -124,7 +124,7 @@ private static async Task<int> SensesSync(Guid entryId,
await api.DeleteSense(entryId, beforeSense.Id);
return 1;
};
Func<IMiniLcmApi, Sense, Sense, Task<int>> replace = async (api, beforeSense, afterSense) => await SenseSync.Sync(entryId, afterSense, beforeSense, api);
Func<IMiniLcmApi, Sense, Sense, Task<int>> replace = async (api, beforeSense, afterSense) => await SenseSync.Sync(entryId, beforeSense, afterSense, api);
return await DiffCollection.Diff(api, beforeSenses, afterSenses, add, remove, replace);
}

Expand Down
6 changes: 3 additions & 3 deletions backend/FwLite/MiniLcm/SyncHelpers/ExampleSentenceSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public static class ExampleSentenceSync
{
public static async Task<int> Sync(Guid entryId,
Guid senseId,
IList<ExampleSentence> afterExampleSentences,
IList<ExampleSentence> beforeExampleSentences,
IList<ExampleSentence> afterExampleSentences,
IMiniLcmApi api)
{
Func<IMiniLcmApi, ExampleSentence, Task<int>> add = async (api, afterExampleSentence) =>
Expand All @@ -23,7 +23,7 @@ public static async Task<int> Sync(Guid entryId,
};
Func<IMiniLcmApi, ExampleSentence, ExampleSentence, Task<int>> replace =
(api, beforeExampleSentence, afterExampleSentence) =>
Sync(entryId, senseId, afterExampleSentence, beforeExampleSentence, api);
Sync(entryId, senseId, beforeExampleSentence, afterExampleSentence, api);
return await DiffCollection.Diff(api,
beforeExampleSentences,
afterExampleSentences,
Expand All @@ -34,8 +34,8 @@ public static async Task<int> Sync(Guid entryId,

public static async Task<int> Sync(Guid entryId,
Guid senseId,
ExampleSentence afterExampleSentence,
ExampleSentence beforeExampleSentence,
ExampleSentence afterExampleSentence,
IMiniLcmApi api)
{
var updateObjectInput = DiffToUpdate(beforeExampleSentence, afterExampleSentence);
Expand Down
4 changes: 2 additions & 2 deletions backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace MiniLcm.SyncHelpers;

public static class PartOfSpeechSync
{
public static async Task<int> Sync(PartOfSpeech[] currentPartsOfSpeech,
PartOfSpeech[] previousPartsOfSpeech,
public static async Task<int> Sync(PartOfSpeech[] previousPartsOfSpeech,
PartOfSpeech[] currentPartsOfSpeech,
IMiniLcmApi api)
{
return await DiffCollection.Diff(api,
Expand Down
4 changes: 2 additions & 2 deletions backend/FwLite/MiniLcm/SyncHelpers/SemanticDomainSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace MiniLcm.SyncHelpers;

public static class SemanticDomainSync
{
public static async Task<int> Sync(SemanticDomain[] currentSemanticDomains,
SemanticDomain[] previousSemanticDomains,
public static async Task<int> Sync(SemanticDomain[] previousSemanticDomains,
SemanticDomain[] currentSemanticDomains,
IMiniLcmApi api)
{
return await DiffCollection.Diff(api,
Expand Down
5 changes: 2 additions & 3 deletions backend/FwLite/MiniLcm/SyncHelpers/SenseSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ namespace MiniLcm.SyncHelpers;
public static class SenseSync
{
public static async Task<int> Sync(Guid entryId,
Sense afterSense,
Sense beforeSense,
Sense afterSense,
IMiniLcmApi api)
{
var updateObjectInput = await SenseDiffToUpdate(beforeSense, afterSense);
if (updateObjectInput is not null) await api.UpdateSense(entryId, beforeSense.Id, updateObjectInput);
var changes = await ExampleSentenceSync.Sync(entryId,
beforeSense.Id,
afterSense.ExampleSentences,
beforeSense.ExampleSentences,
api);
afterSense.ExampleSentences, api);
changes += await DiffCollection.Diff(api,
beforeSense.SemanticDomains,
afterSense.SemanticDomains,
Expand Down
16 changes: 8 additions & 8 deletions backend/FwLite/MiniLcm/SyncHelpers/WritingSystemSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ namespace MiniLcm.SyncHelpers;

public static class WritingSystemSync
{
public static async Task<int> Sync(WritingSystems currentWritingSystems,
WritingSystems previousWritingSystems,
public static async Task<int> Sync(WritingSystems previousWritingSystems,
WritingSystems currentWritingSystems,
IMiniLcmApi api)
{
return await Sync(currentWritingSystems.Vernacular, previousWritingSystems.Vernacular, api) +
await Sync(currentWritingSystems.Analysis, previousWritingSystems.Analysis, api);
return await Sync(previousWritingSystems.Vernacular, currentWritingSystems.Vernacular, api) +
await Sync(previousWritingSystems.Analysis, currentWritingSystems.Analysis, api);
}
public static async Task<int> Sync(WritingSystem[] currentWritingSystems,
WritingSystem[] previousWritingSystems,
public static async Task<int> Sync(WritingSystem[] previousWritingSystems,
WritingSystem[] currentWritingSystems,
IMiniLcmApi api)
{
return await DiffCollection.Diff(api,
Expand All @@ -33,11 +33,11 @@ public static async Task<int> Sync(WritingSystem[] currentWritingSystems,
},
async (api, previousWs, currentWs) =>
{
return await Sync(currentWs, previousWs, api);
return await Sync(previousWs, currentWs, api);
});
}

public static async Task<int> Sync(WritingSystem afterWs, WritingSystem beforeWs, IMiniLcmApi api)
public static async Task<int> Sync(WritingSystem beforeWs, WritingSystem afterWs, IMiniLcmApi api)
{
var updateObjectInput = WritingSystemDiffToUpdate(beforeWs, afterWs);
if (updateObjectInput is not null) await api.UpdateWritingSystem(afterWs.WsId, afterWs.Type, updateObjectInput);
Expand Down

0 comments on commit 98c43ed

Please sign in to comment.