From 602065e16862751c515c2f3391ee8b880e8140b1 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Tue, 4 Apr 2017 08:39:45 +0200 Subject: [PATCH] Docs: Clarified typescript example in README, see #744 --- README.md | 18 ++++++++++++------ src/encoder.js | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 450be2f13..5485315c4 100644 --- a/README.md +++ b/README.md @@ -444,18 +444,24 @@ import * as protobuf from "protobufjs"; import * as Long from "long"; // optional // browser only (alternatively) -import * as protobuf from "./node_modules/protobufjs/index.js"; -import * as Long from "./node_modules/long/dist/long.js"; // optional +// import * as protobuf from "./node_modules/protobufjs/index.js"; +// import * as Long from "./node_modules/long/dist/long.js"; // optional protobuf.load("awesome.proto", function(err, root) { if (err) throw err; // example code - var AwesomeMessage = root.lookupType("AwesomeMessage"); - var message = AwesomeMessage.create({ awesomeField: "hello" }); - var buffer = AwesomeMessage.encode(message).finish(); - ... + const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage"); + + let message = AwesomeMessage.create({ awesomeField: "hello" }); + console.log(`message = ${JSON.stringify(message)}`); + + let buffer = AwesomeMessage.encode(message).finish(); + console.log(`buffer = ${Array.prototype.slice.call(buffer)}`); + + let decoded = AwesomeMessage.decode(buffer); + console.log(`decoded = ${JSON.stringify(decoded)}`); }); ``` diff --git a/src/encoder.js b/src/encoder.js index 0be335c6b..538ff6b89 100644 --- a/src/encoder.js +++ b/src/encoder.js @@ -42,7 +42,7 @@ function encoder(mtype) { type = field.resolvedType instanceof Enum ? "uint32" : field.type, wireType = types.basic[type]; ref = "m" + util.safeProp(field.name); - + // Map fields if (field.map) { gen