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

Added options to disable generation of serializers, to/from json, and… #28

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
104 changes: 61 additions & 43 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export type Options = {
useContext: boolean;
snakeToCamel: boolean;
forceLong: boolean;
serializers: boolean;
n3rdyme marked this conversation as resolved.
Show resolved Hide resolved
toFromJson: boolean;
serviceStub: boolean;
};

export function generateFile(typeMap: TypeMap, fileDesc: FileDescriptorProto, parameter: string): FileSpec {
Expand Down Expand Up @@ -80,60 +83,75 @@ export function generateFile(typeMap: TypeMap, fileDesc: FileDescriptorProto, pa
}
);

// then add the encoder/decoder/base instance
visit(
fileDesc,
(fullName, message) => {
file = file.addProperty(generateBaseInstance(fullName, message, options));
let staticMethods = CodeBlock.empty()
.add('export const %L = ', fullName)
.beginHash()
.addHashEntry(generateEncode(typeMap, fullName, message, options))
.addHashEntry(generateDecode(typeMap, fullName, message, options))
.addHashEntry(generateFromJson(typeMap, fullName, message, options))
.addHashEntry(generateFromPartial(typeMap, fullName, message, options))
.addHashEntry(generateToJson(typeMap, fullName, message, options))
.endHash()
.add(';')
.newLine();
file = file.addCode(staticMethods);
},
options,
(fullName, enumDesc) => {
let staticMethods = CodeBlock.empty()
.beginControlFlow('export namespace %L', fullName)
.addFunction(generateEnumFromJson(fullName, enumDesc))
.addFunction(generateEnumToJson(fullName, enumDesc))
.endControlFlow();
file = file.addCode(staticMethods);
}
);
if (options.serializers || options.toFromJson) {
// then add the encoder/decoder/base instance
visit(
fileDesc,
(fullName, message) => {
file = file.addProperty(generateBaseInstance(fullName, message, options));
let staticMethods = CodeBlock.empty()
.add('export const %L = ', fullName)
.beginHash();

staticMethods = !options.serializers
? staticMethods
: staticMethods.addHashEntry(generateEncode(typeMap, fullName, message, options))
.addHashEntry(generateDecode(typeMap, fullName, message, options));

staticMethods = !options.toFromJson
? staticMethods
: staticMethods.addHashEntry(generateFromJson(typeMap, fullName, message, options))
.addHashEntry(generateFromPartial(typeMap, fullName, message, options))
.addHashEntry(generateToJson(typeMap, fullName, message, options))

staticMethods = staticMethods.endHash()
.add(';')
.newLine();
file = file.addCode(staticMethods);
},
options,
(fullName, enumDesc) => {
if (!options.toFromJson) {
return;
}
let staticMethods = CodeBlock.empty()
.beginControlFlow('export namespace %L', fullName)
.addFunction(generateEnumFromJson(fullName, enumDesc))
.addFunction(generateEnumToJson(fullName, enumDesc))
.endControlFlow();
file = file.addCode(staticMethods);
}
);
}

visitServices(fileDesc, serviceDesc => {
file = file.addInterface(generateService(typeMap, fileDesc, serviceDesc, options));
file = file.addClass(generateServiceClientImpl(typeMap, fileDesc, serviceDesc, options));
file = !options.serviceStub ? file :
file.addClass(generateServiceClientImpl(typeMap, fileDesc, serviceDesc, options));
});

if (fileDesc.service.length > 0) {
if (options.serviceStub && fileDesc.service.length > 0) {
file = file.addInterface(generateRpcType(options));
if (options.useContext) {
file = file.addInterface(generateDataLoadersType(options));
}
}

file = addLongUtilityMethod(file, options);
file = addDeepPartialType(file);

let hasAnyTimestamps = false;
visit(
fileDesc,
(_, messageType) => {
hasAnyTimestamps = hasAnyTimestamps || asSequence(messageType.field).any(isTimestamp);
},
options
);
if (hasAnyTimestamps) {
file = addTimestampMethods(file, options);
if (options.serializers || options.toFromJson) {
file = addLongUtilityMethod(file, options);
file = addDeepPartialType(file);

let hasAnyTimestamps = false;
visit(
fileDesc,
(_, messageType) => {
hasAnyTimestamps = hasAnyTimestamps || asSequence(messageType.field).any(isTimestamp);
},
options
);
if (hasAnyTimestamps) {
file = addTimestampMethods(file, options);
}
}

return file;
Expand Down
11 changes: 10 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { generateFile } from './main';
import { createTypeMap } from './types';
import CodeGeneratorRequest = google.protobuf.compiler.CodeGeneratorRequest;
import CodeGeneratorResponse = google.protobuf.compiler.CodeGeneratorResponse;
import { FileSpec } from 'ts-poet';

// Comment block at the top of every source file, since these comments require specific
// syntax incompatible with ts-poet, we will hard-code the string and prepend to the
// generator output.
const formatSourceFile = (spec: FileSpec) =>
`// @ts-nocheck
/* eslint-disable */
n3rdyme marked this conversation as resolved.
Show resolved Hide resolved
${spec}`;

// this would be the plugin called by the protoc compiler
async function main() {
Expand All @@ -17,7 +26,7 @@ async function main() {
const spec = generateFile(typeMap, file, request.parameter);
return new CodeGeneratorResponse.File({
name: spec.path,
content: spec.toString()
content: formatSourceFile(spec)
});
});
const response = new CodeGeneratorResponse({ file: files });
Expand Down
19 changes: 18 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ export function upperFirst(name: string): string {
}

export function optionsFromParameter(parameter: string): Options {
const options: Options = { useContext: false, snakeToCamel: true, forceLong: false };
const options: Options = {
useContext: false,
snakeToCamel: true,
forceLong: false,
serializers: true,
toFromJson: true,
serviceStub: true,
};

if (parameter) {
if (parameter.includes('context=true')) {
options.useContext = true;
Expand All @@ -46,6 +54,15 @@ export function optionsFromParameter(parameter: string): Options {
if (parameter.includes('forceLong=true')) {
options.forceLong = true;
}
if (parameter.includes('serializers=false')) {
options.serializers = false;
}
if (parameter.includes('toFromJson=false')) {
options.toFromJson = false;
}
if (parameter.includes('serviceStub=false')) {
options.serviceStub = false;
}
}
return options;
}