Skip to content

Commit

Permalink
feat: make private query param types (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov authored Aug 23, 2024
1 parent 04475ab commit f2f1e81
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 46 deletions.
44 changes: 20 additions & 24 deletions generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"regexp"
"sort"
"strings"
"sync"
"time"

"github.com/dave/jennifer/jen"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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(
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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...)
Expand Down Expand Up @@ -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())

Expand All @@ -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
}
Expand Down
23 changes: 12 additions & 11 deletions handler/clickhouse/clickhouse.go

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

27 changes: 16 additions & 11 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 f2f1e81

Please sign in to comment.