Skip to content

Commit

Permalink
YamlManager: Added basic update method
Browse files Browse the repository at this point in the history
  • Loading branch information
xDec0de committed Oct 29, 2024
1 parent e22c570 commit 8782b40
Showing 1 changed file with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.codersky.mcutils.storage.files.yaml;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

Expand All @@ -9,21 +10,30 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

class YamlManager {

private final File file;
private final Yaml yaml;

YamlManager(String path) {
YamlManager(@NotNull String path) {
this.yaml = getNewYaml();
this.file = new File(path);
}

private Yaml getNewYaml() {
final DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
this.yaml = new Yaml(dumperOptions);
this.file = new File(path);
return new Yaml(dumperOptions);
}

@NotNull
Expand Down Expand Up @@ -51,4 +61,11 @@ public boolean save(HashMap<String, Object> keys) {
return false;
}
}

public void update(@NotNull InputStream source, @NotNull HashMap<String, Object> keys) {
final HashMap<String, Object> updMap = getNewYaml().load(source);
for (Map.Entry<String, Object> entry : updMap.entrySet())
if (!keys.containsKey(entry.getKey()))
keys.put(entry.getKey(), entry.getValue());
}
}

0 comments on commit 8782b40

Please sign in to comment.