Skip to content

Commit

Permalink
chore: fix #26
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonxiang committed Sep 27, 2024
1 parent f781c74 commit 6784b3b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
18 changes: 14 additions & 4 deletions packages/core/src/print/gen-enum.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import MagicString from 'magic-string';
import { indentPrefix, OUTPUT_TYPE } from '../constant.js';
import protobuf from 'protobufjs';

const { Type } = protobuf;

/**
* generate typescript files from proto info
Expand All @@ -8,7 +11,7 @@ import { indentPrefix, OUTPUT_TYPE } from '../constant.js';
* @returns
*/
export function genEnum(proto, options) {
const { values, comments, name } = proto;
const { values, comments, name, parent } = proto;

const items = Object.keys(values)
.map((key) => ({
Expand All @@ -21,17 +24,24 @@ export function genEnum(proto, options) {
const result = new MagicString('');

items.forEach((s) => {
if(s.comment) {
if (s.comment) {
result.append(`//${s.comment}\n`);
}
result.append(`${s.name} = ${s.id},\n`);
});

const prefix = options.outputType === OUTPUT_TYPE.definition ? '' : 'export ';

let interfaceName = name;

// deal with nested type conflict problem
if (parent && parent instanceof Type && parent.name) {
interfaceName = parent.name + interfaceName;
}

result
.indent(indentPrefix)
.prepend(`${prefix}enum ${name} {\n`)
.prepend(`${prefix}enum ${interfaceName} {\n`)
.append('}\n\n');

return {
Expand Down Expand Up @@ -60,7 +70,7 @@ export function getJsdocEnum(proto, options) {
const result = new MagicString('');

items.forEach((s) => {
if(s.comment) {
if (s.comment) {
result.append(`//${s.comment}\n`);
}
result.append(`${s.name} = ${s.id},\n`);
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/print/gen-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export function genService(proto, options) {
const prefix = options.outputType === OUTPUT_TYPE.definition ? '' : 'export ';

if (item.comment) {
result.append(`//${item.comment}\n`);
item.comment.split('\n').forEach((line) => {
result.append(`//${line} \n`);
})
}

result.append(
Expand Down
35 changes: 33 additions & 2 deletions packages/core/tests/comment/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { test } from 'uvu';
import * as assert from 'uvu/assert';


const source = `
const sample1 = `
message UpdateHostBankAccWalletInfoResp {
// 0:成功  非0:错误码   
Expand All @@ -19,12 +19,43 @@ message UpdateHostBankAccWalletInfoResp {
`;

test('Comment of Field type should be converted', () => {
const ts = parseProto(source);
const ts = parseProto(sample1);

assert.match(ts, '//50010 bank account already exist');
assert.match(ts, '//50011: bank account number already exist');
assert.match(ts, '//50012 bank account mismatch the one on');
});

const sample2 = `
syntax = "proto3";
service Greeter {
//hello
//hello1
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message Teacher {
string name = 1;
}
message HelloReply {
string message = 1;
int32 test = 2;
Teacher teacher = 3;
}
`;

test('Comment of function type should be converted', () => {
const ts = parseProto(sample2);

assert.match(ts, '//hello');
assert.match(ts, '//hello1');
});


test.run();
23 changes: 23 additions & 0 deletions packages/core/tests/print/nest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,28 @@ test('nest type6 should be converted', () => {
assert.match(ts, '@typedef {Object} Message1');
});


test('nest type7 should be converted', () => {
const source = `
syntax = "proto3";
message UpdateUserRoleReq {
enum Type {
PLATFORM = 0;
PROJECT = 1;
MODULE = 2;
PLUGIN = 3;
}
int32 user_id = 1;
int32 role_id = 2;
Type type = 3;
}
`;

const ts = parseProto(source);
console.log(ts);
assert.match(ts, 'type?: UpdateUserRoleReqType;');
assert.match(ts, 'enum UpdateUserRoleReqType');
})

test.run();

0 comments on commit 6784b3b

Please sign in to comment.