Skip to content

Commit

Permalink
fix: Allow requesting flat scalar values in workspace/configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Enaium authored and angelozerr committed Jan 17, 2025
1 parent 23de6e8 commit 3cfe729
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,13 @@ protected Object findSettings(@Nullable String section) {
if (section == null) {
return config;
}
String[] sections = section.split("[.]");
return findSettings(sections, json);
return findSettings(section, json);
}
return null;
}

protected static Object findSettings(String[] sections, JsonObject jsonObject) {
return SettingsHelper.findSettings(sections, jsonObject);
protected static Object findSettings(String section, JsonObject jsonObject) {
return SettingsHelper.findSettings(section, jsonObject);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Set;

/**
* Helpers for extracting nested settings from json
*/
Expand All @@ -28,20 +29,45 @@ private SettingsHelper() {

/**
* Extract nested settings from json.
* @param sections path to the json element to retrieve
* @param parent Json to look for settings in
*
* @param section path to the json element to retrieve
* @param parent Json to look for settings in
* @return the settings retrieved in the specified section and null if the section was not found.
*/
public static @Nullable JsonElement findSettings(@NotNull String[] sections, @Nullable JsonObject parent) {
public static @Nullable JsonElement findSettings(@Nullable String section, @Nullable JsonObject parent) {
if (section == null || parent == null) {
return null;
}

if (parent.has(section)) {
return parent.get(section);
}

final var sections = section.split("[.]");
JsonElement current = parent;
for (String section : sections) {
for (var split : sections) {
if (current instanceof JsonObject currentObject) {
current = currentObject.get(section);
if (current == null) {
return null;
current = currentObject.get(split);
}
}

if (current != null) {
return current;
}

for (var key : Set.copyOf(parent.keySet())) {
var keySplit = key.split("[.]");
if (sections.length > keySplit.length) {
parent.remove(key);
continue;
}
for (int i = 0; i < sections.length; i++) {
if (!sections[i].equals(keySplit[i])) {
parent.remove(key);
break;
}
}
}
return current;
return parent.isEmpty() ? null : parent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,25 @@ public class SettingsHelperTest extends BasePlatformTestCase {
"subsettingA": 1,
"subsettingB": 2
}
}
},
"flat.scalar.value": "flat value",
"flat.scalar.value2": "flat value2",
"JimmerDTO.Classpath.FindBuilder": false,
"JimmerDTO.Classpath.FindConfiguration": false
}
""";

private static void assertFindSettings(@NotNull String json, @NotNull String[] sections, @Nullable String expectedJsonText) {
private static void assertFindSettings(@NotNull String json, @NotNull String section, @Nullable String expectedJsonText) {
JsonObject jsonObject = JsonParser.parseReader(new StringReader(json)).getAsJsonObject();
JsonElement expectedJson = expectedJsonText == null ? null : JsonParser.parseReader(new StringReader(expectedJsonText));

JsonElement result = SettingsHelper.findSettings(sections, jsonObject);
JsonElement result = SettingsHelper.findSettings(section, jsonObject);

assertEquals(result, expectedJson);
}

public void testGetSettingsIdentityOnEmptySections() {
String[] requestedPath = new String[0];
assertFindSettings(testJson, requestedPath, testJson);
}

public void testGetSettingsObjectValue() {
String[] requestedPath = new String[]{"mylsp"};
var requestedPath = "mylsp";
assertFindSettings(testJson, requestedPath, """
{
"myscalarsetting": "value",
Expand All @@ -67,22 +66,67 @@ public void testGetSettingsObjectValue() {
}

public void testGetSettingsPrimitiveValue() {
String[] requestedPath = new String[]{"mylsp", "myscalarsetting"};
var requestedPath = "mylsp.myscalarsetting";
assertFindSettings(testJson, requestedPath, "\"value\"");
}

public void testGetSettingsDeepPrimitiveValue() {
String[] requestedPath = new String[]{"mylsp", "myobjectsettings", "subsettingA"};
var requestedPath = "mylsp.myobjectsettings.subsettingA";
assertFindSettings(testJson, requestedPath, "1");
}

public void testGetSettingsNonExistingValue() {
String[] requestedPath = new String[]{"mylsp", "nonexistant"};
var requestedPath = "mylsp.nonexistant";
assertFindSettings(testJson, requestedPath, null);
}

public void testGetSettingsEmptyJson() {
String[] requestedPath = new String[]{"mylsp", "myobjectsettings", "subsettingA"};
var requestedPath = "mylsp.myobjectsettings.subsettingA";
assertFindSettings("{}", requestedPath, null);
}

public void testFlatScalarValue() {
var requestedPath = "flat.scalar.value";
assertFindSettings(testJson, requestedPath, "\"flat value\"");
}

public void testFlatScalar() {
var requestedPath = "flat.scalar";
assertFindSettings(testJson, requestedPath, """
{
"flat.scalar.value": "flat value",
"flat.scalar.value2": "flat value2"
}
""");
}

public void testFlatScalarNonExisting() {
var requestedPath = "flat.scalar.nonexistant";
assertFindSettings(testJson, requestedPath, null);
}

public void testJimmerDTO() {
var requestedPath = "JimmerDTO";
assertFindSettings(testJson, requestedPath, """
{
"JimmerDTO.Classpath.FindBuilder": false,
"JimmerDTO.Classpath.FindConfiguration": false
}
""");
}

public void testJimmerDTOClasspath() {
var requestedPath = "JimmerDTO.Classpath";
assertFindSettings(testJson, requestedPath, """
{
"JimmerDTO.Classpath.FindBuilder": false,
"JimmerDTO.Classpath.FindConfiguration": false
}
""");
}

public void testJimmerDTOClasspathFindBuilder() {
var requestedPath = "JimmerDTO.Classpath.FindBuilder";
assertFindSettings(testJson, requestedPath, "false");
}
}

0 comments on commit 3cfe729

Please sign in to comment.