ast

package
v0.0.0-...-fe5df96 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HintDisjunctionOfScalars indicates that the struct was previously
	// represented in the IR by a disjunction of scalars (+ array), the
	// original definition of which is associated to this hint.
	HintDisjunctionOfScalars = "disjunction_of_scalars"

	// HintDiscriminatedDisjunctionOfRefs indicates that the struct was
	// previously represented in the IR by a disjunction of a fixed list of
	// references to structs, the original definition of which is associated
	// to this hint.
	HintDiscriminatedDisjunctionOfRefs = "disjunction_of_refs"

	// HintImplementsVariant indicates that a type implements a variant.
	// ie: dataquery, panelcfg, ...
	HintImplementsVariant = "implements_variant"

	// HintSkipVariantPluginRegistration preserves the variant hint on a type, but
	// tells the jennies to not register it as a plugin.
	HintSkipVariantPluginRegistration = "skip_variant_plugin_registration"
)
View Source
const DiscriminatorCatchAll = "cog_discriminator_catch_all"

Variables

View Source
var ErrCannotMergeSchemas = errors.New("can not merge schemas")

Functions

func TypeName

func TypeName(typeDef Type) string

Types

type Argument

type Argument struct {
	Name string
	Type Type
}

func (*Argument) DeepCopy

func (arg *Argument) DeepCopy() Argument

type ArrayType

type ArrayType struct {
	ValueType Type `yaml:"value_type"`
}

func (ArrayType) DeepCopy

func (t ArrayType) DeepCopy() ArrayType

func (ArrayType) IsArrayOfScalars

func (t ArrayType) IsArrayOfScalars() bool

type Assignment

type Assignment struct {
	// Where
	Path Path

	// What
	Value AssignmentValue

	// How
	Method AssignmentMethod

	Constraints []TypeConstraint `json:",omitempty"`
}

func ArgumentAssignment

func ArgumentAssignment(path Path, argument Argument, opts ...AssignmentOpt) Assignment

func ConstantAssignment

func ConstantAssignment(path Path, value any, opts ...AssignmentOpt) Assignment

func FieldAssignment

func FieldAssignment(field StructField, opts ...AssignmentOpt) Assignment

func (*Assignment) DeepCopy

func (assignment *Assignment) DeepCopy() Assignment

type AssignmentEnvelope

type AssignmentEnvelope struct {
	Type   Type // Should be a ref or a struct only
	Values []EnvelopeFieldValue
}

func (*AssignmentEnvelope) DeepCopy

func (envelope *AssignmentEnvelope) DeepCopy() AssignmentEnvelope

type AssignmentMethod

type AssignmentMethod string
const (
	DirectAssignment AssignmentMethod = "direct" // `foo = bar`
	AppendAssignment AssignmentMethod = "append" // `foo = append(foo, bar)`
)

type AssignmentOpt

type AssignmentOpt func(assignment *Assignment)

func Constraints

func Constraints(constraints []TypeConstraint) AssignmentOpt

func Method

func Method(method AssignmentMethod) AssignmentOpt

type AssignmentValue

type AssignmentValue struct {
	Argument *Argument           `json:",omitempty"`
	Constant any                 `json:",omitempty"`
	Envelope *AssignmentEnvelope `json:",omitempty"`
}

func (*AssignmentValue) DeepCopy

func (value *AssignmentValue) DeepCopy() AssignmentValue

type Builder

type Builder struct {
	// Original data used to derive the builder, stored for read-only access
	// for the jennies and veneers.
	Schema *Schema
	For    Object

	// The builder itself
	// These fields are completely derived from the fields above and can be freely manipulated
	// by veneers.
	Package         string
	Name            string
	Properties      []StructField `json:",omitempty"`
	Options         []Option
	Initializations []Assignment `json:",omitempty"`
	VeneerTrail     []string     `json:",omitempty"`
}

func (*Builder) AddToVeneerTrail

func (builder *Builder) AddToVeneerTrail(veneerName string)

func (*Builder) DeepCopy

func (builder *Builder) DeepCopy() Builder

func (*Builder) MakePath

func (builder *Builder) MakePath(builders Builders, pathAsString string) (Path, error)

type BuilderGenerator

type BuilderGenerator struct {
}

func (*BuilderGenerator) FromAST

func (generator *BuilderGenerator) FromAST(schemas Schemas) []Builder

type Builders

type Builders []Builder

func (Builders) LocateByObject

func (builders Builders) LocateByObject(pkg string, name string) (Builder, bool)

type ComposableSlotType

type ComposableSlotType struct {
	Variant SchemaVariant
}

func (ComposableSlotType) DeepCopy

