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

Add safe config updating #7224

Open
wants to merge 29 commits into
base: dev/feature
Choose a base branch
from
Open

Conversation

Efnilite
Copy link
Member

Description

Changes

  • Fixes New 'backups to keep' entry not generated for existing configs #7184
  • Reformats all classes to be more up to date with code standards
    • Any non-new method changes are for improving code readability, improving the position of the methods in the class or using pattern matching.
  • Deprecates the current method for config updating, replacing it with Config#updateNodes

About updateNodes
Config#updateNodes differs from the current behaviour of Config#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 the databases 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

@Efnilite Efnilite added bug An issue that needs to be fixed. Alternatively, a PR fixing an issue. priority: high Issues with potentially high impact that could be harmful to users. needs testing Needs testing to determine current status or issue validity, or for WIP feature pulls. 2.10 Targeting a 2.10.X version release labels Nov 21, 2024

File backup = FileUtils.backup(configFile);
boolean updated = mainConfig.updateNodes(newConfig);
// mainConfig.getMainNode().set(version.key, Skript.getVersion().toString()); TODO FOR TESTING!
Copy link
Member Author

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

@Efnilite Efnilite removed the priority: high Issues with potentially high impact that could be harmful to users. label Nov 22, 2024
Copy link
Member

@APickledWalrus APickledWalrus left a 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.

src/main/java/ch/njol/skript/config/SimpleNode.java Outdated Show resolved Hide resolved
src/main/java/ch/njol/skript/config/Node.java Outdated Show resolved Hide resolved
src/main/java/ch/njol/skript/config/Node.java Outdated Show resolved Hide resolved
src/main/java/ch/njol/skript/config/Node.java Outdated Show resolved Hide resolved
src/main/java/ch/njol/skript/config/EntryNode.java Outdated Show resolved Hide resolved
src/main/java/ch/njol/skript/config/SectionNode.java Outdated Show resolved Hide resolved
src/main/java/ch/njol/skript/config/SectionNode.java Outdated Show resolved Hide resolved
src/main/java/ch/njol/skript/config/SectionNode.java Outdated Show resolved Hide resolved
src/main/java/ch/njol/skript/config/Config.java Outdated Show resolved Hide resolved
src/main/java/ch/njol/skript/config/Config.java Outdated Show resolved Hide resolved
@Efnilite Efnilite requested a review from Moderocky November 23, 2024 09:14
Copy link
Member

@Moderocky Moderocky left a 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.

@Efnilite Efnilite changed the title Add safe config updating, update config classes Add safe config updating Nov 28, 2024
Copy link
Member

@sovdeeth sovdeeth left a 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) {
Copy link
Member

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
Copy link
Member

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.

Copy link
Member

@APickledWalrus APickledWalrus left a 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);
Copy link
Member

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();
Copy link
Member

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);
Copy link
Member

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:

Suggested change
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) {
Copy link
Member

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.10 Targeting a 2.10.X version release bug An issue that needs to be fixed. Alternatively, a PR fixing an issue. needs testing Needs testing to determine current status or issue validity, or for WIP feature pulls.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants