Search in sources :

Example 1 with EnumValue

use of com.google.protobuf.EnumValue 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)

Aggregations

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 ULong (org.projectnessie.cel.common.ULong)1