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

[QUESTION] hz handler.go 与 handler_single.go传值不一样 #1001

Closed
ypo2478 opened this issue Nov 16, 2023 · 3 comments · Fixed by #1004
Closed

[QUESTION] hz handler.go 与 handler_single.go传值不一样 #1001

ypo2478 opened this issue Nov 16, 2023 · 3 comments · Fixed by #1004
Assignees
Labels
question Further information is requested

Comments

@ypo2478
Copy link
Contributor

ypo2478 commented Nov 16, 2023

handler.go
https://github.com/cloudwego/hertz/blob/c849a7a38e42c4159d8ab12a1598dfbe5e080b3f/cmd/hz/generator/handler.go#L185C1-L187C3

handler_single.go
https://github.com/cloudwego/hertz/blob/c849a7a38e42c4159d8ab12a1598dfbe5e080b3f/cmd/hz/generator/handler.go#L237C1-L245C52

2个地方传的参数不一致,导致我想_single.go使用PackageName失败。

layouts:
  - path: handler_single.go
    delims:
      - '{{'
      - '}}'
    body: |+
      {{.Comment}}
      func {{$.Name}}(ctx context.Context, c *app.RequestContext) {
      	var err error
      	{{if ne .RequestTypeName "" -}}
      	var req {{$.RequestTypeName}}
      	err = c.BindAndValidate(&req)
      	if err != nil {
      		utils.SendErrResponse(ctx, c, consts.StatusOK, err)
      		return
      	}
      	{{end}}
      	resp := {{$.PackageName}}service.New{{$.PackageName | camelcase}}Service(ctx, c).{{$.Name}}(&req)

      	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
      }

是否可以考虑优化成如下:

        # 拷贝hander里面值
	baseData := make(map[string]string)
	keys := reflect.TypeOf(handler)
	values := reflect.ValueOf(handler)
	for i := 0; i < keys.NumField(); i++ {
		if values.Field(i).Kind() == reflect.String {
			baseData[keys.Field(i).Name] = values.Field(i).String()
		}
	}
	// insert new handler
	for _, method := range handler.(Handler).Methods {
		if bytes.Contains(file, []byte(fmt.Sprintf("func %s(", method.Name))) {
			continue
		}

		// Generate additional handlers using templates
		handlerSingleTpl := pkgGen.tpls[handlerSingleTplName]
		if handlerSingleTpl == nil {
			return fmt.Errorf("tpl %s not found", handlerSingleTplName)
		}
		data := make(map[string]string)
		for k, v := range baseData {
			data[k] = v
		}
		data["Comment"] = method.Comment
		data["Name"] = method.Name
		data["RequestTypeName"] = method.RequestTypeName
		data["ReturnTypeName"] = method.ReturnTypeName
		data["Serializer"] = method.Serializer
		data["OutputDir"] = method.OutputDir
		handlerFunc := bytes.NewBuffer(nil)
		err = handlerSingleTpl.Execute(handlerFunc, data)

如果可行的话,我提个PR

@github-actions github-actions bot added the invalid issue invalid issue (not related to Hertz or described in document or not enough information provided) label Nov 16, 2023
Copy link

This issue has been marked as invalid question, please give more information by following the issue template. The issue will be closed in 1 days if no further activity occurs.

@github-actions github-actions bot added the stale label Nov 16, 2023
@li-jin-gou li-jin-gou added question Further information is requested and removed invalid issue invalid issue (not related to Hertz or described in document or not enough information provided) stale labels Nov 16, 2023
@li-jin-gou
Copy link
Member

cc @FGYFFFF

@FGYFFFF
Copy link
Contributor

FGYFFFF commented Nov 17, 2023

@ypo2478 可以的,你提pr吧;不过如果你想要重新写 handler 还有最追加内容的,可以重新自己定义一份 handler,将原有的 handler disable 掉,可能实现起来会更方便

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

Successfully merging a pull request may close this issue.

3 participants