-
Notifications
You must be signed in to change notification settings - Fork 0
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
First attempt at making IChangeContext interface #26
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace SIL.Harmony.Core; | ||
|
||
public interface IChangeContext | ||
{ | ||
public CommitBase Commit { get; } | ||
ValueTask<ObjectSnapshot?> GetSnapshot(Guid entityId); | ||
public async ValueTask<T?> GetCurrent<T>(Guid entityId) where T : class | ||
{ | ||
var snapshot = await GetSnapshot(entityId); | ||
if (snapshot is null) return null; | ||
return (T) snapshot.Entity.DbObject; | ||
} | ||
|
||
public async ValueTask<bool> IsObjectDeleted(Guid entityId) => (await GetSnapshot(entityId))?.EntityIsDeleted ?? true; | ||
internal IObjectBase Adapt(object obj); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace SIL.Harmony.Core; | ||
|
||
[JsonPolymorphic] | ||
public interface IObjectBase | ||
{ | ||
Guid Id { get; } | ||
DateTimeOffset? DeletedAt { get; set; } | ||
|
||
/// <summary> | ||
/// provides the references this object has to other objects, when those objects are deleted | ||
/// <see cref="RemoveReference"/> will be called to remove the reference | ||
/// </summary> | ||
/// <returns></returns> | ||
public Guid[] GetReferences(); | ||
/// <summary> | ||
/// remove a reference to another object, in some cases this may cause this object to be deleted | ||
/// </summary> | ||
/// <param name="id">id of the deleted object</param> | ||
/// <param name="commit"> | ||
/// commit where the reference was removed | ||
/// should be used to set the deleted date for this object | ||
/// </param> | ||
public void RemoveReference(Guid id, CommitBase commit); | ||
|
||
public IObjectBase Copy(); | ||
/// <summary> | ||
/// the name of the object type, this is used to discriminate between different types of objects in the snapshots table | ||
/// </summary> | ||
/// <returns>a stable type name of this object, should not change over time</returns> | ||
public string GetObjectTypeName(); | ||
[JsonIgnore] | ||
public object DbObject { get; } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace SIL.Harmony.Core; | ||
|
||
public class ObjectSnapshot | ||
{ | ||
public static ObjectSnapshot ForTesting(CommitBase commit) | ||
{ | ||
return new ObjectSnapshot | ||
{ | ||
Commit = commit, | ||
Entity = null!, | ||
Id = Guid.Empty, | ||
References = [], | ||
CommitId = commit.Id, | ||
EntityId = Guid.Empty, | ||
IsRoot = false, | ||
TypeName = "Test", | ||
EntityIsDeleted = false | ||
}; | ||
} | ||
//determines column name used in projected object tables, changing this will require a migration | ||
public const string ShadowRefName = "SnapshotId"; | ||
[JsonConstructor] | ||
protected ObjectSnapshot() | ||
{ | ||
} | ||
|
||
[SetsRequiredMembers] | ||
public ObjectSnapshot(IObjectBase entity, CommitBase commit, bool isRoot) : this() | ||
{ | ||
Id = Guid.NewGuid(); | ||
Entity = entity; | ||
References = entity.GetReferences(); | ||
EntityId = entity.Id; | ||
EntityIsDeleted = entity.DeletedAt.HasValue; | ||
TypeName = entity.GetObjectTypeName(); | ||
CommitId = commit.Id; | ||
Commit = commit; | ||
IsRoot = isRoot; | ||
} | ||
|
||
public required Guid Id { get; init; } | ||
public required string TypeName { get; init; } | ||
public required IObjectBase Entity { get; init; } | ||
public required Guid[] References { get; init; } | ||
public required Guid EntityId { get; init; } | ||
public required bool EntityIsDeleted { get; init; } | ||
public required Guid CommitId { get; init; } | ||
public required CommitBase Commit { get; init; } | ||
public required bool IsRoot { get; init; } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
using SIL.Harmony.Core; | ||
using SIL.Harmony.Db; | ||
using SIL.Harmony.Entities; | ||
|
||
namespace SIL.Harmony.Changes; | ||
|
||
public class ChangeContext | ||
public class ChangeContext : IChangeContext | ||
Check failure on line 7 in src/SIL.Harmony/Changes/ChangeContext.cs GitHub Actions / build
Check failure on line 7 in src/SIL.Harmony/Changes/ChangeContext.cs GitHub Actions / build
Check failure on line 7 in src/SIL.Harmony/Changes/ChangeContext.cs GitHub Actions / build
Check failure on line 7 in src/SIL.Harmony/Changes/ChangeContext.cs GitHub Actions / build
|
||
{ | ||
private readonly SnapshotWorker _worker; | ||
private readonly CrdtConfig _crdtConfig; | ||
|
@@ -15,7 +16,7 @@ | |
Commit = commit; | ||
} | ||
|
||
public Commit Commit { get; } | ||
public Commit Commit { get; } // PROBLEM: Interface is CommitBase. How do I do this? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can just change the property type from Commit to CommitBase |
||
public async ValueTask<ObjectSnapshot?> GetSnapshot(Guid entityId) => await _worker.GetSnapshot(entityId); | ||
public async ValueTask<T?> GetCurrent<T>(Guid entityId) where T : class | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Re: line +29] this doesn't compile, it'll need to change to an explicit implementation See this comment inline on Graphite. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks like actually moving this would be more painful than I expected. There's also the fact that since it's stored in the Db changing the Commit type might cause problems there. Instead lets just extract an interface. It should have all the properties but the Commit type will be CommitBase on the interface. We then need to make an explicit interface implementation on ObjectSnapshot. That looks like this:
CommitBase IObjectSnapshot.Commit => Commit
, this basically makes this Commit property only get used if the object is referred to asIObjectSnapshot
if it's referred to asObjectSnapshot
then it will use the public property.