Search in sources :

Example 56 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 57 with Message

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

the class FieldMasks method applyMask.

/**
     * Applies the given {@code FieldMask} to given collection of {@link Message}s.
     * Does not change the {@link Collection} itself.
     *
     * <p>In case the {@code FieldMask} instance contains invalid field declarations, they are
     * ignored and do not affect the execution result.
     *
     * @param mask     {@code FieldMask} to apply to each item of the input {@link Collection}.
     * @param messages {@link Message}s to filter.
     * @param type     type of the {@link Message}s.
     * @return messages with the {@code FieldMask} applied
     */
@Nonnull
public static <M extends Message, B extends Message.Builder> Collection<M> applyMask(FieldMask mask, Collection<M> messages, TypeUrl type) {
    checkNotNull(mask);
    checkNotNull(messages);
    checkNotNull(type);
    final List<M> filtered = new LinkedList<>();
    final ProtocolStringList filter = mask.getPathsList();
    final Class<B> builderClass = getBuilderForType(type);
    if (filter.isEmpty() || builderClass == null) {
        return Collections.unmodifiableCollection(messages);
    }
    try {
        final Constructor<B> builderConstructor = builderClass.getDeclaredConstructor();
        builderConstructor.setAccessible(true);
        for (Message wholeMessage : messages) {
            final M message = messageForFilter(filter, builderConstructor, wholeMessage);
            filtered.add(message);
        }
    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
        // If any reflection failure happens, return all the data without any mask applied.
        log().warn(format(CONSTRUCTOR_INVOCATION_ERROR_LOGGING_PATTERN, builderClass.getCanonicalName()), e);
        return Collections.unmodifiableCollection(messages);
    }
    return Collections.unmodifiableList(filtered);
}
Also used : Message(com.google.protobuf.Message) LinkedList(java.util.LinkedList) ProtocolStringList(com.google.protobuf.ProtocolStringList) InvocationTargetException(java.lang.reflect.InvocationTargetException) Nonnull(javax.annotation.Nonnull)

Example 58 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 59 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 60 with Message

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

the class EventException method getEventMessage.

/**
     * Returns a related event message.
     */
public Message getEventMessage() {
    if (eventMessage instanceof Any) {
        final Any any = (Any) eventMessage;
        Message unpacked = AnyPacker.unpack(any);
        return unpacked;
    }
    return eventMessage;
}
Also used : Message(com.google.protobuf.Message) Any(com.google.protobuf.Any)

Aggregations

Message (com.google.protobuf.Message)145 Test (org.junit.Test)47 Any (com.google.protobuf.Any)25 Event (io.spine.base.Event)11 ByteString (com.google.protobuf.ByteString)9 Command (io.spine.base.Command)9 EntityRecord (io.spine.server.entity.EntityRecord)8 EntityFilters (io.spine.client.EntityFilters)7 Target (io.spine.client.Target)6 TypeUrl (io.spine.type.TypeUrl)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 CommandId (io.spine.base.CommandId)5 ByteArray (voldemort.utils.ByteArray)5 FieldMask (com.google.protobuf.FieldMask)4 GeneratedMessage (com.google.protobuf.GeneratedMessage)4 Timestamp (com.google.protobuf.Timestamp)4 EventContext (io.spine.base.EventContext)4 EntityId (io.spine.client.EntityId)4 EnclosedMessageFieldValueWithCustomInvalidMessage (io.spine.test.validate.msg.EnclosedMessageFieldValueWithCustomInvalidMessage)4