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
This commit is contained in:
Egon Elbre 2022-09-02 16:26:48 +03:00
parent 1343528a43
commit 794a4cc365
6 changed files with 35 additions and 30 deletions

View File

@ -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

View File

@ -0,0 +1,6 @@
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package example
//go:generate go run gen.go

View File

@ -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

View File

@ -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)

View File

@ -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,

View File

@ -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 {