io.grpc.MethodDescriptor.Marshaller Java Examples

The following examples show how to use io.grpc.MethodDescriptor.Marshaller. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BinlogHelper.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
<T> void logRpcMessage(
    long seq,
    EventType eventType,
    Marshaller<T> marshaller,
    T message,
    GrpcLogEntry.Logger logger,
    long callId) {
  Preconditions.checkArgument(
      eventType == EventType.EVENT_TYPE_CLIENT_MESSAGE
          || eventType == EventType.EVENT_TYPE_SERVER_MESSAGE,
      "event type must correspond to client message or server message");
  if (marshaller != BYTEARRAY_MARSHALLER) {
    throw new IllegalStateException("Expected the BinaryLog's ByteArrayMarshaller");
  }
  MaybeTruncated<Message.Builder> pair = createMessageProto((byte[]) message, maxMessageBytes);
  GrpcLogEntry.Builder entryBuilder = newTimestampedBuilder()
      .setSequenceIdWithinCall(seq)
      .setType(eventType)
      .setMessage(pair.proto)
      .setPayloadTruncated(pair.truncated)
      .setLogger(logger)
      .setCallId(callId);
  sink.write(entryBuilder.build());
}
 
Example #2
Source File: BinlogHelper.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Override
<T> void logRpcMessage(
    long seq,
    EventType eventType,
    Marshaller<T> marshaller,
    T message,
    GrpcLogEntry.Logger logger,
    long callId) {
  Preconditions.checkArgument(
      eventType == EventType.EVENT_TYPE_CLIENT_MESSAGE
          || eventType == EventType.EVENT_TYPE_SERVER_MESSAGE,
      "event type must correspond to client message or server message");
  if (marshaller != BYTEARRAY_MARSHALLER) {
    throw new IllegalStateException("Expected the BinaryLog's ByteArrayMarshaller");
  }
  MaybeTruncated<Message.Builder> pair = createMessageProto((byte[]) message, maxMessageBytes);
  GrpcLogEntry.Builder entryBuilder = newTimestampedBuilder()
      .setSequenceIdWithinCall(seq)
      .setType(eventType)
      .setMessage(pair.proto)
      .setPayloadTruncated(pair.truncated)
      .setLogger(logger)
      .setCallId(callId);
  sink.write(entryBuilder.build());
}
 
Example #3
Source File: MethodDescriptorTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getServiceName_returnsNull() {
  Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
  MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
      .setType(MethodType.UNARY)
      .setFullMethodName("foo-bar")
      .build();

  assertNull(md.getServiceName());
}
 
Example #4
Source File: GrpcJsonUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Nullable
private static Message marshallerPrototype(Marshaller<?> marshaller) {
    if (marshaller instanceof PrototypeMarshaller) {
        final Object prototype = ((PrototypeMarshaller<?>) marshaller).getMessagePrototype();
        if (prototype instanceof Message) {
            return (Message) prototype;
        }
    }
    return null;
}
 
Example #5
Source File: ProtoLiteUtilsTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseFromKnowLengthInputStream() throws Exception {
  Marshaller<Type> marshaller = ProtoLiteUtils.marshaller(Type.getDefaultInstance());
  Type expect = Type.newBuilder().setName("expected name").build();

  Type result = marshaller.parse(new CustomKnownLengthInputStream(expect.toByteArray()));
  assertEquals(expect, result);
}
 
Example #6
Source File: DefaultJsonMarshaller.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T deserializeMessage(Marshaller<T> marshaller, InputStream is) throws IOException {
    final PrototypeMarshaller<T> prototypeMarshaller = (PrototypeMarshaller<T>) marshaller;
    final Message prototype = (Message) prototypeMarshaller.getMessagePrototype();
    final Message.Builder builder = prototype.newBuilderForType();
    delegate.mergeValue(is, builder);
    @SuppressWarnings("unchecked")
    final T cast = (T) builder.build();
    return cast;
}
 
Example #7
Source File: ProtoLiteUtilsTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmpty() throws IOException {
  Marshaller<Empty> marshaller = ProtoLiteUtils.marshaller(Empty.getDefaultInstance());
  InputStream is = marshaller.stream(Empty.getDefaultInstance());
  assertEquals(0, is.available());
  byte[] b = new byte[10];
  assertEquals(-1, is.read(b));
  assertArrayEquals(new byte[10], b);
  // Do the same thing again, because the internal state may be different
  assertEquals(-1, is.read(b));
  assertArrayEquals(new byte[10], b);
  assertEquals(-1, is.read());
  assertEquals(0, is.available());
}
 
Example #8
Source File: BinlogHelper.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * Logs the message message. The number of bytes logged is determined by the binary
 * logging configuration.
 */
abstract <T> void logRpcMessage(
    long seq,
    EventType eventType,
    Marshaller<T> marshaller,
    T message,
    GrpcLogEntry.Logger logger,
    long callId);
 
Example #9
Source File: MethodDescriptorTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getServiceName_extractsService() {
  Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
  MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
      .setType(MethodType.UNARY)
      .setFullMethodName("foo/bar")
      .build();

  assertEquals("foo", md.getServiceName());
}
 
Example #10
Source File: BinlogHelper.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
/**
 * Logs the message message. The number of bytes logged is determined by the binary
 * logging configuration.
 */
abstract <T> void logRpcMessage(
    long seq,
    EventType eventType,
    Marshaller<T> marshaller,
    T message,
    GrpcLogEntry.Logger logger,
    long callId);
 
Example #11
Source File: ProtoLiteUtilsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void parseFromKnowLengthInputStream() throws Exception {
  Marshaller<Type> marshaller = ProtoLiteUtils.marshaller(Type.getDefaultInstance());
  Type expect = Type.newBuilder().setName("expected name").build();

  Type result = marshaller.parse(new CustomKnownLengthInputStream(expect.toByteArray()));
  assertEquals(expect, result);
}
 
Example #12
Source File: ProtoLiteUtilsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmpty() throws IOException {
  Marshaller<Empty> marshaller = ProtoLiteUtils.marshaller(Empty.getDefaultInstance());
  InputStream is = marshaller.stream(Empty.getDefaultInstance());
  assertEquals(0, is.available());
  byte[] b = new byte[10];
  assertEquals(-1, is.read(b));
  assertArrayEquals(new byte[10], b);
  // Do the same thing again, because the internal state may be different
  assertEquals(-1, is.read(b));
  assertArrayEquals(new byte[10], b);
  assertEquals(-1, is.read());
  assertEquals(0, is.available());
}
 
Example #13
Source File: ProtoLiteUtilsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void introspection() throws Exception {
  Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance());
  PrototypeMarshaller<Enum> prototypeMarshaller = (PrototypeMarshaller<Enum>) enumMarshaller;
  assertSame(Enum.getDefaultInstance(), prototypeMarshaller.getMessagePrototype());
  assertSame(Enum.class, prototypeMarshaller.getMessageClass());
}
 
Example #14
Source File: ProtoLiteUtilsTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testMismatch() throws Exception {
  Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance());
  // Enum's name and Type's name are both strings with tag 1.
  Enum altProto = Enum.newBuilder().setName(proto.getName()).build();
  assertEquals(proto, marshaller.parse(enumMarshaller.stream(altProto)));
}
 
Example #15
Source File: ProtoLiteUtilsTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void introspection() throws Exception {
  Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance());
  PrototypeMarshaller<Enum> prototypeMarshaller = (PrototypeMarshaller<Enum>) enumMarshaller;
  assertSame(Enum.getDefaultInstance(), prototypeMarshaller.getMessagePrototype());
  assertSame(Enum.class, prototypeMarshaller.getMessageClass());
}
 
Example #16
Source File: ProtoLiteUtilsTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testMismatch() throws Exception {
  Marshaller<Enum> enumMarshaller = ProtoLiteUtils.marshaller(Enum.getDefaultInstance());
  // Enum's name and Type's name are both strings with tag 1.
  Enum altProto = Enum.newBuilder().setName(proto.getName()).build();
  assertEquals(proto, marshaller.parse(enumMarshaller.stream(altProto)));
}
 
