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

Remove LcmCrdt specific MiniLcm models #1114

Merged
merged 9 commits into from
Oct 21, 2024
30 changes: 18 additions & 12 deletions backend/FwLite/FwDataMiniLcmBridge/Api/FwDataMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,23 @@ public Task<WritingSystems> GetWritingSystems()
.Select(ws => ws.Id).ToHashSet();
var writingSystems = new WritingSystems
{
Vernacular = WritingSystemContainer.CurrentVernacularWritingSystems.Select(FromLcmWritingSystem).ToArray(),
Analysis = Cache.ServiceLocator.WritingSystems.CurrentAnalysisWritingSystems.Select(FromLcmWritingSystem).ToArray()
Vernacular = WritingSystemContainer.CurrentVernacularWritingSystems.Select(definition =>
FromLcmWritingSystem(definition, WritingSystemType.Vernacular)).ToArray(),
Analysis = Cache.ServiceLocator.WritingSystems.CurrentAnalysisWritingSystems.Select(definition =>
FromLcmWritingSystem(definition, WritingSystemType.Analysis)).ToArray()
};
CompleteExemplars(writingSystems);
return Task.FromResult(writingSystems);
}

private WritingSystem FromLcmWritingSystem(CoreWritingSystemDefinition ws)
private WritingSystem FromLcmWritingSystem(CoreWritingSystemDefinition ws, WritingSystemType type)
{
return new WritingSystem
{
Id = Guid.Empty,
Type = type,
//todo determine current and create a property for that.
hahn-kev marked this conversation as resolved.
Show resolved Hide resolved
Id = ws.Id,
WsId = ws.Id,
Name = ws.LanguageTag,
Abbreviation = ws.Abbreviation,
Font = ws.DefaultFontName,
Expand All @@ -127,9 +131,9 @@ private WritingSystem FromLcmWritingSystem(CoreWritingSystemDefinition ws)
internal void CompleteExemplars(WritingSystems writingSystems)
{
var wsExemplars = writingSystems.Vernacular.Concat(writingSystems.Analysis)
.DistinctBy(ws => ws.Id)
.DistinctBy(ws => ws.WsId)
.ToDictionary(ws => ws, ws => ws.Exemplars.Select(s => s[0]).ToHashSet());
var wsExemplarsByHandle = wsExemplars.ToFrozenDictionary(kv => GetWritingSystemHandle(kv.Key.Id), kv => kv.Value);
var wsExemplarsByHandle = wsExemplars.ToFrozenDictionary(kv => GetWritingSystemHandle(kv.Key.WsId), kv => kv.Value);

foreach (var entry in EntriesRepository.AllInstances())
{
Expand All @@ -151,7 +155,7 @@ public Task<WritingSystem> CreateWritingSystem(WritingSystemType type, WritingSy
Cache.ServiceLocator.ActionHandler,
() =>
{
Cache.ServiceLocator.WritingSystemManager.GetOrSet(writingSystem.Id.Code, out ws);
Cache.ServiceLocator.WritingSystemManager.GetOrSet(writingSystem.WsId.Code, out ws);
ws.Abbreviation = writingSystem.Abbreviation;
switch (type)
{
Expand All @@ -164,7 +168,7 @@ public Task<WritingSystem> CreateWritingSystem(WritingSystemType type, WritingSy
}
});
if (ws is null) throw new InvalidOperationException("Writing system not found");
return Task.FromResult(FromLcmWritingSystem(ws));
return Task.FromResult(FromLcmWritingSystem(ws, type));
}

public Task<WritingSystem> UpdateWritingSystem(WritingSystemId id, WritingSystemType type, UpdateObjectInput<WritingSystem> update)
Expand Down Expand Up @@ -363,6 +367,7 @@ private Sense FromLexSense(ILexSense sense)
var s = new Sense
{
Id = sense.Guid,
EntryId = sense.Entry.Guid,
Gloss = FromLcmMultiString(sense.Gloss),
Definition = FromLcmMultiString(sense.Definition),
PartOfSpeech = sense.MorphoSyntaxAnalysisRA?.GetPartOfSpeech()?.Name.get_String(enWs).Text ?? "",
Expand All @@ -373,17 +378,18 @@ private Sense FromLexSense(ILexSense sense)
Name = FromLcmMultiString(s.Name),
Code = s.OcmCodes
}).ToList(),
ExampleSentences = sense.ExamplesOS.Select(FromLexExampleSentence).ToList()
ExampleSentences = sense.ExamplesOS.Select(sentence => FromLexExampleSentence(sense.Guid, sentence)).ToList()
};
return s;
}

private ExampleSentence FromLexExampleSentence(ILexExampleSentence sentence)
private ExampleSentence FromLexExampleSentence(Guid senseGuid, ILexExampleSentence sentence)
{
var translation = sentence.TranslationsOC.FirstOrDefault()?.Translation;
return new ExampleSentence
{
Id = sentence.Guid,
SenseId = senseGuid,
Sentence = FromLcmMultiString(sentence.Example),
Reference = sentence.Reference.Text,
Translation = translation is null ? new MultiString() : FromLcmMultiString(translation),
Expand Down Expand Up @@ -686,7 +692,7 @@ public Task<ExampleSentence> CreateExampleSentence(Guid entryId, Guid senseId, E
"Remove example sentence",
Cache.ServiceLocator.ActionHandler,
() => CreateExampleSentence(lexSense, exampleSentence));
return Task.FromResult(FromLexExampleSentence(ExampleSentenceRepository.GetObject(exampleSentence.Id)));
return Task.FromResult(FromLexExampleSentence(senseId, ExampleSentenceRepository.GetObject(exampleSentence.Id)));
}

public Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
Expand All @@ -704,7 +710,7 @@ public Task<ExampleSentence> UpdateExampleSentence(Guid entryId,
var updateProxy = new UpdateExampleSentenceProxy(lexExampleSentence, this);
update.Apply(updateProxy);
});
return Task.FromResult(FromLexExampleSentence(lexExampleSentence));
return Task.FromResult(FromLexExampleSentence(senseId, lexExampleSentence));
}

