Skip to content

Commit

Permalink
Add forceLowercase in enumOf (#8724)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbenr authored Oct 14, 2024
1 parent 289561c commit 7ed548e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
},
"validity" : {
"type" : "string",
"enum" : [ "VALID", "INVALID", "OPTIMISTIC" ]
"enum" : [ "valid", "invalid", "optimistic" ]
},
"execution_block_hash" : {
"type" : "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class GetForkChoice extends RestApiEndpoint {
.withField("weight", UINT64_TYPE, ProtoNodeData::getWeight)
.withField(
"validity",
DeserializableTypeDefinition.enumOf(ProtoNodeValidationStatus.class),
DeserializableTypeDefinition.enumOf(ProtoNodeValidationStatus.class, true),
ProtoNodeData::getValidationStatus)
.withField(
"execution_block_hash",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"justified_checkpoint":{"epoch":"1","root":"0x0000000000000000000000000000000000000000000000000000000000001111"},"finalized_checkpoint":{"epoch":"0","root":"0x0000000000000000000000000000000000000000000000000000000000002222"},"fork_choice_nodes":[{"slot":"32","block_root":"0x0000000000000000000000000000000000000000000000000000000000003333","parent_root":"0x0000000000000000000000000000000000000000000000000000000000004444","justified_epoch":"10","finalized_epoch":"11","weight":"409600000000","validity":"OPTIMISTIC","execution_block_hash":"0x0000000000000000000000000000000000000000000000000000000000006666","extra_data":{"state_root":"0x0000000000000000000000000000000000000000000000000000000000005555","justified_root":"0x0000000000000000000000000000000000000000000000000000000000007777","unrealised_justified_epoch":"12","unrealized_justified_root":"0x0000000000000000000000000000000000000000000000000000000000009999","unrealised_finalized_epoch":"13","unrealized_finalized_root":"0x0000000000000000000000000000000000000000000000000000000000000000"}}],"extra_data":{}}
{"justified_checkpoint":{"epoch":"1","root":"0x0000000000000000000000000000000000000000000000000000000000001111"},"finalized_checkpoint":{"epoch":"0","root":"0x0000000000000000000000000000000000000000000000000000000000002222"},"fork_choice_nodes":[{"slot":"32","block_root":"0x0000000000000000000000000000000000000000000000000000000000003333","parent_root":"0x0000000000000000000000000000000000000000000000000000000000004444","justified_epoch":"10","finalized_epoch":"11","weight":"409600000000","validity":"optimistic","execution_block_hash":"0x0000000000000000000000000000000000000000000000000000000000006666","extra_data":{"state_root":"0x0000000000000000000000000000000000000000000000000000000000005555","justified_root":"0x0000000000000000000000000000000000000000000000000000000000007777","unrealised_justified_epoch":"12","unrealized_justified_root":"0x0000000000000000000000000000000000000000000000000000000000009999","unrealised_finalized_epoch":"13","unrealized_finalized_root":"0x0000000000000000000000000000000000000000000000000000000000000000"}}],"extra_data":{}}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ static <TKey, TValue> DeserializableTypeDefinition<Map<TKey, TValue>> mapOf(

static <TObject extends Enum<TObject>> EnumTypeDefinition<TObject> enumOf(
final Class<TObject> itemType) {
return new EnumTypeDefinition.EnumTypeBuilder<>(itemType).build();
return new EnumTypeDefinition.EnumTypeBuilder<>(itemType, false).build();
}

static <TObject extends Enum<TObject>> EnumTypeDefinition<TObject> enumOf(
final Class<TObject> itemType, final boolean forceLowercase) {
return new EnumTypeDefinition.EnumTypeBuilder<>(itemType, forceLowercase).build();
}

static <TObject> DeserializableObjectTypeDefinitionBuilder<TObject, TObject> object(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.fasterxml.jackson.core.JsonParser;
import java.io.IOException;
import java.util.HashSet;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -135,8 +136,13 @@ public EnumTypeBuilder(final Class<T> itemType, final Function<T, String> serial
}

public EnumTypeBuilder(final Class<T> itemType) {
this.itemType = itemType;
this.serializer = Enum::toString;
this(itemType, false);
}

public EnumTypeBuilder(final Class<T> itemType, final boolean forceLowercase) {
this(
itemType,
forceLowercase ? (val) -> val.toString().toLowerCase(Locale.ROOT) : Enum::toString);
}

public EnumTypeBuilder<T> parser(final Class<T> itemType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ void shouldSerializeEnum() throws Exception {
assertThat(json).isEqualTo("\"yes\"");
}

@Test
void shouldSerializeEnumForceLowerCase() throws Exception {
DeserializableTypeDefinition<YES_NO> definition =
DeserializableTypeDefinition.enumOf(YES_NO.class);
String json = JsonUtil.serialize(YES_NO.YES, definition);
assertThat(json).isEqualTo("\"YES\"");

definition = DeserializableTypeDefinition.enumOf(YES_NO.class, true);
json = JsonUtil.serialize(YES_NO.YES, definition);
assertThat(json).isEqualTo("\"yes\"");
}

@Test
void shouldParseEnum() throws Exception {
assertThat(JsonUtil.parse("\"no\"", definition)).isEqualTo(YesNo.NO);
Expand Down Expand Up @@ -73,4 +85,10 @@ public String toString() {
return displayName;
}
}

@SuppressWarnings("JavaCase")
private enum YES_NO {
YES,
NO
}
}

0 comments on commit 7ed548e

Please sign in to comment.