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 ¶
- type Package
- func (p *Package) AddSwaggerDefinitions(defs map[string]interface{}, typeName, prefix string) (map[string]interface{}, error)
- func (p *Package) HasType(name string) bool
- func (p *Package) Normalize(data []byte, typeName, rootPath string) (json.RawMessage, error)
- func (p *Package) NormalizeDroppingUnknown(data []byte, typeName, rootPath string) (json.RawMessage, []string, error)
- type Source
- type ValidationError
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 ¶
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 ¶
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) Normalize ¶
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 ¶
ValidationError lists the mismatches between a JSON document and the Go type definition.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string