From f309a46ef778d127b2cc7943dd9a1f0cb66f26da Mon Sep 17 00:00:00 2001 From: Yun Lai Date: Wed, 24 Jun 2020 13:22:53 +0000 Subject: [PATCH] add the ability to do server side streaming and added examples --- generator/template.go | 6 ++++ registry/service.go | 2 +- testdata/datasource/datasource.ts | 3 -- testdata/environment.ts | 3 -- testdata/log.ts | 55 ++++--------------------------- 5 files changed, 13 insertions(+), 56 deletions(-) diff --git a/generator/template.go b/generator/template.go index 1a9aa60..8f7aea0 100644 --- a/generator/template.go +++ b/generator/template.go @@ -48,10 +48,16 @@ export type {{.Name}} = { {{define "services"}}{{range .}}export class {{.Name}} { {{- range .Methods}} +{{- if .ServerStreaming }} + static {{.Name}}(req: {{tsType .Input}}, entityNotifier?: gap.NotifyStreamEntityArrival<{{tsType .Output}}>): Promise> { + return gap.gapFetchGRPCStream<{{tsType .Input}}, {{tsType .Output}}>("{{.URL}}", req, entityNotifier) + } +{{- else }} static {{.Name}}(req: {{tsType .Input}}): Promise> { return gap.gapFetchGRPC<{{tsType .Input}}, {{tsType .Output}}>("{{.URL}}", req) } {{- end}} +{{- end}} } {{end}}{{end}} diff --git a/registry/service.go b/registry/service.go index d4100b2..9b2bed6 100644 --- a/registry/service.go +++ b/registry/service.go @@ -44,7 +44,7 @@ func (r *Registry) analyseService(fileData *data.File, packageName string, fileN methodData := &data.Method{ Name: method.GetName(), - URL: "/" + serviceURLPart + "/" + method.GetName(), + URL: "/api/" + serviceURLPart + "/" + method.GetName(), Input: &data.MethodArgument{ Type: inputTypeFQName, IsExternal: isInputTypeExternal, diff --git a/testdata/datasource/datasource.ts b/testdata/datasource/datasource.ts index 1b2c81d..df38d3b 100644 --- a/testdata/datasource/datasource.ts +++ b/testdata/datasource/datasource.ts @@ -3,9 +3,6 @@ */ export enum DataSource { - DataCentre = "DataCentre", - Cloud = "Cloud", - } \ No newline at end of file diff --git a/testdata/environment.ts b/testdata/environment.ts index b4e462e..f780a9f 100644 --- a/testdata/environment.ts +++ b/testdata/environment.ts @@ -3,9 +3,6 @@ */ export enum Environment { - Staging = "Staging", - Production = "Production", - } \ No newline at end of file diff --git a/testdata/log.ts b/testdata/log.ts index 487c7bf..b638bb3 100644 --- a/testdata/log.ts +++ b/testdata/log.ts @@ -2,9 +2,9 @@ * This file is a generated Typescript file for GRPC Gateway, DO NOT MODIFY */ -import * as gap from "gap/admin/lib/useGapFetch" -import * as ComSquareupCashGapEnvironment from "./environment" import * as ComSquareupCashGapDatasourceDatasource from "./datasource/datasource" +import * as ComSquareupCashGapEnvironment from "./environment" +import * as gap from "gap/admin/lib/useGapFetch" type Absent = { [k in Exclude]?: undefined }; type OneOf = @@ -16,64 +16,39 @@ type OneOf = : never); export enum LogEntryLevel { - DEBUG = "DEBUG", - INFO = "INFO", - WARN = "WARN", - ERROR = "ERROR", - } export type LogEntryStackTraceException = { - type?: string - message?: string - } export type LogEntryStackTraceMethod = { - identifier?: string - file?: string - line?: string - } export type LogEntryStackTrace = { - exception?: LogEntryStackTraceException - lines?: LogEntryStackTraceMethod[] - } type BaseLogEntry = { - hostname?: string - level?: LogEntryLevel - elapsed?: number - timestamp?: number - env?: ComSquareupCashGapEnvironment.Environment - hasStackTrace?: boolean - message?: string - tags?: string[] - stackTraces?: LogEntryStackTrace[] - } export type LogEntry = BaseLogEntry @@ -81,66 +56,48 @@ export type LogEntry = BaseLogEntry type BaseLogStream = { - } export type LogStream = BaseLogStream & OneOf<{ dataCentre: DataCentreLogEntries,cloud: CloudLogEntries, }> export type DataCentreLogEntries = { - logs?: LogEntry[] - } export type CloudLogEntries = { - logs?: LogEntry[] - } type BaseFetchLogRequest = { - source?: ComSquareupCashGapDatasourceDatasource.DataSource - } export type FetchLogRequest = BaseFetchLogRequest & OneOf<{ application: string,service: string, }> export type FetchLogResponse = { - result?: LogStream - } export type PushLogRequest = { - entry?: LogEntry - source?: ComSquareupCashGapDatasourceDatasource.DataSource - } export type PushLogResponse = { - success?: boolean - } export class LogService { - static FetchLog(req: FetchLogRequest): Promise> { - return gap.gapFetchGRPC("/com.squareup.cash.gap.LogService/FetchLog", req) + return gap.gapFetchGRPC("/api/com.squareup.cash.gap.LogService/FetchLog", req) } - - static StreamLog(req: FetchLogRequest): Promise> { - return gap.gapFetchGRPC("/com.squareup.cash.gap.LogService/StreamLog", req) + static StreamLog(req: FetchLogRequest, entityNotifier?: gap.NotifyStreamEntityArrival): Promise> { + return gap.gapFetchGRPCStream("/api/com.squareup.cash.gap.LogService/StreamLog", req, entityNotifier) } - static PushLog(req: PushLogRequest): Promise> { - return gap.gapFetchGRPC("/com.squareup.cash.gap.LogService/PushLog", req) + return gap.gapFetchGRPC("/api/com.squareup.cash.gap.LogService/PushLog", req) } - } \ No newline at end of file