Skip to content

Commit

Permalink
Make sync API consistent: always (before, after) (#1303)
Browse files Browse the repository at this point in the history
* Switch API order to (before, after) everywhere

* Update names: previous => before, current => after
  • Loading branch information
rmunn authored Dec 19, 2024
1 parent 4aad0b8 commit d38737a
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 97 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 @@ -224,7 +224,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 @@ -856,7 +856,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 @@ -990,7 +990,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 @@ -1106,7 +1106,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
14 changes: 7 additions & 7 deletions backend/FwLite/FwLiteProjectSync.Tests/EntrySyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ 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);

after.Senses = AutoFaker.Faker.Random.Shuffle([
after.Senses = [.. AutoFaker.Faker.Random.Shuffle([
// copy some senses over, so moves happen
..AutoFaker.Faker.Random.ListItems(createdEntry.Senses),
..(after.Senses)
]).ToList();
..after.Senses
])];

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 @@ -71,7 +71,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 @@ -103,7 +103,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 @@ -117,7 +117,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
6 changes: 3 additions & 3 deletions backend/FwLite/FwLiteProjectSync.Tests/UpdateDiffTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ public void EntryDiffShouldUpdateAllFields()
var entryDiffToUpdate = EntrySync.EntryDiffToUpdate(before, after);
ArgumentNullException.ThrowIfNull(entryDiffToUpdate);
entryDiffToUpdate.Apply(before);
before.Should().BeEquivalentTo(after, options => options.Excluding(x => x.Id)
before.Should().BeEquivalentTo(after, options => options.Excluding(x => x.Id)
.Excluding(x => x.DeletedAt).Excluding(x => x.Senses)
.Excluding(x => x.Components)
.Excluding(x => x.ComplexForms)
.Excluding(x => x.ComplexFormTypes));
}

[Fact]
public async Task SenseDiffShouldUpdateAllFields()
public void SenseDiffShouldUpdateAllFields()
{
var before = new Sense();
var after = AutoFaker.Generate<Sense>();
var senseDiffToUpdate = await SenseSync.SenseDiffToUpdate(before, after);
var senseDiffToUpdate = SenseSync.SenseDiffToUpdate(before, after);
ArgumentNullException.ThrowIfNull(senseDiffToUpdate);
senseDiffToUpdate.Apply(before);
before.Should().BeEquivalentTo(after, options => options.Excluding(x => x.Id).Excluding(x => x.EntryId).Excluding(x => x.DeletedAt).Excluding(x => x.ExampleSentences).Excluding(x => x.SemanticDomains));
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 @@ -448,7 +448,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 @@ -509,7 +509,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 @@ -566,7 +566,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(
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,24 +6,24 @@ 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)
{
return await DiffCollection.DiffAddThenUpdate(beforeEntries, afterEntries, new EntriesDiffApi(api));
}

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 @@ -33,8 +33,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(
Expand All @@ -43,7 +43,7 @@ private static async Task<int> Sync(Guid entryId,
new ComplexFormTypesDiffApi(api, entryId));
}

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(
beforeComponents,
Expand All @@ -53,8 +53,8 @@ private static async Task<int> Sync(IList<ComplexFormComponent> afterComponents,
}

private static async Task<int> SensesSync(Guid entryId,
IList<Sense> afterSenses,
IList<Sense> beforeSenses,
IList<Sense> afterSenses,
IMiniLcmApi api)
{
return await DiffCollection.DiffOrderable(beforeSenses, afterSenses, new SensesDiffApi(api, entryId));
Expand Down Expand Up @@ -96,7 +96,7 @@ public override async Task<int> Remove(Entry entry)

public override Task<int> Replace(Entry before, Entry after)
{
return Sync(after, before, api);
return Sync(before, after, api);
}
}

Expand Down Expand Up @@ -184,7 +184,7 @@ public async Task<int> Remove(Sense sense)

public Task<int> Replace(Sense before, Sense after)
{
return SenseSync.Sync(entryId, after, before, api);
return SenseSync.Sync(entryId, before, after, api);
}
}
}
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)
{
return await DiffCollection.Diff(
Expand All @@ -19,8 +19,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 Expand Up @@ -66,7 +66,7 @@ public override async Task<int> Remove(ExampleSentence beforeExampleSentence)

public override Task<int> Replace(ExampleSentence beforeExampleSentence, ExampleSentence afterExampleSentence)
{
return Sync(entryId, senseId, afterExampleSentence, beforeExampleSentence, api);
return Sync(entryId, senseId, beforeExampleSentence, afterExampleSentence, api);
}
}
}
26 changes: 13 additions & 13 deletions backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace MiniLcm.SyncHelpers;

public static class PartOfSpeechSync
{
public static async Task<int> Sync(PartOfSpeech[] currentPartsOfSpeech,
PartOfSpeech[] previousPartsOfSpeech,
public static async Task<int> Sync(PartOfSpeech[] beforePartsOfSpeech,
PartOfSpeech[] afterPartsOfSpeech,
IMiniLcmApi api)
{
return await DiffCollection.Diff(
previousPartsOfSpeech,
currentPartsOfSpeech,
beforePartsOfSpeech,
afterPartsOfSpeech,
new PartsOfSpeechDiffApi(api));
}

Expand All @@ -24,16 +24,16 @@ public static async Task<int> Sync(PartOfSpeech before,
return updateObjectInput is null ? 0 : 1;
}

public static UpdateObjectInput<PartOfSpeech>? PartOfSpeechDiffToUpdate(PartOfSpeech previousPartOfSpeech, PartOfSpeech currentPartOfSpeech)
public static UpdateObjectInput<PartOfSpeech>? PartOfSpeechDiffToUpdate(PartOfSpeech beforePartOfSpeech, PartOfSpeech afterPartOfSpeech)
{
JsonPatchDocument<PartOfSpeech> patchDocument = new();
patchDocument.Operations.AddRange(MultiStringDiff.GetMultiStringDiff<PartOfSpeech>(nameof(PartOfSpeech.Name),
previousPartOfSpeech.Name,
currentPartOfSpeech.Name));
beforePartOfSpeech.Name,
afterPartOfSpeech.Name));
// TODO: Once we add abbreviations to MiniLcm's PartOfSpeech objects, then:
// patchDocument.Operations.AddRange(GetMultiStringDiff<PartOfSpeech>(nameof(PartOfSpeech.Abbreviation),
// previousPartOfSpeech.Abbreviation,
// currentPartOfSpeech.Abbreviation));
// beforePartOfSpeech.Abbreviation,
// afterPartOfSpeech.Abbreviation));
if (patchDocument.Operations.Count == 0) return null;
return new UpdateObjectInput<PartOfSpeech>(patchDocument);
}
Expand All @@ -46,15 +46,15 @@ public override async Task<int> Add(PartOfSpeech currentPos)
return 1;
}

public override async Task<int> Remove(PartOfSpeech previousPos)
public override async Task<int> Remove(PartOfSpeech beforePos)
{
await api.DeletePartOfSpeech(previousPos.Id);
await api.DeletePartOfSpeech(beforePos.Id);
return 1;
}

public override Task<int> Replace(PartOfSpeech previousPos, PartOfSpeech currentPos)
public override Task<int> Replace(PartOfSpeech beforePos, PartOfSpeech afterPos)
{
return Sync(previousPos, currentPos, api);
return Sync(beforePos, afterPos, api);
}
}
}
Loading

0 comments on commit d38737a

Please sign in to comment.