-
-
Notifications
You must be signed in to change notification settings - Fork 373
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 safe config updating #7224
base: dev/feature
Are you sure you want to change the base?
Add safe config updating #7224
Conversation
|
||
File backup = FileUtils.backup(configFile); | ||
boolean updated = mainConfig.updateNodes(newConfig); | ||
// mainConfig.getMainNode().set(version.key, Skript.getVersion().toString()); TODO FOR TESTING! |
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.
just so i dont forget
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.
Great work so far. Here's what I found.
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.
I don't want the node classes to be reformatted in this PR.
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.
looks good overall but just want some testing assurances
* @return A set of the discovered nodes, guaranteed to be in the order of discovery. | ||
*/ | ||
@Contract(pure = true) | ||
private static @NotNull Set<Node> findNodes(@NotNull SectionNode node) { |
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.
I think this should be discoverNodes, since find implies we're trying to find something specific. Or flattenNodes()
} | ||
|
||
if (newConfig.setValues(mc, version.key, databases.key) || forceUpdate) { // new config is different |
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.
I'm a little worried this behavior isn't maintained. Can you show the results of some tests with a config file updating from like, 2.4 -> 2.9.5 and likewise with 2.4->2.10? I'm not entirely sure how it worked tbh.
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.
As sovde's comment mentioned, I would like to see some config-updating tests for this. Otherwise, it looks great!
return false; | ||
|
||
for (Node node : nodesToUpdate) { | ||
Skript.debug("Updating node %s", node); |
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.
Might be worth having a debug message at the beginning like Updating config <name>
SectionNode parent = getNode(newParent.getPath()); | ||
Preconditions.checkNotNull(parent); | ||
|
||
int idx = node.getIndex(); |
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.
i would spell out index
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(Arrays.hashCode(getPath()), comment); |
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.
Is there a reason you're calculating the path hashcode too? Rather than just:
return Objects.hash(Arrays.hashCode(getPath()), comment); | |
return Objects.hash(getPath(), comment); |
* @return The node at the specified index. May be null. | ||
* @throws IllegalArgumentException if the index is out of bounds | ||
*/ | ||
@Nullable Node getAt(int idx) { |
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.
I would spell out index
}) { | ||
public @NotNull Iterator<Node> iterator() { | ||
return new CheckedIterator<>(fullIterator(), | ||
n -> Objects.nonNull(n) && !n.isVoid()); // double null check to avoid warning |
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.
I think it would be fine to suppress the warning instead with a comment explaining why
Description
Changes
Config#updateNodes
About updateNodes
Config#updateNodes
differs from the current behaviour ofConfig#setValues
as it does not touch any nodes in the existing config ("the old config") that are also present in the config provided with Skript ("the new config"). This ensures safety for critical sections like thedatabases
section.First, the difference between the nodes of the new and old config is found. These nodes that are missing from the old config will then be added to the old config at the index (the amount of nodes prior to the current one) at which they were found in the new config.
Testing
From my testing, this only seems to break when the old config is made invalid by de-indenting one of the entry nodes. This means the path of the following nodes are all different until the start of the next section (at the same indentation as the currently de-indented entry node, not any children). As a node's path is used to determine whether it is missing from the config, this will cause these following nodes to de-indent as well, deleting the current section head. This seems to be fixed by adding a check for whether the parsed config is valid, by using
SectionNode#isValid
.Limitations
Currently, outdated comments are not deleted and I'm not sure whether we want to support keeping user-added comments present. Should this be added?
Removed
My sanity
Target Minecraft Versions: any
Requirements: none
Related Issues: #7184