|
| 1 | +// ServerInterfaceWrapper converts echo contexts to parameters. |
| 2 | +type ServerInterfaceWrapper struct { |
| 3 | + Handler ServerInterface |
| 4 | +} |
| 5 | + |
| 6 | +{{range .}}{{$opid := .OperationId}}// {{$opid}} converts echo context to params. |
| 7 | +func (w *ServerInterfaceWrapper) {{.OperationId}} (ctx *echo.Context) error { |
| 8 | + var err error |
| 9 | +{{range .PathParams}}// ------------- Path parameter "{{.ParamName}}" ------------- |
| 10 | + var {{$varName := .GoVariableName}}{{$varName}} {{.TypeDef}} |
| 11 | +{{if .IsPassThrough}} |
| 12 | + {{$varName}} = ctx.PathParam("{{.ParamName}}") |
| 13 | +{{end}} |
| 14 | +{{if .IsJson}} |
| 15 | + err = json.Unmarshal([]byte(ctx.PathParam("{{.ParamName}}")), &{{$varName}}) |
| 16 | + if err != nil { |
| 17 | + return echo.NewHTTPError(http.StatusBadRequest, "Error unmarshaling parameter '{{.ParamName}}' as JSON") |
| 18 | + } |
| 19 | +{{end}} |
| 20 | +{{if .IsStyled}} |
| 21 | + err = runtime.BindStyledParameterWithOptions("{{.Style}}", "{{.ParamName}}", ctx.PathParam("{{.ParamName}}"), &{{$varName}}, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: {{.Explode}}, Required: {{.Required}}}) |
| 22 | + if err != nil { |
| 23 | + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err)) |
| 24 | + } |
| 25 | +{{end}} |
| 26 | +{{end}} |
| 27 | + |
| 28 | +{{range .SecurityDefinitions}} |
| 29 | + ctx.Set({{.ProviderName | sanitizeGoIdentity | ucFirst}}Scopes, {{toStringArray .Scopes}}) |
| 30 | +{{end}} |
| 31 | + |
| 32 | +{{if .RequiresParamObject}} |
| 33 | + // Parameter object where we will unmarshal all parameters from the context |
| 34 | + var params {{.OperationId}}Params |
| 35 | +{{range $paramIdx, $param := .QueryParams}} |
| 36 | + {{- if (or (or .Required .IsPassThrough) (or .IsJson .IsStyled)) -}} |
| 37 | + // ------------- {{if .Required}}Required{{else}}Optional{{end}} query parameter "{{.ParamName}}" ------------- |
| 38 | + {{ end }} |
| 39 | + {{if .IsStyled}} |
| 40 | + err = runtime.BindQueryParameter("{{.Style}}", {{.Explode}}, {{.Required}}, "{{.ParamName}}", ctx.QueryParams(), ¶ms.{{.GoName}}) |
| 41 | + if err != nil { |
| 42 | + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err)) |
| 43 | + } |
| 44 | + {{else}} |
| 45 | + if paramValue := ctx.QueryParam("{{.ParamName}}"); paramValue != "" { |
| 46 | + {{if .IsPassThrough}} |
| 47 | + params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}paramValue |
| 48 | + {{end}} |
| 49 | + {{if .IsJson}} |
| 50 | + var value {{.TypeDef}} |
| 51 | + err = json.Unmarshal([]byte(paramValue), &value) |
| 52 | + if err != nil { |
| 53 | + return echo.NewHTTPError(http.StatusBadRequest, "Error unmarshaling parameter '{{.ParamName}}' as JSON") |
| 54 | + } |
| 55 | + params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value |
| 56 | + {{end}} |
| 57 | + }{{if .Required}} else { |
| 58 | + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Query argument {{.ParamName}} is required, but not found")) |
| 59 | + }{{end}} |
| 60 | + {{end}} |
| 61 | +{{end}} |
| 62 | + |
| 63 | +{{if .HeaderParams}} |
| 64 | + headers := ctx.Request().Header |
| 65 | +{{range .HeaderParams}}// ------------- {{if .Required}}Required{{else}}Optional{{end}} header parameter "{{.ParamName}}" ------------- |
| 66 | + if valueList, found := headers[http.CanonicalHeaderKey("{{.ParamName}}")]; found { |
| 67 | + var {{.GoName}} {{.TypeDef}} |
| 68 | + n := len(valueList) |
| 69 | + if n != 1 { |
| 70 | + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Expected one value for {{.ParamName}}, got %d", n)) |
| 71 | + } |
| 72 | +{{if .IsPassThrough}} |
| 73 | + params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}valueList[0] |
| 74 | +{{end}} |
| 75 | +{{if .IsJson}} |
| 76 | + err = json.Unmarshal([]byte(valueList[0]), &{{.GoName}}) |
| 77 | + if err != nil { |
| 78 | + return echo.NewHTTPError(http.StatusBadRequest, "Error unmarshaling parameter '{{.ParamName}}' as JSON") |
| 79 | + } |
| 80 | +{{end}} |
| 81 | +{{if .IsStyled}} |
| 82 | + err = runtime.BindStyledParameterWithOptions("{{.Style}}", "{{.ParamName}}", valueList[0], &{{.GoName}}, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: {{.Explode}}, Required: {{.Required}}}) |
| 83 | + if err != nil { |
| 84 | + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err)) |
| 85 | + } |
| 86 | +{{end}} |
| 87 | + params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}{{.GoName}} |
| 88 | + } {{if .Required}}else { |
| 89 | + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Header parameter {{.ParamName}} is required, but not found")) |
| 90 | + }{{end}} |
| 91 | +{{end}} |
| 92 | +{{end}} |
| 93 | + |
| 94 | +{{range .CookieParams}} |
| 95 | + if cookie, err := ctx.Cookie("{{.ParamName}}"); err == nil { |
| 96 | + {{if .IsPassThrough}} |
| 97 | + params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}cookie.Value |
| 98 | + {{end}} |
| 99 | + {{if .IsJson}} |
| 100 | + var value {{.TypeDef}} |
| 101 | + var decoded string |
| 102 | + decoded, err := url.QueryUnescape(cookie.Value) |
| 103 | + if err != nil { |
| 104 | + return echo.NewHTTPError(http.StatusBadRequest, "Error unescaping cookie parameter '{{.ParamName}}'") |
| 105 | + } |
| 106 | + err = json.Unmarshal([]byte(decoded), &value) |
| 107 | + if err != nil { |
| 108 | + return echo.NewHTTPError(http.StatusBadRequest, "Error unmarshaling parameter '{{.ParamName}}' as JSON") |
| 109 | + } |
| 110 | + params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value |
| 111 | + {{end}} |
| 112 | + {{if .IsStyled}} |
| 113 | + var value {{.TypeDef}} |
| 114 | + err = runtime.BindStyledParameterWithOptions("simple", "{{.ParamName}}", cookie.Value, &value, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationCookie, Explode: {{.Explode}}, Required: {{.Required}}}) |
| 115 | + if err != nil { |
| 116 | + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err)) |
| 117 | + } |
| 118 | + params.{{.GoName}} = {{if .HasOptionalPointer}}&{{end}}value |
| 119 | + {{end}} |
| 120 | + }{{if .Required}} else { |
| 121 | + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Query argument {{.ParamName}} is required, but not found")) |
| 122 | + }{{end}} |
| 123 | + |
| 124 | +{{end}}{{/* .CookieParams */}} |
| 125 | + |
| 126 | +{{end}}{{/* .RequiresParamObject */}} |
| 127 | + // Invoke the callback with all the unmarshaled arguments |
| 128 | + err = w.Handler.{{.OperationId}}(ctx{{genParamNames .PathParams}}{{if .RequiresParamObject}}, params{{end}}) |
| 129 | + return err |
| 130 | +} |
| 131 | +{{end}} |
0 commit comments