Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow requesting flat scalar values in workspace/configuration #748

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("[.]");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string array is never empty

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);
}
Comment on lines -51 to -54
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string array is never empty


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");
}
}
Loading