Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for implicit tag if struct field not explicitly marked required/optional #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added generator/generator
Binary file not shown.
12 changes: 8 additions & 4 deletions generator/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,17 @@ func (g *GoGenerator) resolveType(typ *parser.Type) string {
func (g *GoGenerator) formatField(field *parser.Field) string {
tags := ""
jsonTags := ""
if !field.Optional {
if field.Required {
tags = ",required"
} else {
tags = ",optional"
jsonTags = ",omitempty"
}
if !field.Explicit {
tags += ",implicit"
}
var opt typeOption
if field.Optional {
if !field.Required {
opt |= toOptional
}
return fmt.Sprintf(
Expand All @@ -275,7 +279,7 @@ func (g *GoGenerator) formatArguments(arguments []*parser.Field) string {
args := make([]string, len(arguments))
for i, arg := range arguments {
var opt typeOption
if arg.Optional {
if !arg.Required {
opt |= toOptional
}
args[i] = fmt.Sprintf("%s %s", validGoIdent(lowerCamelCase(arg.Name)), g.formatType(g.pkg, g.thrift, arg.Type, opt))
Expand Down Expand Up @@ -544,7 +548,7 @@ func (g *GoGenerator) writeService(out io.Writer, svc *parser.Service) error {
// Response struct
args := make([]*parser.Field, 0, len(method.Exceptions))
if method.ReturnType != nil && method.ReturnType.Name != "void" {
args = append(args, &parser.Field{ID: 0, Name: "value", Optional: true /*len(method.Exceptions) != 0*/, Type: method.ReturnType, Default: nil})
args = append(args, &parser.Field{ID: 0, Name: "value", Required: false /*len(method.Exceptions) != 0*/, Type: method.ReturnType, Default: nil})
}
for _, ex := range method.Exceptions {
args = append(args, ex)
Expand Down
11 changes: 8 additions & 3 deletions parser/grammar.peg
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func ifaceSliceToString(v interface{}) string {
func unionToStruct(u union) *Struct {
st := (*Struct)(u)
for _, f := range st.Fields {
f.Optional = true
f.Required = false
}
return st
}
Expand Down Expand Up @@ -196,8 +196,13 @@ Field ← id:IntConstant _ ':' _ req:FieldReq? _ typ:FieldType _ name:Identifier
Type : typ.(*Type),
Annotations: toAnnotations(annotations),
}
f.Required = true
if req != nil && !req.(bool) {
f.Optional = true
f.Required = false
}
f.Explicit = true
if req == nil {
f.Explicit = false
}
if def != nil {
f.Default = def.([]interface{})[2]
Expand Down Expand Up @@ -247,7 +252,7 @@ Function ← oneway:("oneway" __)? typ:FunctionType __ name:Identifier _ '(' __
if exceptions != nil {
m.Exceptions = exceptions.([]*Field)
for _, e := range m.Exceptions {
e.Optional = true
e.Required = false
}
}
return m, nil
Expand Down
Loading