schema

package
v1.37.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: MPL-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package schema implements parsing of Go types into Encore's schema format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BuiltinKind

type BuiltinKind int
const (
	Invalid BuiltinKind = iota
	Any
	Bool

	Int
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64

	Float32
	Float64
	String
	Bytes

	Time
	UUID
	JSON
	UserID
	Error // builtin "error" type, for convenience

)

func (BuiltinKind) String

func (i BuiltinKind) String() string

type BuiltinType

type BuiltinType struct {
	AST  ast.Expr
	Kind BuiltinKind
}

func (BuiltinType) ASTExpr

func (t BuiltinType) ASTExpr() ast.Expr

func (BuiltinType) Family

func (BuiltinType) Family() TypeFamily

func (BuiltinType) String

func (t BuiltinType) String() string

type Decl

type Decl interface {
	Kind() DeclKind

	DeclaredIn() *pkginfo.File
	// PkgName reports the name if this is a package-level declaration.
	// Otherwise it reports None.
	PkgName() option.Option[string]

	// ASTNode returns the AST node that this declaration represents.
	// It's a *ast.FuncDecl or *ast.TypeSpec.
	ASTNode() ast.Node
	// String returns the shorthand name for this declaration,
	// in the form "pkgname.DeclName".
	String() string
	// TypeParameters are the type parameters on this declaration.
	TypeParameters() []DeclTypeParam
}

Decl is the common interface for different kinds of declarations.

type DeclKind

type DeclKind int

DeclKind represents different kinds of declarations.

const (
	// DeclType represents a type declaration.
	DeclType DeclKind = iota
	// DeclFunc represents a func declaration.
	DeclFunc
)

type DeclTypeParam

type DeclTypeParam struct {
	// AST is the AST node that this type param represents.
	// Note that multiple fields may share the same *ast.Field node,
	// in cases with multiple names, like "type Foo[A, B any]".
	AST *ast.Field

	Name string // the identifier given to the type parameter.
}

DeclTypeParam represents a type parameter on a declaration. For example A in "type Foo[A any] struct { ... }"

type FuncDecl

type FuncDecl struct {
	AST *ast.FuncDecl

	File *pkginfo.File            // the file declaring the type
	Name string                   // the name of the function
	Recv option.Option[*Receiver] // none if not a method
	Type FuncType                 // signature

	// TypeParams are any type parameters on this declaration.
	// (note: instantiated types used within this declaration would not be captured here)
	TypeParams []DeclTypeParam
}

A FuncDecl represents a function declaration.

func (*FuncDecl) ASTNode

func (d *FuncDecl) ASTNode() ast.Node

func (*FuncDecl) DeclaredIn

func (d *FuncDecl) DeclaredIn() *pkginfo.File

func (*FuncDecl) Kind

func (*FuncDecl) Kind() DeclKind

func (*FuncDecl) PkgName

func (d *FuncDecl) PkgName() option.Option[string]

func (*FuncDecl) String

func (d *FuncDecl) String() string

func (*FuncDecl) TypeParameters

func (d *FuncDecl) TypeParameters() []DeclTypeParam

type FuncType

type FuncType struct {
	AST     *ast.FuncType
	Params  []Param
	Results []Param
}

func (FuncType) ASTExpr

func (t FuncType) ASTExpr() ast.Expr

func (FuncType) Family

func (FuncType) Family() TypeFamily

func (FuncType) String

func (t FuncType) String() string

type InterfaceType

type InterfaceType struct {
	AST *ast.InterfaceType

	// EmbeddedIfaces are the interfaces this interface embeds.
	EmbeddedIfaces []Type

	// TODO change these out for more useful information.
	TypeLists []ast.Expr
	Methods   []*ast.Field
}

func (InterfaceType) ASTExpr

func (t InterfaceType) ASTExpr() ast.Expr

func (InterfaceType) Family

func (InterfaceType) Family() TypeFamily

func (InterfaceType) String

func (t InterfaceType) String() string

type ListType

type ListType struct {
	AST  *ast.ArrayType
	Len  int // -1 for a slice
	Elem Type
}

func (ListType) ASTExpr

func (t ListType) ASTExpr() ast.Expr

func (ListType) Family

func (ListType) Family() TypeFamily

func (ListType) String

func (t ListType) String() string

type MapType

type MapType struct {
	AST   *ast.MapType
	Key   Type
	Value Type
}

func (MapType) ASTExpr

func (t MapType) ASTExpr() ast.Expr

func (MapType) Family

func (MapType) Family() TypeFamily

func (MapType) String

func (t MapType) String() string

type NamedType

type NamedType struct {
	AST ast.Expr // *ast.Ident or *ast.SelectorExpr

	// DeclInfo is the declaration info for the declaration
	// this refers to.
	DeclInfo *pkginfo.PkgDeclInfo

	// TypeArgs are the type arguments used to instantiate this named type.
	TypeArgs []Type
	// contains filtered or unexported fields
}

func (NamedType) ASTExpr

func (t NamedType) ASTExpr() ast.Expr

func (NamedType) Decl

func (t NamedType) Decl() *TypeDecl

func (NamedType) Family

func (NamedType) Family() TypeFamily

func (NamedType) String

func (t NamedType) String() string

func (NamedType) WithDecl

func (t NamedType) WithDecl(decl *TypeDecl) NamedType

