jschema

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2022 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func IsValidType

func IsValidType(s string) bool

Types

type ASTNode

type ASTNode struct {
	// JSONType corresponding JSON type for this AST node's value.
	JSONType JSONType

	// SchemaType corresponding schema type for this AST node's value.
	SchemaType string

	// Value a node value.
	// Make sense only for scalars and shortcuts.
	Value string

	// Comment a ast node comment.
	Comment string

	// Rules a map of attached rules.
	Rules *RuleASTNodes

	// Properties contains all object properties.
	// Make sense only for objects.
	Properties *ASTNodes

	// Items contains all array items.
	// Make sense only for arrays.
	Items []ASTNode

	// IsKeyShortcut will be true if this property key is shortcut.
	// Make sense only for AST nodes which are represents object property.
	IsKeyShortcut bool
}

ASTNode an AST node.

type ASTNodes

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

ASTNodes an ordered map of AST nodes. gen:OrderedMap

func NewASTNodes

func NewASTNodes(data map[string]ASTNode, order []string) *ASTNodes

func (*ASTNodes) Delete

func (m *ASTNodes) Delete(k string)

func (*ASTNodes) Each

func (m *ASTNodes) Each(fn eachASTNodesFunc) error

func (*ASTNodes) EachSafe

func (m *ASTNodes) EachSafe(fn eachSafeASTNodesFunc)

func (*ASTNodes) Filter

func (m *ASTNodes) Filter(fn filterASTNodesFunc)

Filter iterates and changes values in the map.

func (*ASTNodes) Find

func (m *ASTNodes) Find(fn findASTNodesFunc) (ASTNodesItem, bool)

Find finds first matched item from the map.

func (*ASTNodes) Get

func (m *ASTNodes) Get(k string) (ASTNode, bool)

Get gets a value by key.

func (*ASTNodes) GetValue

func (m *ASTNodes) GetValue(k string) ASTNode

GetValue gets a value by key.

func (*ASTNodes) Has

func (m *ASTNodes) Has(k string) bool

Has checks that specified key is set.

func (*ASTNodes) Len

func (m *ASTNodes) Len() int

Len returns count of values.

func (*ASTNodes) Map

func (m *ASTNodes) Map(fn mapASTNodesFunc) error

Map iterates and changes values in the map.

func (*ASTNodes) MarshalJSON

func (m *ASTNodes) MarshalJSON() ([]byte, error)

func (*ASTNodes) Set

func (m *ASTNodes) Set(k string, v ASTNode)

Set sets a value with specified key.

func (*ASTNodes) Update

func (m *ASTNodes) Update(k string, fn func(v ASTNode) ASTNode)

Update updates a value with specified key.

type ASTNodesItem

type ASTNodesItem struct {
	Key   string
	Value ASTNode
}

ASTNodesItem represent single data from the ASTNodes.

type Document

type Document interface {
	// NextLexeme returns next lexeme from this document.
	// Might return ParsingError if document isn't valid.
	// Will return io.EOF when no more lexemes are available.
	NextLexeme() (lexeme.LexEvent, error)

	// Len returns length of document in bytes.
	// Might return ParsingError if document isn't valid.
	Len() (uint, error)

	// Check checks that this document is valid.
	// Can return ParsingError if document isn't valid.
	Check() error
}

Document represents a document. It's a concrete data. Data maybe a scalar type or complex type.

Not a thead safe!

Example of the valid documents: - "foo" - [1, 2, 3] - {"foo": "bar"}

type JSONType

type JSONType = string
const (
	JSONTypeNumber   JSONType = "number"
	JSONTypeString   JSONType = "string"
	JSONTypeBoolean  JSONType = "boolean"
	JSONTypeArray    JSONType = "array"
	JSONTypeObject   JSONType = "object"
	JSONTypeShortcut JSONType = "shortcut"
	JSONTypeMixed    JSONType = "mixed"
	JSONTypeNull     JSONType = "null"
)

type ParsingError

type ParsingError interface {
	error

	// Position returns position of buggy character.
	Position() uint

	// Message returns an error message.
	Message() string

	// ErrCode returns an error code.
	ErrCode() int
}

ParsingError indicates something bad was happened during parsing.

type Rule

type Rule interface{}

Rule represents a custom user-defined rule.

type RuleASTNode

type RuleASTNode struct {
	// JSONType corresponding JSON type for this AST node's value.
	JSONType JSONType

	// Value a node value.
	// Make sense only for scalars and shortcuts.
	Value string

	// Comment a ast node comment.
	Comment string

	// Properties contains all object properties.
	// Make sense only for objects.
	Properties *RuleASTNodes

	// Items contains all array items.
	// Make sense only for arrays.
	Items []RuleASTNode

	// Source a source of this rule.
	Source RuleASTNodeSource
}

type RuleASTNodeSource

type RuleASTNodeSource int
const (
	RuleASTNodeSourceUnknown RuleASTNodeSource = iota

	// RuleASTNodeSourceManual indicates rule added manually by the user.
	RuleASTNodeSourceManual

	// RuleASTNodeSourceGenerated indicates rule generated inside the code.
	RuleASTNodeSourceGenerated
)

type RuleASTNodes

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

