schema

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

nolint

nolint

nolint:gochecknoglobals

nolint

Index

Constants

This section is empty.

Variables

View Source
var (
	TrueSchema  = &Schema{boolean: &[]bool{true}[0]}
	FalseSchema = &Schema{boolean: &[]bool{false}[0]}
)
View Source
var CustomGenerators = &customSchema{
	mapSchema: make(map[string]CustomGenerator),
}

nolint:gochecknoglobals

View Source
var Version = "v1"

Functions

func ExtractGoComments

func ExtractGoComments(base string, rootPath string, commentMap map[string]string) error

ExtractGoComments reads all Go files in a given path and its subdirectories, extracting doc comments for exported types and their fields. `base` is the import path prefix to match the final FQNs.

func GetFieldName

func GetFieldName(fieldType reflect.StructField) (string, bool)

func GetNameAndRequiredFlag

func GetNameAndRequiredFlag(field reflect.StructField) (string, bool)

func NewProperties

func NewProperties() *orderedmap.OrderedMap[string, *Schema]

func SetProperties

func SetProperties(field reflect.StructField, s *Schema)

Types

type CustomGenerator

type CustomGenerator func(field reflect.StructField, s *Schema)

type Definitions

type Definitions map[string]*Schema

type Generator

type Generator struct {
	Reflector *Reflector
}

func NewGenerator

func NewGenerator() *Generator

func (*Generator) Generate

func (g *Generator) Generate(obj any) *Schema

type Id

type Id string
const EmptyId Id = ""

func (Id) Add

func (id Id) Add(path string) Id

func (Id) Anchor

func (id Id) Anchor(name string) Id

func (Id) Base

func (id Id) Base() Id

func (Id) Def

func (id Id) Def(name string) Id

func (Id) String

func (id Id) String() string

func (Id) Validate

func (id Id) Validate() error

nolint:err113

type Reflector

type Reflector struct {
	BaseSchemaId               Id
	Anonymous                  bool
	AssignAnchor               bool
	AllowAdditionalProperties  bool
	RequiredFromYamlSchemaTags bool
	DoNotReference             bool
	ExpandedStruct             bool
	FieldNameReflector         func(f reflect.StructField) (string, bool)
	FieldReflector             func(f reflect.StructField, s *Schema)
	FieldNameTag               string
	IgnoredTypes               []any
	Lookup                     func(reflect.Type) Id
	Mapper                     func(reflect.Type) *Schema
	Namer                      func(reflect.Type) string
	KeyNamer                   func(string) string
	AdditionalFields           func(reflect.Type) []reflect.StructField
	CommentMap                 map[string]string
}

func (*Reflector) AddGoComments

func (r *Reflector) AddGoComments(base, path string) error

func (*Reflector) Reflect

func (r *Reflector) Reflect(v any) *Schema

func (*Reflector) ReflectFromType

func (r *Reflector) ReflectFromType(t reflect.Type) *Schema

func (*Reflector) SetBaseSchemaId

func (r *Reflector) SetBaseSchemaId(id string)

type Schema

type Schema struct {
	Version              string                                  `yaml:"$schema,omitempty"`
	Id                   Id                                      `yaml:"$id,omitempty"`
	Anchor               string                                  `yaml:"$anchor,omitempty"`
	Ref                  string                                  `yaml:"$ref,omitempty"`
	DynamicRef           string                                  `yaml:"$dynamicRef,omitempty"`
	Definitions          Definitions                             `yaml:"$defs,omitempty"`
	Comments             string                                  `yaml:"$comment,omitempty"`
	AllOf                []*Schema                               `yaml:"allOf,omitempty"`
	AnyOf                []*Schema                               `yaml:"anyOf,omitempty"`
	OneOf                []*Schema                               `yaml:"oneOf,omitempty"`
	Not                  *Schema                                 `yaml:"not,omitempty"`
	If                   *Schema                                 `yaml:"if,omitempty"`
	Then                 *Schema                                 `yaml:"then,omitempty"`
	Else                 *Schema                                 `yaml:"else,omitempty"`
	DependentSchemas     map[string]*Schema                      `yaml:"dependentSchemas,omitempty"`
	PrefixItems          []*Schema                               `yaml:"prefixItems,omitempty"`
	Items                *Schema                                 `yaml:"items,omitempty"`
	Contains             *Schema                                 `yaml:"contains,omitempty"`
	Properties           *orderedmap.OrderedMap[string, *Schema] `yaml:"properties,omitempty"`
	PatternProperties    map[string]*Schema                      `yaml:"patternProperties,omitempty"`
	AdditionalProperties *Schema                                 `yaml:"additionalProperties,omitempty"`
	PropertyNames        *Schema                                 `yaml:"propertyNames,omitempty"`
	Type                 string                                  `yaml:"type,omitempty"`
	Enum                 []any                                   `yaml:"enum,omitempty"`
	Const                any                                     `yaml:"const,omitempty"`
	MultipleOf           *int64                                  `yaml:"multipleOf,omitempty"`
	Maximum              *int64                                  `yaml:"maximum,omitempty"`
	ExclusiveMaximum     *int64                                  `yaml:"exclusiveMaximum,omitempty"`
	Minimum              *int64                                  `yaml:"minimum,omitempty"`
	ExclusiveMinimum     *int64                                  `yaml:"exclusiveMinimum,omitempty"`
	MaxLength            *uint64                                 `yaml:"maxLength,omitempty"`
	MinLength            *uint64                                 `yaml:"minLength,omitempty"`
	Pattern              string                                  `yaml:"pattern,omitempty"`
	MaxItems             *uint64                                 `yaml:"maxItems,omitempty"`
	MinItems             *uint64                                 `yaml:"minItems,omitempty"`
	UniqueItems          bool                                    `yaml:"uniqueItems,omitempty"`
	MaxContains          *uint64                                 `yaml:"maxContains,omitempty"`
	MinContains          *uint64                                 `yaml:"minContains,omitempty"`
	MaxProperties        *uint64                                 `yaml:"maxProperties,omitempty"`
	MinProperties        *uint64                                 `yaml:"minProperties,omitempty"`
	Secrets              []string                                `yaml:"secrets,omitempty"`
	Required             []string                                `yaml:"required,omitempty"`
	DependentRequired    map[string][]string                     `yaml:"dependentRequired,omitempty"`
	Format               string                                  `yaml:"format,omitempty"`
	ContentEncoding      string                                  `yaml:"contentEncoding,omitempty"`
	ContentMediaType     string                                  `yaml:"contentMediaType,omitempty"`
	ContentSchema        *Schema                                 `yaml:"contentSchema,omitempty"`
	Title                string                                  `yaml:"title,omitempty"`
	Description          string                                  `yaml:"description,omitempty"`
	Default              any                                     `yaml:"default,omitempty"`
	Deprecated           bool                                    `yaml:"deprecated,omitempty"`
	ReadOnly             bool                                    `yaml:"readOnly,omitempty"`
	WriteOnly            bool                                    `yaml:"writeOnly,omitempty"`
	Examples             []any                                   `yaml:"examples,omitempty"`

	Extras map[string]any `yaml:"-"`
	// contains filtered or unexported fields
}

func Reflect

func Reflect(v any) *Schema

func ReflectFromType

func ReflectFromType(t reflect.Type) *Schema

func (*Schema) MarshalYAML

func (t *Schema) MarshalYAML() ([]byte, error)

func (*Schema) UnmarshalYAML

func (t *Schema) UnmarshalYAML(data []byte) error

Jump to

Keyboard shortcuts

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