Skip to content

Commit

Permalink
fix bugs in loading json-based settings
Browse files Browse the repository at this point in the history
  • Loading branch information
wadoon committed Dec 7, 2023
1 parent ac7254d commit 52b4e67
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void readSettings(Configuration props) {
if (cat == null)
return;
propertyEntries.forEach(it -> {
final var value = cat.get(it.getKey());
final var value = it.fromObject( cat.get(it.getKey()) );
if (value != null) {
properties.put(it.getKey(), value);
}
Expand All @@ -127,39 +127,40 @@ public void writeSettings(Configuration props) {
}

protected PropertyEntry<Double> createDoubleProperty(String key, double defValue) {
PropertyEntry<Double> pe = new DefaultPropertyEntry<>(key, defValue, parseDouble);
PropertyEntry<Double> pe = new DefaultPropertyEntry<>(key, defValue, parseDouble, (it) -> (double) it);
propertyEntries.add(pe);
return pe;
}

protected PropertyEntry<Integer> createIntegerProperty(String key, int defValue) {
PropertyEntry<Integer> pe = new DefaultPropertyEntry<>(key, defValue, parseInt);
PropertyEntry<Integer> pe = new DefaultPropertyEntry<>(key, defValue, parseInt,
(it) -> Math.toIntExact((Long) it));
propertyEntries.add(pe);
return pe;
}

protected PropertyEntry<Float> createFloatProperty(String key, float defValue) {
PropertyEntry<Float> pe = new DefaultPropertyEntry<>(key, defValue, parseFloat);
PropertyEntry<Float> pe = new DefaultPropertyEntry<>(key, defValue, parseFloat, (it) -> (float) (double) it);
propertyEntries.add(pe);
return pe;
}

protected PropertyEntry<String> createStringProperty(String key, String defValue) {
PropertyEntry<String> pe = new DefaultPropertyEntry<>(key, defValue, id -> id);
PropertyEntry<String> pe = new DefaultPropertyEntry<>(key, defValue, id -> id, Object::toString);
propertyEntries.add(pe);
return pe;
}

protected PropertyEntry<Boolean> createBooleanProperty(String key, boolean defValue) {
PropertyEntry<Boolean> pe = new DefaultPropertyEntry<>(key, defValue, parseBoolean);
PropertyEntry<Boolean> pe = new DefaultPropertyEntry<>(key, defValue, parseBoolean, (it) -> (Boolean) it);
propertyEntries.add(pe);
return pe;
}

protected PropertyEntry<Set<String>> createStringSetProperty(String key, String defValue) {
PropertyEntry<Set<String>> pe = new DefaultPropertyEntry<>(key, parseStringSet(defValue),
AbstractPropertiesSettings::parseStringSet,
AbstractPropertiesSettings::stringSetToString);
AbstractPropertiesSettings::stringSetToString, (it)->new LinkedHashSet<>((Collection<String>) it));
propertyEntries.add(pe);
return pe;
}
Expand All @@ -174,8 +175,8 @@ protected PropertyEntry<Set<String>> createStringSetProperty(String key, String
protected PropertyEntry<List<String>> createStringListProperty(@NonNull String key,
@Nullable String defValue) {
PropertyEntry<List<String>> pe = new DefaultPropertyEntry<>(key, parseStringList(defValue),
AbstractPropertiesSettings::parseStringList,
AbstractPropertiesSettings::stringListToString);
AbstractPropertiesSettings::parseStringList,
AbstractPropertiesSettings::stringListToString, it -> (List<String>) it);
propertyEntries.add(pe);
return pe;
}
Expand All @@ -195,6 +196,8 @@ default void update() {
}

String value();

T fromObject(@Nullable Object o);
}


Expand All @@ -204,16 +207,19 @@ class DefaultPropertyEntry<T> implements PropertyEntry<T> {
private final Function<String, T> convert;
private final Function<T, String> toString;

private DefaultPropertyEntry(String key, T defaultValue, Function<String, T> convert) {
this(key, defaultValue, convert, Objects::toString);
private final Function<Object, T> fromObject;

private DefaultPropertyEntry(String key, T defaultValue, Function<String, T> convert, Function<Object, T> fromObject) {
this(key, defaultValue, convert, Objects::toString, fromObject);
}

private DefaultPropertyEntry(String key, T defaultValue, Function<String, T> convert,
Function<T, String> toString) {
Function<T, String> toString, Function<Object, T> fromObject) {
this.key = key;
this.defaultValue = defaultValue;
this.convert = convert;
this.toString = toString;
this.fromObject = fromObject;
}

@Override
Expand Down Expand Up @@ -255,5 +261,10 @@ public String value() {
return toString.apply(v);
}
}

@Override
public T fromObject(@Nullable Object o) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void readSettings(Properties props) {
public void writeSettings(Properties props) {
var prefix = "[" + CATEGORY + "]";
for (String activatedFeature : activatedFeatures) {
props.put(prefix + activatedFeature, true);
props.put(prefix + activatedFeature, "true");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ private void loadSettings() {
}
}
} catch (IOException e) {
throw new RuntimeException(e);
LOGGER.error("Could not load settings from {}", filename, e);
}
}

private void load(File file) throws IOException {
if (!file.getName().endsWith(".toml")) {
if (!file.getName().endsWith(".json")) {
try (FileInputStream in = new FileInputStream(file)) {
Properties properties = new Properties();
properties.load(in);
Expand All @@ -107,11 +107,14 @@ private void load(File file) throws IOException {
}
} else {
this.lastReadedConfiguration = Configuration.load(file);
for (Settings settings : settings) {
settings.readSettings(lastReadedConfiguration);
}
}
}

public void saveSettings() {
if (!filename.getName().endsWith(".toml")) {
if (!filename.getName().endsWith(".json")) {
Properties result = new Properties();
for (Settings settings : settings) {
settings.writeSettings(result);
Expand All @@ -124,7 +127,7 @@ public void saveSettings() {
try (var out = new FileOutputStream(filename)) {
result.store(out, "Proof-Independent-Settings-File. Generated " + new Date());
} catch (IOException e) {
throw new RuntimeException(e);
LOGGER.error("Could not store settings to {}", filename, e);
}
}

Expand All @@ -139,7 +142,7 @@ public void saveSettings() {
new BufferedWriter(new FileWriter(filename.toString().replace(".props", ".json")))) {
config.save(out, "Proof-Independent-Settings-File. Generated " + new Date());
} catch (IOException e) {
throw new RuntimeException(e);
LOGGER.error("Could not store settings to {}", filename, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public void setShowWholeTaclet(boolean b) {
* @return the current sizeIndex
*/
public int sizeIndex() {
return sizeIndex.get();
return Math.toIntExact(sizeIndex.get());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import de.uka.ilkd.key.settings.Configuration;
import de.uka.ilkd.key.settings.PathConfig;

import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -165,6 +166,11 @@ public String value() {
}
}

@Override
public Color fromObject(@Nullable Object o) {
return fromHex(o.toString());
}

@Override
public void parseFrom(String v) {
final var old = value();
Expand Down

0 comments on commit 52b4e67

Please sign in to comment.