Skip to content

Commit

Permalink
fix: encode empty message
Browse files Browse the repository at this point in the history
  • Loading branch information
askuzminov committed Dec 25, 2020
1 parent 89a43ee commit 3a7f196
Show file tree
Hide file tree
Showing 12 changed files with 885 additions and 114 deletions.
15 changes: 15 additions & 0 deletions proto/whisk/api/user/v2/user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,23 @@ message TestOneof {
}
}

message TestEmpty {
string id = 1;

message GlobalRegion {}

oneof item {
GlobalRegion global = 2 [ required = true ];
GlobalRegion any = 3;
string local = 4;
}
}

message SearchRecipesResponse {
message MatchedIngredient { string name = 1; }

message Empty {}

message Hit {
MatchedIngredient content = 1 [ deprecated = true ];
repeated MatchedIngredient ingredients = 2 [ required = true ];
Expand All @@ -236,6 +250,7 @@ message SearchRecipesResponse {
repeated Hit hit = 1;
whisk.api.shared.v1.Date date = 2;
repeated whisk.api.shared.v1.Date dates = 3;
Empty empty = 4;
}

message ApiUpdateBusinessApp {
Expand Down
8 changes: 3 additions & 5 deletions src/binary/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ function EncodeWrite(buf: BufWrite, item: FieldType, tag: number, val: unknown)
writeMap[item as WriteMapKeys](buf, tag, val as never);
} else if (isFunction(item)) {
const enc = EncodeHelper(new BufWrite(), item, val);
if (enc.buf.length) {
writeTag(buf, tag, Bytes);
writeVarint(buf, enc.buf.length);
buf.concat(enc.buf);
}
writeTag(buf, tag, Bytes);
writeVarint(buf, enc.buf.length);
buf.concat(enc.buf);
} else if (isArray(item)) {
if (item[0] === 'repeated' && isArray(val)) {
if (PACKED[item[1] as string]) {
Expand Down
28 changes: 27 additions & 1 deletion tests/oneof.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable camelcase */

import { Decode, Encode, oneis, oneof } from '../src';
import { whisk_api_user_v2_TestOneof } from './proto';
import { whisk_api_user_v2_TestEmpty, whisk_api_user_v2_TestOneof } from './proto';
import { whisk } from './protobufjs/out';

describe('protobuf wrapper', () => {
Expand Down Expand Up @@ -49,4 +49,30 @@ describe('protobuf wrapper', () => {
expect(Decode(whisk_api_user_v2_TestOneof, binA)).toMatchObject(data);
expect(whisk.api.user.v2.TestOneof.decode(binB)).toMatchObject(dataB);
});

it('encode/decode', () => {
const dataX: whisk_api_user_v2_TestEmpty = {
id: '1',
item: {
oneof: 'global',
value: {},
},
};

const dataY = {
id: '1',
device: 'global',
global: {},
};

const binA = Encode(whisk_api_user_v2_TestEmpty, dataX);
const binB = whisk.api.user.v2.TestEmpty.encode(dataY).finish();

expect(Array.from(binA)).toMatchObject(Array.from(binB));
expect(Decode(whisk_api_user_v2_TestEmpty, binA)).toEqual(dataX);
expect(whisk.api.user.v2.TestEmpty.decode(binB)).toEqual({
id: '1',
global: {},
});
});
});
82 changes: 82 additions & 0 deletions tests/proto/debug/whisk/api/user/v2/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,69 @@
"extensions": [],
"reserved": []
},
{
"name": "TestEmpty",
"enums": [],
"options": {},
"messages": [
{
"name": "GlobalRegion",
"enums": [],
"options": {},
"messages": [],
"fields": [],
"extends": [],
"extensions": [],
"reserved": []
}
],
"fields": [
{
"name": "id",
"type": "string",
"tag": 1,
"required": false,
"repeated": false,
"optional": false,
"options": {}
},
{
"name": "global",
"type": "GlobalRegion",
"tag": 2,
"required": false,
"repeated": false,
"optional": false,
"options": {
"required": true
},
"oneof": "item"
},
{
"name": "any",
"type": "GlobalRegion",
"tag": 3,
"required": false,
"repeated": false,
"optional": false,
"options": {},
"oneof": "item"
},
{
"name": "local",
"type": "string",
"tag": 4,
"required": false,
"repeated": false,
"optional": false,
"options": {},
"oneof": "item"
}
],
"extends": [],
"extensions": [],
"reserved": []
},
{
"name": "SearchRecipesResponse",
"enums": [],
Expand All @@ -1249,6 +1312,16 @@
"extensions": [],
"reserved": []
},
{
"name": "Empty",
"enums": [],
"options": {},
"messages": [],
"fields": [],
"extends": [],
"extensions": [],
"reserved": []
},
{
"name": "Hit",
"enums": [],
Expand Down Expand Up @@ -1336,6 +1409,15 @@
"repeated": true,
"optional": false,
"options": {}
},
{
"name": "empty",
"type": "Empty",
"tag": 4,
"required": false,
"repeated": false,
"optional": false,
"options": {}
}
],
"extends": [],
Expand Down
9 changes: 9 additions & 0 deletions tests/proto/esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,23 @@ export var whisk_api_user_v2_TestOneof = function () { return [
[11, "deviceType", "enum", 1, "device"],
[12, "customDevice", "string", 1, "device"],
]; };
export var whisk_api_user_v2_TestEmpty = function () { return [
[1, "id", "string", 1],
[2, "global", whisk_api_user_v2_TestEmpty_GlobalRegion, 1, "item"],
[3, "any", whisk_api_user_v2_TestEmpty_GlobalRegion, 0, "item"],
[4, "local", "string", 1, "item"],
]; };
export var whisk_api_user_v2_TestEmpty_GlobalRegion = function () { return []; };
export var whisk_api_user_v2_SearchRecipesResponse = function () { return [
[1, "hit", ["repeated", whisk_api_user_v2_SearchRecipesResponse_Hit], 1],
[2, "date", whisk_api_shared_v1_Date, 0],
[3, "dates", ["repeated", whisk_api_shared_v1_Date], 1],
[4, "empty", whisk_api_user_v2_SearchRecipesResponse_Empty, 0],
]; };
export var whisk_api_user_v2_SearchRecipesResponse_MatchedIngredient = function () { return [
[1, "name", "string", 1],
]; };
export var whisk_api_user_v2_SearchRecipesResponse_Empty = function () { return []; };
export var whisk_api_user_v2_SearchRecipesResponse_Hit = function () { return [
[1, "content", whisk_api_user_v2_SearchRecipesResponse_MatchedIngredient, 0],
[2, "ingredients", ["repeated", whisk_api_user_v2_SearchRecipesResponse_MatchedIngredient], 1],
Expand Down
13 changes: 13 additions & 0 deletions tests/proto/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,16 +783,29 @@ export type whisk_api_user_v2_TestOneof = {
| { oneof: 'customDevice'; value: string; };
};
export const whisk_api_user_v2_TestOneof: Field<whisk_api_user_v2_TestOneof>;
export type whisk_api_user_v2_TestEmpty = {
id: string;
item?:
| { oneof: 'global'; value: whisk_api_user_v2_TestEmpty_GlobalRegion; }
| { oneof: 'any'; value?: whisk_api_user_v2_TestEmpty_GlobalRegion; }
| { oneof: 'local'; value: string; };
};
export const whisk_api_user_v2_TestEmpty: Field<whisk_api_user_v2_TestEmpty>;
export type whisk_api_user_v2_TestEmpty_GlobalRegion = FieldEmpty;
export const whisk_api_user_v2_TestEmpty_GlobalRegion: Field<whisk_api_user_v2_TestEmpty_GlobalRegion>;
export type whisk_api_user_v2_SearchRecipesResponse = {
hit: whisk_api_user_v2_SearchRecipesResponse_Hit[];
date?: whisk_api_shared_v1_Date;
dates: whisk_api_shared_v1_Date[];
empty?: whisk_api_user_v2_SearchRecipesResponse_Empty;
};
export const whisk_api_user_v2_SearchRecipesResponse: Field<whisk_api_user_v2_SearchRecipesResponse>;
export type whisk_api_user_v2_SearchRecipesResponse_MatchedIngredient = {
name: string;
};
export const whisk_api_user_v2_SearchRecipesResponse_MatchedIngredient: Field<whisk_api_user_v2_SearchRecipesResponse_MatchedIngredient>;
export type whisk_api_user_v2_SearchRecipesResponse_Empty = FieldEmpty;
export const whisk_api_user_v2_SearchRecipesResponse_Empty: Field<whisk_api_user_v2_SearchRecipesResponse_Empty>;
export type whisk_api_user_v2_SearchRecipesResponse_Hit = {
/** @deprecated */
content?: whisk_api_user_v2_SearchRecipesResponse_MatchedIngredient;
Expand Down
Loading

0 comments on commit 3a7f196

Please sign in to comment.