Skip to content

Commit

Permalink
unflatten map
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <cwperx@amazon.com>
  • Loading branch information
cwperks committed Jul 20, 2023
1 parent 0b80916 commit 8d1bcb0
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,20 @@ public String get(String setting, String defaultValue) {
return retVal == null ? defaultValue : retVal;
}

/**
* Returns a setting value based on the setting key.
*/
public Settings getNestedSettings(String key) {
return (Settings) settings.get(key);
}

/**
* Returns a setting value based on the setting key.
*/
public List<Settings> getNestedListOfSettings(String key) {
return (List<Settings>) settings.get(key);
}

/**
* Returns the setting value (as float) associated with the setting key. If it does not exists,
* returns the default value provided.
Expand Down Expand Up @@ -663,6 +677,7 @@ private static void fromXContent(XContentParser parser, StringBuilder keyBuilder
fromXContent(parser, keyBuilder, builder, allowNullValues);
} else if (parser.currentToken() == XContentParser.Token.START_ARRAY) {
List<String> list = new ArrayList<>();
List<Object> listOfObjects = new ArrayList<>();
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
list.add(parser.text());
Expand All @@ -671,12 +686,19 @@ private static void fromXContent(XContentParser parser, StringBuilder keyBuilder
} else if (parser.currentToken() == XContentParser.Token.VALUE_BOOLEAN) {
list.add(String.valueOf(parser.text()));
} else {
throw new IllegalStateException("only value lists are allowed in serialized settings");
listOfObjects.add(fromXContent(parser, true, false));
// throw new IllegalStateException("only value lists are allowed in serialized settings");
}
}
String key = keyBuilder.toString();
validateValue(key, list, parser, allowNullValues);
builder.putList(key, list);
if (!listOfObjects.isEmpty()) {
builder.putListOfObjects(key, listOfObjects);
}
if (!list.isEmpty() && !listOfObjects.isEmpty()) {
throw new IllegalStateException("list cannot contain both values and objects");
}
} else if (parser.currentToken() == XContentParser.Token.VALUE_NULL) {
String key = keyBuilder.toString();
validateValue(key, null, parser, allowNullValues);
Expand Down Expand Up @@ -782,6 +804,20 @@ public String get(String key) {
return Settings.toString(map.get(key));
}

/**
* Returns a setting value based on the setting key.
*/
public Settings getNestedSettings(String key) {
return (Settings) map.get(key);
}

/**
* Returns a setting value based on the setting key.
*/
public List<Settings> getNestedListOfSettings(String key) {
return (List<Settings>) map.get(key);
}

/** Return the current secure settings, or {@code null} if none have been set. */
public SecureSettings getSecureSettings() {
return secureSettings.get();
Expand Down Expand Up @@ -1019,6 +1055,19 @@ public Builder putList(String setting, List<String> values) {
return this;
}

/**
* Sets the setting with the provided setting key and a list of values.
*
* @param setting The setting key
* @param values The values
* @return The builder
*/
public Builder putListOfObjects(String setting, List<Object> values) {
remove(setting);
map.put(setting, new ArrayList<>(values));
return this;
}

/**
* Sets all the provided settings including secure settings
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,53 @@ public RestInitializeExtensionAction(ExtensionsManager extensionsManager) {
this.extensionsManager = extensionsManager;
}

private static Map<String, Object> unflattenMap(Map<String, Object> flatMap) {
Map<String, Object> unflattenedMap = new HashMap<>();

for (Map.Entry<String, Object> entry : flatMap.entrySet()) {
String[] keys = entry.getKey().split("\\.");
putNested(unflattenedMap, keys, entry.getValue());
}

return unflattenedMap;
}

private static void putNested(Map<String, Object> map, String[] keys, Object value) {
for (int i = 0; i < keys.length; i++) {
String key = keys[i];

if (i == keys.length - 1) {
map.put(key, value);
} else if (keys[i + 1].matches("\\d+")) {
int index = Integer.parseInt(keys[++i]);

List<Map<String, Object>> list;
if (map.containsKey(key)) {
list = (List<Map<String, Object>>) map.get(key);
} else {
list = new ArrayList<>();
map.put(key, list);
}

while (list.size() <= index) {
list.add(new HashMap<>());
}

map = list.get(index);
} else {
Map<String, Object> nestedMap;
if (map.containsKey(key)) {
nestedMap = (Map<String, Object>) map.get(key);
} else {
nestedMap = new HashMap<>();
map.put(key, nestedMap);
}

map = nestedMap;
}
}
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
String name = null;
Expand Down Expand Up @@ -124,13 +171,26 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client
}
}

Map<String, ?> additionalSettingsMap = extensionMap.entrySet()
Map<String, Object> additionalSettingsMap = extensionMap.entrySet()
.stream()
.filter(kv -> additionalSettingsKeys.contains(kv.getKey()))
.filter(kv -> additionalSettingsKeys.stream().anyMatch(k -> {
if (k.endsWith(".")) {
return kv.getKey().startsWith(k);
} else {
return kv.getKey().equals(k);
}
}))
.collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue()));

System.out.println("additionalSettingsKeys: " + additionalSettingsKeys);
System.out.println("additionalSettingsMap: " + additionalSettingsMap);

Map<String, Object> unflattenedMap = unflattenMap(additionalSettingsMap);

System.out.println("unflattenedMap: " + unflattenedMap);

Settings.Builder output = Settings.builder();
output.loadFromMap(additionalSettingsMap);
output.loadFromMap(unflattenedMap);
extAdditionalSettings.applySettings(output.build());

// Create extension read from initialization request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,8 @@ public String executor() {
contentType,
content,
identityService.getTokenManager()
.issueOnBehalfOfToken(Map.of(StandardTokenClaims.AUDIENCE.getName(), discoveryExtensionNode.getId())) // This gets
// an
// extensions
// uniqueId
.toString(),
.issueOnBehalfOfToken(Map.of(StandardTokenClaims.AUDIENCE.getName(), discoveryExtensionNode.getId()))
.getTokenValue(), // discoveryExtensionNode.getId() is extension's unique id
httpVersion
),
restExecuteOnExtensionResponseHandler
Expand Down

0 comments on commit 8d1bcb0

Please sign in to comment.