Skip to content

Commit

Permalink
chore(java): rename deserializeUnexistentEnumValueAsNull to deseriali…
Browse files Browse the repository at this point in the history
…zeNonexistentAsNull (#1634)

## What does this PR do?

rename deserializeUnexistentEnumValueAsNull to
deserializeNonexistentAsNull

## Related issues


## Does this PR introduce any user-facing change?

<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/incubator-fury/issues/new/choose)
describing the need to do so and update the document if necessary.
-->

- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?


## Benchmark

<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->
  • Loading branch information
chaokunyang authored May 15, 2024
1 parent b904639 commit 33e5dad
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class Config implements Serializable {
private final boolean deserializeUnexistedClass;
private final boolean scalaOptimizationEnabled;
private transient int configHash;
private final boolean deserializeUnexistentEnumValueAsNull;
private final boolean deserializeNonexistentEnumValueAsNull;

public Config(FuryBuilder builder) {
language = builder.language;
Expand Down Expand Up @@ -83,7 +83,7 @@ public Config(FuryBuilder builder) {
}
asyncCompilationEnabled = builder.asyncCompilationEnabled;
scalaOptimizationEnabled = builder.scalaOptimizationEnabled;
deserializeUnexistentEnumValueAsNull = builder.deserializeUnexistentEnumValueAsNull;
deserializeNonexistentEnumValueAsNull = builder.deserializeNonexistentEnumValueAsNull;
}

public Language getLanguage() {
Expand All @@ -103,8 +103,8 @@ public boolean isStringRefIgnored() {
}

/** ignore Enum Deserialize array out of bounds return null. */
public boolean deserializeUnexistentEnumValueAsNull() {
return deserializeUnexistentEnumValueAsNull;
public boolean deserializeNonexistentEnumValueAsNull() {
return deserializeNonexistentEnumValueAsNull;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public final class FuryBuilder {
boolean registerGuavaTypes = true;
boolean scalaOptimizationEnabled = false;
boolean suppressClassRegistrationWarnings = true;
boolean deserializeUnexistentEnumValueAsNull = false;
boolean deserializeNonexistentEnumValueAsNull = false;

public FuryBuilder() {}

Expand Down Expand Up @@ -107,9 +107,9 @@ public FuryBuilder ignoreStringRef(boolean ignoreStringRef) {
}

/** ignore Enum Deserialize array out of bounds. */
public FuryBuilder deserializeUnexistentEnumValueAsNull(
boolean deserializeUnexistentEnumValueAsNull) {
this.deserializeUnexistentEnumValueAsNull = deserializeUnexistentEnumValueAsNull;
public FuryBuilder deserializeNonexistentEnumValueAsNull(
boolean deserializeNonexistentEnumValueAsNull) {
this.deserializeNonexistentEnumValueAsNull = deserializeNonexistentEnumValueAsNull;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.fury.serializer;

import java.util.Arrays;
import org.apache.fury.Fury;
import org.apache.fury.memory.MemoryBuffer;
import org.apache.fury.util.Preconditions;
Expand Down Expand Up @@ -49,9 +50,18 @@ public void write(MemoryBuffer buffer, Enum value) {
@Override
public Enum read(MemoryBuffer buffer) {
int value = buffer.readVarUint32Small7();
if (fury.getConfig().deserializeUnexistentEnumValueAsNull() && value >= enumConstants.length) {
return null;
if (value >= enumConstants.length) {
return handleNonexistentEnumValue(value);
}
return enumConstants[value];
}

private Enum handleNonexistentEnumValue(int value) {
if (fury.getConfig().deserializeNonexistentEnumValueAsNull()) {
return null;
} else {
throw new IllegalArgumentException(
String.format("Enum ordinal %s not in %s", value, Arrays.toString(enumConstants)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testEnumSerializationUnexistentEnumValueAsNull() {
.withLanguage(Language.JAVA)
.withRefTracking(true)
.requireClassRegistration(false)
.deserializeUnexistentEnumValueAsNull(true)
.deserializeNonexistentEnumValueAsNull(true)
.withClassLoader(cls2.getClassLoader());
Fury furyDeserialize = builderDeserialize.build();
Fury furySerialization = builderSerialization.build();
Expand Down

0 comments on commit 33e5dad

Please sign in to comment.