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

feat(hz): optional filed parse for client #1177

Open
wants to merge 2 commits into
base: develop
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
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}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要在名称里指出来 Query 吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不仅仅是 query 场景,可能后续的 form 场景也会有;等用户有需求的时候我再form 场景也给加上

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我这次都给加上吧,要不之后再加就不好加了

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flag 的名字和描述里也说明下是 Client 的?另外这个行为默认enable 有啥问题吗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok。这个会对之前的行为有影响,导致之前传值现在不传值;目前来看,有这个需求的用户还是比较少的,所以还是单独搞一个 flag 出来


// 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
6 changes: 6 additions & 0 deletions cmd/hz/generator/package_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,13 @@ 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)
if v.Kind() == reflect.Pointer && v.IsNil() {
return r
}
switch v.Kind() {
case reflect.Slice, reflect.Array:
for index := 0; index < v.Len(); index++ {
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
Loading