schema

package
v0.0.0-...-59d4d0b Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

package schema implements a minimal JSON Schema library that parses the schema as YAML. The goal of this package is to be able to parse Apple's YAML MDM and Declarative Device Management Schema into a Go struct Abstract Syntax Tree

Index

Constants

This section is empty.

Variables

View Source
var ErrUnexpectedBool = errors.New("unexpected boolean")

Functions

func GenerateFromFile

func GenerateFromFile(path, pkgName string, reps replace.Replacements) ([]byte, error)

GenerateFromFile generates Go types from the schema at path using the given pkgName and optional replacements

func GenerateFromGit

func GenerateFromGit(repoURL, commit, path, pkgName string, reps replace.Replacements) ([]byte, error)

GenerateFromGit generates Go types from the schema at the git repo/commit/path using the given pkgName and optional replacements

Types

type EncodeOption

type EncodeOption func(*Encoder)

EncodeOption allows configuring an Encoder

func WithReplacements

func WithReplacements(reps replace.Replacements) EncodeOption

type Encoder

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

Encoder encodes Schemas to Go types

func NewEncoder

func NewEncoder(f *jen.File, opts ...EncodeOption) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(s *Schema)

type IntegerNumber

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

IntegerNumber holds either an int64 or a float64

func NewIntegerNumber

func NewIntegerNumber[T int64 | float64](v T) *IntegerNumber

func (*IntegerNumber) Float64

func (in *IntegerNumber) Float64() float64

Float64 returns the underlying float64. Float64 will panic if the underlying type is not float64

func (*IntegerNumber) Int64

func (in *IntegerNumber) Int64() int64

Int64 returns the underlying int64. Int64 will panic if the underlying type is not int64

func (*IntegerNumber) IsFloat64

func (in *IntegerNumber) IsFloat64() bool

func (*IntegerNumber) IsInt64

func (in *IntegerNumber) IsInt64() bool

func (*IntegerNumber) MarshalYAML

func (in *IntegerNumber) MarshalYAML() ([]byte, error)

func (*IntegerNumber) UnmarshalYAML

func (in *IntegerNumber) UnmarshalYAML(data []byte) error

func (*IntegerNumber) Value

func (in *IntegerNumber) Value() any

type IntegerNumberString

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

IntegerNumberString holds an int64, float64, or a string

func NewIntegerNumberString

func NewIntegerNumberString[T int64 | float64 | string](v T) *IntegerNumberString

func (*IntegerNumberString) Float64

func (in *IntegerNumberString) Float64() float64

Float64 returns the underlying float64. Float64 will panic if the underlying type is not float64

func (*IntegerNumberString) Int64

func (in *IntegerNumberString) Int64() int64

Int64 returns the underlying int64. Int64 will panic if the underlying type is not int64

func (*IntegerNumberString) IsFloat64

func (in *IntegerNumberString) IsFloat64() bool

func (*IntegerNumberString) IsInt64

func (in *IntegerNumberString) IsInt64() bool

func (*IntegerNumberString) IsString

func (in *IntegerNumberString) IsString() bool

func (*IntegerNumberString) MarshalYAML

func (in *IntegerNumberString) MarshalYAML() ([]byte, error)

func (*IntegerNumberString) String

func (in *IntegerNumberString) String() string

String returns the underlying string. String will panic if the underlying type is not string

func (*IntegerNumberString) UnmarshalYAML

func (in *IntegerNumberString) UnmarshalYAML(data []byte) error

func (*IntegerNumberString) Value

func (in *IntegerNumberString) Value() any

type OrderedMap

type OrderedMap struct {
	Map   map[string]*Schema
	Order []string
}

OrderedMap is an ordered map[string]*Schema

func (*OrderedMap) Iter

func (m *OrderedMap) Iter(f func(key string, schm *Schema))

Iter iterates through keys and values in order

func (*OrderedMap) UnmarshalYAML

func (m *OrderedMap) UnmarshalYAML(f func(interface{}) error) error

type Schema

type Schema struct {
	Title                string      `yaml:"title,omitempty"`
	Type                 TypeSlice   `yaml:"type"`
	Description          string      `yaml:"description,omitempty"`
	AdditionalProperties bool        `yaml:"additionalProperties,omitempty"`
	Required             []string    `yaml:"required,omitempty"`
	Properties           *OrderedMap `yaml:"properties,omitempty"`
	Items                *Schema     `yaml:"items,omitempty"`
	Enum                 []string    `yaml:"enum,omitempty"`
	Default              any         `yaml:"default,omitempty"`
}

Schema represents a JSON Schema (encoded in YAML)

func New

func New(r io.Reader) (*Schema, error)

New parses the yaml schema from r

func NewFromFile

func NewFromFile(path string) (*Schema, error)

NewFromFile parses the yaml schema at path

type Type

type Type string
const (
	TypeArray   Type = "array"
	TypeBool    Type = "boolean"
	TypeInteger Type = "integer"
	TypeNumber  Type = "number"
	TypeObject  Type = "object"
	TypeString  Type = "string"
)

type TypeSlice

type TypeSlice []Type

TypeSlice can represent a single string Type or a slice of Types

func (*TypeSlice) UnmarshalYAML

func (t *TypeSlice) UnmarshalYAML(f func(interface{}) error) error

Jump to

Keyboard shortcuts

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