Update proxy thrift RPC protocol (#29)

* feat: make fromId,toId to optional; add new interface getFlowsByIds

* feat: generate new go proxy codes by thrift 0.12.0
This commit is contained in:
Jay 2019-06-27 02:29:58 +08:00 committed by Xun(Perry) Liu
parent 47350f428c
commit f0ef92c87a
3 changed files with 380 additions and 50 deletions

View File

@ -25,8 +25,8 @@ var _ = bytes.Equal
// - Speed
// - TrafficLevel
type Flow struct {
FromId int64 `thrift:"fromId,1,required" db:"fromId" json:"fromId"`
ToId int64 `thrift:"toId,2,required" db:"toId" json:"toId"`
FromId *int64 `thrift:"fromId,1" db:"fromId" json:"fromId,omitempty"`
ToId *int64 `thrift:"toId,2" db:"toId" json:"toId,omitempty"`
WayId int64 `thrift:"wayId,3,required" db:"wayId" json:"wayId"`
Speed float64 `thrift:"speed,4,required" db:"speed" json:"speed"`
TrafficLevel int32 `thrift:"trafficLevel,5,required" db:"trafficLevel" json:"trafficLevel"`
@ -36,13 +36,19 @@ func NewFlow() *Flow {
return &Flow{}
}
var Flow_FromId_DEFAULT int64
func (p *Flow) GetFromId() int64 {
return p.FromId
if !p.IsSetFromId() {
return Flow_FromId_DEFAULT
}
return *p.FromId
}
var Flow_ToId_DEFAULT int64
func (p *Flow) GetToId() int64 {
return p.ToId
if !p.IsSetToId() {
return Flow_ToId_DEFAULT
}
return *p.ToId
}
func (p *Flow) GetWayId() int64 {
@ -56,13 +62,19 @@ func (p *Flow) GetSpeed() float64 {
func (p *Flow) GetTrafficLevel() int32 {
return p.TrafficLevel
}
func (p *Flow) IsSetFromId() bool {
return p.FromId != nil
}
func (p *Flow) IsSetToId() bool {
return p.ToId != nil
}
func (p *Flow) Read(iprot thrift.TProtocol) error {
if _, err := iprot.ReadStructBegin(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err)
}
var issetFromId bool = false;
var issetToId bool = false;
var issetWayId bool = false;
var issetSpeed bool = false;
var issetTrafficLevel bool = false;
@ -79,7 +91,6 @@ func (p *Flow) Read(iprot thrift.TProtocol) error {
if err := p.ReadField1(iprot); err != nil {
return err
}
issetFromId = true
} else {
if err := iprot.Skip(fieldTypeId); err != nil {
return err
@ -90,7 +101,6 @@ func (p *Flow) Read(iprot thrift.TProtocol) error {
if err := p.ReadField2(iprot); err != nil {
return err
}
issetToId = true
} else {
if err := iprot.Skip(fieldTypeId); err != nil {
return err
@ -141,12 +151,6 @@ func (p *Flow) Read(iprot thrift.TProtocol) error {
if err := iprot.ReadStructEnd(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
}
if !issetFromId{
return thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, fmt.Errorf("Required field FromId is not set"));
}
if !issetToId{
return thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, fmt.Errorf("Required field ToId is not set"));
}
if !issetWayId{
return thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, fmt.Errorf("Required field WayId is not set"));
}
@ -163,7 +167,7 @@ func (p *Flow) ReadField1(iprot thrift.TProtocol) error {
if v, err := iprot.ReadI64(); err != nil {
return thrift.PrependError("error reading field 1: ", err)
} else {
p.FromId = v
p.FromId = &v
}
return nil
}
@ -172,7 +176,7 @@ func (p *Flow) ReadField2(iprot thrift.TProtocol) error {
if v, err := iprot.ReadI64(); err != nil {
return thrift.PrependError("error reading field 2: ", err)
} else {
p.ToId = v
p.ToId = &v
}
return nil
}
@ -222,22 +226,26 @@ func (p *Flow) Write(oprot thrift.TProtocol) error {
}
func (p *Flow) writeField1(oprot thrift.TProtocol) (err error) {
if err := oprot.WriteFieldBegin("fromId", thrift.I64, 1); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field begin error 1:fromId: ", p), err) }
if err := oprot.WriteI64(int64(p.FromId)); err != nil {
return thrift.PrependError(fmt.Sprintf("%T.fromId (1) field write error: ", p), err) }
if err := oprot.WriteFieldEnd(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field end error 1:fromId: ", p), err) }
if p.IsSetFromId() {
if err := oprot.WriteFieldBegin("fromId", thrift.I64, 1); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field begin error 1:fromId: ", p), err) }
if err := oprot.WriteI64(int64(*p.FromId)); err != nil {
return thrift.PrependError(fmt.Sprintf("%T.fromId (1) field write error: ", p), err) }
if err := oprot.WriteFieldEnd(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field end error 1:fromId: ", p), err) }
}
return err
}
func (p *Flow) writeField2(oprot thrift.TProtocol) (err error) {
if err := oprot.WriteFieldBegin("toId", thrift.I64, 2); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field begin error 2:toId: ", p), err) }
if err := oprot.WriteI64(int64(p.ToId)); err != nil {
return thrift.PrependError(fmt.Sprintf("%T.toId (2) field write error: ", p), err) }
if err := oprot.WriteFieldEnd(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field end error 2:toId: ", p), err) }
if p.IsSetToId() {
if err := oprot.WriteFieldBegin("toId", thrift.I64, 2); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field begin error 2:toId: ", p), err) }
if err := oprot.WriteI64(int64(*p.ToId)); err != nil {
return thrift.PrependError(fmt.Sprintf("%T.toId (2) field write error: ", p), err) }
if err := oprot.WriteFieldEnd(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field end error 2:toId: ", p), err) }
}
return err
}
@ -283,6 +291,9 @@ type ProxyService interface {
// Parameters:
// - WayId
GetFlowById(ctx context.Context, wayId int64) (r *Flow, err error)
// Parameters:
// - WayIds
GetFlowsByIds(ctx context.Context, wayIds []int64) (r []*Flow, err error)
}
type ProxyServiceClient struct {
@ -331,6 +342,18 @@ func (p *ProxyServiceClient) GetFlowById(ctx context.Context, wayId int64) (r *F
return _result3.GetSuccess(), nil
}
// Parameters:
// - WayIds
func (p *ProxyServiceClient) GetFlowsByIds(ctx context.Context, wayIds []int64) (r []*Flow, err error) {
var _args4 ProxyServiceGetFlowsByIdsArgs
_args4.WayIds = wayIds
var _result5 ProxyServiceGetFlowsByIdsResult
if err = p.Client_().Call(ctx, "getFlowsByIds", &_args4, &_result5); err != nil {
return
}
return _result5.GetSuccess(), nil
}
type ProxyServiceProcessor struct {
processorMap map[string]thrift.TProcessorFunction
handler ProxyService
@ -351,10 +374,11 @@ func (p *ProxyServiceProcessor) ProcessorMap() map[string]thrift.TProcessorFunct
func NewProxyServiceProcessor(handler ProxyService) *ProxyServiceProcessor {
self4 := &ProxyServiceProcessor{handler:handler, processorMap:make(map[string]thrift.TProcessorFunction)}
self4.processorMap["getAllFlows"] = &proxyServiceProcessorGetAllFlows{handler:handler}
self4.processorMap["getFlowById"] = &proxyServiceProcessorGetFlowById{handler:handler}
return self4
self6 := &ProxyServiceProcessor{handler:handler, processorMap:make(map[string]thrift.TProcessorFunction)}
self6.processorMap["getAllFlows"] = &proxyServiceProcessorGetAllFlows{handler:handler}
self6.processorMap["getFlowById"] = &proxyServiceProcessorGetFlowById{handler:handler}
self6.processorMap["getFlowsByIds"] = &proxyServiceProcessorGetFlowsByIds{handler:handler}
return self6
}
func (p *ProxyServiceProcessor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) {
@ -365,12 +389,12 @@ func (p *ProxyServiceProcessor) Process(ctx context.Context, iprot, oprot thrift
}
iprot.Skip(thrift.STRUCT)
iprot.ReadMessageEnd()
x5 := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, "Unknown function " + name)
x7 := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, "Unknown function " + name)
oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId)
x5.Write(oprot)
x7.Write(oprot)
oprot.WriteMessageEnd()
oprot.Flush(ctx)
return false, x5
return false, x7
}
@ -470,6 +494,54 @@ var retval *Flow
return true, err
}
type proxyServiceProcessorGetFlowsByIds struct {
handler ProxyService
}
func (p *proxyServiceProcessorGetFlowsByIds) Process(ctx context.Context, seqId int32, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) {
args := ProxyServiceGetFlowsByIdsArgs{}
if err = args.Read(iprot); err != nil {
iprot.ReadMessageEnd()
x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err.Error())
oprot.WriteMessageBegin("getFlowsByIds", thrift.EXCEPTION, seqId)
x.Write(oprot)
oprot.WriteMessageEnd()
oprot.Flush(ctx)
return false, err
}
iprot.ReadMessageEnd()
result := ProxyServiceGetFlowsByIdsResult{}
var retval []*Flow
var err2 error
if retval, err2 = p.handler.GetFlowsByIds(ctx, args.WayIds); err2 != nil {
x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, "Internal error processing getFlowsByIds: " + err2.Error())
oprot.WriteMessageBegin("getFlowsByIds", thrift.EXCEPTION, seqId)
x.Write(oprot)
oprot.WriteMessageEnd()
oprot.Flush(ctx)
return true, err2
} else {
result.Success = retval
}
if err2 = oprot.WriteMessageBegin("getFlowsByIds", thrift.REPLY, seqId); err2 != nil {
err = err2
}
if err2 = result.Write(oprot); err == nil && err2 != nil {
err = err2
}
if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil {
err = err2
}
if err2 = oprot.Flush(ctx); err == nil && err2 != nil {
err = err2
}
if err != nil {
return
}
return true, err
}
// HELPER FUNCTIONS AND STRUCTURES
@ -589,11 +661,11 @@ func (p *ProxyServiceGetAllFlowsResult) ReadField0(iprot thrift.TProtocol) erro
tSlice := make([]*Flow, 0, size)
p.Success = tSlice
for i := 0; i < size; i ++ {
_elem6 := &Flow{}
if err := _elem6.Read(iprot); err != nil {
return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", _elem6), err)
_elem8 := &Flow{}
if err := _elem8.Read(iprot); err != nil {
return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", _elem8), err)
}
p.Success = append(p.Success, _elem6)
p.Success = append(p.Success, _elem8)
}
if err := iprot.ReadListEnd(); err != nil {
return thrift.PrependError("error reading list end: ", err)
@ -833,4 +905,234 @@ func (p *ProxyServiceGetFlowByIdResult) String() string {
return fmt.Sprintf("ProxyServiceGetFlowByIdResult(%+v)", *p)
}
// Attributes:
// - WayIds
type ProxyServiceGetFlowsByIdsArgs struct {
WayIds []int64 `thrift:"wayIds,1" db:"wayIds" json:"wayIds"`
}
func NewProxyServiceGetFlowsByIdsArgs() *ProxyServiceGetFlowsByIdsArgs {
return &ProxyServiceGetFlowsByIdsArgs{}
}
func (p *ProxyServiceGetFlowsByIdsArgs) GetWayIds() []int64 {
return p.WayIds
}
func (p *ProxyServiceGetFlowsByIdsArgs) Read(iprot thrift.TProtocol) error {
if _, err := iprot.ReadStructBegin(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err)
}
for {
_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()
if err != nil {
return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err)
}
if fieldTypeId == thrift.STOP { break; }
switch fieldId {
case 1:
if fieldTypeId == thrift.LIST {
if err := p.ReadField1(iprot); err != nil {
return err
}
} else {
if err := iprot.Skip(fieldTypeId); err != nil {
return err
}
}
default:
if err := iprot.Skip(fieldTypeId); err != nil {
return err
}
}
if err := iprot.ReadFieldEnd(); err != nil {
return err
}
}
if err := iprot.ReadStructEnd(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
}
return nil
}
func (p *ProxyServiceGetFlowsByIdsArgs) ReadField1(iprot thrift.TProtocol) error {
_, size, err := iprot.ReadListBegin()
if err != nil {
return thrift.PrependError("error reading list begin: ", err)
}
tSlice := make([]int64, 0, size)
p.WayIds = tSlice
for i := 0; i < size; i ++ {
var _elem9 int64
if v, err := iprot.ReadI64(); err != nil {
return thrift.PrependError("error reading field 0: ", err)
} else {
_elem9 = v
}
p.WayIds = append(p.WayIds, _elem9)
}
if err := iprot.ReadListEnd(); err != nil {
return thrift.PrependError("error reading list end: ", err)
}
return nil
}
func (p *ProxyServiceGetFlowsByIdsArgs) Write(oprot thrift.TProtocol) error {
if err := oprot.WriteStructBegin("getFlowsByIds_args"); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) }
if p != nil {
if err := p.writeField1(oprot); err != nil { return err }
}
if err := oprot.WriteFieldStop(); err != nil {
return thrift.PrependError("write field stop error: ", err) }
if err := oprot.WriteStructEnd(); err != nil {
return thrift.PrependError("write struct stop error: ", err) }
return nil
}
func (p *ProxyServiceGetFlowsByIdsArgs) writeField1(oprot thrift.TProtocol) (err error) {
if err := oprot.WriteFieldBegin("wayIds", thrift.LIST, 1); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field begin error 1:wayIds: ", p), err) }
if err := oprot.WriteListBegin(thrift.I64, len(p.WayIds)); err != nil {
return thrift.PrependError("error writing list begin: ", err)
}
for _, v := range p.WayIds {
if err := oprot.WriteI64(int64(v)); err != nil {
return thrift.PrependError(fmt.Sprintf("%T. (0) field write error: ", p), err) }
}
if err := oprot.WriteListEnd(); err != nil {
return thrift.PrependError("error writing list end: ", err)
}
if err := oprot.WriteFieldEnd(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field end error 1:wayIds: ", p), err) }
return err
}
func (p *ProxyServiceGetFlowsByIdsArgs) String() string {
if p == nil {
return "<nil>"
}
return fmt.Sprintf("ProxyServiceGetFlowsByIdsArgs(%+v)", *p)
}
// Attributes:
// - Success
type ProxyServiceGetFlowsByIdsResult struct {
Success []*Flow `thrift:"success,0" db:"success" json:"success,omitempty"`
}
func NewProxyServiceGetFlowsByIdsResult() *ProxyServiceGetFlowsByIdsResult {
return &ProxyServiceGetFlowsByIdsResult{}
}
var ProxyServiceGetFlowsByIdsResult_Success_DEFAULT []*Flow
func (p *ProxyServiceGetFlowsByIdsResult) GetSuccess() []*Flow {
return p.Success
}
func (p *ProxyServiceGetFlowsByIdsResult) IsSetSuccess() bool {
return p.Success != nil
}
func (p *ProxyServiceGetFlowsByIdsResult) Read(iprot thrift.TProtocol) error {
if _, err := iprot.ReadStructBegin(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err)
}
for {
_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()
if err != nil {
return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err)
}
if fieldTypeId == thrift.STOP { break; }
switch fieldId {
case 0:
if fieldTypeId == thrift.LIST {
if err := p.ReadField0(iprot); err != nil {
return err
}
} else {
if err := iprot.Skip(fieldTypeId); err != nil {
return err
}
}
default:
if err := iprot.Skip(fieldTypeId); err != nil {
return err
}
}
if err := iprot.ReadFieldEnd(); err != nil {
return err
}
}
if err := iprot.ReadStructEnd(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
}
return nil
}
func (p *ProxyServiceGetFlowsByIdsResult) ReadField0(iprot thrift.TProtocol) error {
_, size, err := iprot.ReadListBegin()
if err != nil {
return thrift.PrependError("error reading list begin: ", err)
}
tSlice := make([]*Flow, 0, size)
p.Success = tSlice
for i := 0; i < size; i ++ {
_elem10 := &Flow{}
if err := _elem10.Read(iprot); err != nil {
return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", _elem10), err)
}
p.Success = append(p.Success, _elem10)
}
if err := iprot.ReadListEnd(); err != nil {
return thrift.PrependError("error reading list end: ", err)
}
return nil
}
func (p *ProxyServiceGetFlowsByIdsResult) Write(oprot thrift.TProtocol) error {
if err := oprot.WriteStructBegin("getFlowsByIds_result"); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err) }
if p != nil {
if err := p.writeField0(oprot); err != nil { return err }
}
if err := oprot.WriteFieldStop(); err != nil {
return thrift.PrependError("write field stop error: ", err) }
if err := oprot.WriteStructEnd(); err != nil {
return thrift.PrependError("write struct stop error: ", err) }
return nil
}
func (p *ProxyServiceGetFlowsByIdsResult) writeField0(oprot thrift.TProtocol) (err error) {
if p.IsSetSuccess() {
if err := oprot.WriteFieldBegin("success", thrift.LIST, 0); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field begin error 0:success: ", p), err) }
if err := oprot.WriteListBegin(thrift.STRUCT, len(p.Success)); err != nil {
return thrift.PrependError("error writing list begin: ", err)
}
for _, v := range p.Success {
if err := v.Write(oprot); err != nil {
return thrift.PrependError(fmt.Sprintf("%T error writing struct: ", v), err)
}
}
if err := oprot.WriteListEnd(); err != nil {
return thrift.PrependError("error writing list end: ", err)
}
if err := oprot.WriteFieldEnd(); err != nil {
return thrift.PrependError(fmt.Sprintf("%T write field end error 0:success: ", p), err) }
}
return err
}
func (p *ProxyServiceGetFlowsByIdsResult) String() string {
if p == nil {
return "<nil>"
}
return fmt.Sprintf("ProxyServiceGetFlowsByIdsResult(%+v)", *p)
}

View File

@ -24,6 +24,7 @@ func Usage() {
fmt.Fprintln(os.Stderr, "\nFunctions:")
fmt.Fprintln(os.Stderr, " getAllFlows()")
fmt.Fprintln(os.Stderr, " Flow getFlowById(i64 wayId)")
fmt.Fprintln(os.Stderr, " getFlowsByIds( wayIds)")
fmt.Fprintln(os.Stderr)
os.Exit(0)
}
@ -158,8 +159,8 @@ func main() {
fmt.Fprintln(os.Stderr, "GetFlowById requires 1 args")
flag.Usage()
}
argvalue0, err7 := (strconv.ParseInt(flag.Arg(1), 10, 64))
if err7 != nil {
argvalue0, err11 := (strconv.ParseInt(flag.Arg(1), 10, 64))
if err11 != nil {
Usage()
return
}
@ -167,6 +168,32 @@ func main() {
fmt.Print(client.GetFlowById(context.Background(), value0))
fmt.Print("\n")
break
case "getFlowsByIds":
if flag.NArg() - 1 != 1 {
fmt.Fprintln(os.Stderr, "GetFlowsByIds requires 1 args")
flag.Usage()
}
arg12 := flag.Arg(1)
mbTrans13 := thrift.NewTMemoryBufferLen(len(arg12))
defer mbTrans13.Close()
_, err14 := mbTrans13.WriteString(arg12)
if err14 != nil {
Usage()
return
}
factory15 := thrift.NewTJSONProtocolFactory()
jsProt16 := factory15.GetProtocol(mbTrans13)
containerStruct0 := proxy.NewProxyServiceGetFlowsByIdsArgs()
err17 := containerStruct0.ReadField1(jsProt16)
if err17 != nil {
Usage()
return
}
argvalue0 := containerStruct0.WayIds
value0 := argvalue0
fmt.Print(client.GetFlowsByIds(context.Background(), value0))
fmt.Print("\n")
break
case "":
Usage()
break

View File

@ -1,13 +1,14 @@
struct Flow {
1: required i64 fromId;
2: required i64 toId;
3: required i64 wayId;
4: required double speed;
5: required i32 trafficLevel;
1: optional i64 fromId;
2: optional i64 toId;
3: required i64 wayId;
4: required double speed;
5: required i32 trafficLevel;
}
service ProxyService {
list<Flow> getAllFlows()
Flow getFlowById(1:i64 wayId)
list<Flow> getAllFlows()
Flow getFlowById(1:i64 wayId)
list<Flow> getFlowsByIds(1:list<i64> wayIds)
}