Skip to content

Commit

Permalink
fix local generation
Browse files Browse the repository at this point in the history
  • Loading branch information
peymanmortazavi committed May 31, 2024
1 parent 1ea047c commit 42b7463
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,31 @@
{{- if .HasRepeatedEnumPathParam}}
es []int32
{{- end}}
ok bool
err error
_ = err
)
{{$binding := .}}
{{range $param := .PathParameters}}
{{$enum := $binding.LookupEnum $param}}
val, ok = pathParams[{{$param | printf "%q"}}]
if !ok {
return nil, metadata, status.Errorf(codes.InvalidArgument, "missing parameter %s", {{$param | printf "%q"}})
val = pathParams.ByName({{$param | printf "%q"}})
if val == "" {
return nil, metadata, gateway.ErrPathParameterMissing{Name: {{$param | printf "%q"}}}
}
{{if $param.IsNestedProto3}}
err = protopath.PopulateFieldFromPath(&protoReq, {{$param | printf "%q"}}, val)
if err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", {{$param | printf "%q"}}, err)
return nil, metadata, gateway.ErrPathParameterTypeMismatch{Err: err, Name: {{$param | printf "%q"}}}
}
{{if $enum}}
e{{if $param.IsRepeated}}s{{end}}, err = {{$param.ConvertFuncExpr}}(val{{if $param.IsRepeated}}, {{$binding.RepeatedPathParamSeparator.Separator | printf "%c" | printf "%q"}}{{end}}, {{$enum.GoType $param.Method.Service.File.GoPkg.Path | camelIdentifier}}_value)
if err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "could not parse path as enum value, parameter: %s, error: %v", {{$param | printf "%q"}}, err)
return nil, metadata, gateway.ErrPathParameterInvalidEnum{Err: err, Name: {{$param | printf "%q"}}}
}
{{end}}
{{else if $enum}}
e{{if $param.IsRepeated}}s{{end}}, err = {{$param.ConvertFuncExpr}}(val{{if $param.IsRepeated}}, {{$binding.RepeatedPathParamSeparator.Separator | printf "%c" | printf "%q"}}{{end}}, {{$enum.GoType $param.Method.Service.File.GoPkg.Path | camelIdentifier}}_value)
if err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", {{$param | printf "%q"}}, err)
return nil, metadata, gateway.ErrPathParameterInvalidEnum{Err: err, Name: {{$param | printf "%q"}}}
}
{{else}}
{{- $protoReq := $param.AssignableExprPrep "protoReq" $binding.Method.Service.File.GoPkg.Path -}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{if .Method.GetServerStreaming}}
{{else}}
func local_request_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}(ctx context.Context, marshaler gateway.Marshaler, server {{.Method.Service.InstanceName}}Server, req *http.Request, pathParams gateway.Params) (proto.Message, gateway.ServerMetadata, error)
func local_request_{{.Method.Service.GetName}}_{{.Method.GetName}}_{{.Index}}(ctx context.Context, marshaler gateway.Marshaler, mux *gateway.ServeMux, server {{.Method.Service.InstanceName}}Server, req *http.Request, pathParams gateway.Params) (proto.Message, gateway.ServerMetadata, error)
{{end}}
14 changes: 7 additions & 7 deletions codegen/internal/gengateway/templates/local_trailer.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Register{{$svc.GetName}}{{$.RegisterFuncSuffix}}Server(ctx context.Context,
ctx, cancel := context.WithCancel(ctx)
{{- end }}
defer cancel()
var stream mux.ServerTransportStream
var stream gateway.ServerTransportStream
ctx = grpc.NewContextWithServerTransportStream(ctx, &stream)
inboundMarshaler, outboundMarshaler := mux.MarshalerForRequest(req)
var err error
Expand All @@ -36,19 +36,19 @@ func Register{{$svc.GetName}}{{$.RegisterFuncSuffix}}Server(ctx context.Context,
mux.HTTPError(ctx, outboundMarshaler, w, req, err)
return
}
resp, md, err := local_request_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(annotatedContext, inboundMarshaler, server, req, pathParams)
resp, md, err := local_request_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(annotatedContext, inboundMarshaler, mux, server, req, pathParams)
md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer())
annotatedContext = gateway.NewServerMetadataContext(annotatedContext, md)
if err != nil {
mux.HTTPError(annotatedContext, outboundMarshaler, w, req, err)
return
}

{{ if $b.ResponseBody }}
forward_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(annotatedContext, mux, outboundMarshaler, w, req, response_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}{resp}, mux.GetForwardResponseOptions()...)
{{ else }}
forward_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
{{end}}
{{ if $b.ResponseBody -}}
mux.ForwardResponseMessage(annotatedContext, outboundMarshaler, w, req, response_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}{resp.(*{{.Method.ResponseType.GoType .Method.Service.File.GoPkg.Path}})})
{{ else -}}
mux.ForwardResponseMessage(annotatedContext, outboundMarshaler, w, req, resp)
{{end -}}
})
{{end}}
{{end}}
Expand Down
3 changes: 3 additions & 0 deletions docs/reference/openapi/cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Plug-in Options

TBD
1 change: 1 addition & 0 deletions examples/internal/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins:
opt:
- 'paths=source_relative'
- 'config_search_path=proto'
- 'generate_local=true'
- out: gen
name: openapiv3
opt:
Expand Down
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ nav:
- reference/configuration.md
- Gateway:
- reference/grpc/cli.md
- OpenAPI:
- reference/openapi/cli.md

theme:
name: material
Expand Down

0 comments on commit 42b7463

Please sign in to comment.