2021-01-17 03:29:50 +00:00
|
|
|
package mongo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sunboyy/repogen/internal/code"
|
|
|
|
)
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
const constructorTemplate = `
|
2021-01-17 03:29:50 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func New{{.InterfaceName}}(collection *mongo.Collection) {{.InterfaceName}} {
|
2021-01-23 13:03:16 +00:00
|
|
|
return &{{.ImplStructName}}{
|
2021-01-17 03:29:50 +00:00
|
|
|
collection: collection,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
type {{.ImplStructName}} struct {
|
2021-01-17 03:29:50 +00:00
|
|
|
collection *mongo.Collection
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-01-23 13:03:16 +00:00
|
|
|
type mongoConstructorTemplateData struct {
|
|
|
|
InterfaceName string
|
|
|
|
ImplStructName string
|
2021-01-17 03:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const methodTemplate = `
|
|
|
|
func (r *{{.StructName}}) {{.MethodName}}(ctx context.Context, {{.Parameters}}){{.Returns}} {
|
|
|
|
{{.Implementation}}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
type mongoMethodTemplateData struct {
|
|
|
|
StructName string
|
|
|
|
MethodName string
|
|
|
|
ParamTypes []code.Type
|
|
|
|
ReturnTypes []code.Type
|
|
|
|
Implementation string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (data mongoMethodTemplateData) Parameters() string {
|
|
|
|
var params []string
|
|
|
|
for i, paramType := range data.ParamTypes {
|
|
|
|
params = append(params, fmt.Sprintf("arg%d %s", i, paramType.Code()))
|
|
|
|
}
|
|
|
|
return strings.Join(params, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (data mongoMethodTemplateData) Returns() string {
|
|
|
|
if len(data.ReturnTypes) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(data.ReturnTypes) == 1 {
|
|
|
|
return fmt.Sprintf(" %s", data.ReturnTypes[0].Code())
|
|
|
|
}
|
|
|
|
|
|
|
|
var returns []string
|
|
|
|
for _, returnType := range data.ReturnTypes {
|
|
|
|
returns = append(returns, returnType.Code())
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(" (%s)", strings.Join(returns, ", "))
|
|
|
|
}
|
|
|
|
|
2021-01-26 13:23:52 +00:00
|
|
|
type mongoFindTemplateData struct {
|
|
|
|
EntityType string
|
|
|
|
QuerySpec querySpec
|
|
|
|
}
|
|
|
|
|
2021-01-17 03:29:50 +00:00
|
|
|
const findOneTemplate = ` var entity {{.EntityType}}
|
|
|
|
if err := r.collection.FindOne(ctx, bson.M{
|
2021-01-26 13:23:52 +00:00
|
|
|
{{.QuerySpec.Code}}
|
|
|
|
}).Decode(&entity); err != nil {
|
2021-01-17 03:29:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &entity, nil`
|
|
|
|
|
|
|
|
const findManyTemplate = ` cursor, err := r.collection.Find(ctx, bson.M{
|
2021-01-19 12:35:54 +00:00
|
|
|
{{.QuerySpec.Code}}
|
|
|
|
})
|
2021-01-17 03:29:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var entities []*{{.EntityType}}
|
|
|
|
if err := cursor.All(ctx, &entities); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return entities, nil`
|
2021-01-26 13:23:52 +00:00
|
|
|
|
|
|
|
type mongoDeleteTemplateData struct {
|
|
|
|
QuerySpec querySpec
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteOneTemplate = ` result, err := r.collection.DeleteOne(ctx, bson.M{
|
|
|
|
{{.QuerySpec.Code}}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return result.DeletedCount > 0, nil`
|
|
|
|
|
|
|
|
const deleteManyTemplate = ` result, err := r.collection.DeleteMany(ctx, bson.M{
|
|
|
|
{{.QuerySpec.Code}}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int(result.DeletedCount), nil`
|