genschema

package
v0.0.0-...-711f2e8 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 9 Imported by: 1

Documentation

Overview

Package genschema generates JSON Schema definitions for Go types.

The JSON Schema generation uses invopop/jsonschema, which is based on type reflection. The schema definitions are intended to be embedded in a Go CLI binary to be "generated" on the user's system.

Example

Below is an example of how to generate the schema use the go:generate directive:

// gen.go
package gen

//go:generate go run internal/gen/main.go cmd/example/schemas

And the file called by the go:generate directive in gen.go:

// internal/gen/main.go

//go:build ignore

package main

import (
	"fmt"
	"log"

	"github.com/act3-ai/go-common/pkg/genschema"
	"git.act3-ace.com/ace/example/pkg/apis/example.act3-ace.io/v1alpha1"
)

func main() {
	if len(os.Args) < 1 {
		log.Fatal("Must specify a target directory for schema generation.")
	}
 	// Generate JSON Schema definitions
 	if err := genschema.GenJSONSchema(
 		"cmd/act3-pt/schemas",
 		[]any{&v1alpha1.Configuration{}, &v1alpha1.Data{}},
 		"example.act3-ace.io/v1alpha1",
 		"git.act3-ace.com/ace/example",
 	); err != nil {
 		log.Fatal(fmt.Errorf("JSON Schema generation failed: %w", err))
 	}
}

And finally, embedding the JSON Schema definitions in a CLI and adding the "genschema" command:

// cmd/example/main.go
package main

import (
	"embed"
	"io/fs"
	"log"
	"os"

	"github.com/spf13/cobra"

	commands "github.com/act3-ai/go-common/pkg/cmd"
)

//go:embed schemas/*
var schemas embed.FS

func main() {
	cmd := &cobra.Command{
		Use: "example",
	}

	schemaAssociations := []SchemaAssociation{
		{
			Definition: "schemas/configuration-schema.json",
			FileMatch:  []string{"ace-example-configuration.yaml"},
		},
		{
			Definition: "schemas/data-shema.json",
			FileMatch:  []string{"ace-example-data.json"},
		},
	}

	cmd.AddCommand(
		commands.NewGenschemaCmd(schemas, schemaAssociations),
	)

	if err := cmd.Execute(); err != nil {
		os.Exit(1)
	}
}

Now, running "go generate ./..." before running "go build ./cmd/example" results in a CLI with a "genschema" command that will generate accurate JSON Schema definitions for the provided schemas.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForAPIGroup

func ForAPIGroup(r *jsonschema.Reflector, scheme *runtime.Scheme, group string) (*jsonschema.Schema, error)

ForAPIGroup creates a JSONSchema validator for an API Group recognized by a runtime.Scheme.

The resulting schema validates all Kinds recognized by the Scheme as part of the Group by mapping each "apiVersion" and "kind" to a subschema.

func GenerateGroupSchemas

func GenerateGroupSchemas(dir string, scheme *runtime.Scheme, apiGroups []string, moduleName string) error

GenerateGroupSchemas is a helper to generate all the schemas you want into dir

func GenerateTypeSchemas

func GenerateTypeSchemas(schemaDir string, types []any, baseSchemaID string, moduleName string) error

GenerateTypeSchemas generates JSON Schema definitions for internal Go types

- schemas is a list of types (schema) to a generate schema for. - baseSchemaID is the base name for the schema definitions. Use "apiVersion" values for KRM file schemas. - moduleName is used to add Go comments to the schema as descriptions, pass an empty string to disable this.

GenerateTypeSchemas("schemas", []any{&v1alpha1.Configuration{}, &v1alpha1.Data{}}, "example.act3-ace.io/v1alpha1", "git.act3-ace.com/ace/example")

func WriteSchema

func WriteSchema(schema *jsonschema.Schema, file string) error

WriteSchema marshals a JSONSchema definition to JSON and writes it to file

Types

This section is empty.

Jump to

Keyboard shortcuts

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