Skip to content

Commit

Permalink
fix: support empty package
Browse files Browse the repository at this point in the history
  • Loading branch information
askuzminov committed Oct 13, 2021
1 parent 2735868 commit b38c24d
Show file tree
Hide file tree
Showing 24 changed files with 2,343 additions and 741 deletions.
2,080 changes: 1,374 additions & 706 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,30 @@
"dependencies": {
"@whisklabs/deep-readonly": "~1.0.0",
"@whisklabs/typeguards": "~1.0.0",
"typescript": "~4.4.2"
"typescript": "~4.4.4"
},
"devDependencies": {
"@askuzminov/simple-release": "~1.0.8",
"@types/jest": "^27.0.1",
"@typescript-eslint/eslint-plugin": "^4.31.0",
"@typescript-eslint/parser": "^4.31.0",
"@askuzminov/simple-release": "~1.1.0",
"@types/jest": "^27.0.2",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jsdoc": "^36.0.8",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-jsdoc": "^36.1.1",
"eslint-plugin-prefer-arrow": "^1.2.3",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-unicorn": "^35.0.0",
"eslint-plugin-unicorn": "^37.0.1",
"husky": "^4.3.8",
"jest": "^27.1.0",
"jest-junit": "^12.2.0",
"lint-staged": "^11.1.2",
"prettier": "~2.3.2",
"jest": "^27.2.5",
"jest-junit": "^13.0.0",
"lint-staged": "^11.2.3",
"prettier": "~2.4.1",
"prettier-eslint": "^13.0.0",
"protobufjs": "~6.9.0",
"rimraf": "^3.0.2",
"ts-jest": "~27.0.5",
"ts-node": "^10.2.1"
"ts-node": "^10.3.0"
},
"prettier": {
"arrowParens": "avoid",
Expand Down
15 changes: 15 additions & 0 deletions proto/whisk/api/shared/v1/context.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

import "whisk/api/shared/v1/custom.proto";

enum App {
APP_UNSPECIFIED = 0;
APP_WEB = 1 [ (entry_name) = "web" ];
APP_ANDROID = 2 [ (entry_name) = "android" ];
APP_IOS = 3 [ (entry_name) = "ios" ];
}

message SendMessage {
option (event_name) = "Recipe Viewed";
string id = 1;
}
11 changes: 11 additions & 0 deletions proto/whisk/api/shared/v1/cusom.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

import "google/protobuf/descriptor.proto";

extend google.protobuf.EnumValueOptions {
string entry_name = 50000;
}

extend google.protobuf.MessageOptions {
string event_name = 50001;
}
6 changes: 6 additions & 0 deletions proto/whisk/api/shared/v1/global.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

message Message {
SendMessage id = 1;
App type = 2;
}
8 changes: 8 additions & 0 deletions proto/whisk/api/shared/v1/local.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

package whisk.local;

message Message {
SendMessage id = 1;
App type = 2;
}
10 changes: 5 additions & 5 deletions src/generator/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { isString } from '@whisklabs/typeguards';

import { Parser } from '../parser';
import { MakeOuts } from './generator';
import { checkDublicate, safeString } from './utils';
import { checkDublicate, joinPath, safeString } from './utils';

export function collectEmuns(pack: string, out: MakeOuts, items: Parser.Enum[]) {
for (const msg of items) {
enu(`${pack}_${msg.name}`, out, msg);
enu(joinPath(pack, msg.name), out, msg);
}
}

Expand All @@ -18,7 +18,7 @@ function enu(pack: string, out: MakeOuts, item: Parser.Enum) {

export function collectMessages(pack: string, out: MakeOuts, items: Parser.Message[], parent?: string) {
for (const msg of items) {
const newPack = `${parent ?? pack}_${msg.name}`;
const newPack = joinPath(parent ?? pack, msg.name);
message(pack, out, msg, parent);
if (msg.messages.length > 0) {
collectMessages(pack, out, msg.messages, newPack);
Expand All @@ -27,7 +27,7 @@ export function collectMessages(pack: string, out: MakeOuts, items: Parser.Messa
}

function message(pack: string, out: MakeOuts, item: Parser.Message, parent?: string) {
const base = `${safeString(parent ?? pack)}_${safeString(item.name)}`;
const base = joinPath(parent ?? pack, item.name);
collectEmuns(base, out, item.enums);
const baseName = safeString(base);
const packName = `${pack}.${item.name}`;
Expand All @@ -47,6 +47,6 @@ function service(pack: string, out: MakeOuts, item: Parser.Service) {
}

export function method(pack: string, out: MakeOuts, item: Parser.Method, serv: Parser.Service) {
const sName = `${safeString(pack)}_${serv.name}_${item.name}`;
const sName = joinPath(pack, serv.name, item.name);
checkDublicate(sName, out, `${pack}.${serv.name}.${item.name}`);
}
4 changes: 2 additions & 2 deletions src/generator/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { isNumber, isText } from '@whisklabs/typeguards';

import { Parser } from '../parser';
import { MakeOuts } from './generator';
import { checkSame, safeString, toComment } from './utils';
import { checkSame, joinPath, safeString, toComment } from './utils';

export function enums(pack: string, out: MakeOuts, items: Parser.Enum[]) {
for (const msg of items) {
enu(`${pack}_${msg.name}`, out, msg);
enu(joinPath(pack, msg.name), out, msg);
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/generator/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isPresent, isString } from '@whisklabs/typeguards';
import { Parser } from '../parser';
import { GOOGLE_WRAPPERS, TYPES } from './constants';
import { MakeOuts } from './generator';
import { safeString } from './utils';
import { joinPath, safeString } from './utils';

export const getField = (field: Parser.Field, base: string, name: string, out: MakeOuts, baseName: string): string => {
const type = field.type;
Expand Down Expand Up @@ -44,8 +44,9 @@ export const getStruct = (field: Parser.Field, base: string, out: MakeOuts, base

export const pathField = (field: string, base: string, out: MakeOuts, parent?: string) => {
let inRoot = false;

out.packagesList.forEach(root => {
inRoot = inRoot || field.startsWith(root);
inRoot = inRoot || (root.length > 0 && field.startsWith(root));
});

// Absolute path
Expand All @@ -56,11 +57,13 @@ export const pathField = (field: string, base: string, out: MakeOuts, parent?: s
// Relative path with recursive check
if (isString(parent) && field.indexOf('.') > -1) {
const safeBase = safeString(base);
const par = (parent.startsWith(safeBase) ? parent.slice(safeBase.length + 1) : parent).split('_');
const par = (parent.length > 0 && parent.startsWith(safeBase) ? parent.slice(safeBase.length + 1) : parent).split(
'_'
);

while (par.length) {
const current = par.join('_');
const tryName = safeString(`${base}_${current}_${field}`);
const tryName = joinPath(base, current, field);

if (out.names.has(tryName)) {
return tryName;
Expand All @@ -70,8 +73,12 @@ export const pathField = (field: string, base: string, out: MakeOuts, parent?: s
}
}

if (out.names.has(field)) {
return joinPath(field);
}

// Common path
return safeString(`${base}_${field}`);
return joinPath(base, field);
};

export const isRequiredField = (field: Parser.Field, optional?: boolean) =>
Expand Down
11 changes: 6 additions & 5 deletions src/generator/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { GOOGLE_WRAPPERS, OPTION_MESSAGE_REQUIRED } from './constants';
import { enums } from './enum';
import { getField, getStruct, isRequiredField } from './field';
import { List, MakeOut, MakeOuts } from './generator';
import { camelCase, checkSame, errorColor, safeString, toComment } from './utils';
import { camelCase, checkSame, errorColor, joinPath, safeString, toComment } from './utils';

export function messages(
pack: string,
Expand All @@ -16,7 +16,7 @@ export function messages(
parent?: string
) {
for (const msg of items) {
const newPack = `${parent ?? pack}_${msg.name}`;
const newPack = joinPath(parent ?? pack, msg.name);
list = list.concat(
msg.messages.map(i => ({ name: i.name, pack: newPack })),
msg.enums.map(i => ({ name: i.name, pack: newPack }))
Expand All @@ -41,7 +41,7 @@ function message(
) {
const option = item.options[OPTION_MESSAGE_REQUIRED];
const isMessageRequired = isBoolean(option) ? option : messageRequired;
const base = `${safeString(parent ?? pack)}_${safeString(item.name)}`;
const base = joinPath(parent ?? pack, item.name);
enums(base, out, item.enums);

const runtime: MakeOut[] = [];
Expand Down Expand Up @@ -93,8 +93,9 @@ function message(
}],`
);

cID(field.tag, `${isString(parent) ? `${parent}.` : ''}${packName}.${field.name}`);
cName(naming, `${isString(parent) ? `${parent}.` : ''}${packName}.${field.name}`);
const pathF = `${isString(parent) ? `${parent}.` : ''}${packName}.${field.name}`;
cID(field.tag, pathF);
cName(naming, pathF);
}

for (const one in oneof) {
Expand Down
4 changes: 2 additions & 2 deletions src/generator/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isText } from '@whisklabs/typeguards';
import { Parser } from '../parser';
import { pathField } from './field';
import { MakeOuts } from './generator';
import { safeString, toComment } from './utils';
import { joinPath, toComment } from './utils';

export function services(pack: string, out: MakeOuts, items: Parser.Service[] = []) {
for (const msg of items) {
Expand All @@ -18,7 +18,7 @@ function service(pack: string, out: MakeOuts, item: Parser.Service) {
}

export function method(pack: string, out: MakeOuts, item: Parser.Method, serv: Parser.Service) {
const sName = `${safeString(pack)}_${serv.name}_${item.name}`;
const sName = joinPath(pack, serv.name, item.name);
const input = pathField(item.inputType, pack, out);
const output = pathField(item.outputType, pack, out);

Expand Down
3 changes: 3 additions & 0 deletions src/generator/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isText } from '@whisklabs/typeguards';
import { promises as fs } from 'fs';
import { join } from 'path';

Expand Down Expand Up @@ -48,3 +49,5 @@ export async function walk<T>({ filename, each, result }: WalkInit<T>): Promise<
}

export const toComment = (str: string) => `/** ${str.replace(/\*\//g, '*∕').trim()} */`;

export const joinPath = (...paths: (string | undefined)[]) => safeString(paths.filter(isText).join('_'));
63 changes: 63 additions & 0 deletions tests/proto/debug/whisk/api/shared/v1/context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"syntax": 3,
"imports": [
"whisk/api/shared/v1/custom.proto"
],
"enums": [
{
"name": "App",
"values": {
"APP_UNSPECIFIED": {
"value": 0,
"options": {}
},
"APP_WEB": {
"value": 1,
"options": {
"entry_name": "web"
}
},
"APP_ANDROID": {
"value": 2,
"options": {
"entry_name": "android"
}
},
"APP_IOS": {
"value": 3,
"options": {
"entry_name": "ios"
}
}
},
"options": {}
}
],
"messages": [
{
"name": "SendMessage",
"enums": [],
"options": {
"event_name": "Recipe Viewed"
},
"messages": [],
"fields": [
{
"name": "id",
"type": "string",
"tag": 1,
"required": false,
"repeated": false,
"optional": false,
"options": {}
}
],
"extends": [],
"extensions": [],
"reserved": []
}
],
"options": {},
"extends": [],
"services": []
}
58 changes: 58 additions & 0 deletions tests/proto/debug/whisk/api/shared/v1/cusom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"syntax": 3,
"imports": [
"google/protobuf/descriptor.proto"
],
"enums": [],
"messages": [],
"options": {},
"extends": [
{
"name": "google.protobuf.EnumValueOptions",
"message": {
"name": "google.protobuf.EnumValueOptions",
"enums": [],
"options": {},
"messages": [],
"fields": [
{
"name": "entry_name",
"type": "string",
"tag": 50000,
"required": false,
"repeated": false,
"optional": false,
"options": {}
}
],
"extends": [],
"extensions": [],
"reserved": []
}
},
{
"name": "google.protobuf.MessageOptions",
"message": {
"name": "google.protobuf.MessageOptions",
"enums": [],
"options": {},
"messages": [],
"fields": [
{
"name": "event_name",
"type": "string",
"tag": 50001,
"required": false,
"repeated": false,
"optional": false,
"options": {}
}
],
"extends": [],
"extensions": [],
"reserved": []
}
}
],
"services": []
}

0 comments on commit b38c24d

Please sign in to comment.