Skip to content

Commit

Permalink
Connector API: Followup on #106060 (#107058)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedrazb authored Apr 4, 2024
1 parent 7b25421 commit a32512f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg;
Expand All @@ -45,10 +46,14 @@ public class ConnectorConfiguration implements Writeable, ToXContentObject {

@Nullable
private final String category;
@Nullable
private final Object defaultValue;
@Nullable
private final List<ConfigurationDependency> dependsOn;
@Nullable
private final ConfigurationDisplayType display;
private final String label;
@Nullable
private final List<ConfigurationSelectOption> options;
@Nullable
private final Integer order;
Expand All @@ -58,9 +63,13 @@ public class ConnectorConfiguration implements Writeable, ToXContentObject {
private final boolean sensitive;
@Nullable
private final String tooltip;
@Nullable
private final ConfigurationFieldType type;
@Nullable
private final List<String> uiRestrictions;
@Nullable
private final List<ConfigurationValidation> validations;
@Nullable
private final Object value;

/**
Expand Down Expand Up @@ -380,41 +389,38 @@ public void writeTo(StreamOutput out) throws IOException {

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
if (category != null) {
map.put(CATEGORY_FIELD.getPreferredName(), category);
}

Optional.ofNullable(category).ifPresent(c -> map.put(CATEGORY_FIELD.getPreferredName(), c));
map.put(DEFAULT_VALUE_FIELD.getPreferredName(), defaultValue);
if (dependsOn != null) {
map.put(DEPENDS_ON_FIELD.getPreferredName(), dependsOn.stream().map(ConfigurationDependency::toMap).toList());
}
if (display != null) {
map.put(DISPLAY_FIELD.getPreferredName(), display.toString());
}

Optional.ofNullable(dependsOn)
.ifPresent(d -> map.put(DEPENDS_ON_FIELD.getPreferredName(), d.stream().map(ConfigurationDependency::toMap).toList()));

Optional.ofNullable(display).ifPresent(d -> map.put(DISPLAY_FIELD.getPreferredName(), d.toString()));

map.put(LABEL_FIELD.getPreferredName(), label);
if (options != null) {
map.put(OPTIONS_FIELD.getPreferredName(), options.stream().map(ConfigurationSelectOption::toMap).toList());
}
if (order != null) {
map.put(ORDER_FIELD.getPreferredName(), order);
}
if (placeholder != null) {
map.put(PLACEHOLDER_FIELD.getPreferredName(), placeholder);
}

Optional.ofNullable(options)
.ifPresent(o -> map.put(OPTIONS_FIELD.getPreferredName(), o.stream().map(ConfigurationSelectOption::toMap).toList()));

Optional.ofNullable(order).ifPresent(o -> map.put(ORDER_FIELD.getPreferredName(), o));

Optional.ofNullable(placeholder).ifPresent(p -> map.put(PLACEHOLDER_FIELD.getPreferredName(), p));

map.put(REQUIRED_FIELD.getPreferredName(), required);
map.put(SENSITIVE_FIELD.getPreferredName(), sensitive);
if (tooltip != null) {
map.put(TOOLTIP_FIELD.getPreferredName(), tooltip);
}
if (type != null) {
map.put(TYPE_FIELD.getPreferredName(), type.toString());
}
if (uiRestrictions != null) {
map.put(UI_RESTRICTIONS_FIELD.getPreferredName(), uiRestrictions);
}
if (validations != null) {
map.put(VALIDATIONS_FIELD.getPreferredName(), validations.stream().map(ConfigurationValidation::toMap).toList());
}

Optional.ofNullable(tooltip).ifPresent(t -> map.put(TOOLTIP_FIELD.getPreferredName(), t));

Optional.ofNullable(type).ifPresent(t -> map.put(TYPE_FIELD.getPreferredName(), t.toString()));

Optional.ofNullable(uiRestrictions).ifPresent(u -> map.put(UI_RESTRICTIONS_FIELD.getPreferredName(), u));

Optional.ofNullable(validations)
.ifPresent(v -> map.put(VALIDATIONS_FIELD.getPreferredName(), v.stream().map(ConfigurationValidation::toMap).toList()));

map.put(VALUE_FIELD.getPreferredName(), value);

return map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

Expand Down Expand Up @@ -103,10 +102,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}

public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
map.put(CONSTRAINT_FIELD.getPreferredName(), constraint);
map.put(TYPE_FIELD.getPreferredName(), type.toString());
return map;
return Map.of(CONSTRAINT_FIELD.getPreferredName(), constraint, TYPE_FIELD.getPreferredName(), type.toString());
}

public static ConfigurationValidation fromXContent(XContentParser parser) throws IOException {
Expand Down

0 comments on commit a32512f

Please sign in to comment.