-
Notifications
You must be signed in to change notification settings - Fork 353
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
feat: framework-agnostic service definitions #316
Merged
stephenh
merged 6 commits into
stephenh:main
from
aikoven:feat/generic-service-definition
Jun 28, 2021
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
outputServices=true |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
syntax = "proto3"; | ||
|
||
package simple; | ||
|
||
service Test { | ||
option deprecated = true; | ||
|
||
rpc Unary (TestMessage) returns (TestMessage) {} | ||
rpc ServerStreaming (TestMessage) returns (stream TestMessage) {} | ||
rpc ClientStreaming (stream TestMessage) returns (TestMessage) {} | ||
rpc BidiStreaming (stream TestMessage) returns (stream TestMessage) {} | ||
rpc Deprecated (TestMessage) returns (TestMessage) { | ||
option deprecated = true; | ||
} | ||
rpc Idempotent (TestMessage) returns (TestMessage) { | ||
option idempotency_level = IDEMPOTENT; | ||
} | ||
rpc NoSideEffects (TestMessage) returns (TestMessage) { | ||
option idempotency_level = NO_SIDE_EFFECTS; | ||
} | ||
} | ||
|
||
message TestMessage { | ||
string value = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* eslint-disable */ | ||
import { util, configure, Writer, Reader } from 'protobufjs/minimal'; | ||
import * as Long from 'long'; | ||
|
||
export const protobufPackage = 'simple'; | ||
|
||
export interface TestMessage { | ||
value: string; | ||
} | ||
|
||
const baseTestMessage: object = { value: '' }; | ||
|
||
export const TestMessage = { | ||
encode(message: TestMessage, writer: Writer = Writer.create()): Writer { | ||
if (message.value !== '') { | ||
writer.uint32(10).string(message.value); | ||
} | ||
return writer; | ||
}, | ||
|
||
decode(input: Reader | Uint8Array, length?: number): TestMessage { | ||
const reader = input instanceof Reader ? input : new Reader(input); | ||
let end = length === undefined ? reader.len : reader.pos + length; | ||
const message = { ...baseTestMessage } as TestMessage; | ||
while (reader.pos < end) { | ||
const tag = reader.uint32(); | ||
switch (tag >>> 3) { | ||
case 1: | ||
message.value = reader.string(); | ||
break; | ||
default: | ||
reader.skipType(tag & 7); | ||
break; | ||
} | ||
} | ||
return message; | ||
}, | ||
|
||
fromJSON(object: any): TestMessage { | ||
const message = { ...baseTestMessage } as TestMessage; | ||
if (object.value !== undefined && object.value !== null) { | ||
message.value = String(object.value); | ||
} else { | ||
message.value = ''; | ||
} | ||
return message; | ||
}, | ||
|
||
toJSON(message: TestMessage): unknown { | ||
const obj: any = {}; | ||
message.value !== undefined && (obj.value = message.value); | ||
return obj; | ||
}, | ||
|
||
fromPartial(object: DeepPartial<TestMessage>): TestMessage { | ||
const message = { ...baseTestMessage } as TestMessage; | ||
if (object.value !== undefined && object.value !== null) { | ||
message.value = object.value; | ||
} else { | ||
message.value = ''; | ||
} | ||
return message; | ||
}, | ||
}; | ||
|
||
/** @deprecated */ | ||
export const TestDefinition = { | ||
name: 'Test', | ||
fullName: 'simple.Test', | ||
methods: { | ||
unary: { | ||
name: 'Unary', | ||
requestType: TestMessage, | ||
requestStream: false, | ||
responseType: TestMessage, | ||
responseStream: false, | ||
options: {}, | ||
}, | ||
serverStreaming: { | ||
name: 'ServerStreaming', | ||
requestType: TestMessage, | ||
requestStream: false, | ||
responseType: TestMessage, | ||
responseStream: true, | ||
options: {}, | ||
}, | ||
clientStreaming: { | ||
name: 'ClientStreaming', | ||
requestType: TestMessage, | ||
requestStream: true, | ||
responseType: TestMessage, | ||
responseStream: false, | ||
options: {}, | ||
}, | ||
bidiStreaming: { | ||
name: 'BidiStreaming', | ||
requestType: TestMessage, | ||
requestStream: true, | ||
responseType: TestMessage, | ||
responseStream: true, | ||
options: {}, | ||
}, | ||
/** @deprecated */ | ||
deprecated: { | ||
name: 'Deprecated', | ||
requestType: TestMessage, | ||
requestStream: false, | ||
responseType: TestMessage, | ||
responseStream: false, | ||
options: {}, | ||
}, | ||
idempotent: { | ||
name: 'Idempotent', | ||
requestType: TestMessage, | ||
requestStream: false, | ||
responseType: TestMessage, | ||
responseStream: false, | ||
options: { | ||
idempotencyLevel: 'IDEMPOTENT', | ||
}, | ||
}, | ||
noSideEffects: { | ||
name: 'NoSideEffects', | ||
requestType: TestMessage, | ||
requestStream: false, | ||
responseType: TestMessage, | ||
responseStream: false, | ||
options: { | ||
idempotencyLevel: 'NO_SIDE_EFFECTS', | ||
}, | ||
}, | ||
}, | ||
} as const; | ||
|
||
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; | ||
export 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>; | ||
|
||
// If you get a compile-error about 'Constructor<Long> and ... have no overlap', | ||
// add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. | ||
if (util.Long !== Long) { | ||
util.Long = Long as any; | ||
configure(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { Code, code, def, joinCode } from 'ts-poet'; | ||
import { | ||
FileDescriptorProto, | ||
MethodDescriptorProto, | ||
MethodOptions, | ||
MethodOptions_IdempotencyLevel, | ||
ServiceDescriptorProto, | ||
} from 'ts-proto-descriptors'; | ||
import { camelCase } from './case'; | ||
import { Context } from './context'; | ||
import SourceInfo, { Fields } from './sourceInfo'; | ||
import { messageToTypeName } from './types'; | ||
import { maybeAddComment, maybePrefixPackage } from './utils'; | ||
|
||
/** | ||
* Generates a framework-agnostic service descriptor. | ||
*/ | ||
export function generateServiceDefinition( | ||
ctx: Context, | ||
fileDesc: FileDescriptorProto, | ||
sourceInfo: SourceInfo, | ||
serviceDesc: ServiceDescriptorProto | ||
) { | ||
const chunks: Code[] = []; | ||
|
||
maybeAddComment(sourceInfo, chunks, serviceDesc.options?.deprecated); | ||
|
||
// Service definition | ||
chunks.push(code` | ||
export const ${def(`${serviceDesc.name}Definition`)} = { | ||
`); | ||
|
||
serviceDesc.options?.uninterpretedOption; | ||
chunks.push(code` | ||
name: '${serviceDesc.name}', | ||
fullName: '${maybePrefixPackage(fileDesc, serviceDesc.name)}', | ||
methods: { | ||
`); | ||
|
||
for (const [index, methodDesc] of serviceDesc.method.entries()) { | ||
const info = sourceInfo.lookup(Fields.service.method, index); | ||
maybeAddComment(info, chunks, methodDesc.options?.deprecated); | ||
|
||
chunks.push(code` | ||
${camelCase(methodDesc.name)}: ${generateMethodDefinition(ctx, methodDesc)}, | ||
`); | ||
} | ||
|
||
chunks.push(code` | ||
}, | ||
} as const; | ||
`); | ||
|
||
return joinCode(chunks, { on: '\n' }); | ||
} | ||
|
||
function generateMethodDefinition(ctx: Context, methodDesc: MethodDescriptorProto) { | ||
const inputType = messageToTypeName(ctx, methodDesc.inputType, { keepValueType: true }); | ||
const outputType = messageToTypeName(ctx, methodDesc.outputType, { keepValueType: true }); | ||
|
||
return code` | ||
{ | ||
name: '${methodDesc.name}', | ||
requestType: ${inputType}, | ||
requestStream: ${methodDesc.clientStreaming}, | ||
responseType: ${outputType}, | ||
responseStream: ${methodDesc.serverStreaming}, | ||
options: ${generateMethodOptions(methodDesc.options)} | ||
} | ||
`; | ||
} | ||
|
||
function generateMethodOptions(options: MethodOptions | undefined) { | ||
const chunks: Code[] = []; | ||
|
||
chunks.push(code`{`); | ||
|
||
if (options != null) { | ||
if (options.idempotencyLevel === MethodOptions_IdempotencyLevel.IDEMPOTENT) { | ||
chunks.push(code`idempotencyLevel: 'IDEMPOTENT',`); | ||
} else if (options.idempotencyLevel === MethodOptions_IdempotencyLevel.NO_SIDE_EFFECTS) { | ||
chunks.push(code`idempotencyLevel: 'NO_SIDE_EFFECTS',`); | ||
} | ||
} | ||
|
||
chunks.push(code`}`); | ||
|
||
return joinCode(chunks, { on: '\n' }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ export type Options = { | |
stringEnums: boolean; | ||
constEnums: boolean; | ||
outputClientImpl: boolean | 'grpc-web'; | ||
outputServices: false | 'grpc-js'; | ||
outputServices: boolean | 'grpc-js'; | ||
aikoven marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to add documentation on the option to the Readme There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
addGrpcMetadata: boolean; | ||
addNestjsRestParameter: boolean; | ||
returnObservable: boolean; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the
Definition
suffix as per discussion.Though now it seems that the suffix should be
ServiceDefinition
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @stephenh, thank you for review. What do you think about this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After some thinking, I'm fine with
Definition
suffix as long as we don't overload this term and only use it for services.