repogen/internal/generator/generator.go
2023-05-24 14:01:50 +03:00

40 lines
1 KiB
Go

package generator
import (
"git.kmsign.com/royalcat/repogen/internal/code"
"git.kmsign.com/royalcat/repogen/internal/codegen"
"git.kmsign.com/royalcat/repogen/internal/mongo"
"git.kmsign.com/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()
}