Documentation
¶
Index ¶
- func FormatAST(expr ...*Expr) (string, error)
- func GenerateExprs(ctx context.Context, ast []marshalling.ParsedAST) ([]marshalling.Expr, error)
- func MarshalCueValue(v cue.Value) (string, error)
- func MarshalString(cuestr string) (string, error)
- type AstKind
- type Binding
- type BindingKind
- type Enum
- type Expr
- type KeyValue
- type Lit
- type Local
- type LocalKind
- type Scalar
- type Type
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateExprs ¶
func GenerateExprs(ctx context.Context, ast []marshalling.ParsedAST) ([]marshalling.Expr, error)
func MarshalCueValue ¶
MarshalCueValue returns a typescript type given a cue value.
func MarshalString ¶
Types ¶
type AstKind ¶
type AstKind interface { String() string // contains filtered or unexported methods }
ASTKind is a generalization of all elements in our AST
type Binding ¶
type Binding struct { IndentLevel int Kind BindingKind Members []marshalling.Expr }
Binding represents a complex type: an array, enum, object, etc.
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 Simple bool Members []marshalling.Expr }
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.
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.
type KeyValue ¶
type KeyValue struct { Key string Value marshalling.Expr Optional bool }
KeyValue represents a key and value within a BindingObject or Enum
type Lit ¶
type Lit struct {
Value string
}
Lit represents literal text, such as newlines, comments, spaces, etc.
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 marshalling.Expr // 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.