This tool was originally designed for personal usage. It generates proto files, db model for GORM and gRPC functions based on a schema
package main
import (
"fmt"
"path/to/schema"
"github.com/Pipello/codegen/definition"
)
func main() {
models := []*definition.Model{
schema.MyModel(),
}
generator := definition.ModelGenerator{Models: models}
err := generator.GenerateFiles()
if err != nil {
panic(err)
}
fmt.Println("CODEGEN: success")
}
package schema
import "github.com/Pipello/codegen/definition"
func Device() *definition.Model {
m := &definition.Model{
Name: "MyModel",
Table: "models",
Methods: definition.Get | definition.List | definition.Update | definition.Create,
Fields: []*definition.Field{
{Name: "Name", Type: "string"},
{Name: "IsGenerated", Type: "bool"},
},
}
m.AutoFillProtoIndex()
return m
}
It is required to create a server.go
file to register the actual server. A better solution for this should be implemented.
package server
import (
pb "yourrepo/api"
"yourrepo/internal/services"
"gorm.io/gorm"
)
type Server struct {
pb.UnimplementedIotCollectorServiceServer
myModelService *services.MyModelService
}
func NewServer(db *gorm.DB) *Server {
return &Server{
myModelService: services.NewMyModelService(db),
}
}