shape

package
v1.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: MIT Imports: 9 Imported by: 0

README

x/shape

Package provides representation of types.

It's use in mkunion for example to extract union types and generate TypeScript schema for them. Such schema generation is useful in building application that have end-to-end type safety.

Documentation

Overview

Code generated by mkunion. DO NOT EDIT.

Code generated by mkunion. DO NOT EDIT.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GuardSchemaDef

func GuardSchemaDef() *schema.UnionVariants[Guard]

func InjectPkgName

func InjectPkgName(pkgImportName, pkgName string) func(x Shape)

func MatchGuard

func MatchGuard[TOut any](
	x Guard,
	f1 func(x *Regexp) TOut,
	f2 func(x *Between) TOut,
	f3 func(x *AndGuard) TOut,
	f4 func(x *OrGuard) TOut,
	df func(x Guard) TOut,
) TOut

func MatchGuardR2

func MatchGuardR2[TOut1, TOut2 any](
	x Guard,
	f1 func(x *Regexp) (TOut1, TOut2),
	f2 func(x *Between) (TOut1, TOut2),
	f3 func(x *AndGuard) (TOut1, TOut2),
	f4 func(x *OrGuard) (TOut1, TOut2),
	df func(x Guard) (TOut1, TOut2),
) (TOut1, TOut2)

func MatchShape

func MatchShape[TOut any](
	x Shape,
	f1 func(x *Any) TOut,
	f2 func(x *RefName) TOut,
	f3 func(x *BooleanLike) TOut,
	f4 func(x *StringLike) TOut,
	f5 func(x *NumberLike) TOut,
	f6 func(x *ListLike) TOut,
	f7 func(x *MapLike) TOut,
	f8 func(x *StructLike) TOut,
	f9 func(x *UnionLike) TOut,
	df func(x Shape) TOut,
) TOut

func MatchShapeR2

func MatchShapeR2[TOut1, TOut2 any](
	x Shape,
	f1 func(x *Any) (TOut1, TOut2),
	f2 func(x *RefName) (TOut1, TOut2),
	f3 func(x *BooleanLike) (TOut1, TOut2),
	f4 func(x *StringLike) (TOut1, TOut2),
	f5 func(x *NumberLike) (TOut1, TOut2),
	f6 func(x *ListLike) (TOut1, TOut2),
	f7 func(x *MapLike) (TOut1, TOut2),
	f8 func(x *StructLike) (TOut1, TOut2),
	f9 func(x *UnionLike) (TOut1, TOut2),
	df func(x Shape) (TOut1, TOut2),
) (TOut1, TOut2)

func MustMatchGuard

func MustMatchGuard[TOut any](
	x Guard,
	f1 func(x *Regexp) TOut,
	f2 func(x *Between) TOut,
	f3 func(x *AndGuard) TOut,
	f4 func(x *OrGuard) TOut,
) TOut

func MustMatchGuardR0

func MustMatchGuardR0(
	x Guard,
	f1 func(x *Regexp),
	f2 func(x *Between),
	f3 func(x *AndGuard),
	f4 func(x *OrGuard),
)

func MustMatchGuardR2

func MustMatchGuardR2[TOut1, TOut2 any](
	x Guard,
	f1 func(x *Regexp) (TOut1, TOut2),
	f2 func(x *Between) (TOut1, TOut2),
	f3 func(x *AndGuard) (TOut1, TOut2),
	f4 func(x *OrGuard) (TOut1, TOut2),
) (TOut1, TOut2)

func MustMatchShape

func MustMatchShape[TOut any](
	x Shape,
	f1 func(x *Any) TOut,
	f2 func(x *RefName) TOut,
	f3 func(x *BooleanLike) TOut,
	f4 func(x *StringLike) TOut,
	f5 func(x *NumberLike) TOut,
	f6 func(x *ListLike) TOut,
	f7 func(x *MapLike) TOut,
	f8 func(x *StructLike) TOut,
	f9 func(x *UnionLike) TOut,
) TOut

func MustMatchShapeR0

func MustMatchShapeR0(
	x Shape,
	f1 func(x *Any),
	f2 func(x *RefName),
	f3 func(x *BooleanLike),
	f4 func(x *StringLike),
	f5 func(x *NumberLike),
	f6 func(x *ListLike),
	f7 func(x *MapLike),
	f8 func(x *StructLike),
	f9 func(x *UnionLike),
)

func MustMatchShapeR2

func MustMatchShapeR2[TOut1, TOut2 any](
	x Shape,
	f1 func(x *Any) (TOut1, TOut2),
	f2 func(x *RefName) (TOut1, TOut2),
	f3 func(x *BooleanLike) (TOut1, TOut2),
	f4 func(x *StringLike) (TOut1, TOut2),
	f5 func(x *NumberLike) (TOut1, TOut2),
	f6 func(x *ListLike) (TOut1, TOut2),
	f7 func(x *MapLike) (TOut1, TOut2),
	f8 func(x *StructLike) (TOut1, TOut2),
	f9 func(x *UnionLike) (TOut1, TOut2),
) (TOut1, TOut2)

