Documentation
¶
Index ¶
- type Generator
- func (g *Generator) CleanPkgDirectory(outputDir string) error
- func (g *Generator) FormatCode(source string) (string, error)
- func (g *Generator) GenerateFromSchemaFile(schemaFile, outputDir string) error
- func (g *Generator) GenerateSDK(schema *parser.APISchema, outputDir string) error
- func (g *Generator) RunGoFmt(outputDir string) error
- func (g *Generator) RunGoImports(outputDir string) error
- func (g *Generator) RunStaticcheck(outputDir string) error
- func (g *Generator) SetForceRegenerate(force bool)
- func (g *Generator) ValidateGeneratedCode(pkgPath string) error
- func (g *Generator) WriteFile(path, content string) error
- type GeneratorConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator generates Go SDK code from Proxmox API schema.
The generator orchestrates the entire SDK generation process:
- Reading and parsing schema files
- Generating code using templates
- Formatting and validating generated code
- Writing files to the output directory
It uses the template engine internally to transform schema structures into production-ready Go code.
func NewGenerator ¶
func NewGenerator() *Generator
NewGenerator creates a new code generator.
The generator is ready to use immediately with sensible defaults. It creates an internal template engine for code generation.
Example usage:
gen := NewGenerator()
err := gen.GenerateFromSchemaFile("schema.json", "output/")
func (*Generator) CleanPkgDirectory ¶ added in v9.1.1
CleanPkgDirectory removes generated code from pkg/ directory before regeneration.
This method ensures that removed API endpoints are properly cleaned up.
Protection rules: - ALWAYS preserves pkg/httpclient/ and pkg/encoding/ (manually maintained, never deleted) - Without --force: Preserves test files (*_test.go) in all packages - With --force: Deletes everything except httpclient and encoding
Behavior: - Normal mode: Deletes implementation files but keeps tests - Force mode: Deletes all generated code including tests (but not httpclient and encoding)
This ensures old/removed endpoints are cleaned up while protecting manual code.
func (*Generator) FormatCode ¶
FormatCode formats Go source code using gofmt.
This ensures all generated code follows standard Go formatting conventions. It uses go/format.Source internally, which validates syntax and applies standard formatting rules.
If the source code has syntax errors, this method returns an error.
func (*Generator) GenerateFromSchemaFile ¶
GenerateFromSchemaFile reads a schema JSON file and generates the SDK.
This is a convenience method that combines reading a schema file, parsing it, and generating the SDK in one call. It's the recommended way to generate the SDK from a file.
The schema file should be a JSON file containing a parsed Proxmox API schema (typically generated by the schema generator).
Example:
gen := NewGenerator()
err := gen.GenerateFromSchemaFile("schema/apiSchema.json", "output/")
func (*Generator) GenerateSDK ¶
GenerateSDK generates the complete SDK from a parsed schema.
This is the main entry point for SDK generation from an already-parsed schema. It performs the following steps:
- Validates input parameters
- Generates code files using the template engine
- Formats all generated code with gofmt
- Writes files to the output directory
The output directory will be created if it doesn't exist. Generated files follow the structure:
outputDir/pkg/proxmox/client.go outputDir/pkg/proxmox/types.go outputDir/pkg/cluster/... (future)
Example:
schema := &parser.APISchema{...}
err := gen.GenerateSDK(schema, "output/")
func (*Generator) RunGoFmt ¶ added in v9.1.1
RunGoFmt runs gofmt on all Go files in the output directory.
This ensures all generated files follow standard Go formatting conventions. It applies gofmt -s (simplify code) to all .go files recursively.
This is run as a final pass after code generation to ensure consistency.
func (*Generator) RunGoImports ¶ added in v9.1.1
RunGoImports runs goimports on all Go files in the output directory.
This cleans up imports by:
- Removing unused imports
- Sorting imports alphabetically (stdlib, then third-party, then local)
- Adding missing imports
It recursively processes all .go files in the directory.
func (*Generator) RunStaticcheck ¶ added in v9.1.1
RunStaticcheck runs staticcheck on all packages in the output directory.
This performs comprehensive static analysis to catch common issues, bugs, and style violations in the generated code. If staticcheck is not installed, the check is skipped with a warning.
This is run as a final validation pass after code generation.
func (*Generator) SetForceRegenerate ¶ added in v9.1.1
SetForceRegenerate sets whether to force complete regeneration including tests.
Force regeneration behavior: - pkg/httpclient/ is ALWAYS preserved (never deleted) - With force=true: Deletes and regenerates all files including tests - With force=false (default): Preserves existing test files, regenerates implementation
This allows incremental updates while protecting manually maintained code.
func (*Generator) ValidateGeneratedCode ¶
ValidateGeneratedCode validates that the generated code is syntactically correct.
This method checks all .go files in the specified package directory to ensure they contain valid Go syntax. It uses go/format.Source for validation, which will catch syntax errors.
This is useful as a post-generation sanity check to ensure the template engine produced valid code.
Example:
err := gen.ValidateGeneratedCode("output/pkg/proxmox")
func (*Generator) WriteFile ¶
WriteFile writes content to a file, creating directories as needed.
This method ensures that all parent directories exist before writing the file. Directories are created with 0755 permissions, files with 0644 permissions.
Special handling for test files: - Without --force: Preserves existing test files (user customizations) - With --force: Overwrites all files including tests - Implementation files are always overwritten
The method is used internally during SDK generation to write all generated files to disk.
type GeneratorConfig ¶
type GeneratorConfig struct {
}
GeneratorConfig holds configuration for code generation.
Future enhancements may include:
- Package name overrides
- Custom template paths
- Code style preferences
- Output formatting options