RuleASTNodes an ordered map of rule AST nodes. gen:OrderedMap

func MakeRuleASTNodes

func MakeRuleASTNodes(capacity int) *RuleASTNodes

func NewRuleASTNodes

func NewRuleASTNodes(data map[string]RuleASTNode, order []string) *RuleASTNodes

func (*RuleASTNodes) Delete

func (m *RuleASTNodes) Delete(k string)

func (*RuleASTNodes) Each

func (m *RuleASTNodes) Each(fn eachRuleASTNodesFunc) error

func (*RuleASTNodes) EachSafe

func (m *RuleASTNodes) EachSafe(fn eachSafeRuleASTNodesFunc)

func (*RuleASTNodes) Filter

func (m *RuleASTNodes) Filter(fn filterRuleASTNodesFunc)

Filter iterates and changes values in the map.

func (*RuleASTNodes) Find

func (m *RuleASTNodes) Find(fn findRuleASTNodesFunc) (RuleASTNodesItem, bool)

Find finds first matched item from the map.

func (*RuleASTNodes) Get

func (m *RuleASTNodes) Get(k string) (RuleASTNode, bool)

Get gets a value by key.

func (*RuleASTNodes) GetValue

func (m *RuleASTNodes) GetValue(k string) RuleASTNode

GetValue gets a value by key.

func (*RuleASTNodes) Has

func (m *RuleASTNodes) Has(k string) bool

Has checks that specified key is set.

func (*RuleASTNodes) Len

func (m *RuleASTNodes) Len() int

Len returns count of values.

func (*RuleASTNodes) Map

func (m *RuleASTNodes) Map(fn mapRuleASTNodesFunc) error

Map iterates and changes values in the map.

func (*RuleASTNodes) MarshalJSON

func (m *RuleASTNodes) MarshalJSON() ([]byte, error)

func (*RuleASTNodes) Set

func (m *RuleASTNodes) Set(k string, v RuleASTNode)

Set sets a value with specified key.

func (*RuleASTNodes) Update

func (m *RuleASTNodes) Update(k string, fn func(v RuleASTNode) RuleASTNode)

Update updates a value with specified key.

type RuleASTNodesItem

type RuleASTNodesItem struct {
	Key   string
	Value RuleASTNode
}

RuleASTNodesItem represent single data from the RuleASTNodes.

type Schema

type Schema interface {
	// Len returns length of this schema in bytes.
	// Might return ParsingError if schema isn't valid.
	Len() (uint, error)

	// Example returns an example for this schema.
	// Might return ParsingError if schema isn't valid.
	Example() ([]byte, error)

	// AddType adds a new type to this schema.
	// Might return a ParsingError if add type isn't valid.
	AddType(name string, schema Schema) error

	// AddRule adds a new type to this schema.
	// Might return a ParsingError if add type isn't valid.
	AddRule(name string, schema Rule) error

	// Check checks that this schema is valid.
	// Can return ParsingError if schema isn't valid.
	Check() error

	// Validate validates specified document.
	// Might return a ParsingError if schema isn't valid.
	// Can return a ValidationError if specified document isn't valid for this
	// schema.
	Validate(Document) error

	// GetAST returns a root AST node for this schema.
	GetAST() (ASTNode, error)

	// UsedUserTypes return all used user types.
	UsedUserTypes() ([]string, error)
}

Schema represents a schema. Schema is a some description of expected structure of payload.

type SchemaType

type SchemaType string
const (
	SchemaTypeUndefined SchemaType = ""
	SchemaTypeString    SchemaType = "string"
	SchemaTypeInteger   SchemaType = "integer"
	SchemaTypeFloat     SchemaType = "float"
	SchemaTypeDecimal   SchemaType = "decimal"
	SchemaTypeBoolean   SchemaType = "boolean"
	SchemaTypeObject    SchemaType = "object"
	SchemaTypeArray     SchemaType = "array"
	SchemaTypeNull      SchemaType = "null"
	SchemaTypeEmail     SchemaType = "email"
	SchemaTypeURI       SchemaType = "uri"
	SchemaTypeUUID      SchemaType = "uuid"
	SchemaTypeDate      SchemaType = "date"
	SchemaTypeDateTime  SchemaType = "datetime"
	SchemaTypeEnum      SchemaType = "enum"
	SchemaTypeMixed     SchemaType = "mixed"
	SchemaTypeAny       SchemaType = "any"
)

func GuessSchemaType

func GuessSchemaType(b []byte) (SchemaType, error)

func (SchemaType) IsEqualSoft

func (t SchemaType) IsEqualSoft(x SchemaType) bool

IsEqualSoft compare two types with next assumptions% - Decimal is the same as float; - Email, URI, UUID, Date, and DateTime are the same as string; - Enum, Mixed and Any are the same as any other type.

func (SchemaType) IsOneOf

func (t SchemaType) IsOneOf(tt ...SchemaType) bool

IsOneOf return true if current schema is one of specified.

func (SchemaType) IsScalar

func (t SchemaType) IsScalar() bool

type ValidationError

type ValidationError interface {
	error

	// Message returns an error message.
	Message() string

	// ErrCode returns an error code.
	ErrCode() int
}

ValidationError indicates that validation was failed.

Jump to

Keyboard shortcuts

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