generation

package
v0.0.0-...-ff13663 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatAST

func FormatAST(expr ...*Expr) (string, error)

FormatAST formats the AST used to generate typescript into a string.

func MarshalCueValue

func MarshalCueValue(v cue.Value) (string, error)

MarshalCueValue returns a typescript type given a cue value.

func MarshalString

func MarshalString(cuestr string) (string, error)

MarshalString marshals a Cue string into a Typescript type string, returning an error.

Types

type AstKind

type AstKind interface {
	String() string
	// contains filtered or unexported methods
}

ASTKind is a generalization of all elements in our AST.

This is intended for typescript generation only; it's a higher level abstraction than concrete JS syntax, and a single AST node can represent a large swath of JS (eg. an entire enum, or an entire key/value line).

type Binding

type Binding struct {
	IndentLevel int
	Kind        BindingKind
	Members     []AstKind
}

Binding represents a complex type: an array, enum, object, etc.

func (Binding) String

func (b Binding) String() string

type BindingKind

type BindingKind uint8
const (
	// BindingArray represents a plain ol' javascript array with contents:
	// [1, 2, 3, 4].
	BindingArray BindingKind = iota
	// BindingTypedArray represents a TS Array definition: Array<T>.  When used
	// within a Binding, any members are automatically assumed to be bound using
	// a disjunction.  However, the Members field can also contain a single
	// BindingDisjunction with many values - it does the same thing.
	//
	// Examples:
	//
	// 	Binding{
	// 		Kind: BindingTypedArray,
	// 		Members: []AstKind{
	// 			Type{"string"}, // automatically a disjunction.
	// 			Type{"number"},
	// 	     },
	// 	}
	//
	// is equivalent to:
	//
	// 	Binding{
	// 		Kind: BindingTypedArray,
	// 		Members: []AstKind{
	//			Binding{
	//				Kind: BindingDisjunction,
	//				Members: []AstKind{
	// 					Type{"string"},
	// 					Type{"number"},
	// 	     			},
	// 	     		},
	// 	     	},
	// 	}
	//
	BindingTypedArray
	// BindingObject represents a pojo.
	BindingObject
	// BindingType represents an object used for a type.  These use
	// semi-colons as their field terminators.
	BindingType
	// BindingDisjunction represents an ADT enum - values combined
	// with " | "
	BindingDisjunction
)

type Enum

type Enum struct {
	Name    string
	Members []AstKind
}

An Enum is an ADT - a union type within Cue. We special-case enums because of typescript limitations. A pure `enum Foo {...}` in typescript isn't that fun to use; it's recommended by many to create an Object containing the enum values, then use `typeof EnumName[keyof typeof EnumName]` to define the enum.

In effect, this enum is a higher-kind helper for generating Typescript. It creates concrete AST for enums depending on the members that it contains.

func (Enum) ExprAST

func (e Enum) ExprAST() ([]*Expr, error)

ExprAST is used when creating an enum that has concrete values or complex data types. It creates top-level exports using consts and `keyof typeof` to generate enums.

func (Enum) IsScalarType

func (e Enum) IsScalarType() bool

IsScalarType returns true if the enum is made out of basic scalar types, eg. `string | number`, or `string | null`

func (Enum) ScalarAST

func (e Enum) ScalarAST() (AstKind, error)

ScalarAST returns the disjunction AST for basic types. This is used when representing basic type enums. Output:

string | number

func (Enum) String

func (e Enum) String() string

type Expr

type Expr struct {
	Data AstKind
}

Expr represents a single TS expression, such as a type defintion.

TODO (tonyhb): refactor. We probably dont need Expr wrappers, and it makes typescript code gen function signatures a little ugly (see generateStructBinding). We can probably work with Local being the single top-level identifier.

func (Expr) String

func (e Expr) String() string

type KeyValue

type KeyValue struct {
	Key   string
	Value AstKind

	Optional bool
}

KeyValue represents a key and value within a BindingObject or Enum

func (KeyValue) String

func (kv KeyValue) String() string

type Lit

type Lit struct {
	Value string
}

Lit represents literal text, such as newlines, comments, spaces, etc.

func (Lit) String

func (l Lit) String() string

type Local

type Local struct {
	// Kind is the type of definition for the variable.
	Kind LocalKind
	// Name represents the name for the identifier
	Name string
	// Type represents the optional type definition for the identifier.
	// This is valid for const definitions only.
	Type *string
	// IsExport defines whether this identifier should be exported.
	IsExport bool
	// Value is the value that this identifier refers to.  This could be
	// a scalar, a type, a binding, etc.
	Value AstKind

	// AsType records the "as T" suffix for an identifier, eg:
	// "const Foo = 1 as int;
	AsType *string
}

Local is a concrete AST expr which represents a definition for a variable or type.

func (Local) String

func (l Local) String() string

type LocalKind

type LocalKind uint8

LocalKind represents a local variable or type definition

const (
	LocalConst LocalKind = iota
	LocalType
	LocalInterface
)

func (LocalKind) String

func (l LocalKind) String() string

type Scalar

type Scalar struct {
	Value interface{}
}

Scalar represents a scalar value such as a string, number, boolean, etc.

func (Scalar) String

func (s Scalar) String() string

func (Scalar) Unquoted

func (s Scalar) Unquoted() string

type Type

type Type struct {
	Value string
}

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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