samuel / go-thrift

A native Thrift package for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Thrift byte type should generate signed int8 slice

anxiousmodernman opened this issue · comments

According to this doc:

https://diwakergupta.github.io/thrift-missing-guide/#_types

The Thrift byte type is a signed type. My understanding is that Go's byte is an alias to unsigned uint8.

Using go-thrift, I get the following transformation:

Thrift method signature

    AcmResponse buildAcm(1: list<byte> byteList, 2: string dataType,
                       3: map<string, string> propertiesMap)
        throws(1: InvalidInputException ex1, 2: SecurityServiceException ex2),

Interface go-thrift generated

BuildAcm(byteList []byte, dataType string, propertiesMap map[string]string) (*AcmResponse, error)

The Apache go thrift library generated the following, as a reference:

BuildAcm(byteList []int8, dataType string, propertiesMap map[string]string) (r *AcmResponse, err error)

I noticed this when interoperating with a Java service via Thrift. In Java, the byte type is a signed 8 bit integer.

This is a tough call. While technically Thrift does define byte (thus binary) being a signed bye, this feels the case only because it started life in Java which only has signed types. The most used case for byte/binary I've seen in Thrift is for binary data which is awkward if using int8. It feels most useful, though potentially not totally following the "spec", to use byte instead of int8. It seems rare to actually want signed 8-bit integers except for perhaps signed 8-bit PCM audio.

For the record, a straight-up find/replace in the code generated by go-thrift made my integration tests pass against a Java service expecting a Java byte array byte[]. So, unless I'm missing something, a flag could be passed to the generator that would force this behavior.

[]byte ---> []int8

Would you be willing to accept a PR that simply emits []int8 instead of []byte if a flag is passed? Maybe something like -go.signedbytes:

$ generator --help
Usage of parsimony: [options] inputfile outputpath
  -go.binarystring=false: Always use string for binary instead of []byte
  -go.json.enumnum=false: For JSON marshal enums by number instead of name
  -go.pointers=false: Make all fields pointers
  -go.signedbytes=false: Interpret Thrift byte as signed int8 type

$ generator cassandra.thrift $GOPATH/src/

Yeah, that sounds good, and definitely nice to give people the choice.

@samuel I think we can close this now