Updating working code (#6441)
authormustiikhalil <mustii@mmk.one>
Wed, 10 Feb 2021 19:46:45 +0000 (22:46 +0300)
committerGitHub <noreply@github.com>
Wed, 10 Feb 2021 19:46:45 +0000 (11:46 -0800)
Fixes issues with namespaces in grpc ts

Renamed welcome -> models

.gitignore
grpc/flatbuffers-js-grpc/src/client.ts
grpc/flatbuffers-js-grpc/src/greeter.fbs
grpc/flatbuffers-js-grpc/src/greeter_generated.ts [deleted file]
grpc/flatbuffers-js-grpc/src/greeter_grpc.d.ts
grpc/flatbuffers-js-grpc/src/greeter_grpc.js
grpc/flatbuffers-js-grpc/src/models/hello-reply.ts [new file with mode: 0644]
grpc/flatbuffers-js-grpc/src/models/hello-request.ts [new file with mode: 0644]
grpc/flatbuffers-js-grpc/src/server.ts
grpc/src/compiler/ts_generator.cc

index b783d0a..bd5f231 100644 (file)
@@ -139,3 +139,4 @@ yarn-error.log
 .cache/
 /flatbuffers.lib
 .cmake/
+**/dist
\ No newline at end of file
index 00d1009..264b47d 100644 (file)
@@ -1,10 +1,11 @@
 import grpc from 'grpc';
-import { HelloRequest } from './greeter_generated';
+import { HelloReply } from './models/hello-reply';
+import { HelloRequest } from './models/hello-request';
 import { GreeterClient } from './greeter_grpc';
 import { flatbuffers } from 'flatbuffers';
 
-async function main() {
-    const _server = new GreeterClient('localhost:3000', grpc.credentials.createInsecure());
+async function main(PORT: Number) {
+    const _server = new GreeterClient(`localhost:${PORT}`, grpc.credentials.createInsecure());
     const builder = new flatbuffers.Builder();
     const offset = builder.createString('mustii');
     const root = HelloRequest.createHelloRequest(builder, offset);
@@ -25,4 +26,9 @@ async function main() {
     });
 }
 
-main();
\ No newline at end of file
+var PORT = Number(process.argv.slice(2));
+if (PORT) {
+    main(PORT);
+} else {
+    throw new Error("Requires a valid port number.")
+}
\ No newline at end of file
index b510884..196d6cd 100644 (file)
@@ -1,3 +1,5 @@
+namespace models;
+
 table HelloReply {
   message:string;
 }
@@ -7,6 +9,6 @@ table HelloRequest {
 }
 
 rpc_service Greeter {
-  SayHello(HelloRequest):HelloReply;
-  SayManyHellos(HelloRequest):HelloReply (streaming: "server");
+  SayHello(models.HelloRequest):models.HelloReply;
+  SayManyHellos(models.HelloRequest):models.HelloReply (streaming: "server");
 }
diff --git a/grpc/flatbuffers-js-grpc/src/greeter_generated.ts b/grpc/flatbuffers-js-grpc/src/greeter_generated.ts
deleted file mode 100644 (file)
index 87113f9..0000000
+++ /dev/null
@@ -1,174 +0,0 @@
-// automatically generated by the FlatBuffers compiler, do not modify
-
-/**
- * @constructor
- */
-export class HelloReply {
-  bb: flatbuffers.ByteBuffer|null = null;
-
-  bb_pos:number = 0;
-/**
- * @param number i
- * @param flatbuffers.ByteBuffer bb
- * @returns HelloReply
- */
-__init(i:number, bb:flatbuffers.ByteBuffer):HelloReply {
-  this.bb_pos = i;
-  this.bb = bb;
-  return this;
-};
-
-/**
- * @param flatbuffers.ByteBuffer bb
- * @param HelloReply= obj
- * @returns HelloReply
- */
-static getRootAsHelloReply(bb:flatbuffers.ByteBuffer, obj?:HelloReply):HelloReply {
-  return (obj || new HelloReply()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
-};
-
-/**
- * @param flatbuffers.ByteBuffer bb
- * @param HelloReply= obj
- * @returns HelloReply
- */
-static getSizePrefixedRootAsHelloReply(bb:flatbuffers.ByteBuffer, obj?:HelloReply):HelloReply {
-  bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
-  return (obj || new HelloReply()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
-};
-
-/**
- * @param flatbuffers.Encoding= optionalEncoding
- * @returns string|Uint8Array|null
- */
-message():string|null
-message(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null
-message(optionalEncoding?:any):string|Uint8Array|null {
-  var offset = this.bb!.__offset(this.bb_pos, 4);
-  return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null;
-};
-
-/**
- * @param flatbuffers.Builder builder
- */
-static startHelloReply(builder:flatbuffers.Builder) {
-  builder.startObject(1);
-};
-
-/**
- * @param flatbuffers.Builder builder
- * @param flatbuffers.Offset messageOffset
- */
-static addMessage(builder:flatbuffers.Builder, messageOffset:flatbuffers.Offset) {
-  builder.addFieldOffset(0, messageOffset, 0);
-};
-
-/**
- * @param flatbuffers.Builder builder
- * @returns flatbuffers.Offset
- */
-static endHelloReply(builder:flatbuffers.Builder):flatbuffers.Offset {
-  var offset = builder.endObject();
-  return offset;
-};
-
-static createHelloReply(builder:flatbuffers.Builder, messageOffset:flatbuffers.Offset):flatbuffers.Offset {
-  HelloReply.startHelloReply(builder);
-  HelloReply.addMessage(builder, messageOffset);
-  return HelloReply.endHelloReply(builder);
-}
-
-serialize():Uint8Array {
-  return this.bb!.bytes();
-}
-
-static deserialize(buffer: Uint8Array):HelloReply {
-  return HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(buffer))
-}
-}
-/**
- * @constructor
- */
-export class HelloRequest {
-  bb: flatbuffers.ByteBuffer|null = null;
-
-  bb_pos:number = 0;
-/**
- * @param number i
- * @param flatbuffers.ByteBuffer bb
- * @returns HelloRequest
- */
-__init(i:number, bb:flatbuffers.ByteBuffer):HelloRequest {
-  this.bb_pos = i;
-  this.bb = bb;
-  return this;
-};
-
-/**
- * @param flatbuffers.ByteBuffer bb
- * @param HelloRequest= obj
- * @returns HelloRequest
- */
-static getRootAsHelloRequest(bb:flatbuffers.ByteBuffer, obj?:HelloRequest):HelloRequest {
-  return (obj || new HelloRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
-};
-
-/**
- * @param flatbuffers.ByteBuffer bb
- * @param HelloRequest= obj
- * @returns HelloRequest
- */
-static getSizePrefixedRootAsHelloRequest(bb:flatbuffers.ByteBuffer, obj?:HelloRequest):HelloRequest {
-  bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
-  return (obj || new HelloRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
-};
-
-/**
- * @param flatbuffers.Encoding= optionalEncoding
- * @returns string|Uint8Array|null
- */
-name():string|null
-name(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null
-name(optionalEncoding?:any):string|Uint8Array|null {
-  var offset = this.bb!.__offset(this.bb_pos, 4);
-  return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null;
-};
-
-/**
- * @param flatbuffers.Builder builder
- */
-static startHelloRequest(builder:flatbuffers.Builder) {
-  builder.startObject(1);
-};
-
-/**
- * @param flatbuffers.Builder builder
- * @param flatbuffers.Offset nameOffset
- */
-static addName(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset) {
-  builder.addFieldOffset(0, nameOffset, 0);
-};
-
-/**
- * @param flatbuffers.Builder builder
- * @returns flatbuffers.Offset
- */
-static endHelloRequest(builder:flatbuffers.Builder):flatbuffers.Offset {
-  var offset = builder.endObject();
-  return offset;
-};
-
-static createHelloRequest(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset):flatbuffers.Offset {
-  HelloRequest.startHelloRequest(builder);
-  HelloRequest.addName(builder, nameOffset);
-  return HelloRequest.endHelloRequest(builder);
-}
-
-serialize():Uint8Array {
-  return this.bb!.bytes();
-}
-
-static deserialize(buffer: Uint8Array):HelloRequest {
-  return HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(buffer))
-}
-}
index acd4c2f..402d1ab 100644 (file)
@@ -1,6 +1,7 @@
 // Generated GRPC code for FlatBuffers TS *** DO NOT EDIT ***
 import { flatbuffers } from 'flatbuffers';
-import *  as Greeter_fbs from './greeter_generated';
+import { HelloReply as models_HelloReply } from './models/hello-reply';
+import { HelloRequest as models_HelloRequest } from './models/hello-request';
 
 import * as grpc from 'grpc';
 
@@ -8,47 +9,47 @@ interface IGreeterService extends grpc.ServiceDefinition<grpc.UntypedServiceImpl
   SayHello: IGreeterService_ISayHello;
   SayManyHellos: IGreeterService_ISayManyHellos;
 }
-interface IGreeterService_ISayHello extends grpc.MethodDefinition<Greeter_fbs.HelloRequest, Greeter_fbs.HelloReply> {
-  path: string; // /Greeter/SayHello
+interface IGreeterService_ISayHello extends grpc.MethodDefinition<models_HelloRequest, models_HelloReply> {
+  path: string; // /models.Greeter/SayHello
   requestStream: boolean; // false
   responseStream: boolean; // false
-  requestSerialize: grpc.serialize<Greeter_fbs.HelloRequest>;
-  requestDeserialize: grpc.deserialize<Greeter_fbs.HelloRequest>;
-  responseSerialize: grpc.serialize<Greeter_fbs.HelloReply>;
-  responseDeserialize: grpc.deserialize<Greeter_fbs.HelloReply>;
+  requestSerialize: grpc.serialize<models_HelloRequest>;
+  requestDeserialize: grpc.deserialize<models_HelloRequest>;
+  responseSerialize: grpc.serialize<models_HelloReply>;
+  responseDeserialize: grpc.deserialize<models_HelloReply>;
 }
 
-interface IGreeterService_ISayManyHellos extends grpc.MethodDefinition<Greeter_fbs.HelloRequest, Greeter_fbs.HelloReply> {
-  path: string; // /Greeter/SayManyHellos
+interface IGreeterService_ISayManyHellos extends grpc.MethodDefinition<models_HelloRequest, models_HelloReply> {
+  path: string; // /models.Greeter/SayManyHellos
   requestStream: boolean; // false
   responseStream: boolean; // true
-  requestSerialize: grpc.serialize<Greeter_fbs.HelloRequest>;
-  requestDeserialize: grpc.deserialize<Greeter_fbs.HelloRequest>;
-  responseSerialize: grpc.serialize<Greeter_fbs.HelloReply>;
-  responseDeserialize: grpc.deserialize<Greeter_fbs.HelloReply>;
+  requestSerialize: grpc.serialize<models_HelloRequest>;
+  requestDeserialize: grpc.deserialize<models_HelloRequest>;
+  responseSerialize: grpc.serialize<models_HelloReply>;
+  responseDeserialize: grpc.deserialize<models_HelloReply>;
 }
 
 
 export const GreeterService: IGreeterService;
 
 export interface IGreeterServer {
-  SayHello: grpc.handleUnaryCall<Greeter_fbs.HelloRequest, Greeter_fbs.HelloReply>;
-  SayManyHellos: grpc.handleServerStreamingCall<Greeter_fbs.HelloRequest, Greeter_fbs.HelloReply>;
+  SayHello: grpc.handleUnaryCall<models_HelloRequest, models_HelloReply>;
+  SayManyHellos: grpc.handleServerStreamingCall<models_HelloRequest, models_HelloReply>;
 }
 
 export interface IGreeterClient {
-  SayHello(request: Greeter_fbs.HelloRequest, callback: (error: grpc.ServiceError | null, response: Greeter_fbs.HelloReply) => void): grpc.ClientUnaryCall;
-  SayHello(request: Greeter_fbs.HelloRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: Greeter_fbs.HelloReply) => void): grpc.ClientUnaryCall;
-  SayHello(request: Greeter_fbs.HelloRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: Greeter_fbs.HelloReply) => void): grpc.ClientUnaryCall;
-  SayManyHellos(request: Greeter_fbs.HelloRequest, metadata: grpc.Metadata): grpc.ClientReadableStream<Greeter_fbs.HelloReply>;
-  SayManyHellos(request: Greeter_fbs.HelloRequest, options: Partial<grpc.CallOptions>): grpc.ClientReadableStream<Greeter_fbs.HelloReply>;
+  SayHello(request: models_HelloRequest, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
+  SayHello(request: models_HelloRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
+  SayHello(request: models_HelloRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
+  SayManyHellos(request: models_HelloRequest, metadata: grpc.Metadata): grpc.ClientReadableStream<models_HelloReply>;
+  SayManyHellos(request: models_HelloRequest, options: Partial<grpc.CallOptions>): grpc.ClientReadableStream<models_HelloReply>;
 }
 
 export class GreeterClient extends grpc.Client implements IGreeterClient {
-  constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);  public SayHello(request: Greeter_fbs.HelloRequest, callback: (error: grpc.ServiceError | null, response: Greeter_fbs.HelloReply) => void): grpc.ClientUnaryCall;
-  public SayHello(request: Greeter_fbs.HelloRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: Greeter_fbs.HelloReply) => void): grpc.ClientUnaryCall;
-  public SayHello(request: Greeter_fbs.HelloRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: Greeter_fbs.HelloReply) => void): grpc.ClientUnaryCall;
-  public SayManyHellos(request: Greeter_fbs.HelloRequest, metadata: grpc.Metadata): grpc.ClientReadableStream<Greeter_fbs.HelloReply>;
-  public SayManyHellos(request: Greeter_fbs.HelloRequest, options: Partial<grpc.CallOptions>): grpc.ClientReadableStream<Greeter_fbs.HelloReply>;
+  constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);  public SayHello(request: models_HelloRequest, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
+  public SayHello(request: models_HelloRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
+  public SayHello(request: models_HelloRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
+  public SayManyHellos(request: models_HelloRequest, metadata: grpc.Metadata): grpc.ClientReadableStream<models_HelloReply>;
+  public SayManyHellos(request: models_HelloRequest, options: Partial<grpc.CallOptions>): grpc.ClientReadableStream<models_HelloReply>;
 }
 
index a184ba1..aa3af9a 100644 (file)
@@ -1,55 +1,56 @@
 // Generated GRPC code for FlatBuffers TS *** DO NOT EDIT ***
 import { flatbuffers } from 'flatbuffers';
-import *  as Greeter_fbs from './greeter_generated';
+import { HelloReply as models_HelloReply } from './models/hello-reply';
+import { HelloRequest as models_HelloRequest } from './models/hello-request';
 
 var grpc = require('grpc');
 
-function serialize_HelloReply(buffer_args) {
-  if (!(buffer_args instanceof Greeter_fbs.HelloReply)) {
-    throw new Error('Expected argument of type Greeter_fbs.HelloReply');
+function serialize_models_HelloReply(buffer_args) {
+  if (!(buffer_args instanceof models_HelloReply)) {
+    throw new Error('Expected argument of type HelloReply');
   }
   return buffer_args.serialize();
 }
 
-function deserialize_HelloReply(buffer) {
-  return Greeter_fbs.HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(buffer))
+function deserialize_models_HelloReply(buffer) {
+  return models_HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(buffer))
 }
 
 
-function serialize_HelloRequest(buffer_args) {
-  if (!(buffer_args instanceof Greeter_fbs.HelloRequest)) {
-    throw new Error('Expected argument of type Greeter_fbs.HelloRequest');
+function serialize_models_HelloRequest(buffer_args) {
+  if (!(buffer_args instanceof models_HelloRequest)) {
+    throw new Error('Expected argument of type HelloRequest');
   }
   return buffer_args.serialize();
 }
 
-function deserialize_HelloRequest(buffer) {
-  return Greeter_fbs.HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(buffer))
+function deserialize_models_HelloRequest(buffer) {
+  return models_HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(buffer))
 }
 
 
 var GreeterService = exports.GreeterService = {
   SayHello: {
-    path: '/Greeter/SayHello',
+    path: '/models.Greeter/SayHello',
     requestStream: false,
     responseStream: false,
     requestType: flatbuffers.ByteBuffer,
-    responseType: Greeter_fbs.HelloReply,
-    requestSerialize: serialize_HelloRequest,
-    requestDeserialize: deserialize_HelloRequest,
-    responseSerialize: serialize_HelloReply,
-    responseDeserialize: deserialize_HelloReply,
+    responseType: models_HelloReply,
+    requestSerialize: serialize_models_HelloRequest,
+    requestDeserialize: deserialize_models_HelloRequest,
+    responseSerialize: serialize_models_HelloReply,
+    responseDeserialize: deserialize_models_HelloReply,
   },
   SayManyHellos: {
-    path: '/Greeter/SayManyHellos',
+    path: '/models.Greeter/SayManyHellos',
     requestStream: false,
     responseStream: true,
     requestType: flatbuffers.ByteBuffer,
-    responseType: Greeter_fbs.HelloReply,
-    requestSerialize: serialize_HelloRequest,
-    requestDeserialize: deserialize_HelloRequest,
-    responseSerialize: serialize_HelloReply,
-    responseDeserialize: deserialize_HelloReply,
+    responseType: models_HelloReply,
+    requestSerialize: serialize_models_HelloRequest,
+    requestDeserialize: deserialize_models_HelloRequest,
+    responseSerialize: serialize_models_HelloReply,
+    responseDeserialize: deserialize_models_HelloReply,
   },
 };
 exports.GreeterClient = grpc.makeGenericClientConstructor(GreeterService);
diff --git a/grpc/flatbuffers-js-grpc/src/models/hello-reply.ts b/grpc/flatbuffers-js-grpc/src/models/hello-reply.ts
new file mode 100644 (file)
index 0000000..e68f7a4
--- /dev/null
@@ -0,0 +1,58 @@
+// automatically generated by the FlatBuffers compiler, do not modify
+
+import * as flatbuffers from 'flatbuffers';
+
+
+
+export class HelloReply {
+  bb: flatbuffers.ByteBuffer|null = null;
+  bb_pos = 0;
+__init(i:number, bb:flatbuffers.ByteBuffer):HelloReply {
+  this.bb_pos = i;
+  this.bb = bb;
+  return this;
+}
+
+static getRootAsHelloReply(bb:flatbuffers.ByteBuffer, obj?:HelloReply):HelloReply {
+  return (obj || new HelloReply()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
+}
+
+static getSizePrefixedRootAsHelloReply(bb:flatbuffers.ByteBuffer, obj?:HelloReply):HelloReply {
+  bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
+  return (obj || new HelloReply()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
+}
+
+message():string|null
+message(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null
+message(optionalEncoding?:any):string|Uint8Array|null {
+  const offset = this.bb!.__offset(this.bb_pos, 4);
+  return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null;
+}
+
+static startHelloReply(builder:flatbuffers.Builder) {
+  builder.startObject(1);
+}
+
+static addMessage(builder:flatbuffers.Builder, messageOffset:flatbuffers.Offset) {
+  builder.addFieldOffset(0, messageOffset, 0);
+}
+
+static endHelloReply(builder:flatbuffers.Builder):flatbuffers.Offset {
+  const offset = builder.endObject();
+  return offset;
+}
+
+static createHelloReply(builder:flatbuffers.Builder, messageOffset:flatbuffers.Offset):flatbuffers.Offset {
+  HelloReply.startHelloReply(builder);
+  HelloReply.addMessage(builder, messageOffset);
+  return HelloReply.endHelloReply(builder);
+}
+
+serialize():Uint8Array {
+  return this.bb!.bytes();
+}
+
+static deserialize(buffer: Uint8Array):HelloReply {
+  return HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(buffer))
+}
+}
diff --git a/grpc/flatbuffers-js-grpc/src/models/hello-request.ts b/grpc/flatbuffers-js-grpc/src/models/hello-request.ts
new file mode 100644 (file)
index 0000000..3718167
--- /dev/null
@@ -0,0 +1,58 @@
+// automatically generated by the FlatBuffers compiler, do not modify
+
+import * as flatbuffers from 'flatbuffers';
+
+
+
+export class HelloRequest {
+  bb: flatbuffers.ByteBuffer|null = null;
+  bb_pos = 0;
+__init(i:number, bb:flatbuffers.ByteBuffer):HelloRequest {
+  this.bb_pos = i;
+  this.bb = bb;
+  return this;
+}
+
+static getRootAsHelloRequest(bb:flatbuffers.ByteBuffer, obj?:HelloRequest):HelloRequest {
+  return (obj || new HelloRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
+}
+
+static getSizePrefixedRootAsHelloRequest(bb:flatbuffers.ByteBuffer, obj?:HelloRequest):HelloRequest {
+  bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
+  return (obj || new HelloRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
+}
+
+name():string|null
+name(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null
+name(optionalEncoding?:any):string|Uint8Array|null {
+  const offset = this.bb!.__offset(this.bb_pos, 4);
+  return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null;
+}
+
+static startHelloRequest(builder:flatbuffers.Builder) {
+  builder.startObject(1);
+}
+
+static addName(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset) {
+  builder.addFieldOffset(0, nameOffset, 0);
+}
+
+static endHelloRequest(builder:flatbuffers.Builder):flatbuffers.Offset {
+  const offset = builder.endObject();
+  return offset;
+}
+
+static createHelloRequest(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset):flatbuffers.Offset {
+  HelloRequest.startHelloRequest(builder);
+  HelloRequest.addName(builder, nameOffset);
+  return HelloRequest.endHelloRequest(builder);
+}
+
+serialize():Uint8Array {
+  return this.bb!.bytes();
+}
+
+static deserialize(buffer: Uint8Array):HelloRequest {
+  return HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(buffer))
+}
+}
index 47c2f89..3efb5c8 100644 (file)
@@ -1,12 +1,13 @@
 import grpc from 'grpc';
-import { HelloReply, HelloRequest } from './greeter_generated';
+import { HelloReply } from './models/hello-reply';
+import { HelloRequest } from './models/hello-request';
 import { IGreeterServer, GreeterService } from './greeter_grpc';
 import { flatbuffers } from 'flatbuffers';
 
 class GreeterServer implements IGreeterServer {
 
     SayHello(call: grpc.ServerUnaryCall<HelloRequest>, callback: grpc.sendUnaryData<HelloReply>): void {
-        console.log(`${call.request.name()}`);
+        console.log(`SayHello ${call.request.name()}`);
         const builder = new flatbuffers.Builder();
         const offset = builder.createString(`welcome ${call.request.name()}`);
         const root = HelloReply.createHelloReply(builder, offset);
index fe9731d..98c3d54 100644 (file)
 
 namespace grpc_ts_generator {
 
+grpc::string ToDasherizedCase(const grpc::string pascal_case) {
+  std::string dasherized_case;
+  char p = 0;
+  for (size_t i = 0; i < pascal_case.length(); i++) {
+    char const &c = pascal_case[i];
+    if (flatbuffers::is_alpha_upper(c)) {
+      if (i > 0 && p != flatbuffers::kPathSeparator) dasherized_case += "-";
+      dasherized_case += flatbuffers::CharToLower(c);
+    } else {
+      dasherized_case += c;
+    }
+    p = c;
+  }
+  return dasherized_case;
+}
+
+grpc::string GenerateNamespace(const std::vector<std::string> namepsace,
+                               const std::string filename,
+                               const bool include_separator) {
+  grpc::string path = "";
+  if (include_separator) path += ".";
+
+  for (auto it = namepsace.begin(); it < namepsace.end(); it++) {
+    if (include_separator) path += "/";
+    path += include_separator ? ToDasherizedCase(*it) : *it + "_";
+  }
+
+  if (include_separator) path += "/";
+  path += include_separator ? ToDasherizedCase(filename) : filename;
+  return path;
+}
+
 // MARK: - Shared code
 
-void GenerateImports(grpc_generator::Printer *printer,
+void GenerateImports(const grpc_generator::Service *service,
+                     grpc_generator::Printer *printer,
                      std::map<grpc::string, grpc::string> *dictonary,
                      const bool grpc_var_import) {
   auto vars = *dictonary;
   printer->Print(
       "// Generated GRPC code for FlatBuffers TS *** DO NOT EDIT ***\n");
   printer->Print("import { flatbuffers } from 'flatbuffers';\n");
-  printer->Print(vars,
-                 "import *  as $FBSFile$ from './$Filename$_generated';\n");
+
+  std::set<grpc::string> generated_imports;
+
+  for (auto it = 0; it < service->method_count(); it++) {
+    auto method = service->method(it);
+    auto output = method->get_output_type_name();
+    auto input = method->get_input_type_name();
+    auto input_namespace = method->get_input_namespace_parts();
+
+    vars["OUTPUT"] = output;
+    vars["INPUT"] = input;
+
+    if (generated_imports.find(output) == generated_imports.end()) {
+      generated_imports.insert(output);
+      vars["OUTPUT_DIR"] =
+          GenerateNamespace(method->get_output_namespace_parts(), output, true);
+      vars["Output_alias"] = GenerateNamespace(
+          method->get_output_namespace_parts(), output, false);
+      printer->Print(
+          vars, "import { $OUTPUT$ as $Output_alias$ } from '$OUTPUT_DIR$';\n");
+    }
+    if (generated_imports.find(input) == generated_imports.end()) {
+      generated_imports.insert(input);
+      vars["INPUT_DIR"] =
+          GenerateNamespace(method->get_output_namespace_parts(), input, true);
+      vars["Input_alias"] =
+          GenerateNamespace(method->get_output_namespace_parts(), input, false);
+      printer->Print(
+          vars, "import { $INPUT$ as $Input_alias$ } from '$INPUT_DIR$';\n");
+    }
+  }
   printer->Print("\n");
   if (grpc_var_import)
     printer->Print("var grpc = require('grpc');\n");
@@ -68,10 +130,10 @@ void GenerateSerializeMethod(grpc_generator::Printer *printer,
   auto vars = *dictonary;
   printer->Print(vars, "function serialize_$Type$(buffer_args) {\n");
   printer->Indent();
-  printer->Print(vars, "if (!(buffer_args instanceof $FBSFile$.$Type$)) {\n");
+  printer->Print(vars, "if (!(buffer_args instanceof $Type$)) {\n");
   printer->Indent();
-  printer->Print(
-      vars, "throw new Error('Expected argument of type $FBSFile$.$Type$');\n");
+  printer->Print(vars,
+                 "throw new Error('Expected argument of type $VALUE$');\n");
   printer->Outdent();
   printer->Print("}\n");
   printer->Print(vars, "return buffer_args.serialize();\n");
@@ -86,7 +148,7 @@ void GenerateDeserializeMethod(
   printer->Print(vars, "function deserialize_$Type$(buffer) {\n");
   printer->Indent();
   printer->Print(vars,
-                 "return $FBSFile$.$Type$.getRootAs$Type$(new "
+                 "return $Type$.getRootAs$VALUE$(new "
                  "flatbuffers.ByteBuffer(buffer))\n");
   printer->Outdent();
   printer->Print("}\n\n");
@@ -106,14 +168,18 @@ void GenerateMethods(const grpc_generator::Service *service,
 
     if (generated_functions.find(output) == generated_functions.end()) {
       generated_functions.insert(output);
-      vars["Type"] = output;
+      vars["VALUE"] = output;
+      vars["Type"] = GenerateNamespace(method->get_output_namespace_parts(),
+                                       output, false);
       GenerateSerializeMethod(printer, &vars);
       GenerateDeserializeMethod(printer, &vars);
     }
     printer->Print("\n");
     if (generated_functions.find(input) == generated_functions.end()) {
       generated_functions.insert(input);
-      vars["Type"] = input;
+      vars["VALUE"] = input;
+      vars["Type"] =
+          GenerateNamespace(method->get_input_namespace_parts(), input, false);
       GenerateSerializeMethod(printer, &vars);
       GenerateDeserializeMethod(printer, &vars);
     }
@@ -131,18 +197,20 @@ void GenerateService(const grpc_generator::Service *service,
   for (auto it = 0; it < service->method_count(); it++) {
     auto method = service->method(it);
     vars["MethodName"] = method->name();
-    vars["Output"] = method->get_output_type_name();
-    vars["Input"] = method->get_input_type_name();
+    vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
+                                       method->get_output_type_name(), false);
+    vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
+                                      method->get_input_type_name(), false);
     printer->Print(vars, "$MethodName$: {\n");
     printer->Indent();
     printer->Print(vars, "path: '/$PATH$$ServiceName$/$MethodName$',\n");
     GetStreamType(printer, &*method, &vars);
     printer->Print(vars, "requestType: flatbuffers.ByteBuffer,\n");
-    printer->Print(vars, "responseType: $FBSFile$.$Output$,\n");
-    printer->Print(vars, "requestSerialize: serialize_$Input$,\n");
-    printer->Print(vars, "requestDeserialize: deserialize_$Input$,\n");
-    printer->Print(vars, "responseSerialize: serialize_$Output$,\n");
-    printer->Print(vars, "responseDeserialize: deserialize_$Output$,\n");
+    printer->Print(vars, "responseType: $OUTPUT$,\n");
+    printer->Print(vars, "requestSerialize: serialize_$INPUT$,\n");
+    printer->Print(vars, "requestDeserialize: deserialize_$INPUT$,\n");
+    printer->Print(vars, "responseSerialize: serialize_$OUTPUT$,\n");
+    printer->Print(vars, "responseDeserialize: deserialize_$OUTPUT$,\n");
     printer->Outdent();
     printer->Print("},\n");
   }
@@ -168,7 +236,7 @@ grpc::string Generate(grpc_generator::File *file,
   vars["Filename"] = filename;
   auto printer = file->CreatePrinter(&output);
 
-  GenerateImports(&*printer, &vars, true);
+  GenerateImports(service, &*printer, &vars, true);
   GenerateMethods(service, &*printer, &vars);
   GenerateService(service, &*printer, &vars);
   return output;
@@ -179,22 +247,17 @@ grpc::string Generate(grpc_generator::File *file,
 void FillInterface(grpc_generator::Printer *printer,
                    std::map<grpc::string, grpc::string> *dictonary) {
   auto vars = *dictonary;
-  printer->Print(
-      vars,
-      "interface I$ServiceName$Service_I$MethodName$ extends "
-      "grpc.MethodDefinition<$FBSFile$.$INPUT$, $FBSFile$.$OUTPUT$> {\n");
+  printer->Print(vars,
+                 "interface I$ServiceName$Service_I$MethodName$ extends "
+                 "grpc.MethodDefinition<$INPUT$, $OUTPUT$> {\n");
   printer->Indent();
   printer->Print(vars, "path: string; // /$PATH$$ServiceName$/$MethodName$\n");
   printer->Print(vars, "requestStream: boolean; // $ClientStreaming$\n");
   printer->Print(vars, "responseStream: boolean; // $ServerStreaming$\n");
-  printer->Print(vars,
-                 "requestSerialize: grpc.serialize<$FBSFile$.$INPUT$>;\n");
-  printer->Print(vars,
-                 "requestDeserialize: grpc.deserialize<$FBSFile$.$INPUT$>;\n");
-  printer->Print(vars,
-                 "responseSerialize: grpc.serialize<$FBSFile$.$OUTPUT$>;\n");
-  printer->Print(
-      vars, "responseDeserialize: grpc.deserialize<$FBSFile$.$OUTPUT$>;\n");
+  printer->Print(vars, "requestSerialize: grpc.serialize<$INPUT$>;\n");
+  printer->Print(vars, "requestDeserialize: grpc.deserialize<$INPUT$>;\n");
+  printer->Print(vars, "responseSerialize: grpc.serialize<$OUTPUT$>;\n");
+  printer->Print(vars, "responseDeserialize: grpc.deserialize<$OUTPUT$>;\n");
   printer->Outdent();
   printer->Print("}\n");
 }
@@ -212,8 +275,10 @@ void GenerateInterfaces(const grpc_generator::Service *service,
     vars["ClientStreaming"] = client_streaming ? "true" : "false";
     vars["ServerStreaming"] = server_streaming ? "true" : "false";
     vars["MethodName"] = method->name();
-    vars["INPUT"] = method->get_input_type_name();
-    vars["OUTPUT"] = method->get_output_type_name();
+    vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
+                                       method->get_output_type_name(), false);
+    vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
+                                      method->get_input_type_name(), false);
     FillInterface(printer, &vars);
     printer->Print("\n");
   }
@@ -228,32 +293,32 @@ void GenerateExportedInterface(
   for (auto it = 0; it < service->method_count(); it++) {
     auto method = service->method(it);
     vars["Name"] = method->name();
-    vars["INPUT"] = method->get_input_type_name();
-    vars["OUTPUT"] = method->get_output_type_name();
+    vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
+                                       method->get_output_type_name(), false);
+    vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
+                                      method->get_input_type_name(), false);
     if (method->BidiStreaming()) {
       printer->Print(vars,
-                     "$Name$: grpc.handleBidiStreamingCall<$FBSFile$.$INPUT$, "
-                     "$FBSFile$.$OUTPUT$>;\n");
+                     "$Name$: grpc.handleBidiStreamingCall<$INPUT$, "
+                     "$OUTPUT$>;\n");
       continue;
     }
     if (method->NoStreaming()) {
       printer->Print(vars,
-                     "$Name$: grpc.handleUnaryCall<$FBSFile$.$INPUT$, "
-                     "$FBSFile$.$OUTPUT$>;\n");
+                     "$Name$: grpc.handleUnaryCall<$INPUT$, "
+                     "$OUTPUT$>;\n");
       continue;
     }
     if (method->ClientStreaming()) {
-      printer->Print(
-          vars,
-          "$Name$: grpc.handleClientStreamingCall<$FBSFile$.$INPUT$, "
-          "$FBSFile$.$OUTPUT$>;\n");
+      printer->Print(vars,
+                     "$Name$: grpc.handleClientStreamingCall<$INPUT$, "
+                     "$OUTPUT$>;\n");
       continue;
     }
     if (method->ServerStreaming()) {
-      printer->Print(
-          vars,
-          "$Name$: grpc.handleServerStreamingCall<$FBSFile$.$INPUT$, "
-          "$FBSFile$.$OUTPUT$>;\n");
+      printer->Print(vars,
+                     "$Name$: grpc.handleServerStreamingCall<$INPUT$, "
+                     "$OUTPUT$>;\n");
       continue;
     }
   }
@@ -294,10 +359,10 @@ void GenerateUnaryClientInterface(
     grpc_generator::Printer *printer,
     std::map<grpc::string, grpc::string> *dictonary) {
   auto vars = *dictonary;
-  grpc::string main = "$ISPUBLIC$$MethodName$(request: $FBSFile$.$INPUT$, ";
+  grpc::string main = "$ISPUBLIC$$MethodName$(request: $INPUT$, ";
   grpc::string callback =
       "callback: (error: grpc.ServiceError | null, response: "
-      "$FBSFile$.$OUTPUT$) => void): grpc.ClientUnaryCall;\n";
+      "$OUTPUT$) => void): grpc.ClientUnaryCall;\n";
   auto meta_data = GenerateMetaData() + ", ";
   auto options = GenerateOptions() + ", ";
   printer->Print(vars, (main + callback).c_str());
@@ -312,8 +377,8 @@ void GenerateClientWriteStreamInterface(
   grpc::string main = "$ISPUBLIC$$MethodName$(";
   grpc::string callback =
       "callback: (error: grpc.ServiceError | null, response: "
-      "$FBSFile$.$INPUT$) => void): "
-      "grpc.ClientWritableStream<$FBSFile$.$OUTPUT$>;\n";
+      "$INPUT$) => void): "
+      "grpc.ClientWritableStream<$OUTPUT$>;\n";
   auto meta_data = GenerateMetaData() + ", ";
   auto options = GenerateOptions() + ", ";
   printer->Print(vars, (main + callback).c_str());
@@ -326,9 +391,8 @@ void GenerateClientReadableStreamInterface(
     grpc_generator::Printer *printer,
     std::map<grpc::string, grpc::string> *dictonary) {
   auto vars = *dictonary;
-  grpc::string main = "$ISPUBLIC$$MethodName$(request: $FBSFile$.$INPUT$, ";
-  grpc::string end_function =
-      "): grpc.ClientReadableStream<$FBSFile$.$OUTPUT$>;\n";
+  grpc::string main = "$ISPUBLIC$$MethodName$(request: $INPUT$, ";
+  grpc::string end_function = "): grpc.ClientReadableStream<$OUTPUT$>;\n";
   auto meta_data = GenerateMetaData();
   auto options = GenerateOptions();
   printer->Print(vars, (main + meta_data + end_function).c_str());
@@ -341,7 +405,7 @@ void GenerateDepluxStreamInterface(
   auto vars = *dictonary;
   grpc::string main = "$ISPUBLIC$$MethodName$(";
   grpc::string end_function =
-      "): grpc.ClientDuplexStream<$FBSFile$.$INPUT$, $FBSFile$.$OUTPUT$>;\n";
+      "): grpc.ClientDuplexStream<$INPUT$, $OUTPUT$>;\n";
   auto meta_data = GenerateMetaData();
   auto options = GenerateOptions();
   printer->Print(vars, (main + end_function).c_str());
@@ -360,8 +424,10 @@ void GenerateClientInterface(const grpc_generator::Service *service,
   for (auto it = 0; it < service->method_count(); it++) {
     auto method = service->method(it);
     vars["MethodName"] = method->name();
-    vars["INPUT"] = method->get_input_type_name();
-    vars["OUTPUT"] = method->get_output_type_name();
+    vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
+                                       method->get_output_type_name(), false);
+    vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
+                                      method->get_input_type_name(), false);
     vars["ISPUBLIC"] = "";
 
     if (method->NoStreaming()) {
@@ -401,8 +467,10 @@ void GenerateClientClassInterface(
   for (auto it = 0; it < service->method_count(); it++) {
     auto method = service->method(it);
     vars["MethodName"] = method->name();
-    vars["INPUT"] = method->get_input_type_name();
-    vars["OUTPUT"] = method->get_output_type_name();
+    vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
+                                       method->get_output_type_name(), false);
+    vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
+                                      method->get_input_type_name(), false);
     vars["ISPUBLIC"] = "public ";
     if (method->NoStreaming()) {
       GenerateUnaryClientInterface(printer, &vars);
@@ -444,7 +512,7 @@ grpc::string GenerateInterface(grpc_generator::File *file,
   vars["Filename"] = filename;
   auto printer = file->CreatePrinter(&output);
 
-  GenerateImports(&*printer, &vars, false);
+  GenerateImports(service, &*printer, &vars, false);
   GenerateMainInterface(service, &*printer, &vars);
   printer->Print("\n");
   GenerateClientInterface(service, &*printer, &vars);