modelschema

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 20 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package modelschema loads the Go struct definitions of a tagged model module (e.g. cm-beetle/imdl) at runtime and validates/normalizes JSON documents against them, without compiling that version in.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Package

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

Package holds the top-level type declarations of a parsed Go package.

func LoadPackage

func LoadPackage(ctx context.Context, src Source, version, pkgDir string) (*Package, error)

LoadPackage returns the parsed Go package 'pkgDir' (relative to the module root; empty for the root package) of the given tagged version. Tagged versions are immutable, so successfully parsed packages are cached for the lifetime of the process.

func ParsePackage

func ParsePackage(files map[string][]byte) (*Package, error)

ParsePackage parses Go source files (file name -> content) of a single package.

func (*Package) AddSwaggerDefinitions

func (p *Package) AddSwaggerDefinitions(defs map[string]interface{}, typeName, prefix string) (map[string]interface{}, error)

AddSwaggerDefinitions generates Swagger 2.0 (OpenAPI v2) definitions for the named type and every named struct type reachable from it, adds them to defs with the given name prefix (e.g. "imdl-v0.1.6.onpremisemodel."), and returns a schema referencing the named type.

func (*Package) HasType

func (p *Package) HasType(name string) bool

HasType reports whether the package declares the named type.

func (*Package) Normalize

func (p *Package) Normalize(data []byte, typeName, rootPath string) (json.RawMessage, error)

Normalize validates the JSON document against the named type and returns it as encoding/json would produce after unmarshalling into and marshalling from that type: unknown fields and type mismatches are reported as a *ValidationError, keys are canonicalized and missing fields are filled with zero values. rootPath is used as a prefix in validation messages (e.g. "onpremiseInfraModel").

func (*Package) NormalizeDroppingUnknown added in v0.6.5

func (p *Package) NormalizeDroppingUnknown(data []byte, typeName, rootPath string) (json.RawMessage, []string, error)

NormalizeDroppingUnknown works like Normalize, except that the fields which the named type does not declare are removed instead of being reported. The paths of the removed fields are returned.

type Source

type Source struct {
	// ModulePath is the Go module path (e.g. "github.com/cloud-barista/cm-beetle/imdl").
	ModulePath string
	// Owner, Repo, RepoSubdir and TagPrefix locate the module in its GitHub repository
	// (e.g. "cloud-barista", "cm-beetle", "imdl", "imdl/"). Used as a fallback source.
	Owner      string
	Repo       string
	RepoSubdir string
	TagPrefix  string
}

Source describes where the tagged source code of a model module can be downloaded.

type ValidationError

type ValidationError struct {
	Issues []string
	Total  int
}

ValidationError lists the mismatches between a JSON document and the Go type definition.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Jump to

Keyboard shortcuts

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