public Task DeleteExampleSentence(Guid entryId, Guid senseId, Guid exampleSentenceId)
Expand Down
4 changes: 2 additions & 2 deletions backend/FwLite/FwLiteProjectSync.Tests/SyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public async Task UpdatingAnEntryInEachProjectSyncsAcrossBoth()
{
var crdtApi = _fixture.CrdtApi;
var fwdataApi = _fixture.FwDataApi;
await fwdataApi.CreateWritingSystem(WritingSystemType.Vernacular, new WritingSystem() { Id = new WritingSystemId("es"), Name = "Spanish", Abbreviation = "es", Font = "Arial" });
await fwdataApi.CreateWritingSystem(WritingSystemType.Vernacular, new WritingSystem() { Id = new WritingSystemId("fr"), Name = "French", Abbreviation = "fr", Font = "Arial" });
await fwdataApi.CreateWritingSystem(WritingSystemType.Vernacular, new WritingSystem() { Id = Guid.NewGuid(), Type = WritingSystemType.Vernacular, WsId = new WritingSystemId("es"), Name = "Spanish", Abbreviation = "es", Font = "Arial" });
await fwdataApi.CreateWritingSystem(WritingSystemType.Vernacular, new WritingSystem() { Id = Guid.NewGuid(), Type = WritingSystemType.Vernacular, WsId = new WritingSystemId("fr"), Name = "French", Abbreviation = "fr", Font = "Arial" });
await _syncService.Sync(crdtApi, fwdataApi);

await crdtApi.UpdateEntry(_testEntry.Id, new UpdateObjectInput<Entry>().Set(entry => entry.LexemeForm["es"],"Manzana"));
Expand Down
8 changes: 4 additions & 4 deletions backend/FwLite/FwLiteProjectSync.Tests/UpdateDiffTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public void EntryDiffShouldUpdateAllFields()
var entryDiffToUpdate = CrdtFwdataProjectSyncService.EntryDiffToUpdate(previous, current);
ArgumentNullException.ThrowIfNull(entryDiffToUpdate);
entryDiffToUpdate.Apply(previous);
previous.Should().BeEquivalentTo(current, options => options.Excluding(x => x.Id)
.Excluding(x => x.Senses)
previous.Should().BeEquivalentTo(current, 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));
Expand All @@ -35,7 +35,7 @@ public async Task SenseDiffShouldUpdateAllFields()
var senseDiffToUpdate = await CrdtFwdataProjectSyncService.SenseDiffToUpdate(previous, current);
ArgumentNullException.ThrowIfNull(senseDiffToUpdate);
senseDiffToUpdate.Apply(previous);
previous.Should().BeEquivalentTo(current, options => options.Excluding(x => x.Id).Excluding(x => x.ExampleSentences));
previous.Should().BeEquivalentTo(current, options => options.Excluding(x => x.Id).Excluding(x => x.EntryId).Excluding(x => x.DeletedAt).Excluding(x => x.ExampleSentences));
}

