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

100 lines
2.2 KiB
Go

package mongo
import (
"strings"
"git.kmsign.ru/royalcat/repogen/internal/code"
"git.kmsign.ru/royalcat/repogen/internal/codegen"
"git.kmsign.ru/royalcat/repogen/internal/spec"
)
var errOccurred = codegen.RawStatement("err != nil")
var returnNilErr = codegen.ReturnStatement{
codegen.Identifier("nil"),
codegen.Identifier("err"),
}
var ifErrReturnNilErr = codegen.IfBlock{
Condition: []codegen.Statement{
errOccurred,
},
Statements: []codegen.Statement{
returnNilErr,
},
}
var ifErrReturn0Err = codegen.IfBlock{
Condition: []codegen.Statement{
errOccurred,
},
Statements: []codegen.Statement{
codegen.ReturnStatement{
codegen.Identifier("0"),
codegen.Identifier("err"),
},
},
}
var ifErrReturnFalseErr = codegen.IfBlock{
Condition: []codegen.Statement{
errOccurred,
},
Statements: []codegen.Statement{
codegen.ReturnStatement{
codegen.Identifier("false"),
codegen.Identifier("err"),
},
},
}
type baseMethodGenerator struct {
structModel code.Struct
}
func (g baseMethodGenerator) bsonFieldReference(fieldReference spec.FieldReference) (string, error) {
var bsonTags []string
for _, field := range fieldReference {
tag, err := g.bsonTagFromField(field)
if err != nil {
return "", err
}
bsonTags = append(bsonTags, tag)
}
return strings.Join(bsonTags, "."), nil
}
func (g baseMethodGenerator) bsonTagFromField(field code.StructField) (string, error) {
bsonTag, ok := field.Tag.Lookup("bson")
if !ok {
bsonTag, ok = field.Tag.Lookup("json")
if !ok {
return "", NewBsonTagNotFoundError(field.Name)
}
}
documentKey := strings.Split(bsonTag, ",")[0]
return documentKey, nil
}
func (g baseMethodGenerator) convertQuerySpec(query spec.QuerySpec) (querySpec, error) {
var predicates []predicate
for _, predicateSpec := range query.Predicates {
bsonFieldReference, err := g.bsonFieldReference(predicateSpec.FieldReference)
if err != nil {
return querySpec{}, err
}
predicates = append(predicates, predicate{
Field: bsonFieldReference,
Comparator: predicateSpec.Comparator,
ParamIndex: predicateSpec.ParamIndex,
})
}
return querySpec{
Operator: query.Operator,
Predicates: predicates,
}, nil
}