Skip to content

Commit

Permalink
feat: add query optional
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Aug 26, 2024
1 parent c29f150 commit fc59bc7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cmd/hz/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func Init() *cli.App {
handlerByMethod := cli.BoolFlag{Name: "handler_by_method", Usage: "Generate a separate handler file for each method.", Destination: &globalArgs.HandlerByMethod}
trimGoPackage := cli.StringFlag{Name: "trim_gopackage", Aliases: []string{"trim_pkg"}, Usage: "Trim the prefix of go_package for protobuf.", Destination: &globalArgs.TrimGoPackage}

// client flag
enableClientOptionalFlag := cli.BoolFlag{Name: "enable_optional", Usage: "Optional field do not transfer for thrift if not set.(Only works for query tag)", Destination: &globalArgs.EnableClientOptional}

// app
app := cli.NewApp()
app.Name = "hz"
Expand Down Expand Up @@ -327,6 +330,7 @@ func Init() *cli.App {
&trimGoPackage,

&jsonEnumStrFlag,
&enableClientOptionalFlag,
&queryEnumIntFlag,
&unsetOmitemptyFlag,
&protoCamelJSONTag,
Expand Down
3 changes: 3 additions & 0 deletions cmd/hz/config/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ type Argument struct {
EnableExtends bool
SortRouter bool

// client flag
EnableClientOptional bool

CustomizeLayout string
CustomizeLayoutData string
CustomizePackage string
Expand Down
3 changes: 3 additions & 0 deletions cmd/hz/generator/package_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,9 @@ func (r *request) addHeaders(params map[string]string) *request {
func (r *request) setQueryParam(param string, value interface{}) *request {
if value == nil {
return r
}
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Slice, reflect.Array:
Expand Down
20 changes: 16 additions & 4 deletions cmd/hz/thrift/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func astToService(ast *parser.Thrift, resolver *Resolver, args *config.Argument)
if err != nil {
return nil, err
}
err = parseAnnotationToClient(clientMethod, m.Arguments[0].GetType(), rt)
err = parseAnnotationToClient(clientMethod, m.Arguments[0].GetType(), rt, args.EnableClientOptional)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func newHTTPMethod(s *parser.Service, m *parser.Function, method *generator.Http
return &newMethod, nil
}

func parseAnnotationToClient(clientMethod *generator.ClientMethod, p *parser.Type, symbol ResolvedSymbol) error {
func parseAnnotationToClient(clientMethod *generator.ClientMethod, p *parser.Type, symbol ResolvedSymbol, enableOptional bool) error {
if p == nil {
return fmt.Errorf("get type failed for parse annotatoon to client")
}
Expand All @@ -270,13 +270,21 @@ func parseAnnotationToClient(clientMethod *generator.ClientMethod, p *parser.Typ
for _, field := range st.Fields() {
hasAnnotation := false
isStringFieldType := false
isOptional := false
if field.GetType().String() == "string" {
isStringFieldType = true
}
if field.GetRequiredness() == parser.FieldType_Optional {
isOptional = true
}
if anno := getAnnotation(field.Annotations, AnnotationQuery); len(anno) > 0 {
hasAnnotation = true
query := checkSnakeName(anno[0])
clientMethod.QueryParamsCode += fmt.Sprintf("%q: req.Get%s(),\n", query, field.GoName().String())
if isOptional && enableOptional {
clientMethod.QueryParamsCode += fmt.Sprintf("%q: func() interface{} {\n\t\t\t\tif req.IsSet%s() {\n\t\t\t\t\treturn req.Get%s()\n\t\t\t\t} else {\n\t\t\t\t\treturn nil\n\t\t\t\t}}(),\n", query, field.GoName().String(), field.GoName().String())
} else {
clientMethod.QueryParamsCode += fmt.Sprintf("%q: req.Get%s(),\n", query, field.GoName().String())
}
}

if anno := getAnnotation(field.Annotations, AnnotationPath); len(anno) > 0 {
Expand Down Expand Up @@ -326,7 +334,11 @@ func parseAnnotationToClient(clientMethod *generator.ClientMethod, p *parser.Typ
// cookie do nothing
}
if !hasAnnotation && strings.EqualFold(clientMethod.HTTPMethod, "get") {
clientMethod.QueryParamsCode += fmt.Sprintf("%q: req.Get%s(),\n", checkSnakeName(field.GetName()), field.GoName().String())
if isOptional && enableOptional {
clientMethod.QueryParamsCode += fmt.Sprintf("%q: func() interface{} {\n\t\t\t\tif req.IsSet%s() {\n\t\t\t\t\treturn req.Get%s()\n\t\t\t\t} else {\n\t\t\t\t\treturn nil\n\t\t\t\t}}(),\n", checkSnakeName(field.GetName()), field.GoName().String(), field.GoName().String())
} else {
clientMethod.QueryParamsCode += fmt.Sprintf("%q: req.Get%s(),\n", checkSnakeName(field.GetName()), field.GoName().String())
}
}
}
clientMethod.BodyParamsCode = meta.SetBodyParam
Expand Down

0 comments on commit fc59bc7

Please sign in to comment.