Skip to content

Commit

Permalink
Merge pull request #13 from improbable-eng/bugfix/orphan-map-message
Browse files Browse the repository at this point in the history
Fixes #10 - Messages without packages
  • Loading branch information
MarcusLongmuir authored Jun 25, 2017
2 parents 08ac5c8 + ff52eb3 commit b2b987f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/ExportMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 4 additions & 0 deletions test/proto/orphan.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
syntax = "proto3";

message OrphanMapMessage {
map<string, int64> primitive_ints = 1;
}

message OrphanMessage {
string my_string = 1;
bool my_bool = 2;
Expand Down
25 changes: 25 additions & 0 deletions test/ts_test/src/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit b2b987f

Please sign in to comment.