Documentation ¶
Overview ¶
Package docstoregen provides a code generator for the go-cloud.dev/docstore package.
The generator creates a query code file for the go-cloud.dev/docstore package.
The generator's basic configuration is defined in the Config struct.
The generator's main function is Execute, which generates the query code file.
Example:
package main import ( "github.com/bartventer/docstore-gen" ) type User struct { ID string `docstore:"id"` Name string `docstore:"name"` } func (User) TableName() string { return "user" } type Copmany struct { ID string `docstore:"id"` Name string `docstore:"name"` } func (Copmany) TableName() string { return "company" } func main() { g := docstoregen.NewGenerator(docstoregen.Config{ OutPath: "query", }) g.ApplyInterface(&User{}, &Copmany{}) g.Execute() }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { OutPath string // OutPath specifies the path where the query code will be generated. Example: "/path/to/project/pkg/query" OutFile string // OutFile specifies the name of the query code file. The default value is "gen.go". LoggerHandler slog.Handler // LoggerHandler specifies the handler for the logger used by the generator. Example: slog.NewJSONHandler(os.Stdout, nil) // contains filtered or unexported fields }
Config represents the configuration options for generating query code.
func (*Config) Revise ¶
Revise revises the configuration by setting the absolute path for the output directory, generating a default output path if not provided, and setting the output file path. It also sets the query package name based on the output path. Returns an error if the outpath is invalid.
type Generator ¶
type Generator struct { Config Logger *slog.Logger // logger Data map[string]*genInfo //gen query data }
Generator generate code
func NewGenerator ¶
NewGenerator creates a new instance of Generator with the given configuration. It revises the configuration and panics if there is an error. If the LoggerHandler is not provided in the configuration, it uses a default JSON handler that writes to os.Stdout. Returns a pointer to the created Generator.
func (*Generator) ApplyInterface ¶
func (g *Generator) ApplyInterface(models ...interface{})
ApplyInterface applies the given models as interfaces to the generator. It converts the models into structs using the generate.ConvertStructs function, checks for any errors, and then applies the generated structs to the generator. If there is an error during the conversion or application process, it logs the error and panics with a "check struct fail" message.
Example:
g := docstoregen.NewGenerator(docstoregen.Config{ OutPath: "query", }) g.ApplyInterface(&User{}, &Company{}) // Apply the User and Company models as interfaces to the generator. g.Execute()
func (*Generator) Execute ¶
func (g *Generator) Execute()
Execute generates code based on the provided specifications. It first generates the query file and then logs the progress. If any error occurs during the generation process, it logs the error and panics. Finally, it logs the completion of code generation.
Example:
g := docstoregen.NewGenerator(docstoregen.Config{ OutPath: "query", }) g.ApplyInterface(&User{}, &Company{}) g.Execute() // Generate the query code files.