Skip to content

Commit

Permalink
implementing OptionalFloatThriftCodec and float coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
divyatalesra-2001 committed Feb 19, 2025
1 parent d6eed85 commit 690c432
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.facebook.drift.codec.internal.builtin.LongThriftCodec;
import com.facebook.drift.codec.internal.builtin.MapThriftCodec;
import com.facebook.drift.codec.internal.builtin.OptionalDoubleThriftCodec;
import com.facebook.drift.codec.internal.builtin.OptionalFloatThriftCodec;
import com.facebook.drift.codec.internal.builtin.OptionalIntThriftCodec;
import com.facebook.drift.codec.internal.builtin.OptionalLongThriftCodec;
import com.facebook.drift.codec.internal.builtin.OptionalThriftCodec;
Expand Down Expand Up @@ -186,6 +187,7 @@ public ThriftCodec<?> load(ThriftType type)
addBuiltinCodec(new DoubleArrayThriftCodec());
addBuiltinCodec(new FloatArrayThriftCodec());
addBuiltinCodec(new OptionalDoubleThriftCodec());
addBuiltinCodec(new OptionalFloatThriftCodec());
addBuiltinCodec(new OptionalIntThriftCodec());
addBuiltinCodec(new OptionalLongThriftCodec());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2017 Facebook, Inc.
*
* Licensed 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 com.facebook.drift.codec.internal.builtin;

import com.facebook.drift.codec.ThriftCodec;
import com.facebook.drift.codec.metadata.ThriftType;
import com.facebook.drift.protocol.TProtocolReader;
import com.facebook.drift.protocol.TProtocolWriter;

import javax.annotation.concurrent.Immutable;

import java.util.Optional;

import static java.util.Objects.requireNonNull;

@Immutable
public class OptionalFloatThriftCodec
implements ThriftCodec<Optional<Float>>
{
@Override
public ThriftType getType()
{
return new ThriftType(ThriftType.FLOAT, new TypeToken<Optional<Float>>() {}.getType(), Optional.empty());
}

@Override
public Optional<Float> read(TProtocolReader protocol)
throws Exception
{
requireNonNull(protocol, "protocol is null");
return Optional.of(protocol.readFloat());
}

@Override
public void write(Optional<Float> value, TProtocolWriter protocol)
throws Exception
{
requireNonNull(value, "value is null");
requireNonNull(protocol, "protocol is null");

// write can not be called with a missing value, and instead the write should be skipped
// after check the result from isNull
protocol.writeFloat(value.orElseThrow((() -> new IllegalArgumentException("value is not present"))));
}

@Override
public boolean isNull(Optional<Float> value)
{
return value == null || !value.isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ public static long boxedLongToLong(Long value)
return value;
}

@FromThrift
public static Float floatToBoxedFloat(float value)
{
return value;
}

@ToThrift
public static float boxedFloatToFloat(Float value)
{
return value;
}

@FromThrift
public static float doubleToPrimitiveFloat(double value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,10 @@ public static ParameterizedType toParameterizedType(ThriftTypeReference typeRef)
}

if (ReflectionHelper.isOptional(typeRef.getJavaType())) {
return type(Optional.class, toParameterizedType(typeRef.get().getValueTypeReference()));
ThriftType valueType = typeRef.get().getValueTypeReference().get();
if (!valueType.equals(ThriftType.FLOAT)) {
return type(Optional.class, toParameterizedType(typeRef.get().getValueTypeReference()));
}
}

switch (typeRef.getProtocolType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.facebook.drift.annotations.ThriftUnion;
import com.facebook.drift.codec.ThriftProtocolType;
import com.facebook.drift.codec.internal.builtin.OptionalDoubleThriftCodec;
import com.facebook.drift.codec.internal.builtin.OptionalFloatThriftCodec;
import com.facebook.drift.codec.internal.builtin.OptionalIntThriftCodec;
import com.facebook.drift.codec.internal.builtin.OptionalLongThriftCodec;
import com.facebook.drift.codec.internal.coercion.DefaultJavaCoercions;
Expand Down Expand Up @@ -116,6 +117,7 @@ public ThriftCatalog(Monitor monitor)
this.monitor = monitor;
addDefaultCoercions(DefaultJavaCoercions.class);
addThriftType(new OptionalDoubleThriftCodec().getType());
addThriftType(new OptionalFloatThriftCodec().getType());
addThriftType(new OptionalIntThriftCodec().getType());
addThriftType(new OptionalLongThriftCodec().getType());
}
Expand Down

0 comments on commit 690c432

Please sign in to comment.