Documentation
¶
Index ¶
- func Generate(opts ...GenerateOption) ([]byte, error)
- type GenerateOption
- func WithDefaultTag(enabled bool) GenerateOption
- func WithEnvTag(enabled bool) GenerateOption
- func WithInput(path string) GenerateOption
- func WithInputPaths(paths ...string) GenerateOption
- func WithJSONTag(enabled bool) GenerateOption
- func WithOutputDir(dir string) GenerateOption
- func WithOutputFile(name string) GenerateOption
- func WithPackage(name string) GenerateOption
- func WithStructName(name string) GenerateOption
- func WithTOMLTag(enabled bool) GenerateOption
- func WithYAMLTag(enabled bool) GenerateOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Generate ¶
func Generate(opts ...GenerateOption) ([]byte, error)
Generate parses one or more config files and produces Go source code with a typed struct. The struct fields include env, yaml, json, and toml tags inferred from the config structure, plus default value tags.
output, err := generate.Generate(
generate.WithInput("config.yaml"),
generate.WithStructName("AppConfig"),
generate.WithPackage("config"),
)
os.WriteFile("config.go", output, 0644)
Types ¶
type GenerateOption ¶
type GenerateOption func(*genConfig)
GenerateOption configures the code generation process. Use the With* functions to create options.
func WithDefaultTag ¶
func WithDefaultTag(enabled bool) GenerateOption
WithDefaultTag controls whether default value tags are generated. Default is true.
func WithEnvTag ¶
func WithEnvTag(enabled bool) GenerateOption
WithEnvTag controls whether env struct tags are generated. Default is true.
func WithInput ¶
func WithInput(path string) GenerateOption
WithInput sets the input config file path. Exactly one of WithInput or WithInputPaths must be provided.
func WithInputPaths ¶
func WithInputPaths(paths ...string) GenerateOption
WithInputPaths sets multiple input config files to merge. Exactly one of WithInput or WithInputPaths must be provided.
func WithJSONTag ¶
func WithJSONTag(enabled bool) GenerateOption
WithJSONTag controls whether json struct tags are generated. Default is true.
func WithOutputDir ¶
func WithOutputDir(dir string) GenerateOption
WithOutputDir sets the output directory for the generated file. Defaults to ".".
func WithOutputFile ¶
func WithOutputFile(name string) GenerateOption
WithOutputFile sets the output file name. Defaults to "<struct_snake>.go".
func WithPackage ¶
func WithPackage(name string) GenerateOption
WithPackage sets the Go package name for the generated file. If not provided, the package name is inferred from the output directory name.
func WithStructName ¶
func WithStructName(name string) GenerateOption
WithStructName sets the name of the generated Go struct. Defaults to "Config".
func WithTOMLTag ¶
func WithTOMLTag(enabled bool) GenerateOption
WithTOMLTag controls whether toml struct tags are generated. Default is true.
func WithYAMLTag ¶
func WithYAMLTag(enabled bool) GenerateOption
WithYAMLTag controls whether yaml struct tags are generated. Default is true.