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

40 lines
1 KiB
Go

package generator
import (
"git.kmsign.ru/royalcat/repogen/internal/code"
"git.kmsign.ru/royalcat/repogen/internal/codegen"
"git.kmsign.ru/royalcat/repogen/internal/mongo"
"git.kmsign.ru/royalcat/repogen/internal/spec"
)
// GenerateRepository generates repository implementation code from repository
// interface specification.
func GenerateRepository(packageName string, structModel code.Struct,
interfaceName string, methodSpecs []spec.MethodSpec) (string, error) {
generator := mongo.NewGenerator(structModel, interfaceName)
codeBuilder := codegen.NewBuilder(
"repogen",
packageName,
generator.Imports(),
)
constructorBuilder, err := generator.GenerateConstructor()
if err != nil {
return "", err
}
codeBuilder.AddImplementer(constructorBuilder)
codeBuilder.AddImplementer(generator.GenerateStruct())
for _, method := range methodSpecs {
methodBuilder, err := generator.GenerateMethod(method)
if err != nil {
return "", err
}
codeBuilder.AddImplementer(methodBuilder)
}
return codeBuilder.Build()
}