Skip to content

Commit 5ce0e43

Browse files
committed
feat: add ContextLoadFunc for custom context loading in templates and configuration
- Introduced `ContextLoadFunc` to `config.go`, `template.tmpl`, and `template.go`. - Updated `main.go` to allow `context_load_func` as a configurable flag.
1 parent 750c6e3 commit 5ce0e43

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

generate/http/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Config struct {
1818
DataRespType protogen.GoIdent
1919

2020
ServerHandlerFunc protogen.GoIdent
21+
ContextLoadFunc string
2122
}
2223

2324
type GenConfig struct {
@@ -37,6 +38,7 @@ func NewGenConf(g *protogen.GeneratedFile, conf *Config) *GenConfig {
3738
DataResponseType: g.QualifiedGoIdent(conf.DataRespType),
3839

3940
ServerHandlerWrapperFunc: g.QualifiedGoIdent(conf.ServerHandlerFunc),
41+
ContextLoadFunc: conf.ContextLoadFunc,
4042
}
4143
genConf := &GenConfig{
4244
omitempty: conf.Omitempty,

generate/template/template.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type PackageDesc struct {
7373
ValidateFunc string
7474

7575
ServerHandlerWrapperFunc string
76+
ContextLoadFunc string
7677
}
7778

7879
func (s *ServiceDesc) Execute() (string, error) {

generate/template/template.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv {{$svrType}}HTTPServer) {{
5454
return nil, err
5555
}
5656
{{- end}}
57-
out, err := srv.{{.Name}}(ctx, &in)
57+
out, err := srv.{{.Name}}(ctx{{$packageDesc.ContextLoadFunc}}, &in)
5858
if err != nil {
5959
return nil, err
6060
}

main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ var (
2727
templateFile = flag.String("template_file", "", "template file, if not set, use default template")
2828
swaggerAuthHeader = flag.String("swagger_auth_header", swaggerAuthComment, "swagger auth header")
2929

30-
routerType = flag.String("router_type", defaultHTTPxPackage+";Router", "router type")
31-
contextType = flag.String("context_type", defaultHTTPxPackage+";Context", "context type")
32-
handlerType = flag.String("handler_type", defaultHTTPxPackage+";Handler", "handler type")
30+
routerType = flag.String("router_type", defaultHTTPxPackage+";Router", "router type")
31+
contextType = flag.String("context_type", defaultHTTPxPackage+";Context", "context type")
32+
handlerType = flag.String("handler_type", defaultHTTPxPackage+";Handler", "handler type")
33+
contextLoadFunc = flag.String("context_load_func", ".Context()", "context load func")
3334

3435
errorRespType = flag.String("error_resp_type", defaultHTTPzPackage+";ErrorResponse", "error response type")
3536
dataRespType = flag.String("data_resp_type", defaultHTTPzPackage+";DataResponse", "data response type, must support generic")
@@ -119,6 +120,7 @@ func extractConfig() (*http.Config, error) {
119120
DataRespType: _dataRespType,
120121

121122
ServerHandlerFunc: _serverHandlerFunc,
123+
ContextLoadFunc: *contextLoadFunc,
122124
}
123125
return conf, nil
124126
}

0 commit comments

Comments
 (0)