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

Formalizing Patches. Fix #46 #47

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

Conversation

umesh-timalsina
Copy link
Contributor

@umesh-timalsina umesh-timalsina commented Oct 6, 2022

No description provided.

@umesh-timalsina umesh-timalsina changed the title Formalizing Patches Formalizing Patches. Fix #46 Oct 6, 2022
@umesh-timalsina
Copy link
Contributor Author

Still a work in progress, however, its at the stage to finalize changes/review

@umesh-timalsina umesh-timalsina marked this pull request as ready for review October 10, 2022 17:44
Copy link
Collaborator

@brollb brollb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. I added a comment about how we might be able to refactor it a little further to remove some of the remaining boilerplate!

@@ -25,4 +27,17 @@ export class NodeChangeSet implements ChangeSet {
diffObj.value
);
}

validated(fn: (ch: NodeChangeSet) => boolean, errMsg: string): Result<NodeChangeSet, Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are passing the validation function here (which is only called once per patch operation right before applying it), it probably doesn't really buy us much defining this here. I know I initially suggested something like this but it might make more sense to just define a validation function for patch operations. I made an example of what I mean in a commit and pushed it.

lib/common/JSONImporter/NodeChangeSet.ts Outdated Show resolved Hide resolved
this.nodeSearchUtils = nodeSearchUtils;
}

keyLengthValidator(change) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither would this (at least not on the base class where I am not convinced it makes the most sense).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept it for reuse in couple of places

}

async put(node: Core.Node, change: NodeChangeSet, resolvedSelectors: NodeSelections): PatchResultPromise {
return this._validChange(change)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example of what I meant. Each patch operation just defines their own _put and _delete methods which accept a validated change. Validation can be defined in another method, _validChange. (I am sure there is a better name - maybe getValidChange or something?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two changes I had to make:

  1. There should be two validators: validChangePut and validChangeDelete.
  2. Not every error can be resolved (deletion for MemberAttributes for example with the above signature).

_put = async (node: Core.Node, change: NodeChangeSet, resolvedSelectors: NodeSelections): PatchResultPromise => {
const [/*type*/, name] = change.key;
this.core.setAttribute(node, name, change.value || '');
return new PatchResult(change); // I am not sure we need this PatchResult as it seems that it only ever returns the NodeChangeSet that was passed to it...?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't remove PatchResult but does this ever return something other than the change that was applied?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on your suggestion, I have created a PatchInfo object and the signature of _put and _delete methods go as follows:

class PatchInfo {
    nodeId: GmeCommon.Path;
    nodeGuid: Core.GUID;
    target: string;
    appliedPatch: ChangeSet;
}

type PatchFunction = (node: Core.Node, change: NodeChangeSet, resolvedSelectors: NodeSelections) => Promise<void>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants