aboutsummaryrefslogtreecommitdiff
path: root/protobuf-nano
diff options
context:
space:
mode:
authorEric Anderson <ejona@google.com>2015-09-03 17:14:29 -0700
committerEric Anderson <ejona@google.com>2015-09-03 17:31:16 -0700
commitd42559c6afa666ed7d96a1c324fe09f0d0f8f7ed (patch)
tree85254926e3b1bf6ce1a3c7c0619192d19b24b303 /protobuf-nano
parent47a7ccf0cba241e0e13ed0cb946f6b82c055b10b (diff)
downloadgrpc-grpc-java-d42559c6afa666ed7d96a1c324fe09f0d0f8f7ed.tar.gz
Swap nano from Parser to message factory
This reduces the amount of logic built into the generated code. If we swap to an alternative form of decoding we should have greater ability to adapt the existing API to make use of the new one. Previously most changes would require duplicating all the nano marshalling code.
Diffstat (limited to 'protobuf-nano')
-rw-r--r--protobuf-nano/src/main/java/io/grpc/protobuf/nano/MessageNanoFactory.java (renamed from protobuf-nano/src/main/java/io/grpc/protobuf/nano/Parser.java)9
-rw-r--r--protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java7
2 files changed, 8 insertions, 8 deletions
diff --git a/protobuf-nano/src/main/java/io/grpc/protobuf/nano/Parser.java b/protobuf-nano/src/main/java/io/grpc/protobuf/nano/MessageNanoFactory.java
index a82e59f5c..76892c944 100644
--- a/protobuf-nano/src/main/java/io/grpc/protobuf/nano/Parser.java
+++ b/protobuf-nano/src/main/java/io/grpc/protobuf/nano/MessageNanoFactory.java
@@ -31,16 +31,13 @@
package io.grpc.protobuf.nano;
-import com.google.protobuf.nano.CodedInputByteBufferNano;
import com.google.protobuf.nano.MessageNano;
-import java.io.IOException;
-
/**
- * Parser for parsing nano proto messages.
+ * Produce new message instances. Used by a marshaller to deserialize incoming messages.
*
* <p>Should be implemented by generated code.
*/
-public interface Parser<T extends MessageNano> {
- T parse(CodedInputByteBufferNano input) throws IOException;
+public interface MessageNanoFactory<T extends MessageNano> {
+ T newInstance();
}
diff --git a/protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java b/protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java
index 9375fc8df..43c4b1079 100644
--- a/protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java
+++ b/protobuf-nano/src/main/java/io/grpc/protobuf/nano/NanoUtils.java
@@ -49,7 +49,8 @@ public class NanoUtils {
private NanoUtils() {}
/** Adapt {@code parser} to a {@code Marshaller}. */
- public static <T extends MessageNano> Marshaller<T> marshaller(final Parser<T> parser) {
+ public static <T extends MessageNano> Marshaller<T> marshaller(
+ final MessageNanoFactory<T> factory) {
return new Marshaller<T>() {
@Override
public InputStream stream(T value) {
@@ -63,7 +64,9 @@ public class NanoUtils {
CodedInputByteBufferNano input =
CodedInputByteBufferNano.newInstance(ByteStreams.toByteArray(stream));
input.setSizeLimit(Integer.MAX_VALUE);
- return parser.parse(input);
+ T message = factory.newInstance();
+ message.mergeFrom(input);
+ return message;
} catch (IOException ipbe) {
throw Status.INTERNAL.withDescription("Failed parsing nano proto message").withCause(ipbe)
.asRuntimeException();