Skip to content

package gogen

import "github.com/cloudboss/unobin/pkg/gogen"

Package gogen generates Go library source code from external schema formats (CFN registry schemas, TF provider schemas, DCL YAML). Sub-packages handle their own schema format; this package provides the shared types, type mapping, Go source rendering, and the top-level Generate orchestrator.

Functions

func ConfigurationFile

func ConfigurationFile(cs ConfigurationSchema, packageName, from string) ([]byte, error)

ConfigurationFile renders a configuration.go that declares the library-level provider config struct. Each field is wrapped in the cfg.* type matching its primitive Go type; non-required fields use a pointer so the decoder treats them as optional.

func DataSourceFile

func DataSourceFile(ds DataSourceSchema, from string) ([]byte, error)

DataSourceFile renders a Go source file for one data source into the data/ sub-package.

func GoMod

func GoMod(modulePath, replaceUnobin, unobinVersion string) ([]byte, error)

GoMod renders a go.mod file for a generated library. unobinVersion pins the unobin requirement when it is a release version; otherwise the v0.0.0 placeholder stands in, which only resolves with a replace. When replaceUnobin is non-empty, its absolute path is used to add a replace directive for the unobin dependency.

func LibraryFile

func LibraryFile(
    packageName string,
    resources []ResourceSchema,
    dataSources []DataSourceSchema,
    configuration *ConfigurationSchema,
    modulePath, from string,
) ([]byte, error)

LibraryFile renders a library.go that registers all resources and data sources. It lives in the root package and imports the resources/ and data/ sub-packages (only the ones that have content). configuration may be nil; when present and non-empty, the registration references the ProviderConfig struct declared in configuration.go.

func PointerType

func PointerType(goType string) string

PointerType returns the pointer-wrapped Go type for optional fields. For bare types (string, int64, bool, float64) it returns "*T". For reference types ([]T, map[K]V, any) it returns the type unchanged since slices, maps, and interfaces are nil-able by default.

func ResourceFile

func ResourceFile(rs ResourceSchema, from string) ([]byte, error)

ResourceFile renders a Go source file for one resource into the resources/ sub-package.

func UBTag

func UBTag(fieldName string) string

UBTag returns the ub tag value for a field name.

Types

type ConfigurationSchema

type ConfigurationSchema struct {
    GoName      string
    Description string
    Fields      []Field
}

ConfigurationSchema describes the operator-facing library configuration. Fields carry primitive Go types ("string", "int64", "float64", "bool", "[]string", "map[string]string", "any", ...); the renderer wraps each in the matching cfg.* wrapper type when it emits the struct.

type DataSourceSchema

type DataSourceSchema struct {
    GoName       string
    CloudType    string
    Description  string
    InputFields  []Field
    OutputFields []Field
}

DataSourceSchema describes one cloud data source for code generation.

type Field

type Field struct {
    Name        string
    GoType      string
    Description string
    Required    bool
}

Field is one property of a resource or data source.

type Input

type Input struct {
    Resources     []string
    OutDir        string
    ModulePath    string
    From          string
    ReplaceUnobin string // local path to github.com/cloudboss/unobin for go.mod replace
    UnobinVersion string // the CLI's own unobin version, pinned in the generated go.mod
}

Input configures a generation run.

type Output

type Output struct {
    ModulePath  string
    Resources   int
    DataSources int
}

Output reports what was generated.

func Generate

func Generate(ctx context.Context, adapter SchemaAdapter, in Input) (*Output, error)

Generate fetches schemas from the adapter, renders Go source files, and writes them to disk. Resources go into a resources/ sub-package and data sources into a data/ sub-package so that name collisions between resource and data source types cannot happen.

type ResourceSchema

type ResourceSchema struct {
    GoName            string
    CloudType         string
    Description       string
    InputFields       []Field
    OutputFields      []Field
    CreateOnlyFields  []string
    PrimaryIdentifier []string
}

ResourceSchema describes one cloud resource type for code generation.

type SchemaAdapter

type SchemaAdapter interface {
    Name() string
    FetchResources(ctx context.Context, resources []string) ([]ResourceSchema, error)
    FetchDataSources(ctx context.Context, resources []string) ([]DataSourceSchema, error)
    FetchConfiguration(ctx context.Context) (*ConfigurationSchema, error)
}

SchemaAdapter fetches resource and data source schemas from an external source (e.g. a TF provider schema). FetchConfiguration returns the library-level configuration schema (the provider's own config block in TF). A nil return means the source has no configuration to expose; the generated library then omits the Configuration field.