Skip to content

Commit

Permalink
test: output-index
Browse files Browse the repository at this point in the history
  • Loading branch information
blorgon1 committed May 3, 2023
1 parent ebc369e commit 62fa8fc
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 2 deletions.
11 changes: 10 additions & 1 deletion integration/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parse } from "path";
import { promisify } from "util";
import { generateFile, makeUtils } from "../src/main";
import { createTypeMap } from "../src/types";
import { prefixDisableLinter } from "../src/utils";
import { generateIndexFiles } from "../src/utils";
import { getTsPoetOpts, optionsFromParameter } from "../src/options";
import { Context } from "../src/context";
import { generateTypeRegistry } from "../src/generate-type-registry";
Expand Down Expand Up @@ -56,6 +56,15 @@ async function generate(binFile: string, baseDir: string, parameter: string) {

await promisify(writeFile)(filePath, code.toString({ ...getTsPoetOpts(options), path }));
}

if (options.outputIndex) {
for (const [path, code] of generateIndexFiles(request.protoFile, options)) {
const filePath = `${baseDir}/${path}`;
const dirPath = parse(filePath).dir;
await promisify(mkdir)(dirPath, { recursive: true }).catch(() => {});
await promisify(writeFile)(filePath, code.toString({ ...getTsPoetOpts(options), path }));
}
}
}

main().then(() => {
Expand Down
14 changes: 14 additions & 0 deletions integration/output-index/a-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Index from '.';

describe('output-index', () => {
it('generates index files correctly', () => {
expect(Index).toMatchObject({
base: {
A: {
encode: expect.any(Function),
decode: expect.any(Function),
},
},
});
});
});
Binary file added integration/output-index/a.bin
Binary file not shown.
6 changes: 6 additions & 0 deletions integration/output-index/a.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";
package base;

message A {
string a = 1;
}
77 changes: 77 additions & 0 deletions integration/output-index/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* eslint-disable */
import * as _m0 from "protobufjs/minimal";

export interface A {
a: string;
}

function createBaseA(): A {
return { a: "" };
}

export const A = {
encode(message: A, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.a !== "") {
writer.uint32(10).string(message.a);
}
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): A {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseA();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
if (tag !== 10) {
break;
}

message.a = reader.string();
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},

fromJSON(object: any): A {
return { a: isSet(object.a) ? String(object.a) : "" };
},

toJSON(message: A): unknown {
const obj: any = {};
message.a !== undefined && (obj.a = message.a);
return obj;
},

create<I extends Exact<DeepPartial<A>, I>>(base?: I): A {
return A.fromPartial(base ?? {});
},

fromPartial<I extends Exact<DeepPartial<A>, I>>(object: I): A {
const message = createBaseA();
message.a = object.a ?? "";
return message;
},
};

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

type DeepPartial<T> = T extends Builtin ? T
: T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
type Exact<P, I extends P> = P extends Builtin ? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
3 changes: 3 additions & 0 deletions integration/output-index/index.base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable */

export * from "./a";
3 changes: 3 additions & 0 deletions integration/output-index/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable */

export * as base from "./index.base";
1 change: 1 addition & 0 deletions integration/output-index/parameters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
outputIndex=true
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function generateIndexFiles(files: FileDescriptorProto[], options: Option
};
for (const { name, package: pkg } of files) {
const moduleName = name.replace(".proto", options.fileSuffix);
const pkgParts = pkg.split('.');
const pkgParts = pkg.length > 0 ? pkg.split('.') : [];

const branch = pkgParts.reduce<PackageTree>((branch, part, i): PackageTree => {
if (!(part in branch.leaves)) {
Expand Down

0 comments on commit 62fa8fc

Please sign in to comment.