generator

package
v0.0.0-...-8d47722 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package generator creates JSON schemas from Go types, bridging Go struct definitions to JSON Schema for YAML validation workflows.

Many configuration systems define their structure using Go structs with JSON or YAML tags.

This package extracts that structure into a JSON Schema that can validate configuration files, power IDE autocompletion, and generate documentation.

It leverages jsonschema for reflection while adding Go source comment extraction for richer schema descriptions.

Usage

Create a *Generator with your configuration struct and call Generator.Generate:

type Config struct {
    Port    int    `json:"port"    jsonschema:"title=Port"`
    Timeout string `json:"timeout" jsonschema:"title=Timeout"`
}

gen := generator.New(Config{})
schemaBytes, err := gen.Generate()

The resulting JSON Schema can be used with go.jacobcolvin.com/niceyaml/schema/validator to validate YAML files against your Go type definitions.

Comment Extraction

By default, schemas contain only type information. Use WithPackagePaths to parse Go source files and extract doc comments as schema descriptions:

gen := generator.New(Config{},
    generator.WithPackagePaths("./config/..."),
)

This walks the Go AST to find comments on types and struct fields, then includes them in the schema's "description" fields.

The default comment formatter, DefaultLookupCommentFunc, also appends pkg.go.dev URLs for each type, providing a quick reference link in IDEs that display schema descriptions.

For custom comment formatting, use WithLookupCommentFunc to provide a LookupCommentFunc that transforms the comment map into description strings.

Reflector Customization

The underlying github.com/invopop/jsonschema.Reflector can be customized via WithReflector to control schema generation behavior like reference handling and additional properties.

Index

Constants

This section is empty.

Variables

View Source
var ErrGoModNotFound = errors.New("go.mod not found")

ErrGoModNotFound is returned when go.mod cannot be found when searching for the module root directory.

Functions

func DefaultLookupCommentFunc

func DefaultLookupCommentFunc(commentMap map[string]string) func(t reflect.Type, f string) string

DefaultLookupCommentFunc is the default LookupCommentFunc used by New.

It combines source comments with pkg.go.dev documentation URLs, formatting the result as the comment text followed by a link to the type's documentation.

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator generates a JSON schema from a Go type using reflection. The underlying schema generation is handled by jsonschema.

When package paths are provided via WithPackagePaths, it extracts source comments to include as schema descriptions.

Create instances with New.

func New

func New(reflectTarget any, opts ...Option) *Generator

New creates a new *Generator for the given Go type.

The reflectTarget should be an instance of the type to generate a schema for (typically a zero value like Config{}).

Use WithPackagePaths to enable doc comment extraction from source files.

func (*Generator) Generate

func (g *Generator) Generate() ([]byte, error)

Generate creates a JSON schema from the configured Go type and returns it as indented JSON bytes.

type LookupCommentFunc

type LookupCommentFunc func(commentMap map[string]string) func(t reflect.Type, f string) string

LookupCommentFunc is a factory function that creates a comment lookup function from a comment map.

The comment map keys follow the pattern "pkg.TypeName" for type comments and "pkg.TypeName.FieldName" for field comments.

The returned function looks up comments by reflect.Type and field name (empty field name for type-level comments).

See DefaultLookupCommentFunc for the default implementation.

type Option

type Option func(*Generator)

Option configures a Generator.

Available options:

func WithLookupCommentFunc

func WithLookupCommentFunc(f LookupCommentFunc) Option

WithLookupCommentFunc is an Option that sets a custom LookupCommentFunc for transforming source comments into schema descriptions.

func WithPackagePaths

func WithPackagePaths(paths ...string) Option

WithPackagePaths is an Option that specifies Go package paths to parse for extracting doc comments as schema descriptions.

Paths can use "..." wildcards (e.g., "./config/...").

func WithReflector

func WithReflector(r *jsonschema.Reflector) Option

WithReflector is an Option that sets a custom *jsonschema.Reflector.

func WithTests

func WithTests(include bool) Option

WithTests is an Option that controls whether test files are included when loading packages for comment extraction.

Jump to

Keyboard shortcuts

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