From 794a4cc3650bfe3b7ebab7ad191bfaf1234260d5 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Fri, 2 Sep 2022 16:26:48 +0300 Subject: [PATCH] private/apigen: make code compile with 1.16 Our Test Versions still requires 1.16 to be compatible with our oldest uplink versions. These changes make the code compile with 1.16. Also, it makes go generate work in private/apigen/example. Change-Id: Ib2f7493941a16f361328fe01d2be293f26123719 --- private/apigen/example/api.gen.go | 4 +- private/apigen/example/doc.go | 6 +++ private/apigen/example/gen.go | 1 - private/apigen/gogen.go | 4 +- private/apigen/gogen_test.go | 2 +- .../console/consoleweb/consoleapi/api.gen.go | 48 +++++++++---------- 6 files changed, 35 insertions(+), 30 deletions(-) create mode 100644 private/apigen/example/doc.go diff --git a/private/apigen/example/api.gen.go b/private/apigen/example/api.gen.go index a689769da..f53bf1d51 100644 --- a/private/apigen/example/api.gen.go +++ b/private/apigen/example/api.gen.go @@ -23,7 +23,7 @@ const dateLayout = "2006-01-02T15:04:05.999Z" var ErrTestapiAPI = errs.Class("example testapi api") type TestAPIService interface { - GenTestAPI(context.Context, uuid.UUID, time.Time, string, struct{ Content string }) (*struct { + GenTestAPI(ctx context.Context, path string, id uuid.UUID, date time.Time, request struct{ Content string }) (*struct { ID uuid.UUID Date time.Time PathParam string @@ -103,7 +103,7 @@ func (h *TestAPIHandler) handleGenTestAPI(w http.ResponseWriter, r *http.Request return } - retVal, httpErr := h.service.GenTestAPI(ctx, id, date, path, payload) + retVal, httpErr := h.service.GenTestAPI(ctx, path, id, date, payload) if httpErr.Err != nil { api.ServeError(h.log, w, httpErr.Status, httpErr.Err) return diff --git a/private/apigen/example/doc.go b/private/apigen/example/doc.go new file mode 100644 index 000000000..a15204f8c --- /dev/null +++ b/private/apigen/example/doc.go @@ -0,0 +1,6 @@ +// Copyright (C) 2022 Storj Labs, Inc. +// See LICENSE for copying information. + +package example + +//go:generate go run gen.go diff --git a/private/apigen/example/gen.go b/private/apigen/example/gen.go index 1c78f65ec..987bb66bd 100644 --- a/private/apigen/example/gen.go +++ b/private/apigen/example/gen.go @@ -1,7 +1,6 @@ // Copyright (C) 2022 Storj Labs, Inc. // See LICENSE for copying information. -//go:generate go run gen.go //go:build ignore // +build ignore diff --git a/private/apigen/gogen.go b/private/apigen/gogen.go index 51ba2a119..4c00f7275 100644 --- a/private/apigen/gogen.go +++ b/private/apigen/gogen.go @@ -310,11 +310,11 @@ func handleParams(pf func(format string, a ...interface{}), i func(paths ...stri switch params { case &queryParams: - pf("if !r.URL.Query().Has(\"%s\") {", param.Name) + pf("%s := r.URL.Query().Get(\"%s\")", varName, param.Name) + pf("if %s == \"\" {", varName) pf("api.ServeError(h.log, w, http.StatusBadRequest, errs.New(\"parameter '%s' can't be empty\"))", param.Name) pf("return") pf("}") - pf("%s := r.URL.Query().Get(\"%s\")", varName, param.Name) pf("") case &pathParams: pf("%s, ok := mux.Vars(r)[\"%s\"]", varName, param.Name) diff --git a/private/apigen/gogen_test.go b/private/apigen/gogen_test.go index e8531dd9a..2d99e85a2 100644 --- a/private/apigen/gogen_test.go +++ b/private/apigen/gogen_test.go @@ -45,7 +45,7 @@ func (a auth) IsAuthenticated(ctx context.Context, r *http.Request, isCookieAuth func (a auth) RemoveAuthCookie(w http.ResponseWriter) {} -func (s service) GenTestAPI(ctx context.Context, id uuid.UUID, date time.Time, pathParam string, body struct{ Content string }) (*response, api.HTTPError) { +func (s service) GenTestAPI(ctx context.Context, pathParam string, id uuid.UUID, date time.Time, body struct{ Content string }) (*response, api.HTTPError) { return &response{ ID: id, Date: date, diff --git a/satellite/console/consoleweb/consoleapi/api.gen.go b/satellite/console/consoleweb/consoleapi/api.gen.go index a753b8434..8819804f4 100644 --- a/satellite/console/consoleweb/consoleapi/api.gen.go +++ b/satellite/console/consoleweb/consoleapi/api.gen.go @@ -267,11 +267,11 @@ func (h *ProjectManagementHandler) handleGenGetSingleBucketUsageRollup(w http.Re return } - if !r.URL.Query().Has("projectID") { + projectIDParam := r.URL.Query().Get("projectID") + if projectIDParam == "" { api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'projectID' can't be empty")) return } - projectIDParam := r.URL.Query().Get("projectID") projectID, err := uuid.FromString(projectIDParam) if err != nil { @@ -279,17 +279,17 @@ func (h *ProjectManagementHandler) handleGenGetSingleBucketUsageRollup(w http.Re return } - if !r.URL.Query().Has("bucket") { + bucket := r.URL.Query().Get("bucket") + if bucket == "" { api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'bucket' can't be empty")) return } - bucket := r.URL.Query().Get("bucket") - if !r.URL.Query().Has("since") { + sinceParam := r.URL.Query().Get("since") + if sinceParam == "" { api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'since' can't be empty")) return } - sinceParam := r.URL.Query().Get("since") since, err := time.Parse(dateLayout, sinceParam) if err != nil { @@ -297,11 +297,11 @@ func (h *ProjectManagementHandler) handleGenGetSingleBucketUsageRollup(w http.Re return } - if !r.URL.Query().Has("before") { + beforeParam := r.URL.Query().Get("before") + if beforeParam == "" { api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'before' can't be empty")) return } - beforeParam := r.URL.Query().Get("before") before, err := time.Parse(dateLayout, beforeParam) if err != nil { @@ -335,11 +335,11 @@ func (h *ProjectManagementHandler) handleGenGetBucketUsageRollups(w http.Respons return } - if !r.URL.Query().Has("projectID") { + projectIDParam := r.URL.Query().Get("projectID") + if projectIDParam == "" { api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'projectID' can't be empty")) return } - projectIDParam := r.URL.Query().Get("projectID") projectID, err := uuid.FromString(projectIDParam) if err != nil { @@ -347,11 +347,11 @@ func (h *ProjectManagementHandler) handleGenGetBucketUsageRollups(w http.Respons return } - if !r.URL.Query().Has("since") { + sinceParam := r.URL.Query().Get("since") + if sinceParam == "" { api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'since' can't be empty")) return } - sinceParam := r.URL.Query().Get("since") since, err := time.Parse(dateLayout, sinceParam) if err != nil { @@ -359,11 +359,11 @@ func (h *ProjectManagementHandler) handleGenGetBucketUsageRollups(w http.Respons return } - if !r.URL.Query().Has("before") { + beforeParam := r.URL.Query().Get("before") + if beforeParam == "" { api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'before' can't be empty")) return } - beforeParam := r.URL.Query().Get("before") before, err := time.Parse(dateLayout, beforeParam) if err != nil { @@ -397,17 +397,17 @@ func (h *ProjectManagementHandler) handleGenGetAPIKeys(w http.ResponseWriter, r return } - if !r.URL.Query().Has("search") { + search := r.URL.Query().Get("search") + if search == "" { api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'search' can't be empty")) return } - search := r.URL.Query().Get("search") - if !r.URL.Query().Has("limit") { + limitParam := r.URL.Query().Get("limit") + if limitParam == "" { api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'limit' can't be empty")) return } - limitParam := r.URL.Query().Get("limit") limitParamU64, err := strconv.ParseUint(limitParam, 10, 32) if err != nil { @@ -416,11 +416,11 @@ func (h *ProjectManagementHandler) handleGenGetAPIKeys(w http.ResponseWriter, r } limit := uint(limitParamU64) - if !r.URL.Query().Has("page") { + pageParam := r.URL.Query().Get("page") + if pageParam == "" { api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'page' can't be empty")) return } - pageParam := r.URL.Query().Get("page") pageParamU64, err := strconv.ParseUint(pageParam, 10, 32) if err != nil { @@ -429,11 +429,11 @@ func (h *ProjectManagementHandler) handleGenGetAPIKeys(w http.ResponseWriter, r } page := uint(pageParamU64) - if !r.URL.Query().Has("order") { + orderParam := r.URL.Query().Get("order") + if orderParam == "" { api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'order' can't be empty")) return } - orderParam := r.URL.Query().Get("order") orderParamU64, err := strconv.ParseUint(orderParam, 10, 8) if err != nil { @@ -442,11 +442,11 @@ func (h *ProjectManagementHandler) handleGenGetAPIKeys(w http.ResponseWriter, r } order := console.APIKeyOrder(orderParamU64) - if !r.URL.Query().Has("orderDirection") { + orderDirectionParam := r.URL.Query().Get("orderDirection") + if orderDirectionParam == "" { api.ServeError(h.log, w, http.StatusBadRequest, errs.New("parameter 'orderDirection' can't be empty")) return } - orderDirectionParam := r.URL.Query().Get("orderDirection") orderDirectionParamU64, err := strconv.ParseUint(orderDirectionParam, 10, 8) if err != nil {