Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -681,6 +681,8 @@ private static boolean isSupported(TypeRef<?> typeRef, TypeResolutionContext ctx
}
Tuple2<TypeRef<?>, TypeRef<?>> mapKeyValueType = getMapKeyValueType(typeRef);
return isSupported(mapKeyValueType.f0) && isSupported(mapKeyValueType.f1);
} else if (cls.isEnum()) {
return true;
} else {
ctx.checkNoCycle(typeRef);
return isBean(typeRef, ctx.appendTypePath(typeRef));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public static <T> RowEncoder<T> bean(Class<T> beanClass, BinaryRowWriter writer)
* <li>primitive types: boolean, int, double, etc.
* <li>boxed types: Boolean, Integer, Double, etc.
* <li>String
* <li>Enum (as String)
* <li>java.math.BigDecimal, java.math.BigInteger
* <li>time related: java.sql.Date, java.sql.Timestamp, java.time.LocalDate, java.time.Instant
* <li>Optional and friends: OptionalInt, OptionalLong, OptionalDouble
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public static String getElemAccessMethodName(TypeRef<?> type, TypeResolutionCont
return "getMap";
} else if (TypeUtils.isBean(type, ctx)) {
return "getStruct";
} else if (type.getRawType().isEnum()) {
return "getString";
} else {
// take unknown type as OBJECT_TYPE, return as sliced MemoryBuffer
// slice MemoryBuffer, then deserialize in EncodeExpressionBuilder.deserializeFor
Expand Down Expand Up @@ -98,6 +100,8 @@ public static TypeRef<?> getElemReturnType(TypeRef<?> type, TypeResolutionContex
return TypeRef.of(BinaryMap.class);
} else if (TypeUtils.isBean(type, ctx)) {
return TypeRef.of(BinaryRow.class);
} else if (type.getRawType().isEnum()) {
return TypeUtils.STRING_TYPE;
} else {
// take unknown type as OBJECT_TYPE, return as sliced MemoryBuffer
// slice MemoryBuffer, then deserialize in EncodeExpressionBuilder.deserializeFor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fury.format.encoder;

import java.util.Optional;
import lombok.Data;
import org.apache.fury.format.row.binary.BinaryRow;
import org.apache.fury.memory.MemoryBuffer;
import org.apache.fury.memory.MemoryUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

public class EnumTest {
public enum TestEnum {
A,
B
}

@Data
public static class EnumValue {
public TestEnum f1;
public Optional<TestEnum> f2;

public EnumValue() {}
}

@Test
public void testEnumPresent() {
EnumValue v = new EnumValue();
v.f1 = TestEnum.B;
v.f2 = Optional.of(TestEnum.A);
RowEncoder<EnumValue> encoder = Encoders.bean(EnumValue.class);
BinaryRow row = encoder.toRow(v);
MemoryBuffer buffer = MemoryUtils.wrap(row.toBytes());
row.pointTo(buffer, 0, buffer.size());
EnumValue deserializedV = encoder.fromRow(row);
Assert.assertEquals(v, deserializedV);
}

@Test
public void testEnumAbsent() {
EnumValue v = new EnumValue();
v.f1 = TestEnum.A;
v.f2 = Optional.empty();
RowEncoder<EnumValue> encoder = Encoders.bean(EnumValue.class);
BinaryRow row = encoder.toRow(v);
MemoryBuffer buffer = MemoryUtils.wrap(row.toBytes());
row.pointTo(buffer, 0, buffer.size());
EnumValue deserializedV = encoder.fromRow(row);
Assert.assertEquals(v, deserializedV);
}
}
Loading