Search in sources :

Example 11 with Message

use of com.google.protobuf.Message in project core-java by SpineEventEngine.

the class TypeConverterShould method checkMapping.

private static void checkMapping(Object javaObject, Message protoObject) {
    final Any wrapped = AnyPacker.pack(protoObject);
    final Object mappedJavaObject = TypeConverter.toObject(wrapped, javaObject.getClass());
    assertEquals(javaObject, mappedJavaObject);
    final Any restoredWrapped = TypeConverter.toAny(mappedJavaObject);
    final Message restored = AnyPacker.unpack(restoredWrapped);
    assertEquals(protoObject, restored);
}
Also used : Message(com.google.protobuf.Message) Any(com.google.protobuf.Any)

Example 12 with Message

use of com.google.protobuf.Message in project core-java by SpineEventEngine.

the class IdSetFunctions method put.

/**
     * Puts a function into the map.
     *
     * @param eventClass the class of the event handled by the function
     * @param func       the function instance
     * @param <E>        the type of the event message
     */
public <E extends Message> void put(Class<E> eventClass, IdSetEventFunction<I, E> func) {
    final EventClass clazz = EventClass.of(eventClass);
    @SuppressWarnings("unchecked") final IdSetEventFunction<I, Message> // since we want to store {@code IdSetFunction}s for various event types.
    casted = (IdSetEventFunction<I, Message>) func;
    map.put(clazz, casted);
}
Also used : EventClass(io.spine.type.EventClass) Message(com.google.protobuf.Message) IdSetEventFunction(io.spine.server.entity.idfunc.IdSetEventFunction)

Example 13 with Message

use of com.google.protobuf.Message in project core-java by SpineEventEngine.

the class IdSetFunctions method get.

/**
     * Obtains a function for the passed event class.
     *
     * @param eventClass the class of the event message
     * @param <E>        the type of the event message
     * @return the function wrapped into {@code Optional} or empty {@code Optional}
     * if there is no matching function
     */
public <E extends Message> Optional<IdSetEventFunction<I, E>> get(Class<E> eventClass) {
    final EventClass clazz = EventClass.of(eventClass);
    final IdSetEventFunction<I, Message> func = map.get(clazz);
    // we ensure the type when we put into the map.
    @SuppressWarnings("unchecked") final IdSetEventFunction<I, E> result = (IdSetEventFunction<I, E>) func;
    return Optional.fromNullable(result);
}
Also used : EventClass(io.spine.type.EventClass) Message(com.google.protobuf.Message) IdSetEventFunction(io.spine.server.entity.idfunc.IdSetEventFunction)

Example 14 with Message

use of com.google.protobuf.Message in project core-java by SpineEventEngine.

the class MatchFilter method checkFields.

private static boolean checkFields(Message object, FieldFilter filter) {
    final Optional<Field> fieldOptional = Field.forFilter(object.getClass(), filter);
    if (!fieldOptional.isPresent()) {
        return false;
    }
    final Field field = fieldOptional.get();
    final Optional<Message> value;
    try {
        value = field.getValue(object);
    } catch (IllegalStateException ignored) {
        // Wrong Message class -> does not satisfy the criteria.
        return false;
    }
    final Collection<Any> expectedAnys = filter.getValueList();
    final Collection<Message> expectedValues = Collections2.transform(expectedAnys, unpackFunc());
    if (!value.isPresent()) {
        /* If there is no value in the field return `true`
               if the list of required values is also empty. */
        final boolean nothingIsExpected = expectedValues.isEmpty();
        return nothingIsExpected;
    }
    final boolean result = expectedValues.contains(value.get());
    return result;
}
Also used : Field(io.spine.server.reflect.Field) Message(com.google.protobuf.Message) Any(com.google.protobuf.Any)

Example 15 with Message

use of com.google.protobuf.Message in project core-java by SpineEventEngine.

the class Sample method messageValueFor.

private static Message messageValueFor(FieldDescriptor field) {
    final TypeUrl messageType = TypeUrl.from(field.getMessageType());
    final Class<? extends Message> javaClass = classFor(messageType);
    final Message fieldValue = messageOfType(javaClass);
    return fieldValue;
}
Also used : Message(com.google.protobuf.Message) TypeUrl(io.spine.type.TypeUrl)

Aggregations

Message (com.google.protobuf.Message)266 Test (org.junit.Test)84 Any (com.google.protobuf.Any)30 ByteString (com.google.protobuf.ByteString)17 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)15 Command (io.spine.core.Command)14 DynamicMessage (com.google.protobuf.DynamicMessage)13 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)10 EntityRecord (io.spine.server.entity.EntityRecord)10 Test (org.junit.jupiter.api.Test)9 HeronTuples (com.twitter.heron.proto.system.HeronTuples)8 Event (io.spine.core.Event)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 Method (java.lang.reflect.Method)8 Event (io.spine.base.Event)7 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)7 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)7 Descriptor (com.google.protobuf.Descriptors.Descriptor)6 Builder (com.google.protobuf.Message.Builder)5