Search in sources :

Example 1 with ULong

use of org.projectnessie.cel.common.ULong in project cel-java by projectnessie.

the class FieldDescription method getFrom.

/**
 * GetFrom returns the accessor method associated with the field on the proto generated struct.
 *
 * <p>If the field is not set, the proto default value is returned instead.
 *
 * <p>This function implements the FieldType.GetFrom function contract which can be used to
 * operate on more than just protobuf field accesses; however, the target here must be a
 * protobuf.Message.
 */
public Object getFrom(Db db, Object target) {
    if (!(target instanceof Message)) {
        throw new IllegalArgumentException(String.format("unsupported field selection target: (%s)%s", target.getClass().getName(), target));
    }
    Message v = (Message) target;
    // pbRef = v.protoReflect();
    Descriptor pbDesc = v.getDescriptorForType();
    Object fieldVal;
    FieldDescriptor fd;
    if (pbDesc == desc.getContainingType()) {
        // When the target protobuf shares the same message descriptor instance as the field
        // descriptor, use the cached field descriptor value.
        fd = desc;
    } else {
        // Otherwise, fallback to a dynamic lookup of the field descriptor from the target
        // instance as an attempt to use the cached field descriptor will result in a panic.
        fd = pbDesc.findFieldByName(name());
    }
    fieldVal = getValueFromField(fd, v);
    Class<?> fieldType = fieldVal.getClass();
    if (fd.getJavaType() != JavaType.MESSAGE || fieldType.isPrimitive() || fieldType.isEnum() || fieldType == byte[].class || fieldType == Boolean.class || fieldType == Byte.class || fieldType == Short.class || fieldType == Integer.class || fieldType == Long.class || fieldType == Float.class || fieldType == Double.class || fieldType == String.class) {
        // Fast-path return for primitive types.
        return fieldVal;
    }
    if (fieldType == ULong.class) {
        return ((ULong) fieldVal).longValue();
    }
    if (fieldVal instanceof EnumValue) {
        return (long) ((EnumValue) fieldVal).getNumber();
    }
    if (fieldVal instanceof Message) {
        return maybeUnwrapDynamic(db, (Message) fieldVal);
    }
    throw new UnsupportedOperationException("IMPLEMENT ME");
// TODO implement this
// if (field)
// switch fv := fieldVal.(type) {
// case bool, []byte, float32, float64, int32, int64, string, uint32, uint64,
// protoreflect.List:
// return fv, nil
// case protoreflect.Map:
// // Return a wrapper around the protobuf-reflected Map types which carries additional
// // information about the key and value definitions of the map.
// return &Map{Map: fv, KeyType: keyType, ValueType: valueType}, nil
// default:
// return fv, nil
// }
}
Also used : ULong(org.projectnessie.cel.common.ULong) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) EnumValue(com.google.protobuf.EnumValue) ByteString(com.google.protobuf.ByteString) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 2 with ULong

use of org.projectnessie.cel.common.ULong in project cel-java by projectnessie.

the class UintTest method uintConvertToNative_Ptr_Uint64.

@Test
void uintConvertToNative_Ptr_Uint64() {
    // 18446744073709551612 --> -4L
    ULong val = uintOf(-4L).convertToNative(ULong.class);
    assertThat(val).isEqualTo(ULong.valueOf(-4L));
}
Also used : ULong(org.projectnessie.cel.common.ULong) Test(org.junit.jupiter.api.Test)

Aggregations

ULong (org.projectnessie.cel.common.ULong)2 ByteString (com.google.protobuf.ByteString)1 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 EnumValue (com.google.protobuf.EnumValue)1 Message (com.google.protobuf.Message)1 Test (org.junit.jupiter.api.Test)1