Skip to content

Commit 5d4992d

Browse files
authored
refactor: add keep case args (#5)
1 parent 0e8005e commit 5d4992d

File tree

8 files changed

+80
-19
lines changed

8 files changed

+80
-19
lines changed

README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,17 @@ $ tsproto --path ./hero-proto
4242
Output:
4343

4444
```ts
45-
import { Metadata } from '@grpc/grpc-js';
45+
import * as grpc from '@midwayjs/grpc';
4646

4747
export namespace hero {
48-
export interface HeroesService {
49-
findOne(data: HeroById, metadata?: Metadata): Promise<Hero>;
48+
export interface HeroService {
49+
findOne(data: HeroById): Promise<Hero>;
50+
}
51+
/**
52+
* HeroService client interface
53+
*/
54+
export interface HeroServiceClient {
55+
findOne(options?: grpc.IClientOptions): grpc.IClientUnaryService<HeroById, Hero>;
5056
}
5157
export interface HeroById {
5258
id?: number;
@@ -56,6 +62,8 @@ export namespace hero {
5662
name?: string;
5763
}
5864
}
65+
66+
5967
```
6068

6169
## Usage
@@ -91,4 +99,5 @@ The following options are available:
9199
[array] [default: ["node_modules","dist"]]
92100
--comments, -c Add comments from proto [boolean] [default: true]
93101
--verbose Log all output to console [boolean] [default: true]
102+
--keepCase, -k keep property case [boolean] [default: true]
94103
```

bin/tsproto.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const options = {
99
output: null,
1010
target: ['.proto'],
1111
ignore: ['node_modules', 'dist'],
12-
keepCase: false,
12+
keepCase: true,
1313
comments: true,
1414
verbose: true,
1515
};
@@ -56,6 +56,12 @@ const cli = usage('Extract and merge locale files.\nUsage: $0 [options]')
5656
default: options.verbose,
5757
type: 'boolean',
5858
})
59+
.option('keepCase', {
60+
alias: 'k',
61+
describe: 'keep property case',
62+
default: options.keepCase,
63+
type: 'boolean',
64+
})
5965
.demandOption(
6066
['path'],
6167
red.bold(
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2015 gRPC authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
syntax = "proto3";
15+
16+
package hello;
17+
18+
service Service {
19+
rpc SayHello( HelloRequest ) returns( HelloResponse ) {}
20+
}
21+
22+
message HelloRequest {
23+
int32 key_filed = 1;
24+
}
25+
26+
message HelloResponse {
27+
string value_filed = 2;
28+
}

test/index.test.ts

+33-15
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import { remove, readFileSync } from 'fs-extra';
66
describe('/test/index.test.ts', () => {
77
it('test generate ts interface', async () => {
88
const compiler = new Compiler({
9-
path: ['test/fixtures'],
9+
path: ['test/fixtures/common'],
1010
target: ['.proto'],
1111
ignore: ['node_modules', 'dist'],
1212
});
1313

1414
compiler.compile();
1515

16-
expect(existsSync(join(__dirname, './fixtures/hero.ts'))).toBeTruthy();
17-
expect(existsSync(join(__dirname, './fixtures/helloworld.ts'))).toBeTruthy();
18-
expect(existsSync(join(__dirname, './fixtures/math.ts'))).toBeTruthy();
16+
expect(existsSync(join(__dirname, './fixtures/common/hero.ts'))).toBeTruthy();
17+
expect(existsSync(join(__dirname, './fixtures/common/helloworld.ts'))).toBeTruthy();
18+
expect(existsSync(join(__dirname, './fixtures/common/math.ts'))).toBeTruthy();
1919

20-
const content = readFileSync(join(__dirname, './fixtures/math.ts'), 'utf8');
20+
const content = readFileSync(join(__dirname, './fixtures/common/math.ts'), 'utf8');
2121
expect(content.includes('add(data: AddArgs): Promise<Num>;')).toBeTruthy();
2222
expect(content.includes('addMore(data: AddArgs): Promise<void>;')).toBeTruthy();
2323
expect(content.includes('sumMany(data: AddArgs): Promise<void>;')).toBeTruthy();
@@ -32,26 +32,44 @@ describe('/test/index.test.ts', () => {
3232
expect(content.includes('addMany(options?: grpc.IClientOptions): grpc.IClientWritableStreamService<AddArgs, Num>;')).toBeTruthy();
3333
expect(content.includes('addEmpty(options?: grpc.IClientOptions): grpc.IClientUnaryService<any, any>;')).toBeTruthy();
3434

35-
await remove(join(__dirname, './fixtures/hero.ts'));
36-
await remove(join(__dirname, './fixtures/helloworld.ts'));
37-
await remove(join(__dirname, './fixtures/math.ts'));
38-
await remove(join(__dirname, './fixtures/hello_stream.ts'));
35+
await remove(join(__dirname, './fixtures/common/hero.ts'));
36+
await remove(join(__dirname, './fixtures/common/helloworld.ts'));
37+
await remove(join(__dirname, './fixtures/common/math.ts'));
38+
await remove(join(__dirname, './fixtures/common/hello_stream.ts'));
3939
});
4040

4141
it('test generate ts interface to specified directory', async () => {
4242
const compiler = new Compiler({
43-
path: ['test/fixtures'],
43+
path: ['test/fixtures/common'],
4444
target: ['.proto'],
4545
ignore: ['node_modules', 'dist'],
46-
output: 'test/fixtures/domain'
46+
output: 'test/fixtures/common/domain'
4747
});
4848

4949
compiler.compile();
5050

51-
expect(existsSync(join(__dirname, './fixtures/domain/hero.ts'))).toBeTruthy();
52-
expect(existsSync(join(__dirname, './fixtures/domain/helloworld.ts'))).toBeTruthy();
53-
expect(existsSync(join(__dirname, './fixtures/domain/math.ts'))).toBeTruthy();
51+
expect(existsSync(join(__dirname, './fixtures/common/domain/hero.ts'))).toBeTruthy();
52+
expect(existsSync(join(__dirname, './fixtures/common/domain/helloworld.ts'))).toBeTruthy();
53+
expect(existsSync(join(__dirname, './fixtures/common/domain/math.ts'))).toBeTruthy();
5454

55-
await remove(join(__dirname, './fixtures/domain'));
55+
await remove(join(__dirname, './fixtures/common/domain'));
56+
});
57+
58+
it('test generate ts interface keep case', async () => {
59+
const compiler = new Compiler({
60+
path: ['test/fixtures/keep_case'],
61+
target: ['.proto'],
62+
ignore: ['node_modules', 'dist'],
63+
output: 'test/fixtures/keep_case/domain',
64+
keepCase: true,
65+
});
66+
67+
compiler.compile();
68+
69+
expect(existsSync(join(__dirname, './fixtures/keep_case/domain/helloworld_keep_case.ts'))).toBeTruthy();
70+
const content = readFileSync(join(__dirname, './fixtures/keep_case/domain/helloworld_keep_case.ts'), 'utf8');
71+
expect(content.includes('key_filed')).toBeTruthy();
72+
73+
await remove(join(__dirname, './fixtures/keep_case/domain'));
5674
});
5775
});

0 commit comments

Comments
 (0)