Skip to content

Commit

Permalink
feat(gen): add OperationName type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
saccho committed Oct 30, 2024
1 parent 7e342ba commit 4ba7154
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
4 changes: 2 additions & 2 deletions gen/_template/client.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (c *{{ if $op.WebhookInfo }}Webhook{{ end }}Client) send{{ $op.Name }}(ctx
c.requests.Add(ctx, 1, metric.WithAttributes(otelAttrs...))

// Start a span for this request.
ctx, span := c.cfg.Tracer.Start(ctx, {{ quote $op.Name }},
ctx, span := c.cfg.Tracer.Start(ctx, {{ $op.Name }}Operation,
{{- if $hasOTELAttrs }}
trace.WithAttributes(otelAttrs...),
{{- end }}
Expand Down Expand Up @@ -283,7 +283,7 @@ func (c *{{ if $op.WebhookInfo }}Webhook{{ end }}Client) send{{ $op.Name }}(ctx
{
{{- $securityName := $s.Type.Name }}
{{ if $otel }}stage = {{ printf "Security:%s" $securityName | quote }}{{ end }}
switch err := c.security{{ $securityName }}(ctx, {{ quote $op.Name }}, r); {
switch err := c.security{{ $securityName }}(ctx, {{ $op.Name }}Operation, r); {
case err == nil: // if NO error
satisfied[{{ div $idx 8 }}] |= 1 << {{ mod $idx 8 }}
case errors.Is(err, ogenerrors.ErrSkipClientSecurity):
Expand Down
8 changes: 4 additions & 4 deletions gen/_template/handlers.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s *{{ if $op.WebhookInfo }}Webhook{{ end }}Server) handle{{ $op.Name }}Req
{{- end }}

// Start a span for this request.
ctx, span := s.cfg.Tracer.Start(r.Context(), {{ quote $op.Name }},
ctx, span := s.cfg.Tracer.Start(r.Context(), {{ $op.Name }}Operation,
{{- if $hasOTELAttrs }}
trace.WithAttributes(otelAttrs...),
{{- end }}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (s *{{ if $op.WebhookInfo }}Webhook{{ end }}Server) handle{{ $op.Name }}Req
err error
{{- if or $op.Request $op.Params $op.Security.Securities }}
opErrContext = ogenerrors.OperationContext{
Name: {{ quote $op.Name }},
Name: {{ $op.Name }}Operation,
ID: {{ quote $op.Spec.OperationID }},
}
{{- end }}
Expand All @@ -99,7 +99,7 @@ func (s *{{ if $op.WebhookInfo }}Webhook{{ end }}Server) handle{{ $op.Name }}Req
{{- range $idx, $s := $securities }}
{{- $securityName := $s.Type.Name }}
{
sctx, ok, err := s.security{{ $securityName }}(ctx, {{ quote $op.Name }}, r)
sctx, ok, err := s.security{{ $securityName }}(ctx, {{ $op.Name }}Operation, r)
if err != nil {
err = &ogenerrors.SecurityError{
OperationContext: opErrContext,
Expand Down Expand Up @@ -193,7 +193,7 @@ func (s *{{ if $op.WebhookInfo }}Webhook{{ end }}Server) handle{{ $op.Name }}Req
if m := s.cfg.Middleware; m != nil {
mreq := middleware.Request{
Context: ctx,
OperationName: {{ quote $op.Name }},
OperationName: {{ $op.Name }}Operation,
OperationSummary: {{ quote $op.Summary }},
OperationID: {{ quote $op.Spec.OperationID }},
Body: {{- if $op.Request }}request{{- else }}nil{{- end }},
Expand Down
17 changes: 17 additions & 0 deletions gen/_template/operations.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{ define "operations" }}
{{- /*gotype: github.com/ogen-go/ogen/gen.TemplateConfig*/ -}}
{{ template "header" $ }}

// OperationName is the ogen operation name
type OperationName = string

const (
{{- range $op := $.Operations }}
{{ $op.Name }}Operation OperationName = {{ quote $op.Name }}
{{- end }}
{{- range $op := $.Webhooks }}
{{ $op.Name }}Operation OperationName = {{ quote $op.Name }}
{{- end }}
)

{{ end }}
2 changes: 1 addition & 1 deletion gen/_template/router.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (s *WebhookServer) Handler(webhookName string) http.Handler {
{{- range $route := $r.Routes }}
case {{ quote $route.Method }}:
{{- $op := $route.Operation }}
r.name = {{ quote $op.Name }}
r.name = {{ $op.Name }}Operation
r.summary = {{ quote $op.Summary }}
r.operationID = {{ quote $op.Spec.OperationID }}
r.pathPattern = {{ quote $route.Path }}
Expand Down
18 changes: 9 additions & 9 deletions gen/_template/security.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ type SecurityHandler interface {
// Handle{{ $s.Type.Name }} handles {{ $name }} security.
{{- template "godoc" $s.GoDoc }}
{{- if $s.Format.IsCustomSecurity }}
Handle{{ $s.Type.Name }}(ctx context.Context, operationName string, req *http.Request) (context.Context, error)
Handle{{ $s.Type.Name }}(ctx context.Context, operationName OperationName, req *http.Request) (context.Context, error)
{{- else }}
Handle{{ $s.Type.Name }}(ctx context.Context, operationName string, t {{ $s.Type.Name }}) (context.Context, error)
Handle{{ $s.Type.Name }}(ctx context.Context, operationName OperationName, t {{ $s.Type.Name }}) (context.Context, error)
{{- end }}
{{- end }}
}
Expand All @@ -36,7 +36,7 @@ func findAuthorization(h http.Header, prefix string) (string, bool) {
{{- if $s.Format.IsOAuth2Security }}
var oauth2Scopes{{ $s.Type.Name }} = map[string][]string {
{{- range $operationName, $scopes := $s.Scopes }}
{{ quote $operationName }}: []string{
{{ $operationName }}Operation: []string{
{{- range $scope := $scopes }}
{{ quote $scope }},
{{- end}}
Expand All @@ -45,7 +45,7 @@ var oauth2Scopes{{ $s.Type.Name }} = map[string][]string {
}

{{- end }}
func (s *Server) security{{ $s.Type.Name }}(ctx context.Context, operationName string, req *http.Request) (context.Context, bool, error) {
func (s *Server) security{{ $s.Type.Name }}(ctx context.Context, operationName OperationName, req *http.Request) (context.Context, bool, error) {
{{- if $s.Format.IsAPIKeySecurity }}
var t {{ $s.Type.Name }}
const parameterName = {{ quote $s.ParameterName }}
Expand Down Expand Up @@ -124,22 +124,22 @@ type SecuritySource interface {
{{- template "godoc" $s.GoDoc }}
{{- if $s.Format.IsCustomSecurity }}
{{- if $.SecurityReentrantEnabled }}
{{ $s.Type.Name }}(ctx context.Context, operationName string, req *http.Request, client *Client) error
{{ $s.Type.Name }}(ctx context.Context, operationName OperationName, req *http.Request, client *Client) error
{{- else }}
{{ $s.Type.Name }}(ctx context.Context, operationName string, req *http.Request) error
{{ $s.Type.Name }}(ctx context.Context, operationName OperationName, req *http.Request) error
{{- end }}
{{- else }}
{{- if $.SecurityReentrantEnabled }}
{{ $s.Type.Name }}(ctx context.Context, operationName string, client *Client) ({{ $s.Type.Name }}, error)
{{ $s.Type.Name }}(ctx context.Context, operationName OperationName, client *Client) ({{ $s.Type.Name }}, error)
{{- else }}
{{ $s.Type.Name }}(ctx context.Context, operationName string) ({{ $s.Type.Name }}, error)
{{ $s.Type.Name }}(ctx context.Context, operationName OperationName) ({{ $s.Type.Name }}, error)
{{- end }}
{{- end }}
{{- end }}
}

{{- range $s := $.Securities }}
func (s *Client) security{{ $s.Type.Name }}(ctx context.Context, operationName string, req *http.Request) error {
func (s *Client) security{{ $s.Type.Name }}(ctx context.Context, operationName OperationName, req *http.Request) error {
{{- if $s.Format.IsCustomSecurity }}
{{- if $.SecurityReentrantEnabled }}
if err := s.sec.{{ $s.Type.Name }}(ctx, operationName, req, s); err != nil {
Expand Down
1 change: 1 addition & 0 deletions gen/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ func (g *Generator) WriteSource(fs FileSystem, pkgName string) error {
{"faker", features.Has(DebugExampleTests)},
{"unimplemented", features.Has(OgenUnimplemented) && genServer},
{"labeler", features.Has(OgenOtel) && genServer},
{"operations", (genClient || genServer)},
} {
t := t
if !t.enabled {
Expand Down

0 comments on commit 4ba7154

Please sign in to comment.