type Param

type Param struct {
	AST  *ast.Field
	Name option.Option[string] // parameter name, or None if a type-only parameter.
	Type Type
}

Param represents a parameter or result field.

type Parser

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

Parser parses Go types into Encore's schema format.

func NewParser

func NewParser(c *parsectx.Context, l *pkginfo.Loader) *Parser

NewParser constructs a new schema parser.

func (*Parser) ParseFuncDecl

func (p *Parser) ParseFuncDecl(file *pkginfo.File, fd *ast.FuncDecl) (*FuncDecl, bool)

ParseFuncDecl parses the func from a package declaration. It errors if the type is not a func declaration.

func (*Parser) ParseType

func (p *Parser) ParseType(file *pkginfo.File, expr ast.Expr) Type

ParseType parses the schema from a type expression.

func (*Parser) ParseTypeDecl

func (p *Parser) ParseTypeDecl(d *pkginfo.PkgDeclInfo) *TypeDecl

ParseTypeDecl parses the type from a package declaration. It errors if the declaration is not a type.

type PointerType

type PointerType struct {
	AST  *ast.StarExpr
	Elem Type
}

func (PointerType) ASTExpr

func (t PointerType) ASTExpr() ast.Expr

func (PointerType) Family

func (PointerType) Family() TypeFamily

func (PointerType) String

func (t PointerType) String() string

type Receiver

type Receiver struct {
	AST *ast.FieldList
	// Name is the name of the receiver (e.g. "a" in "func (a *Foo) Bar()").
	// It's None if the receiver is unnamed (e.g. "func (*Foo) Bar()").
	Name option.Option[string]

	// Type is the type of the receiver.
	// It's either a NamedType or a NamedType wrapped in a PointerType.
	Type Type

	// Decl is the underlying type declaration the receiver points to.
	Decl *TypeDecl
}

A Receiver represents a method receiver. It describes the name and type of the receiver.

type StructField

type StructField struct {
	// AST is the AST node that this field represents.
	// Note that multiple fields may share the same *ast.Field node,
	// in cases with multiple names, like "Foo, Bar int".
	AST *ast.Field

	Name option.Option[string] // field name, or None if anonymous
	Type Type
	Doc  string
	Tag  structtag.Tags
}

func (*StructField) IsAnonymous

func (f *StructField) IsAnonymous() bool

func (*StructField) IsExported

func (f *StructField) IsExported() bool

type StructType

type StructType struct {
	AST    *ast.StructType
	Fields []StructField
}

func (StructType) ASTExpr

func (t StructType) ASTExpr() ast.Expr

func (StructType) Family

func (StructType) Family() TypeFamily

func (StructType) String

func (t StructType) String() string

type Type

type Type interface {
	Family() TypeFamily
	ASTExpr() ast.Expr
	String() string // Resolve to a string representation of the type.
}

type TypeDecl

type TypeDecl struct {
	// AST is the AST node that this declaration represents.
	AST *ast.TypeSpec

	Info *pkginfo.PkgDeclInfo // the underlying declaration info
	File *pkginfo.File        // the file declaring the type
	Name string               // name of the type declaration
	Type Type                 // the declaration's underlying type

	// TypeParams are any type parameters on this declaration.
	// (note: instantiated types used within this declaration would not be captured here)
	TypeParams []DeclTypeParam
}

TypeDecl represents a type declaration.

func (*TypeDecl) ASTNode

func (d *TypeDecl) ASTNode() ast.Node

func (*TypeDecl) Clone

func (d *TypeDecl) Clone() *TypeDecl

func (*TypeDecl) DeclaredIn

func (d *TypeDecl) DeclaredIn() *pkginfo.File

func (*TypeDecl) Kind

func (*TypeDecl) Kind() DeclKind

func (*TypeDecl) PkgName

func (d *TypeDecl) PkgName() option.Option[string]

func (*TypeDecl) String

func (d *TypeDecl) String() string

func (*TypeDecl) TypeParameters

func (d *TypeDecl) TypeParameters() []DeclTypeParam

type TypeDeclRef

type TypeDeclRef struct {
	Decl     *TypeDecl
	TypeArgs []Type
	Pointers int
}

TypeDeclRef is a reference to a type declaration, through zero or more pointers and possibly with type arguments.

func (*TypeDeclRef) ToType

func (r *TypeDeclRef) ToType() Type

type TypeFamily

type TypeFamily int
const (
	Unknown TypeFamily = iota
	Named
	Struct
	Map
	List
	Builtin
	Pointer
	Func
	Interface
	TypeParamRef
)

type TypeParamRefType

type TypeParamRefType struct {
	AST *ast.Ident

	Decl  Decl // the declaration this type parameter is defined on
	Index int  // Index into the type parameter slice on the declaration
}

TypeParamRefType is a reference to a `TypeParameter` within a declaration block

func (TypeParamRefType) ASTExpr

func (t TypeParamRefType) ASTExpr() ast.Expr

func (TypeParamRefType) Family

func (TypeParamRefType) Family() TypeFamily

func (TypeParamRefType) String

func (t TypeParamRefType) String() string

Directories

Path Synopsis
Package schematest provides utilities for writing tests that make assertions about schema types and declarations.
Package schematest provides utilities for writing tests that make assertions about schema types and declarations.

Jump to

Keyboard shortcuts

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