Skip to content

Commit d38737a

Browse files
authored
Make sync API consistent: always (before, after) (#1303)
* Switch API order to (before, after) everywhere * Update names: previous => before, current => after
1 parent 4aad0b8 commit d38737a

File tree

12 files changed

+97
-97
lines changed

12 files changed

+97
-97
lines changed

backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update WritingSystem",
224224
"Revert WritingSystem",
225225
async () =>
226226
{
227-
await WritingSystemSync.Sync(after, before, this);
227+
await WritingSystemSync.Sync(before, after, this);
228228
});
229229
return await GetWritingSystem(after.WsId, after.Type) ?? throw new NullReferenceException($"unable to find {after.Type} writing system with id {after.WsId}");
230230
}
@@ -856,7 +856,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update Entry",
856856
"Revert entry",
857857
async () =>
858858
{
859-
await EntrySync.Sync(after, before, this);
859+
await EntrySync.Sync(before, after, this);
860860
});
861861
return await GetEntry(after.Id) ?? throw new NullReferenceException("unable to find entry with id " + after.Id);
862862
}
@@ -990,7 +990,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update Sense",
990990
"Revert Sense",
991991
async () =>
992992
{
993-
await SenseSync.Sync(entryId, after, before, this);
993+
await SenseSync.Sync(entryId, before, after, this);
994994
});
995995
return await GetSense(entryId, after.Id) ?? throw new NullReferenceException("unable to find sense with id " + after.Id);
996996
}
@@ -1106,7 +1106,7 @@ await Cache.DoUsingNewOrCurrentUOW("Update Example Sentence",
11061106
"Revert Example Sentence",
11071107
async () =>
11081108
{
1109-
await ExampleSentenceSync.Sync(entryId, senseId, after, before, this);
1109+
await ExampleSentenceSync.Sync(entryId, senseId, before, after, this);
11101110
});
11111111
return await GetExampleSentence(entryId, senseId, after.Id) ?? throw new NullReferenceException("unable to find example sentence with id " + after.Id);
11121112
}

backend/FwLite/FwLiteProjectSync.Tests/EntrySyncTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ public async Task CanSyncRandomEntries()
3333
var createdEntry = await _fixture.CrdtApi.CreateEntry(await AutoFaker.EntryReadyForCreation(_fixture.CrdtApi));
3434
var after = await AutoFaker.EntryReadyForCreation(_fixture.CrdtApi, entryId: createdEntry.Id);
3535

36-
after.Senses = AutoFaker.Faker.Random.Shuffle([
36+
after.Senses = [.. AutoFaker.Faker.Random.Shuffle([
3737
// copy some senses over, so moves happen
3838
..AutoFaker.Faker.Random.ListItems(createdEntry.Senses),
39-
..(after.Senses)
40-
]).ToList();
39+
..after.Senses
40+
])];
4141

