Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix codegen for maps with wrapper value type #370

Merged
merged 7 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion integration/simple/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ export const protobufPackage = 'google.protobuf';
* .setNanos((int) ((millis % 1000) * 1000000)).build();
*
*
* Example 5: Compute Timestamp from current time in Python.
* Example 5: Compute Timestamp from Java `Instant.now()`.
*
* Instant now = Instant.now();
*
* Timestamp timestamp =
* Timestamp.newBuilder().setSeconds(now.getEpochSecond())
* .setNanos(now.getNano()).build();
*
*
* Example 6: Compute Timestamp from current time in Python.
*
* timestamp = Timestamp()
* timestamp.GetCurrentTime()
Expand Down
5 changes: 5 additions & 0 deletions integration/simple/simple-json-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ describe('simple json', () => {
"1": 0,
},
"mapOfBytes": Object {},
"mapOfStringValues": Object {},
"mapOfTimestamps": Object {},
"nameLookup": Object {},
}
Expand All @@ -212,6 +213,7 @@ describe('simple json', () => {
"entitiesById": Object {},
"intLookup": Object {},
"mapOfBytes": Object {},
"mapOfStringValues": Object {},
"mapOfTimestamps": Object {
"a": 1970-01-01T00:16:40.000Z,
"b": 1970-01-01T00:33:20.000Z,
Expand All @@ -233,6 +235,7 @@ describe('simple json', () => {
a: new Uint8Array([1, 2]),
b: new Uint8Array([1, 2, 3]),
},
mapOfStringValues: {},
};
const json = SimpleWithMap.toJSON(s1);
expect(json).toMatchInlineSnapshot(`
Expand All @@ -243,6 +246,7 @@ describe('simple json', () => {
"a": "AQI=",
"b": "AQID",
},
"mapOfStringValues": Object {},
"mapOfTimestamps": Object {},
"nameLookup": Object {},
}
Expand Down Expand Up @@ -272,6 +276,7 @@ describe('simple json', () => {
3,
],
},
"mapOfStringValues": Object {},
"mapOfTimestamps": Object {},
"nameLookup": Object {},
}
Expand Down
5 changes: 5 additions & 0 deletions integration/simple/simple-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,12 @@ describe('simple', () => {
intLookup: { 1: 2, 2: 1 },
mapOfTimestamps: {},
mapOfBytes: {},
mapOfStringValues: {},
aikoven marked this conversation as resolved.
Show resolved Hide resolved
};
const s2 = PbSimpleWithMap.toObject(PbSimpleWithMap.decode(SimpleWithMap.encode(s1).finish()));
delete (s1 as any).mapOfTimestamps;
delete (s1 as any).mapOfBytes;
delete (s1 as any).mapOfStringValues;
expect(s2).toEqual(s1);
aikoven marked this conversation as resolved.
Show resolved Hide resolved
});

Expand All @@ -245,6 +247,7 @@ describe('simple', () => {
intLookup: { 1: 0 },
mapOfTimestamps: {},
mapOfBytes: {},
mapOfStringValues: {},
aikoven marked this conversation as resolved.
Show resolved Hide resolved
};
const s2 = SimpleWithMap.decode(SimpleWithMap.encode(s1).finish());
expect(s2).toEqual(s1);
Expand All @@ -264,6 +267,7 @@ describe('simple', () => {
nameLookup: {},
mapOfTimestamps: {},
mapOfBytes: {},
mapOfStringValues: {},
});
});

Expand Down Expand Up @@ -298,6 +302,7 @@ describe('simple', () => {
"2": 0,
},
"mapOfBytes": Object {},
"mapOfStringValues": Object {},
aikoven marked this conversation as resolved.
Show resolved Hide resolved
"mapOfTimestamps": Object {},
"nameLookup": Object {},
}
Expand Down
Binary file modified integration/simple/simple.bin
Binary file not shown.
1 change: 1 addition & 0 deletions integration/simple/simple.proto
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ message SimpleWithMap {
map<int32, int32> intLookup = 3;
map<string, google.protobuf.Timestamp> mapOfTimestamps = 4;
map<string, bytes> mapOfBytes = 5;
map<string, google.protobuf.StringValue> mapOfStringValues = 6;
}

message SimpleWithSnakeCaseMap {
Expand Down
108 changes: 108 additions & 0 deletions integration/simple/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export interface SimpleWithMap {
intLookup: { [key: number]: number };
mapOfTimestamps: { [key: string]: Date };
mapOfBytes: { [key: string]: Uint8Array };
mapOfStringValues: { [key: string]: string | undefined };
aikoven marked this conversation as resolved.
Show resolved Hide resolved
}

export interface SimpleWithMap_EntitiesByIdEntry {
Expand Down Expand Up @@ -220,6 +221,11 @@ export interface SimpleWithMap_MapOfBytesEntry {
value: Uint8Array;
}

export interface SimpleWithMap_MapOfStringValuesEntry {
key: string;
value: string | undefined;
}

export interface SimpleWithSnakeCaseMap {
entitiesById: { [key: number]: Entity };
}
Expand Down Expand Up @@ -1182,6 +1188,9 @@ export const SimpleWithMap = {
Object.entries(message.mapOfBytes).forEach(([key, value]) => {
SimpleWithMap_MapOfBytesEntry.encode({ key: key as any, value }, writer.uint32(42).fork()).ldelim();
});
Object.entries(message.mapOfStringValues).forEach(([key, value]) => {
SimpleWithMap_MapOfStringValuesEntry.encode({ key: key as any, value }, writer.uint32(50).fork()).ldelim();
});
return writer;
},

Expand All @@ -1194,6 +1203,7 @@ export const SimpleWithMap = {
message.intLookup = {};
message.mapOfTimestamps = {};
message.mapOfBytes = {};
message.mapOfStringValues = {};
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
Expand Down Expand Up @@ -1227,6 +1237,12 @@ export const SimpleWithMap = {
message.mapOfBytes[entry5.key] = entry5.value;
}
break;
case 6:
const entry6 = SimpleWithMap_MapOfStringValuesEntry.decode(reader, reader.uint32());
if (entry6.value !== undefined) {
message.mapOfStringValues[entry6.key] = entry6.value;
}
break;
default:
reader.skipType(tag & 7);
break;
Expand All @@ -1242,6 +1258,7 @@ export const SimpleWithMap = {
message.intLookup = {};
message.mapOfTimestamps = {};
message.mapOfBytes = {};
message.mapOfStringValues = {};
if (object.entitiesById !== undefined && object.entitiesById !== null) {
Object.entries(object.entitiesById).forEach(([key, value]) => {
message.entitiesById[Number(key)] = Entity.fromJSON(value);
Expand All @@ -1267,6 +1284,11 @@ export const SimpleWithMap = {
message.mapOfBytes[key] = bytesFromBase64(value as string);
});
}
if (object.mapOfStringValues !== undefined && object.mapOfStringValues !== null) {
Object.entries(object.mapOfStringValues).forEach(([key, value]) => {
message.mapOfStringValues[key] = value as string | undefined;
});
}
return message;
},

Expand Down Expand Up @@ -1302,6 +1324,12 @@ export const SimpleWithMap = {
obj.mapOfBytes[k] = base64FromBytes(v);
});
}
obj.mapOfStringValues = {};
if (message.mapOfStringValues) {
Object.entries(message.mapOfStringValues).forEach(([k, v]) => {
obj.mapOfStringValues[k] = v;
});
}
return obj;
},

Expand All @@ -1312,6 +1340,7 @@ export const SimpleWithMap = {
message.intLookup = {};
message.mapOfTimestamps = {};
message.mapOfBytes = {};
message.mapOfStringValues = {};
if (object.entitiesById !== undefined && object.entitiesById !== null) {
Object.entries(object.entitiesById).forEach(([key, value]) => {
if (value !== undefined) {
Expand Down Expand Up @@ -1347,6 +1376,13 @@ export const SimpleWithMap = {
}
});
}
if (object.mapOfStringValues !== undefined && object.mapOfStringValues !== null) {
Object.entries(object.mapOfStringValues).forEach(([key, value]) => {
if (value !== undefined) {
message.mapOfStringValues[key] = value;
}
});
}
return message;
},
};
Expand Down Expand Up @@ -1712,6 +1748,78 @@ export const SimpleWithMap_MapOfBytesEntry = {
},
};

const baseSimpleWithMap_MapOfStringValuesEntry: object = { key: '' };

export const SimpleWithMap_MapOfStringValuesEntry = {
encode(message: SimpleWithMap_MapOfStringValuesEntry, writer: Writer = Writer.create()): Writer {
if (message.key !== '') {
writer.uint32(10).string(message.key);
}
if (message.value !== undefined) {
StringValue.encode({ value: message.value! }, writer.uint32(18).fork()).ldelim();
}
return writer;
},

decode(input: Reader | Uint8Array, length?: number): SimpleWithMap_MapOfStringValuesEntry {
const reader = input instanceof Reader ? input : new Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = { ...baseSimpleWithMap_MapOfStringValuesEntry } as SimpleWithMap_MapOfStringValuesEntry;
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.key = reader.string();
break;
case 2:
message.value = StringValue.decode(reader, reader.uint32()).value;
break;
default:
reader.skipType(tag & 7);
break;
}
}
return message;
},

fromJSON(object: any): SimpleWithMap_MapOfStringValuesEntry {
const message = { ...baseSimpleWithMap_MapOfStringValuesEntry } as SimpleWithMap_MapOfStringValuesEntry;
if (object.key !== undefined && object.key !== null) {
message.key = String(object.key);
} else {
message.key = '';
}
if (object.value !== undefined && object.value !== null) {
message.value = String(object.value);
} else {
message.value = undefined;
}
return message;
},

toJSON(message: SimpleWithMap_MapOfStringValuesEntry): unknown {
const obj: any = {};
message.key !== undefined && (obj.key = message.key);
message.value !== undefined && (obj.value = message.value);
return obj;
},

fromPartial(object: DeepPartial<SimpleWithMap_MapOfStringValuesEntry>): SimpleWithMap_MapOfStringValuesEntry {
const message = { ...baseSimpleWithMap_MapOfStringValuesEntry } as SimpleWithMap_MapOfStringValuesEntry;
if (object.key !== undefined && object.key !== null) {
message.key = object.key;
} else {
message.key = '';
}
if (object.value !== undefined && object.value !== null) {
message.value = object.value;
} else {
message.value = undefined;
}
return message;
},
};

const baseSimpleWithSnakeCaseMap: object = {};

export const SimpleWithSnakeCaseMap = {
Expand Down
7 changes: 6 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,9 @@ function generateFromJson(ctx: Context, fullName: string, messageDesc: Descripto
(options.useDate === DateOption.DATE || options.useDate === DateOption.TIMESTAMP)
) {
return code`${utils.fromJsonTimestamp}(${from})`;
} else if (isValueType(ctx, valueType)) {
const type = basicTypeName(ctx, valueType);
return code`${from} as ${type}`;
} else {
const type = basicTypeName(ctx, valueType);
return code`${type}.fromJSON(${from})`;
Expand Down Expand Up @@ -1064,7 +1067,7 @@ function generateToJson(ctx: Context, fullName: string, messageDesc: DescriptorP
return code`${from}`;
} else if (isTimestamp(valueType) && options.useDate === DateOption.TIMESTAMP) {
return code`${utils.fromTimestamp}(${from}).toISOString()`;
} else if (isScalar(valueType)) {
} else if (isScalar(valueType) || isValueType(ctx, valueType)) {
return code`${from}`;
} else {
const type = basicTypeName(ctx, valueType);
Expand Down Expand Up @@ -1168,6 +1171,8 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri
(options.useDate === DateOption.DATE || options.useDate === DateOption.STRING)
) {
return code`${from}`;
} else if (isValueType(ctx, valueType)) {
return code`${from}`;
} else {
const type = basicTypeName(ctx, valueType);
return code`${type}.fromPartial(${from})`;
Expand Down