cloudwego/thriftgo

FR: filed comment in idl to code generated

hxzhao527 opened this issue · 3 comments

enum EEE{
    One = 1, // one
   Two, // two
}

struct One{
    1: EEE which // which
}

to generate

type EEE int64
const (
    EEE_One EEE = 1 // one
    EEE_Two EEE = 2 // two
)

type One struct{
    Which EEE // which
}

you can try this: -g go:reserve_comments

what is the syntax of reserve_comments? Is there any doc to describe it?

this is the code generated from the above idl, but the comments of enum arg lost.

// Code generated by thriftgo (0.2.12). DO NOT EDIT.

package xxx

import (
        "database/sql"
        "database/sql/driver"
        "fmt"
        "github.com/apache/thrift/lib/go/thrift"
)

type EEE int64

const (
        EEE_One EEE = 1
        EEE_Two EEE = 2
)

func (p EEE) String() string {
        switch p {
        case EEE_One:
                return "One"
        case EEE_Two:
                return "Two"
        }
        return "<UNSET>"
}

func EEEFromString(s string) (EEE, error) {
        switch s {
        case "One":
                return EEE_One, nil
        case "Two":
                return EEE_Two, nil
        }
        return EEE(0), fmt.Errorf("not a valid EEE string")
}

func EEEPtr(v EEE) *EEE { return &v }
func (p *EEE) Scan(value interface{}) (err error) {
        var result sql.NullInt64
        err = result.Scan(value)
        *p = EEE(result.Int64)
        return
}

func (p *EEE) Value() (driver.Value, error) {
        if p == nil {
                return nil, nil
        }
        return int64(*p), nil
}

type One struct {
        // which
        Which EEE `thrift:"which,1" json:"which"`
}

func NewOne() *One {
        return &One{}
}

func (p *One) GetWhich() (v EEE) {
        return p.Which
}

var fieldIDToName_One = map[int16]string{
        1: "which",
}

func (p *One) Read(iprot thrift.TProtocol) (err error) {

        var fieldTypeId thrift.TType
        var fieldId int16

        if _, err = iprot.ReadStructBegin(); err != nil {
                goto ReadStructBeginError
        }

        for {
                _, fieldTypeId, fieldId, err = iprot.ReadFieldBegin()
                if err != nil {
                        goto ReadFieldBeginError
                }
                if fieldTypeId == thrift.STOP {
                        break
                }

                switch fieldId {
                case 1:
                        if fieldTypeId == thrift.I32 {
                                if err = p.ReadField1(iprot); err != nil {
                                        goto ReadFieldError
                                }
                        } else {
                                if err = iprot.Skip(fieldTypeId); err != nil {
                                        goto SkipFieldError
                                }
                        }
                default:
                        if err = iprot.Skip(fieldTypeId); err != nil {
                                goto SkipFieldError
                        }
                }

                if err = iprot.ReadFieldEnd(); err != nil {
                        goto ReadFieldEndError
                }
        }
        if err = iprot.ReadStructEnd(); err != nil {
                goto ReadStructEndError
        }

        return nil
ReadStructBeginError:
        return thrift.PrependError(fmt.Sprintf("%T read struct begin error: ", p), err)
ReadFieldBeginError:
        return thrift.PrependError(fmt.Sprintf("%T read field %d begin error: ", p, fieldId), err)
ReadFieldError:
        return thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fieldId, fieldIDToName_One[fieldId]), err)
SkipFieldError:
        return thrift.PrependError(fmt.Sprintf("%T field %d skip type %d error: ", p, fieldId, fieldTypeId), err)

ReadFieldEndError:
        return thrift.PrependError(fmt.Sprintf("%T read field end error", p), err)
ReadStructEndError:
        return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
}

func (p *One) ReadField1(iprot thrift.TProtocol) error {
        if v, err := iprot.ReadI32(); err != nil {
                return err
        } else {
                p.Which = EEE(v)
        }
        return nil
}

func (p *One) Write(oprot thrift.TProtocol) (err error) {
        var fieldId int16
        if err = oprot.WriteStructBegin("One"); err != nil {
                goto WriteStructBeginError
        }
        if p != nil {
                if err = p.writeField1(oprot); err != nil {
                        fieldId = 1
                        goto WriteFieldError
                }

        }
        if err = oprot.WriteFieldStop(); err != nil {
                goto WriteFieldStopError
        }
        if err = oprot.WriteStructEnd(); err != nil {
                goto WriteStructEndError
        }
        return nil
WriteStructBeginError:
        return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
WriteFieldError:
        return thrift.PrependError(fmt.Sprintf("%T write field %d error: ", p, fieldId), err)
WriteFieldStopError:
        return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", p), err)
WriteStructEndError:
        return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", p), err)
}

func (p *One) writeField1(oprot thrift.TProtocol) (err error) {
        if err = oprot.WriteFieldBegin("which", thrift.I32, 1); err != nil {
                goto WriteFieldBeginError
        }
        if err := oprot.WriteI32(int32(p.Which)); err != nil {
                return err
        }
        if err = oprot.WriteFieldEnd(); err != nil {
                goto WriteFieldEndError
        }
        return nil
WriteFieldBeginError:
        return thrift.PrependError(fmt.Sprintf("%T write field 1 begin error: ", p), err)
WriteFieldEndError:
        return thrift.PrependError(fmt.Sprintf("%T write field 1 end error: ", p), err)
}

func (p *One) String() string {
        if p == nil {
                return "<nil>"
        }
        return fmt.Sprintf("One(%+v)", *p)
}
  1. You need to upgrade your thriftgo to v0.3.2 (the latest version)
  2. run thriftgo cmd with -g go:reserve_comments. You can run "thriftgo -help" to get more details.
  3. the code generated will be like:
    `
    const (
    // one
    EEE_One EEE = 1
    // two
    EEE_Two EEE = 2
    )

type One struct {
// which
Which EEE thrift:"which,1" frugal:"1,default,EEE" json:"which"
}

`