Search in sources :

Example 16 with Any

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

the class TypeConverter method toAny.

/**
     * Performs the {@link Object} to {@link Any} mapping.
     *
     * @param value the {@link Object} value to convert
     * @param <T>   the converted object type
     * @return the packed value
     */
public static <T> Any toAny(T value) {
    checkNotNull(value);
    // Must be checked at runtime
    @SuppressWarnings("unchecked") final Class<T> srcClass = (Class<T>) value.getClass();
    final AnyCaster<T> caster = AnyCaster.forType(srcClass);
    final Message message = caster.reverse().convert(value);
    checkNotNull(message);
    final Any result = AnyPacker.pack(message);
    return result;
}
Also used : Message(com.google.protobuf.Message) Any(com.google.protobuf.Any)

Example 17 with Any

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

the class Wrapper method pack.

public Any pack(T value) {
    checkNotNull(value);
    final W wrapper = doForward(value);
    final Any result = AnyPacker.pack(wrapper);
    return result;
}
Also used : Any(com.google.protobuf.Any)

Example 18 with Any

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

the class AnyPacker method pack.

/**
     * Wraps {@link Message} object inside of {@link Any} instance.
     *
     * <p>If an instance of {@code Any} passed, this instance is returned.
     *
     * @param message the message to pack
     * @return the wrapping instance of {@link Any} or the message itself, if it is {@code Any}
     */
public static Any pack(Message message) {
    if (message instanceof Any) {
        return (Any) message;
    }
    final TypeUrl typeUrl = TypeUrl.from(message.getDescriptorForType());
    final String typeUrlPrefix = typeUrl.getPrefix();
    final Any result = Any.pack(message, typeUrlPrefix);
    return result;
}
Also used : TypeUrl(io.spine.type.TypeUrl) Any(com.google.protobuf.Any)

Example 19 with Any

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

the class Attribute method setValue.

/**
     * Sets the value of the attribute in the passed builder.
     */
public final void setValue(B builder, T value) {
    final Map<String, Any> map = getMutableMap(builder);
    final Any packed = this.pack(value);
    map.put(getName(), packed);
}
Also used : Any(com.google.protobuf.Any)

Example 20 with Any

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

the class Aggregate method toSnapshot.

/**
     * Transforms the current state of the aggregate into the {@link Snapshot} instance.
     *
     * @return new snapshot
     */
@CheckReturnValue
Snapshot toSnapshot() {
    final Any state = AnyPacker.pack(getState());
    final Snapshot.Builder builder = Snapshot.newBuilder().setState(state).setVersion(getVersion()).setTimestamp(getCurrentTime());
    return builder.build();
}
Also used : Any(com.google.protobuf.Any) CheckReturnValue(javax.annotation.CheckReturnValue)

Aggregations

Any (com.google.protobuf.Any)212 Test (org.junit.Test)88 Message (com.google.protobuf.Message)45 TypeConverter.toAny (io.spine.protobuf.TypeConverter.toAny)27 EntityRecord (io.spine.server.entity.EntityRecord)24 TypeUrl (io.spine.type.TypeUrl)15 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)11 Version (io.spine.core.Version)11 Customer (io.spine.test.commandservice.customer.Customer)11 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)10 CdsUpdate (io.grpc.xds.XdsClient.CdsUpdate)9 GivenVersion (io.spine.core.given.GivenVersion)9 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)8 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)8 CustomerId (io.spine.test.commandservice.customer.CustomerId)8 Query (io.spine.client.Query)7 Project (io.spine.test.storage.Project)7 FieldMask (com.google.protobuf.FieldMask)6 LbEndpoint (io.grpc.xds.Endpoints.LbEndpoint)6 EntityId (io.spine.client.EntityId)6