2021-01-17 03:29:50 +00:00
|
|
|
package mongo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2021-01-23 13:03:16 +00:00
|
|
|
"io"
|
2021-01-17 03:29:50 +00:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/sunboyy/repogen/internal/code"
|
|
|
|
"github.com/sunboyy/repogen/internal/spec"
|
|
|
|
)
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
// NewGenerator creates a new instance of MongoDB repository generator
|
|
|
|
func NewGenerator(structModel code.Struct, interfaceName string) RepositoryGenerator {
|
|
|
|
return RepositoryGenerator{
|
2021-01-21 11:56:30 +00:00
|
|
|
StructModel: structModel,
|
2021-01-23 13:03:16 +00:00
|
|
|
InterfaceName: interfaceName,
|
2021-01-17 03:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
// RepositoryGenerator provides repository constructor and method generation from provided specification
|
|
|
|
type RepositoryGenerator struct {
|
2021-01-21 11:56:30 +00:00
|
|
|
StructModel code.Struct
|
|
|
|
InterfaceName string
|
2021-01-17 03:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
// GenerateConstructor generates mongo repository struct implementation and constructor for the struct
|
|
|
|
func (g RepositoryGenerator) GenerateConstructor(buffer io.Writer) error {
|
|
|
|
tmpl, err := template.New("mongo_repository_base").Parse(constructorTemplate)
|
2021-01-17 03:29:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
tmplData := mongoConstructorTemplateData{
|
|
|
|
InterfaceName: g.InterfaceName,
|
|
|
|
ImplStructName: g.structName(),
|
2021-01-17 03:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(buffer, tmplData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
// GenerateMethod generates implementation of from provided method specification
|
|
|
|
func (g RepositoryGenerator) GenerateMethod(methodSpec spec.MethodSpec, buffer io.Writer) error {
|
2021-01-17 03:29:50 +00:00
|
|
|
tmpl, err := template.New("mongo_repository_method").Parse(methodTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
implementation, err := g.generateMethodImplementation(methodSpec)
|
2021-01-17 03:29:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var paramTypes []code.Type
|
2021-01-23 13:03:16 +00:00
|
|
|
for _, param := range methodSpec.Params[1:] {
|
2021-01-17 03:29:50 +00:00
|
|
|
paramTypes = append(paramTypes, param.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmplData := mongoMethodTemplateData{
|
|
|
|
StructName: g.structName(),
|
2021-01-23 13:03:16 +00:00
|
|
|
MethodName: methodSpec.Name,
|
2021-01-17 03:29:50 +00:00
|
|
|
ParamTypes: paramTypes,
|
2021-01-23 13:03:16 +00:00
|
|
|
ReturnTypes: methodSpec.Returns,
|
2021-01-17 03:29:50 +00:00
|
|
|
Implementation: implementation,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(buffer, tmplData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
func (g RepositoryGenerator) generateMethodImplementation(methodSpec spec.MethodSpec) (string, error) {
|
2021-01-17 03:29:50 +00:00
|
|
|
switch operation := methodSpec.Operation.(type) {
|
|
|
|
case spec.FindOperation:
|
|
|
|
return g.generateFindImplementation(operation)
|
2021-01-26 13:23:52 +00:00
|
|
|
case spec.DeleteOperation:
|
|
|
|
return g.generateDeleteImplementation(operation)
|
2021-01-17 03:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 13:23:52 +00:00
|
|
|
return "", OperationNotSupportedError
|
2021-01-17 03:29:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
func (g RepositoryGenerator) generateFindImplementation(operation spec.FindOperation) (string, error) {
|
2021-01-17 03:29:50 +00:00
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
2021-01-26 13:23:52 +00:00
|
|
|
querySpec, err := g.mongoQuerySpec(operation.Query)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2021-01-17 03:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmplData := mongoFindTemplateData{
|
2021-01-19 12:26:26 +00:00
|
|
|
EntityType: g.StructModel.Name,
|
2021-01-26 13:23:52 +00:00
|
|
|
QuerySpec: querySpec,
|
2021-01-17 03:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if operation.Mode == spec.QueryModeOne {
|
|
|
|
tmpl, err := template.New("mongo_repository_findone").Parse(findOneTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(buffer, tmplData); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tmpl, err := template.New("mongo_repository_findmany").Parse(findManyTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(buffer, tmplData); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer.String(), nil
|
|
|
|
}
|
|
|
|
|
2021-01-26 13:23:52 +00:00
|
|
|
func (g RepositoryGenerator) generateDeleteImplementation(operation spec.DeleteOperation) (string, error) {
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
|
|
|
|
querySpec, err := g.mongoQuerySpec(operation.Query)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmplData := mongoDeleteTemplateData{
|
|
|
|
QuerySpec: querySpec,
|
|
|
|
}
|
|
|
|
|
|
|
|
if operation.Mode == spec.QueryModeOne {
|
|
|
|
tmpl, err := template.New("mongo_repository_deleteone").Parse(deleteOneTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(buffer, tmplData); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tmpl, err := template.New("mongo_repository_deletemany").Parse(deleteManyTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(buffer, tmplData); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g RepositoryGenerator) mongoQuerySpec(query spec.QuerySpec) (querySpec, error) {
|
|
|
|
var predicates []predicate
|
|
|
|
|
|
|
|
for _, predicateSpec := range query.Predicates {
|
|
|
|
structField, ok := g.StructModel.Fields.ByName(predicateSpec.Field)
|
|
|
|
if !ok {
|
|
|
|
return querySpec{}, fmt.Errorf("struct field %s not found", predicateSpec.Field)
|
|
|
|
}
|
|
|
|
|
|
|
|
bsonTag, ok := structField.Tags["bson"]
|
|
|
|
if !ok {
|
|
|
|
return querySpec{}, BsonTagNotFoundError
|
|
|
|
}
|
|
|
|
|
|
|
|
predicates = append(predicates, predicate{Field: bsonTag[0], Comparator: predicateSpec.Comparator})
|
|
|
|
}
|
|
|
|
|
|
|
|
return querySpec{
|
|
|
|
Operator: query.Operator,
|
|
|
|
Predicates: predicates,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
func (g RepositoryGenerator) structName() string {
|
2021-01-21 11:56:30 +00:00
|
|
|
return g.InterfaceName + "Mongo"
|
2021-01-17 03:29:50 +00:00
|
|
|
}
|