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

Add forceLowercase in enumOf #8724

Merged
merged 6 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -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 @@ -234,7 +234,7 @@ public class BeaconRestApiTypes {
"Array of indices for blob sidecars to request for in the specified block. Returns all blob sidecars in the block if not specified."));

private static final EnumTypeDefinition<BroadcastValidationParameter> BROADCAST_VALIDATION_VALUE =
new EnumTypeDefinition.EnumTypeBuilder<>(BroadcastValidationParameter.class)
new EnumTypeDefinition.EnumTypeBuilder<>(BroadcastValidationParameter.class, false)
.example("consensus_and_equivocation")
.description(PARAM_BROADCAST_VALIDATION_DESCRIPTION)
.format("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 @@ -134,9 +135,10 @@ public EnumTypeBuilder(final Class<T> itemType, final Function<T, String> serial
this.serializer = serializer;
}

public EnumTypeBuilder(final Class<T> itemType) {
tbenr marked this conversation as resolved.
Show resolved Hide resolved
public EnumTypeBuilder(final Class<T> itemType, final boolean forceLowercase) {
this.itemType = itemType;
this.serializer = Enum::toString;
this.serializer =
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
}
}