Skip to content

Commit

Permalink
feat: allow custom query params
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Dec 17, 2024
1 parent bdb2d78 commit 79c104c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 51 deletions.
21 changes: 5 additions & 16 deletions generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,6 @@ 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 {
Expand All @@ -240,18 +236,11 @@ func exec() error {
continue
}

// Adds query params type once
if addQueryParams {
file.Comment(queryParamType + " http query params private type")
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, queryParamType, p)
code, err = fmtQueryParam(path.FuncName, p)
if err != nil {
return err
}
Expand All @@ -275,7 +264,7 @@ func exec() error {

// Adds queryParams options
if len(queryParams) > 0 {
funcArgs = append(funcArgs, jen.Id(queryParamName).Op("...").Id(queryParamType))
funcArgs = append(funcArgs, jen.Id(queryParamName).Op("...").Add(fmtQueryParamType()))
}

typeMeth := jen.Id(path.FuncName).Params(funcArgs...)
Expand Down Expand Up @@ -658,7 +647,7 @@ func fmtComment(c string) string {
}

// fmtQueryParam returns a query param
func fmtQueryParam(funcName, queryParamType string, p *Parameter) (*jen.Statement, error) {
func fmtQueryParam(funcName string, p *Parameter) (*jen.Statement, error) {
keyFuncName := funcName + p.Schema.CamelName
keyVarName := jen.Id(p.Schema.lowerCamel())

Expand All @@ -676,9 +665,9 @@ func fmtQueryParam(funcName, queryParamType string, p *Parameter) (*jen.Statemen
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(queryParamType)).
Params(keyVarName.Clone().Add(getType(p.Schema))).Params(fmtQueryParamType()).
Block(
jen.Return(jen.Id(queryParamType).Values(jen.Lit(p.Schema.name), value)),
jen.Return(fmtQueryParamType().Values(jen.Lit(p.Schema.name), value)),
)
return param, nil
}
Expand Down
19 changes: 8 additions & 11 deletions handler/clickhouse/clickhouse.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions handler/kafka/kafka.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 12 additions & 21 deletions handler/service/service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 79c104c

Please sign in to comment.