2021-01-23 13:03:16 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
2023-05-24 11:01:50 +00:00
|
|
|
"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"
|
2021-01-23 13:03:16 +00:00
|
|
|
)
|
|
|
|
|
2022-10-18 12:37:50 +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
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
generator := mongo.NewGenerator(structModel, interfaceName)
|
2021-01-23 13:03:16 +00:00
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
codeBuilder := codegen.NewBuilder(
|
|
|
|
"repogen",
|
|
|
|
packageName,
|
|
|
|
generator.Imports(),
|
|
|
|
)
|
2021-01-23 13:03:16 +00:00
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
constructorBuilder, err := generator.GenerateConstructor()
|
|
|
|
if err != nil {
|
2021-01-23 13:03:16 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
codeBuilder.AddImplementer(constructorBuilder)
|
|
|
|
codeBuilder.AddImplementer(generator.GenerateStruct())
|
2021-01-23 13:03:16 +00:00
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
for _, method := range methodSpecs {
|
|
|
|
methodBuilder, err := generator.GenerateMethod(method)
|
|
|
|
if err != nil {
|
2021-01-23 13:03:16 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2022-10-18 12:37:50 +00:00
|
|
|
codeBuilder.AddImplementer(methodBuilder)
|
2021-01-23 13:03:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 12:37:50 +00:00
|
|
|
return codeBuilder.Build()
|
2021-01-23 13:03:16 +00:00
|
|
|
}
|