parse

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2025 License: MIT Imports: 14 Imported by: ? modules, 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

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidGlob = errors.New(`invalid glob pattern`)

ErrInvalidGlob is returned when (one of the) glob patterns is invalid

View Source
var ErrParsingSchema = errors.New(`could not parse schema`)

ErrParsingSchema is returned when the jsonschema compiler could not parse the json file

View Source
var ErrReadFile = errors.New(`cannot read file`)

ErrReadFile is returned when a file cannot be

View Source
var ErrUnknownSchema = errors.New("unknown schema")

ErrUnknownSchema is returned when the schema is not resolved or found

Functions

func DepthMap

func DepthMap(roots []*jsonschema.Schema, classes []*domain.Class, relations []*domain.Relation) map[*domain.Class]int

DepthMap w.r.t. the provided jsonschema.Schema roots that are directly parsed from the glob patterns. Each directly referenced file (e.g. some/file/schema.json) must have a depth of 0. For each not directly referenced file the distance is the shortest distance from any root to that file using the classical Dijkstra's shortest-path algorithm.

func NewCompiler

func NewCompiler(baseURI string) (*jsonschema.Compiler, error)

NewCompiler for baseURI. If the baseURI is an empty string "" the current working directory is used.

func ReadSchema

func ReadSchema(compiler *jsonschema.Compiler, filepath string, strict bool) (*jsonschema.Schema, error)

ReadSchema from file into a jsonschema.Schema

Types

type Cache

type Cache interface {
	// Process a schema
	Process(schema *jsonschema.Schema)
	// HasProcessed a schema
	HasProcessed(schema *jsonschema.Schema) bool
	// Schemas stored in Cache
	Schemas() []*jsonschema.Schema
}

Cache tracks processed jsonschema.Schema's

type ClassParser

type ClassParser struct {
	*Parser
	// contains filtered or unexported fields
}

ClassParser tracks the transformation from jsonschema.Schema to Class

func (*ClassParser) Classes

func (p *ClassParser) Classes() ([]*domain.Class, error)

Classes returns the parsed Class slice that can be used by transformations

func (*ClassParser) NewClass

func (p *ClassParser) NewClass(schema *jsonschema.Schema) (*domain.Class, error)

NewClass for a jsonschema.Schema

func (*ClassParser) NewProperty

func (p *ClassParser) NewProperty(parent *jsonschema.Schema, name string, value *jsonschema.Schema) (*domain.Property, error)

NewProperty for a Class based on its property jsonschema.Schema

func (*ClassParser) PropertyRef

func (p *ClassParser) PropertyRef(parent *jsonschema.Schema, value *jsonschema.Schema) (*jsonschema.Schema, error)

PropertyRef handles properties which are defined with a $ref (possibly with an anchor '#")

func (*ClassParser) Relations

func (p *ClassParser) Relations() ([]*domain.Relation, error)

Relations returns the parsed Reference's between various domain.Class and domain.Property

type Loader

type Loader func(url string) (io.ReadCloser, error)

Loader used by jsonschema.Compiler::Loaders

func NewFileLoader

func NewFileLoader(workingDirectory string) Loader

NewFileLoader constructs a loader that can read from disk

type MapCache

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

MapCache tracks processed jsonschema.Schema's

func (*MapCache) HasProcessed

func (c *MapCache) HasProcessed(schema *jsonschema.Schema) bool

HasProcessed returns true iff a jsonschema.Schema is already processed

func (*MapCache) Process

func (c *MapCache) Process(schema *jsonschema.Schema)

Process marks a jsonschema.Schema as processed

func (*MapCache) Schemas

func (c *MapCache) Schemas() []*jsonschema.Schema

Schemas stored in Cache

type Parser

type Parser struct {
	// Globs to search for JSON files
	Globs []string

	// StrictMode will error if a JSON file contained in the Globs cannot be parsed to a jsonschema.Schema
	// default false where its logged as a warning
	StrictMode bool

	// BaseURI used for resolving schemas
	BaseURI string

	// Cache implementation to process nested object's and $defs
	Cache Cache

	// Depth of external $refs to follow (or -1 to follow all)
	// 0 implies only referenced schemas
	Depth int

	// Compiler used to load the jsonschema.Schema's
	Compiler *jsonschema.Compiler
	// contains filtered or unexported fields
}

Parser for .json files to execute transforms

func NewParser

func NewParser(globs ...string) *Parser

NewParser for glob patterns, e.g. "*", "**/*.json", ...

func (*Parser) Classes

func (p *Parser) Classes() ([]*domain.Class, error)

Classes returns the parsed Class slice that can be used by transformations

func (*Parser) Relations

func (p *Parser) Relations() ([]*domain.Relation, error)

Relations returns the parsed Reference's between various domain.Class and domain.Property

func (*Parser) Schemas

func (p *Parser) Schemas() ([]*jsonschema.Schema, error)

Schemas read by the parser

func (*Parser) SetBaseURI

func (p *Parser) SetBaseURI(baseURI string) *Parser

SetBaseURI from which file:// $id's are resolved

func (*Parser) SetDepth

func (p *Parser) SetDepth(depth int) *Parser

SetDepth to only follow $refs that are 'depth' deep

type Reference

type Reference struct {
	// ReferenceType can be oneOf, $ref, allOf, etc
	Type ReferenceType

	// From which jsonschema.Schema the relation started, i.e. with $ref
	From       *jsonschema.Schema
	FromParent *jsonschema.Schema

	// To which jsonschema.Schema the relation points (which could be a jsonschema.Schema or $anchor)
	To       *jsonschema.Schema
	ToParent *jsonschema.Schema
}

Reference between two jsonschema.Schema's

type ReferenceType

type ReferenceType string
const AllOf ReferenceType = "allOf"

AllOf between schemas using oneOf

const AnyOf ReferenceType = "anyOf"

AnyOf between schemas using oneOf

const OneOf ReferenceType = "oneOf"

OneOf between schemas using oneOf

const Ref ReferenceType = "$ref"

Ref between schemas using $ref

Jump to

Keyboard shortcuts

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