structs

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2026 License: MPL-2.0 Imports: 13 Imported by: 0

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 Field

type Field struct {
	Name    string
	Type    string
	JSONTag string
}

Field represents a struct field in the generated output.

type Option

type Option func(*options)

Option configures the Flatten generator.

func WithExtraScalars

func WithExtraScalars(names ...string) Option

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

func WithHeaderComment(comment string) Option

WithHeaderComment overrides the default "DO NOT EDIT" header comment.

func WithPackageName

func WithPackageName(name string) Option

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL