-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add support for list overrides #134
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Resource } from 'rdf-object'; | ||
|
||
/** | ||
* Transforms a resource based on the contents of an override step. | ||
*/ | ||
export interface IOverrideStep { | ||
/** | ||
* Determines if this handler can apply the given override step to the resource. | ||
* | ||
* @param config - The resource to override. | ||
* @param step - The override step to apply. | ||
* | ||
* @returns true if this handler should be used. | ||
*/ | ||
canHandle: (config: Resource, step: Resource) => boolean; | ||
|
||
/** | ||
* Applies the changes described in the given override step to the resource. | ||
* | ||
* @param config - The resource to override. | ||
* @param step - The override step to apply. | ||
* | ||
* @returns The modified resource. | ||
*/ | ||
handle: (config: Resource, step: Resource) => Resource; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Resource } from 'rdf-object'; | ||
import { PREFIX_OO } from '../../rdf/Iris'; | ||
import type { IOverrideStep } from './IOverrideStep'; | ||
import { extractOverrideStepFields, findResourceIndex, getPropertyResourceList } from './OverrideUtil'; | ||
|
||
/** | ||
* Override step that inserts elements in a list after a specific element. | ||
* | ||
* Uses the following override step fields: | ||
* - `overrideParameter`: Parameter of the original object that contains the list. | ||
* - `overrideTarget`: Element already in the list that is used as reference. This can be a named node or a literal. | ||
* - `overrideValue`: Element(s) to insert immediately after the target element. | ||
*/ | ||
export class OverrideListInsertAfter implements IOverrideStep { | ||
public canHandle(config: Resource, step: Resource): boolean { | ||
return step.property.type.value === PREFIX_OO('OverrideListInsertAfter'); | ||
} | ||
|
||
public handle(config: Resource, step: Resource): Resource { | ||
const { parameters, targets, values } = extractOverrideStepFields(step, { parameters: 1, targets: 1 }); | ||
|
||
const list = getPropertyResourceList(config, parameters[0]); | ||
|
||
const index = findResourceIndex(list, targets[0]); | ||
|
||
// +1 so we start after the selected element | ||
list.splice(index + 1, 0, ...values); | ||
|
||
return config; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { Resource } from 'rdf-object'; | ||
import { PREFIX_OO } from '../../rdf/Iris'; | ||
import { ErrorResourcesContext } from '../../util/ErrorResourcesContext'; | ||
import type { IOverrideStep } from './IOverrideStep'; | ||
import { extractOverrideStepFields, getPropertyResourceList } from './OverrideUtil'; | ||
|
||
/** | ||
* Override step that inserts elements in a list at a specific index. | ||
* A negative index can be used to count from the back of the list. | ||
* An index of `-0` can be used to insert at the end of the list. | ||
* | ||
* Uses the following override step fields: | ||
* - `overrideParameter`: Parameter of the original object that contains the list. | ||
* - `overrideTarget`: A literal containing the index. | ||
* - `overrideValue`: Element(s) to insert at the chosen index. | ||
*/ | ||
export class OverrideListInsertAt implements IOverrideStep { | ||
public canHandle(config: Resource, step: Resource): boolean { | ||
return step.property.type.value === PREFIX_OO('OverrideListInsertAt'); | ||
} | ||
|
||
public handle(config: Resource, step: Resource): Resource { | ||
const { parameters, targets, values } = extractOverrideStepFields(step, { parameters: 1, targets: 1 }); | ||
|
||
const list = getPropertyResourceList(config, parameters[0]); | ||
|
||
const val = targets[0].value; | ||
if (!/^-?\d+$/u.test(val)) { | ||
throw new ErrorResourcesContext(`Invalid index in Override step OverrideListInsertAt for parameter ${parameters[0].value}: ${val}`, { | ||
config, | ||
step, | ||
}); | ||
} | ||
|
||
// Support adding elements at the end using -0 | ||
if (val === '-0') { | ||
list.push(...values); | ||
} else { | ||
const index = Number.parseInt(val, 10); | ||
list.splice(index, 0, ...values); | ||
} | ||
|
||
return config; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Instead of adding the handlers here, could we pass them through the
options
fromComponentsManagerBuilder
?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.
No strong opinion either way, just that this was based on how this is done in some other classes:
Components.js/lib/preprocess/ConfigPreprocessorComponentMapped.ts
Lines 39 to 45 in d33d4c2
Components.js/lib/preprocess/ParameterHandler.ts
Lines 21 to 27 in a553310
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.
Oh, I see. In that case, you can ignore my comment.