diff --git a/generator/main.go b/generator/main.go index 6a15982..412a677 100644 --- a/generator/main.go +++ b/generator/main.go @@ -12,7 +12,6 @@ import ( "regexp" "sort" "strings" - "sync" "time" "github.com/dave/jennifer/jen" @@ -61,11 +60,11 @@ func main() { } const ( - doerName = "doer" - handlerTypeName = "Handler" - queryParamName = "query" - queryParamTypeName = "queryParam" - queryParamArraySize = 2 + doerName = "doer" + handlerTypeName = "Handler" + queryParamName = "query" + queryParamTypeSuffix = "Query" + queryParamArraySize = 2 ) // nolint:funlen,gocognit,gocyclo // It's a generator, it's supposed to be long, and we won't expand it. @@ -158,9 +157,6 @@ func exec() error { ).Parens(jen.List(jen.Index().Byte(), jen.Error())), ).Line() - // A private type to limit query params to the declared keys/values list - queryParamType := jen.Type().Id(queryParamTypeName).Index(jen.Lit(queryParamArraySize)).String() - clientFields := make([]jen.Code, 0, len(pkgs)) clientValues := jen.Dict{} clientTypeValues := make([]jen.Code, 0, len(pkgs)) @@ -188,10 +184,7 @@ func exec() error { handlerType := file.Type().Id(handlerTypeName) // Adds private types (interfaces) - privateTypes := file.Add(doer) - - // We want to add the query params type when there is any param - addQueryParams := new(sync.Once) + file.Add(doer) // Creates the "new" constructor file.Func().Id(newHandler).Params(jen.Id(doerName).Id(doerName)).Id(handlerName).Block( @@ -217,6 +210,10 @@ func exec() error { // Interface and implementation args funcArgs := []jen.Code{ctx} + // We want to add the query params type when there is any param + addQueryParams := true + queryParamType := lowerFirst(path.FuncName) + queryParamTypeSuffix + // Collects params: in path and in query // Adds to schemas to render enums for _, p := range path.Parameters { @@ -231,18 +228,17 @@ func exec() error { } // Adds query params type once - addQueryParams.Do(func() { - privateTypes.Add( - jen.Comment(queryParamTypeName+" http query params private type").Line(), - queryParamType.Clone().Line(), - ) - }) + if addQueryParams { + file.Comment(queryParamType + " http query params private type").Line() + file.Type().Id(queryParamType).Index(jen.Lit(queryParamArraySize)).String() + addQueryParams = false + } queryParams = append(queryParams, p.Schema) // Adds param function (request modifier) var code *jen.Statement - code, err = fmtQueryParam(path.FuncName, p) + code, err = fmtQueryParam(path.FuncName, queryParamType, p) if err != nil { return err } @@ -266,7 +262,7 @@ func exec() error { // Adds queryParams options if len(queryParams) > 0 { - funcArgs = append(funcArgs, jen.Id(queryParamName).Op("...").Id(queryParamTypeName)) + funcArgs = append(funcArgs, jen.Id(queryParamName).Op("...").Id(queryParamType)) } typeMeth := jen.Id(path.FuncName).Params(funcArgs...) @@ -617,7 +613,7 @@ func fmtComment(c string) string { } // fmtQueryParam returns a query param -func fmtQueryParam(funcName string, p *Parameter) (*jen.Statement, error) { +func fmtQueryParam(funcName, queryParamType string, p *Parameter) (*jen.Statement, error) { keyFuncName := funcName + p.Schema.CamelName keyVarName := jen.Id(p.Schema.lowerCamel()) @@ -631,9 +627,9 @@ func fmtQueryParam(funcName string, p *Parameter) (*jen.Statement, error) { param := jen.Comment(fmt.Sprintf("%s %s", keyFuncName, fmtComment(p.Description))) param.Line() param.Func().Id(keyFuncName). - Params(keyVarName.Clone().Add(getType(p.Schema))).Params(jen.Id(queryParamTypeName)). + Params(keyVarName.Clone().Add(getType(p.Schema))).Params(jen.Id(queryParamType)). Block( - jen.Return(jen.Id(queryParamTypeName).Values(jen.Lit(p.Schema.name), value)), + jen.Return(jen.Id(queryParamType).Values(jen.Lit(p.Schema.name), value)), ) return param, nil } diff --git a/handler/clickhouse/clickhouse.go b/handler/clickhouse/clickhouse.go index 4c0ac04..47fc56e 100644 --- a/handler/clickhouse/clickhouse.go +++ b/handler/clickhouse/clickhouse.go @@ -38,7 +38,7 @@ type Handler interface { // ServiceClickHouseQueryStats return statistics on recent queries // GET /v1/project/{project}/service/{service_name}/clickhouse/query/stats // https://api.aiven.io/doc/#tag/Service:_ClickHouse/operation/ServiceClickHouseQueryStats - ServiceClickHouseQueryStats(ctx context.Context, project string, serviceName string, query ...queryParam) ([]QueryOutAlt, error) + ServiceClickHouseQueryStats(ctx context.Context, project string, serviceName string, query ...serviceClickHouseQueryStatsQuery) ([]QueryOutAlt, error) // ServiceClickHouseTieredStorageSummary get the ClickHouse tiered storage summary // GET /v1/project/{project}/service/{service_name}/clickhouse/tiered-storage/summary @@ -51,9 +51,6 @@ type doer interface { Do(ctx context.Context, operationID, method, path string, in any, query ...[2]string) ([]byte, error) } -// queryParam http query params private type -type queryParam [2]string - func NewHandler(doer doer) ClickHouseHandler { return ClickHouseHandler{doer} } @@ -112,21 +109,25 @@ func (h *ClickHouseHandler) ServiceClickHouseQuery(ctx context.Context, project return out, nil } +// serviceClickHouseQueryStatsQuery http query params private type + +type serviceClickHouseQueryStatsQuery [2]string + // ServiceClickHouseQueryStatsLimit Limit for number of results -func ServiceClickHouseQueryStatsLimit(limit int) queryParam { - return queryParam{"limit", fmt.Sprintf("%d", limit)} +func ServiceClickHouseQueryStatsLimit(limit int) serviceClickHouseQueryStatsQuery { + return serviceClickHouseQueryStatsQuery{"limit", fmt.Sprintf("%d", limit)} } // ServiceClickHouseQueryStatsOffset Offset for retrieved results based on sort order -func ServiceClickHouseQueryStatsOffset(offset int) queryParam { - return queryParam{"offset", fmt.Sprintf("%d", offset)} +func ServiceClickHouseQueryStatsOffset(offset int) serviceClickHouseQueryStatsQuery { + return serviceClickHouseQueryStatsQuery{"offset", fmt.Sprintf("%d", offset)} } // ServiceClickHouseQueryStatsOrderByType Order in which to sort retrieved results -func ServiceClickHouseQueryStatsOrderByType(orderByType OrderByType) queryParam { - return queryParam{"order_by", fmt.Sprintf("%s", orderByType)} +func ServiceClickHouseQueryStatsOrderByType(orderByType OrderByType) serviceClickHouseQueryStatsQuery { + return serviceClickHouseQueryStatsQuery{"order_by", fmt.Sprintf("%s", orderByType)} } -func (h *ClickHouseHandler) ServiceClickHouseQueryStats(ctx context.Context, project string, serviceName string, query ...queryParam) ([]QueryOutAlt, error) { +func (h *ClickHouseHandler) ServiceClickHouseQueryStats(ctx context.Context, project string, serviceName string, query ...serviceClickHouseQueryStatsQuery) ([]QueryOutAlt, error) { p := make([][2]string, 0, len(query)) for _, v := range query { p = append(p, v) diff --git a/handler/service/service.go b/handler/service/service.go index 64ff676..71ffa7d 100644 --- a/handler/service/service.go +++ b/handler/service/service.go @@ -99,7 +99,7 @@ type Handler interface { // ServiceGet get service information // GET /v1/project/{project}/service/{service_name} // https://api.aiven.io/doc/#tag/Service/operation/ServiceGet - ServiceGet(ctx context.Context, project string, serviceName string, query ...queryParam) (*ServiceGetOut, error) + ServiceGet(ctx context.Context, project string, serviceName string, query ...serviceGetQuery) (*ServiceGetOut, error) // ServiceGetMigrationStatus get migration status // GET /v1/project/{project}/service/{service_name}/migration @@ -159,7 +159,7 @@ type Handler interface { // ServiceUpdate update service configuration // PUT /v1/project/{project}/service/{service_name} // https://api.aiven.io/doc/#tag/Service/operation/ServiceUpdate - ServiceUpdate(ctx context.Context, project string, serviceName string, in *ServiceUpdateIn, query ...queryParam) (*ServiceUpdateOut, error) + ServiceUpdate(ctx context.Context, project string, serviceName string, in *ServiceUpdateIn, query ...serviceUpdateQuery) (*ServiceUpdateOut, error) } // doer http client @@ -167,9 +167,6 @@ type doer interface { Do(ctx context.Context, operationID, method, path string, in any, query ...[2]string) ([]byte, error) } -// queryParam http query params private type -type queryParam [2]string - func NewHandler(doer doer) ServiceHandler { return ServiceHandler{doer} } @@ -360,11 +357,15 @@ func (h *ServiceHandler) ServiceEnableWrites(ctx context.Context, project string return out.Until, nil } +// serviceGetQuery http query params private type + +type serviceGetQuery [2]string + // ServiceGetIncludeSecrets Explicitly indicates that the client wants to read secrets that might be returned by this endpoint. -func ServiceGetIncludeSecrets(includeSecrets bool) queryParam { - return queryParam{"include_secrets", fmt.Sprintf("%t", includeSecrets)} +func ServiceGetIncludeSecrets(includeSecrets bool) serviceGetQuery { + return serviceGetQuery{"include_secrets", fmt.Sprintf("%t", includeSecrets)} } -func (h *ServiceHandler) ServiceGet(ctx context.Context, project string, serviceName string, query ...queryParam) (*ServiceGetOut, error) { +func (h *ServiceHandler) ServiceGet(ctx context.Context, project string, serviceName string, query ...serviceGetQuery) (*ServiceGetOut, error) { path := fmt.Sprintf("/v1/project/%s/service/%s", url.PathEscape(project), url.PathEscape(serviceName)) b, err := h.doer.Do(ctx, "ServiceGet", "GET", path, nil) if err != nil { @@ -513,11 +514,15 @@ func (h *ServiceHandler) ServiceTaskGet(ctx context.Context, project string, ser return &out.Task, nil } +// serviceUpdateQuery http query params private type + +type serviceUpdateQuery [2]string + // ServiceUpdateAllowUncleanPoweroff Allows or disallows powering off a service if some WAL segments are not available for a future restoration of the service, which might result in data loss when powering the service back on -func ServiceUpdateAllowUncleanPoweroff(allowUncleanPoweroff bool) queryParam { - return queryParam{"allow_unclean_poweroff", fmt.Sprintf("%t", allowUncleanPoweroff)} +func ServiceUpdateAllowUncleanPoweroff(allowUncleanPoweroff bool) serviceUpdateQuery { + return serviceUpdateQuery{"allow_unclean_poweroff", fmt.Sprintf("%t", allowUncleanPoweroff)} } -func (h *ServiceHandler) ServiceUpdate(ctx context.Context, project string, serviceName string, in *ServiceUpdateIn, query ...queryParam) (*ServiceUpdateOut, error) { +func (h *ServiceHandler) ServiceUpdate(ctx context.Context, project string, serviceName string, in *ServiceUpdateIn, query ...serviceUpdateQuery) (*ServiceUpdateOut, error) { path := fmt.Sprintf("/v1/project/%s/service/%s", url.PathEscape(project), url.PathEscape(serviceName)) b, err := h.doer.Do(ctx, "ServiceUpdate", "PUT", path, in) if err != nil {