Documentation
¶
Overview ¶
Package structs provides code generation utilities for optimizing Go struct types. The primary use case is flattening deeply nested API model structs into decode-efficient versions where nested struct fields are replaced with map[string]any, eliminating the decode -> allocate -> re-encode cycle that occurs when the CloudQuery SDK serializes nested fields as JSON columns.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Flatten ¶
func Flatten(srcPath string, configs []StructConfig, outputDir string, opts ...Option) error
Flatten parses the Go source file at srcPath, finds the structs described by configs, replaces deeply nested struct fields with map[string]any, and writes generated files into outputDir.
Scalar types and string-based enum types are kept typed. All other types (structs, interfaces, cross-package types, maps) are flattened to map[string]any. Pointer wrappers are preserved for scalar types but collapsed for complex types.
Types ¶
type Option ¶
type Option func(*options)
Option configures the Flatten generator.
func WithExtraScalars ¶
WithExtraScalars adds type names that should be treated as scalars (kept typed) in addition to the built-in set (string, bool, float64, int64, int).
func WithHeaderComment ¶
WithHeaderComment overrides the default "DO NOT EDIT" header comment.
func WithPackageName ¶
WithPackageName sets the Go package name for generated files. Default: basename of the output directory.
func WithSortFields ¶
func WithSortFields() Option
WithSortFields sorts generated fields: primary key fields (named "ID") first, then alphabetically by name. ExtraFields always come before source fields, preserving their original order.
type StructConfig ¶
type StructConfig struct {
// SourceName is the struct name in the source file (e.g. "VulnerabilityFinding").
SourceName string
// OutputName is the generated struct name (e.g. "customVulnerabilityFinding").
OutputName string
// OutputFile is the filename for the generated file (e.g. "vulnerability_finding_generated.go").
OutputFile string
// ExtraFields are additional fields prepended to the generated struct.
ExtraFields []Field
}
StructConfig defines how to transform one source struct into an optimized version.