Example 1:

enum MyBzType { Unknown = 0; // unknown BOYIN = 1; } type EncDecEnumSample struct {ID int64 'thrift:" ID,1" json:" ID "' StuID int64 'thrift:"stu_id,2" Json :"stu_id" 'BzID int64' thrift: "bz_id,3" JSON :" bz_id" 'MyBzType enum_type.MyBzType' thrift:" my_bz_type,4" json:"my_bz_type"` }Copy the code

2 Decoding part first (FAST) : visible transmission using i32

func (p *EncDecEnumSample) FastReadField4(buf []byte) (int, error) { offset := 0 if v, l, err := bthrift.Binary.ReadI32(buf[offset:]); err ! = nil { return offset, err } else { offset += l p.MyBzType = enum_type.MyBzType(v) } return offset, nil } func (p *EncDecEnumSample) ReadField4(iprot thrift.TProtocol) error { if v, err := iprot.ReadI32(); err ! = nil { return err } else { p.MyBzType = enum_type.MyBzType(v) } return nil }Copy the code

3 Encoding part: i32 used for visible transmission

func (p *EncDecEnumSample) fastWriteField4(buf []byte, binaryWriter bthrift.BinaryWriter) int { offset := 0 offset += bthrift.Binary.WriteFieldBegin(buf[offset:], "my_bz_type", thrift.I32, 4) offset += bthrift.Binary.WriteI32(buf[offset:], int32(p.MyBzType)) offset += bthrift.Binary.WriteFieldEnd(buf[offset:]) return offset } func (p *EncDecEnumSample) writeField4(oprot thrift.TProtocol) (err error) { if err = oprot.WriteFieldBegin("my_bz_type", thrift.I32, 4); err ! = nil { goto WriteFieldBeginError } if err := oprot.WriteI32(int32(p.MyBzType)); err ! = nil { return err } if err = oprot.WriteFieldEnd(); err ! = nil { goto WriteFieldEndError } return nil WriteFieldBeginError: return thrift.PrependError(fmt.Sprintf("%T write field 4 begin error: ", p), err) WriteFieldEndError: return thrift.PrependError(fmt.Sprintf("%T write field 4 end error: ", p), err) }Copy the code

Conclusion:

1: Thrift enum value is transmitted. Values are transmitted using I32. Tag information is not transmitted

2: Based on the conclusion of 1, it is also ok if the argument is passed directly to an integer of type I32, as long as it is in the scope of the enumeration

3: If the API upgrades integers of type I32 directly to enumerated types, the reverse is also possible. There will be no compatibility issues as long as the scope is appropriate.