func (slot ComposableSlotType) DeepCopy() ComposableSlotType

type DisjunctionType

type DisjunctionType struct {
	Branches Types

	// If the branches are references to structs, some languages will need
	// extra context to be able to distinguish between them. Golang, for
	// example, doesn't support sum types (disjunctions of fixed types).
	// To emulate sum types for these languages, we need a way to
	// discriminate against every possible type.
	//
	// To do that, we need two things:
	//	- a discriminator: the name of a field that is present in all types.
	//	  The value of which identifies the type being used.
	//  - a mapping: associating a "discriminator value" to a type.
	Discriminator        string            `json:",omitempty"`
	DiscriminatorMapping map[string]string `json:",omitempty"`
}

func (DisjunctionType) DeepCopy

func (t DisjunctionType) DeepCopy() DisjunctionType

type EnumType

type EnumType struct {
	Values []EnumValue // possible values. Value types might be different
}

func (EnumType) DeepCopy

func (t EnumType) DeepCopy() EnumType

type EnumValue

type EnumValue struct {
	Type  Type
	Name  string
	Value any
}

func (EnumValue) DeepCopy

func (t EnumValue) DeepCopy() EnumValue

type EnvelopeFieldValue

type EnvelopeFieldValue struct {
	Path  Path            // where to assign within the struct/ref
	Value AssignmentValue // what to assign
}

func (*EnvelopeFieldValue) DeepCopy

func (value *EnvelopeFieldValue) DeepCopy() EnvelopeFieldValue

type IntersectionType

type IntersectionType struct {
	Branches []Type
}

func (IntersectionType) DeepCopy

func (inter IntersectionType) DeepCopy() IntersectionType

type JenniesHints

type JenniesHints map[string]any

meant to be used by jennies, to gain a finer control on the codegen from schemas

type Kind

type Kind string
const (
	KindDisjunction Kind = "disjunction"
	KindRef         Kind = "ref"

	KindStruct Kind = "struct"
	KindEnum   Kind = "enum"
	KindMap    Kind = "map"

	KindArray Kind = "array"

	KindScalar       Kind = "scalar"
	KindIntersection Kind = "intersection"

	KindComposableSlot Kind = "composable_slot"
)

type MapType

type MapType struct {
	IndexType Type
	ValueType Type
}

func (MapType) DeepCopy

func (t MapType) DeepCopy() MapType

type Object

type Object struct {
	Name        string
	Comments    []string `json:",omitempty"`
	Type        Type
	SelfRef     RefType
	PassesTrail []string `json:",omitempty"`
}

named declaration of a type

func NewObject

func NewObject(pkg string, name string, objectType Type, passesTrail ...string) Object

func (*Object) AddToPassesTrail

func (object *Object) AddToPassesTrail(trail string)

func (Object) DeepCopy

func (object Object) DeepCopy() Object

func (Object) Equal

func (object Object) Equal(other Object) bool

type Op

type Op string
const (
	MinLengthOp        Op = "minLength"
	MaxLengthOp        Op = "maxLength"
	MultipleOfOp       Op = "multipleOf"
	EqualOp            Op = "=="
	NotEqualOp         Op = "!="
	LessThanOp         Op = "<"
	LessThanEqualOp    Op = "<="
	GreaterThanOp      Op = ">"
	GreaterThanEqualOp Op = ">="
)

type Option

type Option struct {
	Name             string
	Comments         []string `json:",omitempty"`
	VeneerTrail      []string `json:",omitempty"`
	Args             []Argument
	Assignments      []Assignment
	Default          *OptionDefault `json:",omitempty"`
	IsConstructorArg bool
}

func (*Option) AddToVeneerTrail

func (opt *Option) AddToVeneerTrail(veneerName string)

func (*Option) DeepCopy

func (opt *Option) DeepCopy() Option

type OptionDefault

type OptionDefault struct {
	ArgsValues []any
}

type Path

type Path []PathItem

func PathFromStructField

func PathFromStructField(field StructField) Path

func (Path) Append

func (path Path) Append(suffix Path) Path

func (Path) DeepCopy

func (path Path) DeepCopy() Path

func (Path) Last

func (path Path) Last() PathItem

func (Path) String

func (path Path) String() string

type PathItem

type PathItem struct {
	Identifier string
	Type       Type // any
	// useful mostly for composability purposes, when a field Type is "any"
	// and we're trying to "compose in" something of a known type.
	TypeHint *Type `json:",omitempty"`
}

func (PathItem) DeepCopy

func (item PathItem) DeepCopy() PathItem

type RefType

type RefType struct {
	ReferredPkg  string `yaml:"referred_pkg"`
	ReferredType string `yaml:"referred_type"`
}