42-
await EntrySync.Sync(after, createdEntry, _fixture.CrdtApi);
42+
await EntrySync.Sync(createdEntry, after, _fixture.CrdtApi);
4343
var actual = await _fixture.CrdtApi.GetEntry(after.Id);
4444
actual.Should().NotBeNull();
4545
actual.Should().BeEquivalentTo(after, options => options
@@ -71,7 +71,7 @@ public async Task CanChangeComplexFormVisSync_Components()
7171
after.Components[0].ComponentEntryId = component2.Id;
7272
after.Components[0].ComponentHeadword = component2.Headword();
7373

74-
await EntrySync.Sync(after, complexForm, _fixture.CrdtApi);
74+
await EntrySync.Sync(complexForm, after, _fixture.CrdtApi);
7575

7676
var actual = await _fixture.CrdtApi.GetEntry(after.Id);
7777
actual.Should().NotBeNull();
@@ -103,7 +103,7 @@ public async Task CanChangeComplexFormViaSync_ComplexForms()
103103
after.ComplexForms[0].ComplexFormEntryId = complexForm2.Id;
104104
after.ComplexForms[0].ComplexFormHeadword = complexForm2.Headword();
105105

106-
await EntrySync.Sync(after, component, _fixture.CrdtApi);
106+
await EntrySync.Sync(component, after, _fixture.CrdtApi);
107107

108108
var actual = await _fixture.CrdtApi.GetEntry(after.Id);
109109
actual.Should().NotBeNull();
@@ -117,7 +117,7 @@ public async Task CanChangeComplexFormTypeViaSync()
117117
var entry = await _fixture.CrdtApi.CreateEntry(new() { LexemeForm = { { "en", "complexForm1" } } });
118118
var after = (Entry) entry.Copy();
119119
after.ComplexFormTypes = [complexFormType];
120-
await EntrySync.Sync(after, entry, _fixture.CrdtApi);
120+
await EntrySync.Sync(entry, after, _fixture.CrdtApi);
121121

122122
var actual = await _fixture.CrdtApi.GetEntry(after.Id);
123123
actual.Should().NotBeNull();

backend/FwLite/FwLiteProjectSync.Tests/UpdateDiffTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ public void EntryDiffShouldUpdateAllFields()
2727
var entryDiffToUpdate = EntrySync.EntryDiffToUpdate(before, after);
2828
ArgumentNullException.ThrowIfNull(entryDiffToUpdate);
2929
entryDiffToUpdate.Apply(before);
30-
before.Should().BeEquivalentTo(after, options => options.Excluding(x => x.Id)
30+
before.Should().BeEquivalentTo(after, options => options.Excluding(x => x.Id)
3131
.Excluding(x => x.DeletedAt).Excluding(x => x.Senses)
3232
.Excluding(x => x.Components)
3333
.Excluding(x => x.ComplexForms)
3434
.Excluding(x => x.ComplexFormTypes));
3535
}
3636

3737
[Fact]
38-
public async Task SenseDiffShouldUpdateAllFields()
38+
public void SenseDiffShouldUpdateAllFields()
3939
{
4040
var before = new Sense();
4141
var after = AutoFaker.Generate<Sense>();
42-
var senseDiffToUpdate = await SenseSync.SenseDiffToUpdate(before, after);
42+
var senseDiffToUpdate = SenseSync.SenseDiffToUpdate(before, after);
4343
ArgumentNullException.ThrowIfNull(senseDiffToUpdate);
4444
senseDiffToUpdate.Apply(before);
4545
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));

backend/FwLite/FwLiteProjectSync/CrdtFwdataProjectSyncService.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,26 @@ private async Task<SyncResult> Sync(IMiniLcmApi crdtApi, IMiniLcmApi fwdataApi,
6565
}
6666

6767
var currentFwDataWritingSystems = await fwdataApi.GetWritingSystems();
68-
var crdtChanges = await WritingSystemSync.Sync(currentFwDataWritingSystems, projectSnapshot.WritingSystems, crdtApi);
69-
var fwdataChanges = await WritingSystemSync.Sync(await crdtApi.GetWritingSystems(), currentFwDataWritingSystems, fwdataApi);
68+
var crdtChanges = await WritingSystemSync.Sync(projectSnapshot.WritingSystems, currentFwDataWritingSystems, crdtApi);
69+
var fwdataChanges = await WritingSystemSync.Sync(currentFwDataWritingSystems, await crdtApi.GetWritingSystems(), fwdataApi);
7070

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

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

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

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

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

9090
//todo push crdt changes to lexbox

backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public async Task<WritingSystem> UpdateWritingSystem(WritingSystemId id, Writing
6969

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

@@ -448,7 +448,7 @@ public async Task<Entry> UpdateEntry(Guid id,
448448

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

@@ -509,7 +509,7 @@ public async Task<Sense> UpdateSense(Guid entryId,
509509

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

@@ -566,7 +566,7 @@ public async Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
566566
ExampleSentence before,
567567
ExampleSentence after)
568568
{
569-
await ExampleSentenceSync.Sync(entryId, senseId, after, before, this);
569+
await ExampleSentenceSync.Sync(entryId, senseId, before, after, this);
570570
return await GetExampleSentence(entryId, senseId, after.Id) ?? throw new NullReferenceException();
571571
}
572572

backend/FwLite/MiniLcm/SyncHelpers/ComplexFormTypeSync.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace MiniLcm.SyncHelpers;
55

66
public static class ComplexFormTypeSync
77
{
8-
public static async Task<int> Sync(ComplexFormType[] afterComplexFormTypes,
9-
ComplexFormType[] beforeComplexFormTypes,
8+
public static async Task<int> Sync(ComplexFormType[] beforeComplexFormTypes,
9+
ComplexFormType[] afterComplexFormTypes,
1010
IMiniLcmApi api)
1111
{
1212
return await DiffCollection.Diff(

backend/FwLite/MiniLcm/SyncHelpers/EntrySync.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ namespace MiniLcm.SyncHelpers;
66

77
public static class EntrySync
88
{
9-
public static async Task<int> Sync(Entry[] afterEntries,
10-
Entry[] beforeEntries,
9+
public static async Task<int> Sync(Entry[] beforeEntries,
10+
Entry[] afterEntries,
1111
IMiniLcmApi api)
1212
{
1313
return await DiffCollection.DiffAddThenUpdate(beforeEntries, afterEntries, new EntriesDiffApi(api));
1414
}
1515

16-
public static async Task<int> Sync(Entry afterEntry, Entry beforeEntry, IMiniLcmApi api)
16+
public static async Task<int> Sync(Entry beforeEntry, Entry afterEntry, IMiniLcmApi api)
1717
{
1818
try
1919
{
2020
var updateObjectInput = EntryDiffToUpdate(beforeEntry, afterEntry);
2121
if (updateObjectInput is not null) await api.UpdateEntry(afterEntry.Id, updateObjectInput);
22-
var changes = await SensesSync(afterEntry.Id, afterEntry.Senses, beforeEntry.Senses, api);
22+
var changes = await SensesSync(afterEntry.Id, beforeEntry.Senses, afterEntry.Senses, api);
2323

24-
changes += await Sync(afterEntry.Components, beforeEntry.Components, api);
25-
changes += await Sync(afterEntry.ComplexForms, beforeEntry.ComplexForms, api);
26-
changes += await Sync(afterEntry.Id, afterEntry.ComplexFormTypes, beforeEntry.ComplexFormTypes, api);
24+
changes += await Sync(beforeEntry.Components, afterEntry.Components, api);
25+
changes += await Sync(beforeEntry.ComplexForms, afterEntry.ComplexForms, api);
26+
changes += await Sync(afterEntry.Id, beforeEntry.ComplexFormTypes, afterEntry.ComplexFormTypes, api);
2727
return changes + (updateObjectInput is null ? 0 : 1);
2828
}
2929
catch (Exception e)
@@ -33,8 +33,8 @@ public static async Task<int> Sync(Entry afterEntry, Entry beforeEntry, IMiniLcm
3333
}
3434

3535
private static async Task<int> Sync(Guid entryId,
36-
IList<ComplexFormType> afterComplexFormTypes,
3736
IList<ComplexFormType> beforeComplexFormTypes,
37+
IList<ComplexFormType> afterComplexFormTypes,
3838
IMiniLcmApi api)
3939
{
4040
return await DiffCollection.Diff(
@@ -43,7 +43,7 @@ private static async Task<int> Sync(Guid entryId,
4343
new ComplexFormTypesDiffApi(api, entryId));
4444
}
4545

46-
private static async Task<int> Sync(IList<ComplexFormComponent> afterComponents, IList<ComplexFormComponent> beforeComponents, IMiniLcmApi api)
46+
private static async Task<int> Sync(IList<ComplexFormComponent> beforeComponents, IList<ComplexFormComponent> afterComponents, IMiniLcmApi api)
4747
{
4848
return await DiffCollection.Diff(
4949
beforeComponents,
@@ -53,8 +53,8 @@ private static async Task<int> Sync(IList<ComplexFormComponent> afterComponents,
5353
}
5454

5555
private static async Task<int> SensesSync(Guid entryId,
56-
IList<Sense> afterSenses,
5756
IList<Sense> beforeSenses,
57+
IList<Sense> afterSenses,
5858
IMiniLcmApi api)
5959
{
6060
return await DiffCollection.DiffOrderable(beforeSenses, afterSenses, new SensesDiffApi(api, entryId));
@@ -96,7 +96,7 @@ public override async Task<int> Remove(Entry entry)
9696

9797
public override Task<int> Replace(Entry before, Entry after)
9898
{
99-
return Sync(after, before, api);
99+
return Sync(before, after, api);
100100
}
101101
}
102102

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

185185
public Task<int> Replace(Sense before, Sense after)
186186
{
187-
return SenseSync.Sync(entryId, after, before, api);
187+
return SenseSync.Sync(entryId, before, after, api);
188188
}
189189
}
190190
}

backend/FwLite/MiniLcm/SyncHelpers/ExampleSentenceSync.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public static class ExampleSentenceSync
77
{
88
public static async Task<int> Sync(Guid entryId,
99
Guid senseId,
10-
IList<ExampleSentence> afterExampleSentences,
1110
IList<ExampleSentence> beforeExampleSentences,
11+
IList<ExampleSentence> afterExampleSentences,
1212
IMiniLcmApi api)
1313
{
1414
return await DiffCollection.Diff(
@@ -19,8 +19,8 @@ public static async Task<int> Sync(Guid entryId,
1919

2020
public static async Task<int> Sync(Guid entryId,
2121
Guid senseId,
22-
ExampleSentence afterExampleSentence,
2322
ExampleSentence beforeExampleSentence,
23+
ExampleSentence afterExampleSentence,
2424
IMiniLcmApi api)
2525
{
2626
var updateObjectInput = DiffToUpdate(beforeExampleSentence, afterExampleSentence);
@@ -66,7 +66,7 @@ public override async Task<int> Remove(ExampleSentence beforeExampleSentence)
6666

6767
public override Task<int> Replace(ExampleSentence beforeExampleSentence, ExampleSentence afterExampleSentence)
6868
{
69-
return Sync(entryId, senseId, afterExampleSentence, beforeExampleSentence, api);
69+
return Sync(entryId, senseId, beforeExampleSentence, afterExampleSentence, api);
7070
}
7171
}
7272
}

backend/FwLite/MiniLcm/SyncHelpers/PartOfSpeechSync.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ namespace MiniLcm.SyncHelpers;
55

66
public static class PartOfSpeechSync
77
{
8-
public static async Task<int> Sync(PartOfSpeech[] currentPartsOfSpeech,
9-
PartOfSpeech[] previousPartsOfSpeech,
8+
public static async Task<int> Sync(PartOfSpeech[] beforePartsOfSpeech,
9+
PartOfSpeech[] afterPartsOfSpeech,
1010
IMiniLcmApi api)
1111
{
1212
return await DiffCollection.Diff(
13-
previousPartsOfSpeech,
14-
currentPartsOfSpeech,
13+
beforePartsOfSpeech,
14+
afterPartsOfSpeech,
1515
new PartsOfSpeechDiffApi(api));
1616
}
1717

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

27-
public static UpdateObjectInput<PartOfSpeech>? PartOfSpeechDiffToUpdate(PartOfSpeech previousPartOfSpeech, PartOfSpeech currentPartOfSpeech)
27+
public static UpdateObjectInput<PartOfSpeech>? PartOfSpeechDiffToUpdate(PartOfSpeech beforePartOfSpeech, PartOfSpeech afterPartOfSpeech)
2828
{
2929
JsonPatchDocument<PartOfSpeech> patchDocument = new();
3030
patchDocument.Operations.AddRange(MultiStringDiff.GetMultiStringDiff<PartOfSpeech>(nameof(PartOfSpeech.Name),
31-
previousPartOfSpeech.Name,
32-
currentPartOfSpeech.Name));
31+
beforePartOfSpeech.Name,
32+
afterPartOfSpeech.Name));
3333
// TODO: Once we add abbreviations to MiniLcm's PartOfSpeech objects, then:
3434
// patchDocument.Operations.AddRange(GetMultiStringDiff<PartOfSpeech>(nameof(PartOfSpeech.Abbreviation),
35-
// previousPartOfSpeech.Abbreviation,
36-
// currentPartOfSpeech.Abbreviation));
35+
// beforePartOfSpeech.Abbreviation,
36+
// afterPartOfSpeech.Abbreviation));
3737
if (patchDocument.Operations.Count == 0) return null;
3838
return new UpdateObjectInput<PartOfSpeech>(patchDocument);
3939
}
@@ -46,15 +46,15 @@ public override async Task<int> Add(PartOfSpeech currentPos)
4646
return 1;
4747
}
4848

49-
public override async Task<int> Remove(PartOfSpeech previousPos)
49+
public override async Task<int> Remove(PartOfSpeech beforePos)
5050
{
51-
await api.DeletePartOfSpeech(previousPos.Id);
51+
await api.DeletePartOfSpeech(beforePos.Id);
5252
return 1;
5353
}
5454

55-
public override Task<int> Replace(PartOfSpeech previousPos, PartOfSpeech currentPos)
55+
public override Task<int> Replace(PartOfSpeech beforePos, PartOfSpeech afterPos)
5656
{
57-
return Sync(previousPos, currentPos, api);
57+
return Sync(beforePos, afterPos, api);
5858
}
5959
}
6060
}

0 commit comments

Comments
 (0)