|
| 1 | +// ServerInterfaceWrapper converts fiber contexts to parameters. |
| 2 | +type ServerInterfaceWrapper struct { |
| 3 | + Handler ServerInterface |
| 4 | +} |
| 5 | + |
| 6 | +{{range .}}{{$opid := .OperationId}}// {{$opid}} converts fiber context to params. |
| 7 | +func (w *ServerInterfaceWrapper) {{.OperationId}} (ctx *fiber.Ctx) error { |
| 8 | + var err error |
| 9 | +{{range .PathParams}}// ------------- Path parameter "{{.ParamName}}" ------------- |
| 10 | + var {{$varName := .GoVariableName}}{{$varName}} {{.TypeDef}} |
| 11 | +{{if .IsPassThrough}} |
| 12 | + {{$varName}} = ctx.Params("{{.ParamName}}") |
| 13 | +{{end}} |
| 14 | +{{if .IsJson}} |
| 15 | + err = json.Unmarshal([]byte(ctx.Params("{{.ParamName}}")), &{{$varName}}) |
| 16 | + if err != nil { |
| 17 | + return fiber.NewError(http.StatusBadRequest, "Error unmarshalling parameter '{{.ParamName}}' as JSON") |
| 18 | + } |
| 19 | +{{end}} |
| 20 | +{{if .IsStyled}} |
| 21 | + err = runtime.BindStyledParameterWithLocation("{{.Style}}",{{.Explode}}, "{{.ParamName}}", runtime.ParamLocationPath, ctx.Params("{{.ParamName}}"), &{{$varName}}) |
| 22 | + if err != nil { |
| 23 | + return fiber.NewError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err)) |
| 24 | + } |
| 25 | +{{end}} |
| 26 | +{{end}} |
| 27 | + |
| 28 | +{{range .SecurityDefinitions}} |
| 29 | + ctx.Locals({{.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 | + |
| 36 | +{{if .QueryParams}} |
| 37 | + bURL := ctx.Request().URI().FullURI() |
| 38 | + sURL := string(bURL) |
| 39 | + u, err := url.Parse(sURL) |
| 40 | + if err != nil { |
| 41 | + return fiber.NewError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameters: %s", err)) |
| 42 | + } |
| 43 | +{{end}} |
| 44 | + |
| 45 | +{{range $paramIdx, $param := .QueryParams}} |
| 46 | + {{- if (or (or .Required .IsPassThrough) (or .IsJson .IsStyled)) -}} |
| 47 | + // ------------- {{if .Required}}Required{{else}}Optional{{end}} query parameter "{{.ParamName}}" ------------- |
| 48 | + {{ end }} |
| 49 | + {{if .IsStyled}} |
| 50 | + |
| 51 | + err = runtime.BindQueryParameter("{{.Style}}", {{.Explode}}, {{.Required}}, "{{.ParamName}}", u.Query(), ¶ms.{{.GoName}}) |
| 52 | + if err != nil { |
| 53 | + return fiber.NewError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err)) |
| 54 | + } |
| 55 | + {{else}} |
| 56 | + if paramValue := ctx.Query("{{.ParamName}}"); paramValue != "" { |
| 57 | + {{if .IsPassThrough}} |
| 58 | + params.{{.GoName}} = {{if not .Required}}&{{end}}paramValue |
| 59 | + {{end}} |
| 60 | + {{if .IsJson}} |
| 61 | + var value {{.TypeDef}} |
| 62 | + err = json.Unmarshal([]byte(paramValue), &value) |
| 63 | + if err != nil { |
| 64 | + return fiber.NewError(http.StatusBadRequest, "Error unmarshalling parameter '{{.ParamName}}' as JSON") |
| 65 | + } |
| 66 | + params.{{.GoName}} = {{if not .Required}}&{{end}}value |
| 67 | + {{end}} |
| 68 | + }{{if .Required}} else { |
| 69 | + return fiber.NewError(http.StatusBadRequest, fmt.Sprintf("Query argument {{.ParamName}} is required, but not found")) |
| 70 | + }{{end}} |
| 71 | + {{end}} |
| 72 | +{{end}} |
| 73 | + |
| 74 | +{{if .HeaderParams}} |
| 75 | + headers := http.Header{} |
| 76 | + ctx.Request().Header.VisitAll(func(k, v []byte) { |
| 77 | + key := string(k) |
| 78 | + value := string(v) |
| 79 | + headers.Add(key, value) |
| 80 | + }) |
| 81 | + |
| 82 | +{{range .HeaderParams}}// ------------- {{if .Required}}Required{{else}}Optional{{end}} header parameter "{{.ParamName}}" ------------- |
| 83 | + if valueList, found := headers[http.CanonicalHeaderKey("{{.ParamName}}")]; found { |
| 84 | + var {{.GoName}} {{.TypeDef}} |
| 85 | + n := len(valueList) |
| 86 | + if n != 1 { |
| 87 | + return fiber.NewError(http.StatusBadRequest, fmt.Sprintf("Expected one value for {{.ParamName}}, got %d", n)) |
| 88 | + } |
| 89 | +{{if .IsPassThrough}} |
| 90 | + params.{{.GoName}} = {{if not .Required}}&{{end}}valueList[0] |
| 91 | +{{end}} |
| 92 | +{{if .IsJson}} |
| 93 | + err = json.Unmarshal([]byte(valueList[0]), &{{.GoName}}) |
| 94 | + if err != nil { |
| 95 | + return fiber.NewError(http.StatusBadRequest, "Error unmarshalling parameter '{{.ParamName}}' as JSON") |
| 96 | + } |
| 97 | +{{end}} |
| 98 | +{{if .IsStyled}} |
| 99 | + err = runtime.BindStyledParameterWithLocation("{{.Style}}",{{.Explode}}, "{{.ParamName}}", runtime.ParamLocationHeader, valueList[0], &{{.GoName}}) |
| 100 | + if err != nil { |
| 101 | + return fiber.NewError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err)) |
| 102 | + } |
| 103 | +{{end}} |
| 104 | + params.{{.GoName}} = {{if not .Required}}&{{end}}{{.GoName}} |
| 105 | + } {{if .Required}}else { |
| 106 | + return fiber.NewError(http.StatusBadRequest, fmt.Sprintf("Header parameter {{.ParamName}} is required, but not found")) |
| 107 | + }{{end}} |
| 108 | +{{end}} |
| 109 | +{{end}} |
| 110 | + |
| 111 | +{{range .CookieParams}} |
| 112 | + if v := ctx.Cookies("{{.ParamName}}"); v == "" { |
| 113 | + {{if .IsPassThrough}} |
| 114 | + params.{{.GoName}} = {{if not .Required}}&{{end}}v |
| 115 | + {{end}} |
| 116 | + {{if .IsJson}} |
| 117 | + var value {{.TypeDef}} |
| 118 | + var decoded string |
| 119 | + decoded, err := url.QueryUnescape(v) |
| 120 | + if err != nil { |
| 121 | + return fiber.NewError(http.StatusBadRequest, "Error unescaping cookie parameter '{{.ParamName}}'") |
| 122 | + } |
| 123 | + err = json.Unmarshal([]byte(decoded), &value) |
| 124 | + if err != nil { |
| 125 | + return fiber.NewError(http.StatusBadRequest, "Error unmarshalling parameter '{{.ParamName}}' as JSON") |
| 126 | + } |
| 127 | + params.{{.GoName}} = {{if not .Required}}&{{end}}value |
| 128 | + {{end}} |
| 129 | + {{if .IsStyled}} |
| 130 | + var value {{.TypeDef}} |
| 131 | + err = runtime.BindStyledParameterWithLocation("simple",{{.Explode}}, "{{.ParamName}}", runtime.ParamLocationCookie, v, &value) |
| 132 | + if err != nil { |
| 133 | + return fiber.NewError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter {{.ParamName}}: %s", err)) |
| 134 | + } |
| 135 | + params.{{.GoName}} = {{if not .Required}}&{{end}}value |
| 136 | + {{end}} |
| 137 | + }{{if .Required}} else { |
| 138 | + return fiber.NewError(http.StatusBadRequest, fmt.Sprintf("Query argument {{.ParamName}} is required, but not found")) |
| 139 | + }{{end}} |
| 140 | + |
| 141 | +{{end}}{{/* .CookieParams */}} |
| 142 | + |
| 143 | +{{end}}{{/* .RequiresParamObject */}} |
| 144 | + // Invoke the callback with all the unmarshalled arguments |
| 145 | + err = w.Handler.{{.OperationId}}(ctx{{genParamNames .PathParams}}{{if .RequiresParamObject}}, params{{end}}) |
| 146 | + return err |
| 147 | +} |
| 148 | +{{end}} |
0 commit comments