repogen/internal/mongo/count.go
2023-05-24 14:04:29 +03:00

39 lines
856 B
Go

package mongo
import (
"git.kmsign.ru/royalcat/repogen/internal/codegen"
"git.kmsign.ru/royalcat/repogen/internal/spec"
)
func (g RepositoryGenerator) generateCountBody(
operation spec.CountOperation) (codegen.FunctionBody, error) {
querySpec, err := g.convertQuerySpec(operation.Query)
if err != nil {
return nil, err
}
return codegen.FunctionBody{
codegen.DeclAssignStatement{
Vars: []string{"count", "err"},
Values: codegen.StatementList{
codegen.NewChainBuilder("r").
Chain("collection").
Call("CountDocuments",
codegen.Identifier("arg0"),
querySpec.Code(),
).Build(),
},
},
ifErrReturn0Err,
codegen.ReturnStatement{
codegen.CallStatement{
FuncName: "int",
Params: codegen.StatementList{
codegen.Identifier("count"),
},
},
codegen.Identifier("nil"),
},
}, nil
}