Skip to content

Commit

Permalink
Better Enum handling
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <widdis@gmail.com>
  • Loading branch information
dbwiddis committed Sep 16, 2022
1 parent 30cbc50 commit 1e8d26d
Showing 1 changed file with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.opensearch.common.unit.ByteSizeValue;
import org.opensearch.common.unit.TimeValue;
import java.io.IOException;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -72,7 +73,7 @@ public WriteableSetting(Setting<?> setting, WriteableSettingGenericType type) {
*/
public WriteableSetting(StreamInput in) throws IOException {
// Read the type
this.type = WriteableSettingGenericType.valueOf(in.readString());
this.type = in.readEnum(WriteableSettingGenericType.class);
// Read the key
String key = in.readString();
// Read the default value
Expand All @@ -86,13 +87,9 @@ public WriteableSetting(StreamInput in) throws IOException {
// We are using known types so don't need the parser
// We are not using validator
// Read properties
int size = in.readVInt();
Property[] propArray = new Property[size];
for (int i = 0; i < size; i++) {
propArray[i] = Property.valueOf(in.readString());
}
EnumSet<Property> propSet = in.readEnumSet(Property.class);
// Put it all in a setting object
this.setting = createSetting(type, key, defaultValue, fallback, propArray);
this.setting = createSetting(type, key, defaultValue, fallback, propSet.toArray(Property[]::new));
}

private static WriteableSettingGenericType getGenericTypeFromDefault(Setting<?> setting) {
Expand Down Expand Up @@ -180,7 +177,7 @@ private Setting<?> createSetting(
@Override
public void writeTo(StreamOutput out) throws IOException {
// Write the type
out.writeString(type.name());
out.writeEnum(type);
// Write the key
out.writeString(setting.getKey());
// Write the default value
Expand All @@ -194,10 +191,7 @@ public void writeTo(StreamOutput out) throws IOException {
// We are using known types so don't need the parser
// We are not using validator
// Write properties
out.writeVInt(setting.getProperties().size());
for (Property prop : setting.getProperties()) {
out.writeString(prop.name());
}
out.writeEnumSet(setting.getProperties());
}

private void writeDefaultValue(StreamOutput out, Object defaultValue) throws IOException {
Expand Down

0 comments on commit 1e8d26d

Please sign in to comment.