Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change client to return instances of our classes.
Have client return instances of our classes. This changes the generated client code so that it returns instances of the generated thrift classes instead of plain objects. This change is a step toward having exception classes derive from Error (creditkarma#178) and be distinguishable from each other (creditkarma#181). The change itself is small and easy. The compiler already generates "MyEndpoint__Result" classes that have a read() function that instantiates the class, I just needed to call it from the appropriate place in the Client class. It's hard to understand this change without full knowledge of how the generated code fits together, but here's a diff of a generated ping function before and after anyway: ``` public ping(context?: Context): Promise<string> { const writer: thrift.TTransport = new this.transport(); const output: thrift.TProtocol = new this.protocol(writer); output.writeMessageBegin("ping", thrift.MessageType.CALL, this.incrementRequestId()); const args: IPing__ArgsArgs = {}; Ping__ArgsCodec.encode(args, output); output.writeMessageEnd(); return this.connection.send(writer.flush(), context).then((data: Buffer) => { const reader: thrift.TTransport = this.transport.receiver(data); const input: thrift.TProtocol = new this.protocol(reader); try { const { fieldName: fieldName, messageType: messageType }: thrift.IThriftMessage = input.readMessageBegin(); if (fieldName === "ping") { if (messageType === thrift.MessageType.EXCEPTION) { const err: thrift.TApplicationException = thrift.TApplicationExceptionCodec.decode(input); input.readMessageEnd(); return Promise.reject(err); } else { - const result: IPing__Result = Ping__ResultCodec.decode(input); + const result: Ping__Result = Ping__Result.read(input); input.readMessageEnd(); if (result.success != null) { return Promise.resolve(result.success); } else { return Promise.reject(new thrift.TApplicationException(thrift.TApplicationExceptionType.UNKNOWN, "ping failed: unknown result")); } } } else { return Promise.reject(new thrift.TApplicationException(thrift.TApplicationExceptionType.WRONG_METHOD_NAME, "Received a response to an unknown RPC function: " + fieldName)); } } catch (err) { return Promise.reject(err); } }); } ``` You'll notice that I also changed the type of `result` from `IPing__Result` to `Ping__Result`. This isn't important and could be reverted, but I think it's better this way. I like keeping types specific until the point where it's useful for them to be generic, and I'd advocate to make the same change to the types of struct class members (would need to change these types, I think: https://github.com/creditkarma/thrift-typescript/blob/e8931ff0d98c871360af99bbfa805cfd088d2f0e/src/main/render/thrift-server/struct/class.ts#L293 and https://github.com/creditkarma/thrift-typescript/blob/e8931ff0d98c871360af99bbfa805cfd088d2f0e/src/main/render/thrift-server/struct/reader.ts#L51).