[Fact]
Expand All @@ -46,6 +46,6 @@ public void ExampleSentenceDiffShouldUpdateAllFields()
var exampleSentenceDiffToUpdate = CrdtFwdataProjectSyncService.ExampleDiffToUpdate(previous, current);
ArgumentNullException.ThrowIfNull(exampleSentenceDiffToUpdate);
exampleSentenceDiffToUpdate.Apply(previous);
previous.Should().BeEquivalentTo(current, options => options.Excluding(x => x.Id));
previous.Should().BeEquivalentTo(current, options => options.Excluding(x => x.Id).Excluding(x => x.SenseId).Excluding(x => x.DeletedAt));
}
}
2 changes: 1 addition & 1 deletion backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
DryRunRecords.Add(new DryRunRecord(nameof(UpdateWritingSystem),
$"Update writing system {type}, changes: {update.Summarize()}"));
var ws = await api.GetWritingSystems();
return (type switch

Check warning on line 30 in backend/FwLite/FwLiteProjectSync/DryRunMiniLcmApi.cs

View workflow job for this annotation

GitHub Actions / Publish FW Lite app for Mac

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(MiniLcm.Models.WritingSystemType)2' is not covered.
{
WritingSystemType.Vernacular => ws.Vernacular,
WritingSystemType.Analysis => ws.Analysis
}).First(w => w.Id == id);
}).First(w => w.WsId == id);
}

