repogen/internal/generator/generator.go

41 lines
1 KiB
Go
Raw Permalink Normal View History

2021-01-23 13:03:16 +00:00
package generator
import (
2023-05-24 11:04:29 +00:00
"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"
2021-01-23 13:03:16 +00:00
)
// GenerateRepository generates repository implementation code from repository
// interface specification.
func GenerateRepository(packageName string, structModel code.Struct,
interfaceName string, methodSpecs []spec.MethodSpec) (string, error) {
2021-01-23 13:03:16 +00:00
generator := mongo.NewGenerator(structModel, interfaceName)
2021-01-23 13:03:16 +00:00
codeBuilder := codegen.NewBuilder(
"repogen",
packageName,
generator.Imports(),
)
2021-01-23 13:03:16 +00:00
constructorBuilder, err := generator.GenerateConstructor()
if err != nil {
2021-01-23 13:03:16 +00:00
return "", err
}
codeBuilder.AddImplementer(constructorBuilder)
codeBuilder.AddImplementer(generator.GenerateStruct())
2021-01-23 13:03:16 +00:00
for _, method := range methodSpecs {
methodBuilder, err := generator.GenerateMethod(method)
if err != nil {
2021-01-23 13:03:16 +00:00
return "", err
}
codeBuilder.AddImplementer(methodBuilder)
2021-01-23 13:03:16 +00:00
}
return codeBuilder.Build()
2021-01-23 13:03:16 +00:00
}