Index: third_party/protobuf/src/google/protobuf/compiler/js/well_known_types/any.js |
diff --git a/third_party/protobuf/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java b/third_party/protobuf/src/google/protobuf/compiler/js/well_known_types/any.js |
similarity index 52% |
copy from third_party/protobuf/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java |
copy to third_party/protobuf/src/google/protobuf/compiler/js/well_known_types/any.js |
index cbe742e5b91f3b5822de7d189d7953d768af4a22..22f18919bc845817ce20bba3eaf433e78cf43756 100644 |
--- a/third_party/protobuf/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java |
+++ b/third_party/protobuf/src/google/protobuf/compiler/js/well_known_types/any.js |
@@ -28,54 +28,53 @@ |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
-package com.google.protobuf; |
+/* This code will be inserted into generated code for |
+ * google/protobuf/any.proto. */ |
-import junit.framework.TestCase; |
+/** |
+ * Returns the type name contained in this instance, if any. |
+ * @return {string|undefined} |
+ */ |
+proto.google.protobuf.Any.prototype.getTypeName = function() { |
+ return this.getTypeUrl().split('/').pop(); |
+}; |
-import java.io.ByteArrayOutputStream; |
-import java.io.IOException; |
-import java.nio.ByteBuffer; |
-import java.util.Arrays; |
-import java.util.Random; |
/** |
- * Tests for {@link ByteBufferWriter}. |
+ * Packs the given message instance into this Any. |
+ * @param {!Uint8Array} serialized The serialized data to pack. |
+ * @param {string} name The type name of this message object. |
+ * @param {string=} opt_typeUrlPrefix the type URL prefix. |
*/ |
-public class ByteBufferWriterTest extends TestCase { |
- |
- public void testHeapBuffer() throws IOException { |
- // Test a small and large buffer. |
- testWrite(ByteBuffer.allocate(100)); |
- testWrite(ByteBuffer.allocate(1024 * 100)); |
+proto.google.protobuf.Any.prototype.pack = function(serialized, name, |
+ opt_typeUrlPrefix) { |
+ if (!opt_typeUrlPrefix) { |
+ opt_typeUrlPrefix = 'type.googleapis.com/'; |
} |
- public void testDirectBuffer() throws IOException { |
- // Test a small and large buffer. |
- testWrite(ByteBuffer.allocateDirect(100)); |
- testWrite(ByteBuffer.allocateDirect(1024 * 100)); |
+ if (opt_typeUrlPrefix.substr(-1) != '/') { |
+ this.setTypeUrl(opt_typeUrlPrefix + '/' + name); |
+ } else { |
+ this.setTypeUrl(opt_typeUrlPrefix + name); |
} |
- private void testWrite(ByteBuffer buffer) throws IOException { |
- fillRandom(buffer); |
- ByteArrayOutputStream os = new ByteArrayOutputStream(buffer.remaining()); |
- ByteBufferWriter.write(buffer, os); |
- assertEquals(0, buffer.position()); |
- assertTrue(Arrays.equals(toArray(buffer), os.toByteArray())); |
- } |
+ this.setValue(serialized); |
+}; |
- private void fillRandom(ByteBuffer buf) { |
- byte[] bytes = new byte[buf.remaining()]; |
- new Random().nextBytes(bytes); |
- buf.put(bytes); |
- buf.flip(); |
- return; |
- } |
- private byte[] toArray(ByteBuffer buf) { |
- int originalPosition = buf.position(); |
- byte[] bytes = new byte[buf.remaining()]; |
- buf.get(bytes); |
- buf.position(originalPosition); |
- return bytes; |
+/** |
+ * @template T |
+ * Unpacks this Any into the given message object. |
+ * @param {function(Uint8Array):T} deserialize Function that will deserialize |
+ * the binary data properly. |
+ * @param {string} name The expected type name of this message object. |
+ * @return {?T} If the name matched the expected name, returns the deserialized |
+ * object, otherwise returns undefined. |
+ */ |
+proto.google.protobuf.Any.prototype.unpack = function(deserialize, name) { |
+ if (this.getTypeName() == name) { |
+ return deserialize(this.getValue_asU8()); |
+ } else { |
+ return null; |
} |
-} |
+}; |