public IAsyncEnumerable<PartOfSpeech> GetPartsOfSpeech()
Expand Down
4 changes: 2 additions & 2 deletions backend/FwLite/FwLiteProjectSync/MiniLcmImport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public async Task ImportProject(IMiniLcmApi importTo, IMiniLcmApi importFrom, in
foreach (var ws in writingSystems.Analysis)
{
await importTo.CreateWritingSystem(WritingSystemType.Analysis, ws);
logger.LogInformation("Imported ws {WsId}", ws.Id);
logger.LogInformation("Imported ws {WsId}", ws.WsId);
}

foreach (var ws in writingSystems.Vernacular)
{
await importTo.CreateWritingSystem(WritingSystemType.Vernacular, ws);
logger.LogInformation("Imported ws {WsId}", ws.Id);
logger.LogInformation("Imported ws {WsId}", ws.WsId);
}

await foreach (var partOfSpeech in importFrom.GetPartsOfSpeech())
Expand Down
2 changes: 1 addition & 1 deletion backend/FwLite/LcmCrdt.Tests/Changes/ComplexFormTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task DeleteEntryComponent()
complexEntry.Should().NotBeNull();
var component = complexEntry!.Components.First();

await fixture.DataModel.AddChange(Guid.NewGuid(), new DeleteChange<CrdtComplexFormComponent>(component.Id));
await fixture.DataModel.AddChange(Guid.NewGuid(), new DeleteChange<ComplexFormComponent>(component.Id));
complexEntry = await fixture.Api.GetEntry(complexEntry.Id);
complexEntry.Should().NotBeNull();
complexEntry!.Components.Should().NotContain(c => c.Id == component.Id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void NewChangeIPatchDoc_ThrowsForRemoveAtIndex()
{
var patch = new JsonPatchDocument<Entry>();
patch.Operations.Add(new Operation<Entry>("remove", "/senses/1", null, null));
var act = () => new JsonPatchChange<Entry>(Guid.NewGuid(), patch, JsonSerializerOptions.Default);
var act = () => new JsonPatchChange<Entry>(Guid.NewGuid(), patch);
act.Should().Throw<NotSupportedException>();
}

Expand Down
1 change: 0 additions & 1 deletion backend/FwLite/LcmCrdt.Tests/Data/FilteringTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using LcmCrdt.Data;
using MiniLcm.Models;
using Entry = LcmCrdt.Objects.Entry;

namespace LcmCrdt.Tests.Data;

Expand Down
4 changes: 2 additions & 2 deletions backend/FwLite/LcmCrdt.Tests/EntityCopyMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public static IEnumerable<object[]> GetEntityTypes()
[MemberData(nameof(GetEntityTypes))]
public void EntityCopyMethodShouldCopyAllFields(Type type)
{
type.IsAssignableTo(typeof(IObjectBase)).Should().BeTrue();
var entity = (IObjectBase) _autoFaker.Generate(type);
type.IsAssignableTo(typeof(IObjectWithId)).Should().BeTrue();
var entity = (IObjectWithId) _autoFaker.Generate(type);
var copy = entity.Copy();
copy.Should().BeEquivalentTo(entity, options => options.IncludingAllRuntimeProperties());
}
Expand Down
28 changes: 13 additions & 15 deletions backend/FwLite/LcmCrdt.Tests/JsonPatchEntryRewriteTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using LcmCrdt.Changes.Entries;
using LcmCrdt.Objects;
using MiniLcm.Models;
using SIL.Harmony.Changes;
using SystemTextJsonPatch;
using Entry = LcmCrdt.Objects.Entry;

namespace LcmCrdt.Tests;

Expand All @@ -17,7 +15,7 @@ public void ChangesFromJsonPatch_AddComponentMakesAddEntryComponentChange()
var patch = new JsonPatchDocument<MiniLcm.Models.Entry>();
var componentEntry = new Entry() { Id = Guid.NewGuid(), LexemeForm = { { "en", "component" } } };
patch.Add(entry => entry.Components, ComplexFormComponent.FromEntries(_entry, componentEntry));
var changes = Entry.ChangesFromJsonPatch(_entry, patch);
var changes = _entry.ToChanges(patch);
var addEntryComponentChange =
changes.Should().ContainSingle().Which.Should().BeOfType<AddEntryComponentChange>().Subject;
addEntryComponentChange.ComplexFormEntryId.Should().Be(_entry.Id);
Expand All @@ -33,9 +31,9 @@ public void ChangesFromJsonPatch_RemoveComponentMakesDeleteChange()
var complexFormComponent = ComplexFormComponent.FromEntries(_entry, componentEntry);
_entry.Components.Add(complexFormComponent);
patch.Remove(entry => entry.Components, 0);
var changes = Entry.ChangesFromJsonPatch(_entry, patch);
var changes = _entry.ToChanges(patch);
var removeEntryComponentChange = changes.Should().ContainSingle().Which.Should()
.BeOfType<DeleteChange<CrdtComplexFormComponent>>().Subject;
.BeOfType<DeleteChange<ComplexFormComponent>>().Subject;
removeEntryComponentChange.EntityId.Should().Be(complexFormComponent.Id);
}

Expand All @@ -48,7 +46,7 @@ public void ChangesFromJsonPatch_ReplaceComponentMakesReplaceEntryComponentChang
_entry.Components.Add(complexFormComponent);
var newComponentId = Guid.NewGuid();
patch.Replace(entry => entry.Components, complexFormComponent with { ComponentEntryId = newComponentId, ComponentHeadword = "new" }, 0);
var changes = Entry.ChangesFromJsonPatch(_entry, patch);
var changes = _entry.ToChanges(patch);
var setComplexFormComponentChange = changes.Should().ContainSingle().Which.Should()
.BeOfType<SetComplexFormComponentChange>().Subject;
setComplexFormComponentChange.EntityId.Should().Be(complexFormComponent.Id);
Expand All @@ -66,7 +64,7 @@ public void ChangesFromJsonPatch_ReplaceComponentWithNewComplexFormIdMakesReplac
_entry.Components.Add(complexFormComponent);
var newComplexFormId = Guid.NewGuid();
patch.Replace(entry => entry.Components, complexFormComponent with { ComplexFormEntryId = newComplexFormId, ComplexFormHeadword = "new" }, 0);
var changes = Entry.ChangesFromJsonPatch(_entry, patch);
var changes = _entry.ToChanges(patch);
var setComplexFormComponentChange = changes.Should().ContainSingle().Which.Should()
.BeOfType<SetComplexFormComponentChange>().Subject;
setComplexFormComponentChange.EntityId.Should().Be(complexFormComponent.Id);
Expand All @@ -81,7 +79,7 @@ public void ChangesFromJsonPatch_AddComplexFormMakesAddEntryComponentChange()
var patch = new JsonPatchDocument<MiniLcm.Models.Entry>();
var componentEntry = new Entry() { Id = Guid.NewGuid(), LexemeForm = { { "en", "complex form" } } };
patch.Add(entry => entry.ComplexForms, ComplexFormComponent.FromEntries(_entry, componentEntry));
var changes = Entry.ChangesFromJsonPatch(componentEntry, patch);
var changes = componentEntry.ToChanges(patch);
var addEntryComponentChange =
changes.Should().ContainSingle().Which.Should().BeOfType<AddEntryComponentChange>().Subject;
addEntryComponentChange.ComplexFormEntryId.Should().Be(_entry.Id);
Expand All @@ -97,9 +95,9 @@ public void ChangesFromJsonPatch_RemoveComplexFormMakesDeleteChange()
var complexFormComponent = ComplexFormComponent.FromEntries(_entry, componentEntry);
componentEntry.ComplexForms.Add(complexFormComponent);
patch.Remove(entry => entry.ComplexForms, 0);
var changes = Entry.ChangesFromJsonPatch(componentEntry, patch);
var changes = componentEntry.ToChanges(patch);
var removeEntryComponentChange = changes.Should().ContainSingle().Which.Should()
.BeOfType<DeleteChange<CrdtComplexFormComponent>>().Subject;
.BeOfType<DeleteChange<ComplexFormComponent>>().Subject;
removeEntryComponentChange.EntityId.Should().Be(complexFormComponent.Id);
}

