You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "Chris Bannister (JIRA)" <ji...@apache.org> on 2016/01/10 12:09:39 UTC

[jira] [Comment Edited] (THRIFT-3533) Can not send nil pointer as service method argument

    [ https://issues.apache.org/jira/browse/THRIFT-3533?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15090984#comment-15090984 ] 

Chris Bannister edited comment on THRIFT-3533 at 1/10/16 11:09 AM:
-------------------------------------------------------------------

In python you can write this,

service.method(None) which works fine, but in go if you do service.method(nil) it panics.

Im not sure which is correct.

This is the code from https://github.com/apache/aurora/blob/6b768bd3dd053b776a5b713c4fd5ce95e31e666b/api/src/main/thrift/org/apache/aurora/gen/api.thrift#L1008 for create_job args.Write in python,

{code}
  def write(self, oprot):
    if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
      oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
      return
    oprot.writeStructBegin('createJob_args')
    if self.description is not None:
      oprot.writeFieldBegin('description', TType.STRUCT, 1)
      self.description.write(oprot)
      oprot.writeFieldEnd()
    if self.lock is not None:
      oprot.writeFieldBegin('lock', TType.STRUCT, 3)
      self.lock.write(oprot)
      oprot.writeFieldEnd()
    oprot.writeFieldStop()
    oprot.writeStructEnd()
{/code}

and In Go it looks like this

{code}
  func (p *AuroraSchedulerManagerCreateJobArgs) Write(oprot thrift.TProtocol) error {
	if err := oprot.WriteStructBegin("createJob_args"); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
	}
	if err := p.writeField1(oprot); err != nil {
		return err
	}
	if err := p.writeField3(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
  }
{/code}

Go will insert the 'x.IsSet' for optional fields, but args can't be optional.



was (Author: zariel):
In python you can write this,

service.method(None) which works fine, but in go if you do service.method(nil) it panics.

Im not sure which is correct.

This is the code from https://github.com/apache/aurora/blob/6b768bd3dd053b776a5b713c4fd5ce95e31e666b/api/src/main/thrift/org/apache/aurora/gen/api.thrift#L1008 for create_job args.Write in python,

{code}
  def write(self, oprot):
    if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and self.thrift_spec is not None and fastbinary is not None:
      oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))
      return
    oprot.writeStructBegin('createJob_args')
    if self.description is not None:
      oprot.writeFieldBegin('description', TType.STRUCT, 1)
      self.description.write(oprot)
      oprot.writeFieldEnd()
    if self.lock is not None:
      oprot.writeFieldBegin('lock', TType.STRUCT, 3)
      self.lock.write(oprot)
      oprot.writeFieldEnd()
    oprot.writeFieldStop()
    oprot.writeStructEnd()
{/code}

and In Go it looks like this

{code}
func (p *AuroraSchedulerManagerCreateJobArgs) Write(oprot thrift.TProtocol) error {
	if err := oprot.WriteStructBegin("createJob_args"); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
	}
	if err := p.writeField1(oprot); err != nil {
		return err
	}
	if err := p.writeField3(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
}
{/code}

Go will insert the 'x.IsSet' for optional fields, but args can't be optional.


> Can not send nil pointer as service method argument
> ---------------------------------------------------
>
>                 Key: THRIFT-3533
>                 URL: https://issues.apache.org/jira/browse/THRIFT-3533
>             Project: Thrift
>          Issue Type: Bug
>          Components: Go - Compiler
>    Affects Versions: 0.9.3
>            Reporter: Chris Bannister
>
> If you try to send a nil struct as an argument to a service method a panic occurs as it tries to write out all the fields in the struct, the python generated code has guarding 'if self.x != None:' for every struct field, whereas Go only outputs the 'if p.IsSet()' when the field is declared optional, which is not possible for argument lists.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)