func (RefType) DeepCopy

func (t RefType) DeepCopy() RefType

func (RefType) String

func (t RefType) String() string

type ScalarKind

type ScalarKind string
const (
	KindNull   ScalarKind = "null"
	KindAny    ScalarKind = "any"
	KindBytes  ScalarKind = "bytes"
	KindString ScalarKind = "string"

	KindFloat32 ScalarKind = "float32"
	KindFloat64 ScalarKind = "float64"

	KindUint8  ScalarKind = "uint8"
	KindUint16 ScalarKind = "uint16"
	KindUint32 ScalarKind = "uint32"
	KindUint64 ScalarKind = "uint64"
	KindInt8   ScalarKind = "int8"
	KindInt16  ScalarKind = "int16"
	KindInt32  ScalarKind = "int32"
	KindInt64  ScalarKind = "int64"

	KindBool ScalarKind = "bool"
)

type ScalarType

type ScalarType struct {
	ScalarKind  ScalarKind       `yaml:"scalar_kind"` // bool, bytes, string, int, float, ...
	Value       any              `json:",omitempty"`  // if value isn't nil, we're representing a constant scalar
	Constraints []TypeConstraint `json:",omitempty"`
}

func (ScalarType) DeepCopy

func (scalarType ScalarType) DeepCopy() ScalarType

func (ScalarType) IsConcrete

func (scalarType ScalarType) IsConcrete() bool

type Schema

type Schema struct {
	Package    string
	Metadata   SchemaMeta `json:",omitempty"`
	EntryPoint string     `json:",omitempty"`
	Objects    *orderedmap.Map[string, Object]
}

func NewSchema

func NewSchema(pkg string, metadata SchemaMeta) *Schema

func (*Schema) AddObject

func (schema *Schema) AddObject(object Object)

func (*Schema) AddObjects

func (schema *Schema) AddObjects(objects ...Object)

func (*Schema) DeepCopy

func (schema *Schema) DeepCopy() Schema

func (*Schema) LocateObject

func (schema *Schema) LocateObject(name string) (Object, bool)

func (*Schema) Merge

func (schema *Schema) Merge(other *Schema) error

func (*Schema) Resolve

func (schema *Schema) Resolve(typeDef Type) (Type, bool)

type SchemaKind

type SchemaKind string
const (
	SchemaKindCore       SchemaKind = "core"
	SchemaKindComposable SchemaKind = "composable"
)

type SchemaMeta

type SchemaMeta struct {
	Kind       SchemaKind    `json:",omitempty"`
	Variant    SchemaVariant `json:",omitempty"`
	Identifier string        `json:",omitempty"`
}

func (SchemaMeta) Equal

func (meta SchemaMeta) Equal(other SchemaMeta) bool

type SchemaVariant

type SchemaVariant string
const (
	SchemaVariantPanel     SchemaVariant = "panelcfg"
	SchemaVariantDataQuery SchemaVariant = "dataquery"
)

type Schemas

type Schemas []*Schema

func (Schemas) Consolidate

func (schemas Schemas) Consolidate() (Schemas, error)

func (Schemas) DeepCopy

func (schemas Schemas) DeepCopy() []*Schema

func (Schemas) LocateObject

func (schemas Schemas) LocateObject(pkg string, name string) (Object, bool)

type StructField

type StructField struct {
	Name        string
	Comments    []string `json:",omitempty"`
	Type        Type
	Required    bool
	PassesTrail []string `json:",omitempty"`
}

func NewStructField

func NewStructField(name string, fieldType Type, opts ...StructFieldOption) StructField

func (*StructField) AddToPassesTrail

func (structField *StructField) AddToPassesTrail(trail string)

func (StructField) DeepCopy

func (structField StructField) DeepCopy() StructField

type StructFieldOption

type StructFieldOption func(field *StructField)

func Comments

func Comments(comments []string) StructFieldOption

func PassesTrail

func PassesTrail(trail string) StructFieldOption

func Required

func Required() StructFieldOption

type StructType

type StructType struct {
	Fields []StructField
}

func (StructType) DeepCopy

func (structType StructType) DeepCopy() StructType

func (StructType) FieldByName

func (structType StructType) FieldByName(name string) (StructField, bool)

type Type

