From ff52eb30807a7df3e3d1b1b0f4c07bb875fdd30b Mon Sep 17 00:00:00 2001 From: Marcus Longmuir Date: Sun, 25 Jun 2017 21:10:57 +0100 Subject: [PATCH] Fixes #10 - Messages without packages --- src/ExportMap.ts | 8 +++++--- test/proto/orphan.proto | 4 ++++ test/ts_test/src/maps.ts | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/ExportMap.ts b/src/ExportMap.ts index eb0a31dd..8e18cee4 100644 --- a/src/ExportMap.ts +++ b/src/ExportMap.ts @@ -39,15 +39,17 @@ export class ExportMap { } : undefined, }; - const entryName = `${scope ? scope + "." : ""}${message.getName()}`; + const packagePrefix = scope ? scope + "." : ""; + + const entryName = `${packagePrefix}${message.getName()}`; this.messageMap[entryName] = messageEntry; message.getNestedTypeList().forEach(nested => { - this.exportNested(scope + "." + message.getName(), fileDescriptor, nested); + this.exportNested(`${packagePrefix}${message.getName()}`, fileDescriptor, nested); }); message.getEnumTypeList().forEach(enumType => { - const identifier = scope + "." + message.getName() + "." + enumType.getName(); + const identifier = `${packagePrefix}${message.getName()}.${enumType.getName()}`; this.enumMap[identifier] = { pkg: fileDescriptor.getPackage(), fileName: fileDescriptor.getName(), diff --git a/test/proto/orphan.proto b/test/proto/orphan.proto index 7551c6e8..c26e3d97 100644 --- a/test/proto/orphan.proto +++ b/test/proto/orphan.proto @@ -1,5 +1,9 @@ syntax = "proto3"; +message OrphanMapMessage { + map primitive_ints = 1; +} + message OrphanMessage { string my_string = 1; bool my_bool = 2; diff --git a/test/ts_test/src/maps.ts b/test/ts_test/src/maps.ts index 637e2537..97442f6d 100644 --- a/test/ts_test/src/maps.ts +++ b/test/ts_test/src/maps.ts @@ -4,6 +4,7 @@ import {ExternalEnum} from "../generated/othercom/external_enum_pb"; import {ExternalChildMessage} from "../generated/othercom/external_child_message_pb"; import InternalEnum = MapMessage.InternalEnum; import InternalChildMessage = MapMessage.InternalChildMessage; +import {OrphanMapMessage} from "../generated/orphan_pb"; describe("maps", () => { describe("message maps", () => { @@ -155,6 +156,30 @@ describe("maps", () => { }); }); + describe("orphan message maps", () => { + it("should allow setting and getting map fields of messages without a package", () => { + const parentMsg = new OrphanMapMessage(); + const myMap = parentMsg.getPrimitiveIntsMap(); + myMap.set("first", 123).set("second", 456); + + assert.strictEqual(parentMsg.getPrimitiveIntsMap().getLength() as number, 2); + const firstKey = parentMsg.getPrimitiveIntsMap().keys().next().value; + assert.strictEqual(firstKey as string, "first"); + + const firstEntry = parentMsg.getPrimitiveIntsMap().entries().next().value; + assert.strictEqual(firstEntry[0] as string, "first"); + assert.strictEqual(firstEntry[1] as number, 123); + + assert.deepEqual(parentMsg.getPrimitiveIntsMap().toObject() as Array<[string, number]>, [ + ["first", 123], + ["second", 456], + ]); + + assert.strictEqual(myMap.del("first") as boolean, true); + assert.strictEqual(myMap.getLength() as number, 1); + }); + }); + describe("toObject", () => { it("should have types", () => { const parentMsg = new MapMessage();