Skip to content

Commit

Permalink
Fix serializing and deserializing empty config
Browse files Browse the repository at this point in the history
  • Loading branch information
ogesaku committed May 3, 2024
1 parent 23f5be5 commit b63cca6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/main/java/com/coditory/quark/config/ConfigFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.snakeyaml.engine.v2.api.Dump;
import org.snakeyaml.engine.v2.api.DumpSettings;
import org.snakeyaml.engine.v2.api.Load;
import org.snakeyaml.engine.v2.api.LoadSettings;
import org.snakeyaml.engine.v2.api.Dump;
import org.snakeyaml.engine.v2.common.FlowStyle;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -93,7 +93,7 @@ public Config parse(String config) {
LoadSettings settings = LoadSettings.builder().build();
Load yaml = new Load(settings);
Map<String, Object> map = (Map<String, Object>) yaml.loadFromString(config);
return Config.of(map);
return map == null || map.isEmpty() ? Config.empty() : Config.of(map);
}

@SuppressWarnings("unchecked")
Expand All @@ -107,6 +107,7 @@ public Config parse(InputStream config) {

@Override
public String stringify(Config config) {
if (config.isEmpty()) return "";
DumpSettings settings = DumpSettings.builder().setDefaultFlowStyle(FlowStyle.BLOCK).build();
Dump dump = new Dump(settings);
dump.dumpToString(config.toMap());
Expand All @@ -123,7 +124,7 @@ private static class JsonConfigParser implements ConfigFormatParser {
@SuppressWarnings("unchecked")
public Config parse(String config) {
Map<String, Object> map = gson.fromJson(config, Map.class);
return Config.of(map);
return map == null || map.isEmpty() ? Config.empty() : Config.of(map);
}

@Override
Expand Down Expand Up @@ -162,6 +163,7 @@ public Config parse(InputStream config) throws Exception {

@Override
public String stringify(Config config) {
if (config.isEmpty()) return "";
return config.toFlatMap().entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(joining("\n", "", "\n"));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/coditory/quark/config/Preconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static <T> T expectNonNull(T value) {

public static <T> T expectNonNull(T value, String name) {
if (value == null) {
String message = message("Expected non-blank string", name);
String message = message("Expected non-null", name);
throw new IllegalArgumentException(message);
}
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ class ConfigJsonFormatSpec extends Specification {
then:
result.getStringList("f") == ["F0", "F1"]
}

def "should serialize and deserialize empty config"() {
expect:
ConfigFactory.parseJson("") == Config.empty()
and:
ConfigFormatter.toJson(Config.empty()) == "{}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ class ConfigPropertiesFormatSpec extends Specification {
then:
result.getStringList("f") == ["F0", "F1"]
}

def "should serialize and deserialize empty config"() {
expect:
ConfigFactory.parseProperties("") == Config.empty()
and:
ConfigFormatter.toProperties(Config.empty()) == ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@ class ConfigYamlFormatSpec extends Specification {
then:
result.getStringList("f") == ["F0", "F1"]
}

def "should serialize and deserialize empty config"() {
expect:
ConfigFactory.parseYaml("") == Config.empty()
and:
ConfigFormatter.toYaml(Config.empty()) == ""
}
}

0 comments on commit b63cca6

Please sign in to comment.