Example #17
Source File: InternalClientInterceptors.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
public static <ReqT, RespT> ClientInterceptor wrapClientInterceptor(
    final ClientInterceptor interceptor,
    final Marshaller<ReqT> reqMarshaller,
    final Marshaller<RespT> respMarshaller) {
  return ClientInterceptors.wrapClientInterceptor(interceptor, reqMarshaller, respMarshaller);
}
 
Example #18
Source File: InternalClientInterceptors.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
public static <ReqT, RespT> ClientInterceptor wrapClientInterceptor(
    final ClientInterceptor interceptor,
    final Marshaller<ReqT> reqMarshaller,
    final Marshaller<RespT> respMarshaller) {
  return ClientInterceptors.wrapClientInterceptor(interceptor, reqMarshaller, respMarshaller);
}
 
Example #19
Source File: JsonMarshaller.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Create a {@code Marshaller} for json protos of the same type as {@code defaultInstance}.
 *
 * <p>This is an unstable API and has not been optimized yet for performance.
 */
public static <T extends Message> Marshaller<T> jsonMarshaller(final T defaultInstance) {
  final Parser parser = JsonFormat.parser();
  final Printer printer = JsonFormat.printer();
  return jsonMarshaller(defaultInstance, parser, printer);
}
 
Example #20
Source File: GrpcMessageMarshaller.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static MessageType marshallerType(Marshaller<?> marshaller) {
    return marshaller instanceof PrototypeMarshaller ? MessageType.PROTOBUF : MessageType.UNKNOWN;
}
 
Example #21
Source File: DefaultJsonMarshaller.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public <T> void serializeMessage(Marshaller<T> marshaller, T message, OutputStream os) throws IOException {
    delegate.writeValue((Message) message, os);
}
 
Example #22
Source File: BinaryLogProviderTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
Marshaller<String> delegate() {
  return StringMarshaller.INSTANCE;
}
 
Example #23
Source File: BinaryLogProviderTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
Marshaller<Integer> delegate() {
  return IntegerMarshaller.INSTANCE;
}
 
Example #24
Source File: BinaryLogProviderTest.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
Marshaller<String> delegate() {
  return StringMarshaller.INSTANCE;
}
 
Example #25
Source File: BinaryLogProviderTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
Marshaller<Integer> delegate() {
  return IntegerMarshaller.INSTANCE;
}
 
Example #26
Source File: JsonMarshaller.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/**
 * Create a {@code Marshaller} for json protos of the same type as {@code defaultInstance}.
 *
 * <p>This is an unstable API and has not been optimized yet for performance.
 */
public static <T extends Message> Marshaller<T> jsonMarshaller(final T defaultInstance) {
  final Parser parser = JsonFormat.parser();
  final Printer printer = JsonFormat.printer();
  return jsonMarshaller(defaultInstance, parser, printer);
}
 
Example #27
Source File: ProtoUtils.java    From grpc-nebula-java with Apache License 2.0 2 votes vote down vote up
/**
 * Create a {@link Marshaller} for protos of the same type as {@code defaultInstance}.
 *
 * @since 1.0.0
 */
public static <T extends Message> Marshaller<T> marshaller(final T defaultInstance) {
  return ProtoLiteUtils.marshaller(defaultInstance);
}
 
Example #28
Source File: ProtoLiteUtils.java    From grpc-java with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@link Marshaller} for protos of the same type as {@code defaultInstance}.
 *
 * @since 1.0.0
 */
public static <T extends MessageLite> Marshaller<T> marshaller(T defaultInstance) {
  // TODO(ejona): consider changing return type to PrototypeMarshaller (assuming ABI safe)
  return new MessageMarshaller<>(defaultInstance);
}
 
Example #29
Source File: ProtoUtils.java    From grpc-java with Apache License 2.0 2 votes vote down vote up
/**
 * Create a {@link Marshaller} for protos of the same type as {@code defaultInstance}.
 *
 * @since 1.0.0
 */
public static <T extends Message> Marshaller<T> marshaller(final T defaultInstance) {
  return ProtoLiteUtils.marshaller(defaultInstance);
}
 
Example #30
Source File: GrpcJsonMarshaller.java    From armeria with Apache License 2.0 2 votes vote down vote up
/**
 * Deserializes a gRPC message from JSON.
 */
<T> T deserializeMessage(Marshaller<T> marshaller, InputStream is) throws IOException;