Skip to content

Commit

Permalink
refactor: method nil checks
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Aug 29, 2024
1 parent 2015f94 commit 660b89f
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,26 +129,24 @@ func (def TemplateName) callHandleFunc(handlerFuncLit *ast.FuncLit) *ast.ExprStm
}

func (def TemplateName) funcLit(method *ast.FuncType, files []*ast.File) (*ast.FuncLit, []*ast.ImportSpec, error) {
if def.handler == "" {
if method == nil {
return def.httpRequestReceiverTemplateHandlerFunc(), nil, nil
}
lit := &ast.FuncLit{
Type: httpHandlerFuncType(),
Body: &ast.BlockStmt{},
}
call := &ast.CallExpr{Fun: callReceiverMethod(def.fun)}
if method.Params.NumFields() != len(def.call.Args) {
return nil, nil, errWrongNumberOfArguments(def, method)
}
var formStruct *ast.StructType
if method != nil {
if method.Params.NumFields() != len(def.call.Args) {
return nil, nil, errWrongNumberOfArguments(def, method)
for pi, pt := range fieldListTypes(method.Params) {
if err := checkArgument(method, pi, def.call.Args[pi], pt, files); err != nil {
return nil, nil, err
}
for pi, pt := range fieldListTypes(method.Params) {
if err := checkArgument(method, pi, def.call.Args[pi], pt, files); err != nil {
return nil, nil, err
}
if s, ok := findFormStruct(pt, files); ok {
formStruct = s
}
if s, ok := findFormStruct(pt, files); ok {
formStruct = s
}
}
const errVarIdent = "err"
Expand Down Expand Up @@ -258,7 +256,7 @@ func (def TemplateName) funcLit(method *ast.FuncType, files []*ast.File) (*ast.F
}

const dataVarIdent = "data"
if method != nil && len(method.Results.List) > 1 {
if len(method.Results.List) > 1 {
errVar := ast.NewIdent("err")

lit.Body.List = append(lit.Body.List,
Expand Down

0 comments on commit 660b89f

Please sign in to comment.