type Type struct {
	Kind     Kind
	Nullable bool
	Default  any `json:",omitempty"`

	Disjunction    *DisjunctionType    `json:",omitempty"`
	Array          *ArrayType          `json:",omitempty"`
	Enum           *EnumType           `json:",omitempty"`
	Map            *MapType            `json:",omitempty"`
	Struct         *StructType         `json:",omitempty"`
	Ref            *RefType            `json:",omitempty"`
	Scalar         *ScalarType         `json:",omitempty"`
	Intersection   *IntersectionType   `json:",omitempty"`
	ComposableSlot *ComposableSlotType `json:",omitempty" yaml:"composable_slot"`

	Hints       JenniesHints `json:",omitempty"`
	PassesTrail []string     `json:",omitempty"`
}

Struct representing every type defined by the IR. Bonus: in a way that can be (un)marshaled to/from JSON, which is useful for unit tests.

func Any

func Any() Type

func Bool

func Bool(opts ...TypeOption) Type

func Bytes

func Bytes(opts ...TypeOption) Type

func NewArray

func NewArray(valueType Type, opts ...TypeOption) Type

func NewComposableSlot

func NewComposableSlot(variant SchemaVariant) Type

func NewDisjunction

func NewDisjunction(branches Types, opts ...TypeOption) Type

func NewEnum

func NewEnum(values []EnumValue, opts ...TypeOption) Type

func NewIntersection

func NewIntersection(branches []Type) Type

func NewMap

func NewMap(indexType Type, valueType Type, opts ...TypeOption) Type

func NewRef

func NewRef(referredPkg string, referredTypeName string, opts ...TypeOption) Type

func NewScalar

func NewScalar(kind ScalarKind, opts ...TypeOption) Type

func NewStruct

func NewStruct(fields ...StructField) Type

func Null

func Null() Type

func String

func String(opts ...TypeOption) Type

func (*Type) AddToPassesTrail

func (t *Type) AddToPassesTrail(trail string)

func (Type) AsArray

func (t Type) AsArray() ArrayType

func (Type) AsComposableSlot

func (t Type) AsComposableSlot() ComposableSlotType

func (Type) AsDisjunction

func (t Type) AsDisjunction() DisjunctionType

func (Type) AsEnum

func (t Type) AsEnum() EnumType

func (Type) AsIntersection

func (t Type) AsIntersection() IntersectionType

func (Type) AsMap

func (t Type) AsMap() MapType

func (Type) AsRef

func (t Type) AsRef() RefType

func (Type) AsScalar

func (t Type) AsScalar() ScalarType

func (Type) AsStruct

func (t Type) AsStruct() StructType

func (Type) DeepCopy

func (t Type) DeepCopy() Type

func (Type) HasHint

func (t Type) HasHint(hintName string) bool

func (Type) ImplementedVariant

func (t Type) ImplementedVariant() string

func (Type) ImplementsVariant

func (t Type) ImplementsVariant() bool

func (Type) IsAny

func (t Type) IsAny() bool

func (Type) IsAnyOf

func (t Type) IsAnyOf(kinds ...Kind) bool

func (Type) IsArray

func (t Type) IsArray() bool

func (Type) IsComposableSlot

func (t Type) IsComposableSlot() bool

func (Type) IsConcreteScalar

func (t Type) IsConcreteScalar() bool

func (Type) IsDisjunction

func (t Type) IsDisjunction() bool

func (Type) IsEnum

func (t Type) IsEnum() bool

func (Type) IsIntersection

func (t Type) IsIntersection() bool

func (Type) IsMap

func (t Type) IsMap() bool

func (Type) IsNull

func (t Type) IsNull() bool

func (Type) IsRef

func (t Type) IsRef() bool

func (Type) IsScalar

func (t Type) IsScalar() bool

func (Type) IsStruct

func (t Type) IsStruct() bool

func (Type) IsStructGeneratedFromDisjunction

func (t Type) IsStructGeneratedFromDisjunction() bool

func (Type) IsStructOrRef

func (t Type) IsStructOrRef() bool

type TypeConstraint

type TypeConstraint struct {
	Op   Op
	Args []any
}

func (TypeConstraint) DeepCopy

func (constraint TypeConstraint) DeepCopy() TypeConstraint

type TypeOption

type TypeOption func(def *Type)

func Default

func Default(value any) TypeOption

func Discriminator

func Discriminator(discriminator string, mapping map[string]string) TypeOption

func Hints

func Hints(hints JenniesHints) TypeOption

func Nullable

func Nullable() TypeOption

func Trail

func Trail(trail string) TypeOption

func Value

func Value(value any) TypeOption

type Types

type Types []Type

func (Types) HasNullType

func (types Types) HasNullType() bool

func (Types) HasOnlyRefs

func (types Types) HasOnlyRefs() bool

func (Types) HasOnlyScalarOrArray

func (types Types) HasOnlyScalarOrArray() bool

func (Types) NonNullTypes

func (types Types) NonNullTypes() Types

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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