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 committed Jan 16, 2025
1 parent 23de6e8 commit 5d64070
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 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,7 +15,6 @@

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

/**
Expand All @@ -28,15 +27,22 @@ 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);
}
JsonElement current = parent;
for (String section : sections) {
for (String split : section.split("[.]")) {
if (current instanceof JsonObject currentObject) {
current = currentObject.get(section);
current = currentObject.get(split);
if (current == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,22 @@ public class SettingsHelperTest extends BasePlatformTestCase {
"subsettingA": 1,
"subsettingB": 2
}
}
},
"flat.scalar.value": "flat value"
}
""";

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 +63,27 @@ 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\"");
}
}

0 comments on commit 5d64070

Please sign in to comment.