func ShapeSchemaDef

func ShapeSchemaDef() *schema.UnionVariants[Shape]

func ToStr

func ToStr(x Shape) string

func ToTypeScript

func ToTypeScript(x Shape, option *TypeScriptOptions) string

Types

type AndGuard

type AndGuard struct {
	L []Guard
}

func (*AndGuard) AcceptGuard

func (r *AndGuard) AcceptGuard(v GuardVisitor) any

type Any

type Any struct{}

func (*Any) AcceptShape

func (r *Any) AcceptShape(v ShapeVisitor) any

type Between

type Between struct {
	Min int
	Max int
}

func (*Between) AcceptGuard

func (r *Between) AcceptGuard(v GuardVisitor) any

type BooleanLike

type BooleanLike struct{}

func (*BooleanLike) AcceptShape

func (r *BooleanLike) AcceptShape(v ShapeVisitor) any

type FieldLike

type FieldLike struct {
	Name string
	Type Shape
}

type Guard

type Guard interface {
	AcceptGuard(g GuardVisitor) any
}

type GuardVisitor

type GuardVisitor interface {
	VisitRegexp(v *Regexp) any
	VisitBetween(v *Between) any
	VisitAndGuard(v *AndGuard) any
	VisitOrGuard(v *OrGuard) any
}

type ListLike

type ListLike struct {
	//Extend *ListLike
	//Guard  Guard
	Element Shape
}

func (*ListLike) AcceptShape

func (r *ListLike) AcceptShape(v ShapeVisitor) any

type MapLike

type MapLike struct {
	//Extend *MapLike
	//Guard  Guard
	Key Shape
	Val Shape
}

func (*MapLike) AcceptShape

func (r *MapLike) AcceptShape(v ShapeVisitor) any

type NumberLike

type NumberLike struct {
}

func (*NumberLike) AcceptShape

func (r *NumberLike) AcceptShape(v ShapeVisitor) any

type OrGuard

type OrGuard struct {
	L []Guard
}

func (*OrGuard) AcceptGuard

func (r *OrGuard) AcceptGuard(v GuardVisitor) any

type RefName

type RefName struct {
	Name          string
	PkgName       string
	PkgImportName string
}

func (*RefName) AcceptShape

func (r *RefName) AcceptShape(v ShapeVisitor) any

type Regexp

type Regexp struct {
	Regexp string
}

func (*Regexp) AcceptGuard

func (r *Regexp) AcceptGuard(v GuardVisitor) any

type Shape

type Shape interface {
	AcceptShape(g ShapeVisitor) any
}

func FromAst

func FromAst(x any, fx ...func(x Shape)) Shape

func FromGo

func FromGo(x any) Shape

func FromGoReflect

func FromGoReflect(x reflect.Type, infiniteRecursionFix map[string]Shape) Shape

type ShapeVisitor

type ShapeVisitor interface {
	VisitAny(v *Any) any
	VisitRefName(v *RefName) any
	VisitBooleanLike(v *BooleanLike) any
	VisitStringLike(v *StringLike) any
	VisitNumberLike(v *NumberLike) any
	VisitListLike(v *ListLike) any
	VisitMapLike(v *MapLike) any
	VisitStructLike(v *StructLike) any
	VisitUnionLike(v *UnionLike) any
}

type StringLike

type StringLike struct {
}

func (*StringLike) AcceptShape

func (r *StringLike) AcceptShape(v ShapeVisitor) any

type StructLike

type StructLike struct {
	Name          string
	PkgName       string
	PkgImportName string
	Fields        []*FieldLike
}

func (*StructLike) AcceptShape

func (r *StructLike) AcceptShape(v ShapeVisitor) any

type TypeScriptOptions

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

func (*TypeScriptOptions) IsCurrentPkgName

func (o *TypeScriptOptions) IsCurrentPkgName(pkgName string) bool

func (*TypeScriptOptions) NeedsToImportPkgName

func (o *TypeScriptOptions) NeedsToImportPkgName(pkg packageName, imp packageImportName)

type TypeScriptRenderer

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

func NewTypeScriptRenderer

func NewTypeScriptRenderer() *TypeScriptRenderer

func (*TypeScriptRenderer) AddStruct

func (r *TypeScriptRenderer) AddStruct(like *StructLike)

func (*TypeScriptRenderer) AddUnion

func (r *TypeScriptRenderer) AddUnion(x *UnionLike)

func (*TypeScriptRenderer) WriteToDir

func (r *TypeScriptRenderer) WriteToDir(dir string) error

type UnionLike

type UnionLike struct {
	Name          string
	PkgName       string
	PkgImportName string
	Variant       []Shape
}

func (*UnionLike) AcceptShape

func (r *UnionLike) AcceptShape(v ShapeVisitor) any

Jump to

Keyboard shortcuts

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