Expand All @@ -112,7 +110,7 @@ public void ChangesFromJsonPatch_ReplaceComplexFormMakesReplaceEntryComponentCha
componentEntry.ComplexForms.Add(complexFormComponent);
var newComponentId = Guid.NewGuid();
patch.Replace(entry => entry.ComplexForms, complexFormComponent with { ComponentEntryId = newComponentId, ComponentHeadword = "new" }, 0);
var changes = Entry.ChangesFromJsonPatch(componentEntry, patch);
var changes = componentEntry.ToChanges(patch);
var setComplexFormComponentChange = changes.Should().ContainSingle().Which.Should()
.BeOfType<SetComplexFormComponentChange>().Subject;
setComplexFormComponentChange.EntityId.Should().Be(complexFormComponent.Id);
Expand All @@ -130,7 +128,7 @@ public void ChangesFromJsonPatch_ReplaceComplexFormWithNewComplexFormIdMakesRepl
componentEntry.ComplexForms.Add(complexFormComponent);
var newComplexFormId = Guid.NewGuid();
patch.Replace(entry => entry.ComplexForms, complexFormComponent with { ComplexFormEntryId = newComplexFormId, ComplexFormHeadword = "new" }, 0);
var changes = Entry.ChangesFromJsonPatch(componentEntry, patch);
var changes = componentEntry.ToChanges(patch);
var setComplexFormComponentChange = changes.Should().ContainSingle().Which.Should()
.BeOfType<SetComplexFormComponentChange>().Subject;
setComplexFormComponentChange.EntityId.Should().Be(complexFormComponent.Id);
Expand All @@ -145,7 +143,7 @@ public void ChangesFromJsonPatch_AddComplexFormTypeMakesAddComplexFormTypeChange
var patch = new JsonPatchDocument<MiniLcm.Models.Entry>();
var complexFormType = new ComplexFormType() { Id = Guid.NewGuid(), Name = new MultiString() };
patch.Add(entry => entry.ComplexFormTypes, complexFormType);
var changes = Entry.ChangesFromJsonPatch(_entry, patch);
var changes = _entry.ToChanges(patch);
var addComplexFormTypeChange =
changes.Should().ContainSingle().Which.Should().BeOfType<AddComplexFormTypeChange>().Subject;
addComplexFormTypeChange.EntityId.Should().Be(_entry.Id);
Expand All @@ -159,7 +157,7 @@ public void ChangesFromJsonPatch_RemoveComplexFormTypeMakesRemoveComplexFormType
var complexFormType = new ComplexFormType() { Id = Guid.NewGuid(), Name = new MultiString() };
_entry.ComplexFormTypes.Add(complexFormType);
patch.Remove(entry => entry.ComplexFormTypes, 0);
var changes = Entry.ChangesFromJsonPatch(_entry, patch);
var changes = _entry.ToChanges(patch);
var removeComplexFormTypeChange = changes.Should().ContainSingle().Which.Should()
.BeOfType<RemoveComplexFormTypeChange>().Subject;
removeComplexFormTypeChange.EntityId.Should().Be(_entry.Id);
Expand All @@ -174,7 +172,7 @@ public void ChangesFromJsonPatch_ReplaceComplexFormTypeMakesReplaceComplexFormTy
_entry.ComplexFormTypes.Add(complexFormType);
var newComplexFormType = new ComplexFormType() { Id = Guid.NewGuid(), Name = new MultiString() };
patch.Replace(entry => entry.ComplexFormTypes, newComplexFormType, 0);
var changes = Entry.ChangesFromJsonPatch(_entry, patch);
var changes = _entry.ToChanges(patch);
var replaceComplexFormTypeChange = changes.Should().ContainSingle().Which.Should()
.BeOfType<ReplaceComplexFormTypeChange>().Subject;
replaceComplexFormTypeChange.EntityId.Should().Be(_entry.Id);
Expand Down
Loading
Loading