short/internal/oas/oas_response_encoders_gen.go

87 lines
2 KiB
Go
Raw Permalink Normal View History

2024-01-29 18:32:16 +00:00
// Code generated by ogen, DO NOT EDIT.
package oas
import (
"net/http"
"github.com/go-faster/errors"
"github.com/go-faster/jx"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"github.com/ogen-go/ogen/conv"
"github.com/ogen-go/ogen/uri"
)
func encodeCreateShortURLResponse(response CreateShortURLRes, w http.ResponseWriter, span trace.Span) error {
2024-01-29 18:32:16 +00:00
switch response := response.(type) {
case *CreateShortURLCreated:
2024-01-29 18:32:16 +00:00
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(201)
span.SetStatus(codes.Ok, http.StatusText(201))
e := new(jx.Encoder)
response.Encode(e)
if _, err := e.WriteTo(w); err != nil {
return errors.Wrap(err, "write")
}
return nil
case *CreateShortURLBadRequest:
2024-01-29 18:32:16 +00:00
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(400)
span.SetStatus(codes.Error, http.StatusText(400))
e := new(jx.Encoder)
response.Encode(e)
if _, err := e.WriteTo(w); err != nil {
return errors.Wrap(err, "write")
}
return nil
default:
return errors.Errorf("unexpected response type: %T", response)
}
}
func encodeRedirectLongURLResponse(response RedirectLongURLRes, w http.ResponseWriter, span trace.Span) error {
2024-01-29 18:32:16 +00:00
switch response := response.(type) {
case *RedirectLongURLTemporaryRedirect:
2024-01-29 18:32:16 +00:00
// Encoding response headers.
{
h := uri.NewHeaderEncoder(w.Header())
// Encode "Location" header.
{
cfg := uri.HeaderParameterEncodingConfig{
Name: "Location",
Explode: false,
}
if err := h.EncodeParam(cfg, func(e uri.Encoder) error {
if val, ok := response.Location.Get(); ok {
return e.EncodeValue(conv.StringToString(val))
}
return nil
}); err != nil {
return errors.Wrap(err, "encode Location header")
}
}
}
w.WriteHeader(307)
span.SetStatus(codes.Ok, http.StatusText(307))
return nil
case *RedirectLongURLNotFound:
2024-01-29 18:32:16 +00:00
w.WriteHeader(404)
span.SetStatus(codes.Error, http.StatusText(404))
return nil
default:
return errors.Errorf("unexpected response type: %T", response)
}
}