Documentation
¶
Overview ¶
Package vuka is the runtime of Vuka programs: Result and Option.
Vuka code writes Result[User], Ok(u), Err(e), Some(x) and None unqualified; the transpiler points them here. Go code uses the package like any other.
Index ¶
- func Arg[T any](c *Call, i int) T
- func As[T any](v any) T
- func AttrOf[T any](c *Call) (T, bool)
- func Handler(f func(r *http.Request) (Node, error)) http.Handler
- func Instance[F any](s *Instances, build func() F) F
- func RegisterTempl(key string, components ...TemplComponent)
- func String(ctx context.Context, n Node) (string, error)
- func Void(tag string) bool
- func Walk(ctx context.Context, n Node, r Renderer) error
- func Write(w http.ResponseWriter, r *http.Request, n Node) error
- type Attr
- type Boundary
- type Builder
- type Call
- type Decl
- type Decorated
- type Decorator
- type Element
- type Field
- type File
- type Func
- type Group
- type HTMLRenderer
- type Instances
- type Node
- func Child(v any) Node
- func El(tag string, attrs []Attr, children ...Node) Node
- func ErrorBoundary(fallback func(err error) Node, children ...Node) Node
- func Fragment(children ...Node) Node
- func Nodes(build func(add func(Node))) Node
- func Safe(html string) Node
- func Text(v any) Node
- func Try(n Node, err error) Node
- type NodeFunc
- type Option
- type Param
- type RawHTML
- type Renderer
- type Result
- type Static
- type Statics
- type Style
- type TemplComponent
- type TextNode
- type TryNode
- type Type
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶ added in v0.2.0
As converts a value held in an any back to T; nil is T's zero value. It panics, naming both types, when a decorator put something else there.
func Handler ¶ added in v0.5.0
Handler serves the Node f builds for each request through templ.Handler: rendered in full with the request's context, then sent as text/html. An error from f or from rendering answers 500 with only the status text.
func Instance ¶
Instance returns the instantiation of a generic decorated function whose type is F, building it the first time.
func RegisterTempl ¶ added in v0.6.0
func RegisterTempl(key string, components ...TemplComponent)
RegisterTempl records the components compiled from a .templ file. Generated code calls it.
func Void ¶ added in v0.5.0
Void reports whether tag is a void element: no children, no closing tag.
func Walk ¶ added in v0.5.0
Walk sends n's tree to r. Groups, Builders and Child conversions are flattened (r never sees them); a TryNode with an error stops the walk with it; a Boundary walks its children into a recording first, and replays it into r — or, when they fail, walks Fallback(err) instead, so r never sees the failed part. Inside a Boundary an opaque Node is rendered while recording and replayed as Opaque of its HTML (a RawHTML). Children on a void element are an error. The first error from r stops the walk.
Types ¶
type Attr ¶ added in v0.2.0
Attr is one attribute of an element. In HTML it renders the way templ renders the same attribute written as an expression:
- nil and false omit it, true writes it bare;
- class takes a string or anything templ.Classes does ([]string, map[string]bool, templ.KV, …);
- style takes a Style or anything templ's style attributes do, sanitized by templ (an unsafe property or value becomes templ's placeholder);
- href on a and link, action on form and data on object are URLs: templ.URL keeps relative URLs and http, https, mailto, tel, ftp and ftps, and turns anything else into about:invalid#TemplFailedSanitizationURL; a templ.SafeURL passes as is. Like templ, other attributes (img src, …) aren't URL-checked;
- a templ.ComponentScript writes its Call; a Node is rendered and escaped; anything else goes through fmt.Sprint and is escaped (templ.ResolveAttributeValue).
className and htmlFor name class and for. Names must be letters, digits, - _ : and . (templ only ever sees literal names; here a dynamic one could smuggle in another attribute), else Render fails.
type Boundary ¶ added in v0.5.0
Boundary renders Children or, when one of them fails, Fallback(err) in place of everything they produced.
type Builder ¶ added in v0.5.0
type Builder func(add func(Node))
Builder produces children as it runs: a loop, if or match block. It runs once per Render or Walk, each added child rendered as it is added.
type Call ¶ added in v0.2.0
type Call struct {
Name string
Receiver any // the receiver, for a method
Args []any // the arguments; a variadic parameter is one slice
Results []any // the results, once Next has run (or Return was called)
// contains filtered or unexported fields
}
Call is one call of a decorated function, passed down its decorators.
func NewCall ¶ added in v0.2.0
NewCall starts a call through chain, ending in invoke. Generated code calls it.
func (*Call) Attr ¶ added in v0.2.0
Attr fills ptr with the declaration's typed attribute of ptr's element type, reporting whether there is one: var r Route; c.Attr(&r).
func (*Call) Context ¶ added in v0.2.0
Context is the call's context.Context argument, or context.Background().
func (*Call) Next ¶ added in v0.2.0
func (c *Call) Next()
Next runs the rest of the chain: the next decorator, or the function itself. It can be called more than once.
func (*Call) Return ¶ added in v0.2.0
Return sets the results without running the function; the rest of the chain is skipped unless Next is called.
type Decl ¶ added in v0.6.0
type Decl struct {
Name string // "main.ShowPet", "PetAdmin.Index"
Pkg string // the package's import path
File string // the source file's base name, and the line of the name, for messages
Line int
Attrs []any // the declaration's typed attributes
// Func is the function, wrapped by the declaration's other decorators, so
// calling it runs them too. For a method it is the method expression:
// func(recv, args…).
Func any
Recv reflect.Type // the receiver type, for a method
Params []Param // the parameters, receiver excluded
Results []reflect.Type
Variadic bool
}
Decl describes a declaration for a declarer: a decorator of type func(d *vuka.Decl), which runs once at program start rather than around each call. Declarers register things — routes, commands, jobs — from the declarations they decorate:
func Get(path string) func(*vuka.Decl) {
return func(d *vuka.Decl) { routes[path] = d.Func }
}
func FuncDecl ¶ added in v0.6.0
FuncDecl describes a function for its declarers, given its parameters' names. Generated code calls it.
func MethodDecl ¶ added in v0.6.0
MethodDecl describes a method, d.Func being its method expression. Generated code calls it.
type Decorated ¶
type Decorated[F any] struct { // contains filtered or unexported fields }
Decorated holds a decorated function, built from its decorators on first use. Vuka generates one per decorated function or method.
type Decorator ¶ added in v0.2.0
type Decorator func(c *Call)
Decorator wraps a call of any function: it runs code around c.Next(), may change c.Args before it, read or replace c.Results after it, call it again (retries) or not at all (caches). Written once, it decorates any function or method:
func logged(c *vuka.Call) {
fmt.Println("calling", c.Name, c.Args)
c.Next()
}
A decorator with parameters is a function returning one; it runs once per decorated function, so state it keeps isn't shared between functions.
type Element ¶ added in v0.5.0
Element is an element: <Tag Attrs>Children</Tag>. A void element (br, img, input, …; see Void) has no closing tag, and children on one are an error; in HTML, html is preceded by the doctype.
Text inside script follows templ's Go expressions in a script: it is JSON-encoded (templ's runtime.ScriptContentOutsideStringLiteral), so it arrives as a quoted JS string with < > & escaped. templ has no expressions inside style; here text in a style element is written as is with "</" written "<\/", so it can't end the element.
type Field ¶ added in v0.3.0
type Field struct {
Name string
Type reflect.Type
Tag reflect.StructTag
Injected bool // a parameter of New: not blank, not tagged inject:"-"
}
Field is a field of a decorated struct.
type File ¶ added in v0.6.0
type File string
File names a file of the program's source tree. A string literal written where a decorator or typed attribute takes a File is checked when the program is compiled (it must exist, in the package's directory or below) and embedded into the binary; the File then holds "importpath:relative/path" and Bytes returns the embedded content:
func Template(view vuka.File) func(*vuka.Decl) { … view.Bytes() … }
@Template("views/pet.html")
func showPet(id int) Pet { … }
For a .templ file, TemplComponents also gives its components. The file may be in a subdirectory that is a package of its own (views/pets.templ with `package views`): its File is then that package's ("module/views:pets.templ") and the generated code imports it.
func (File) Bytes ¶ added in v0.6.0
Bytes is the file's content: embedded, or for a File Vuka didn't embed (a conversion such as vuka.File("x.txt")) read from disk.
type Func ¶ added in v0.2.0
type Func struct {
Name string // "main.charge", "Store.Save"
Attrs []any // the declaration's typed attributes
ErrIndex int // the trailing error result's index, or -1
CtxIndex int // the context.Context argument's index, or -1
Zero func() []any
}
Func describes a decorated function. Vuka generates one per function.
type HTMLRenderer ¶ added in v0.5.0
type HTMLRenderer struct {
// contains filtered or unexported fields
}
HTMLRenderer is the Renderer writing HTML: what Render does. Escaping and sanitizing are templ's (see Attr and Element).
func NewHTMLRenderer ¶ added in v0.5.0
func NewHTMLRenderer(ctx context.Context, w io.Writer) *HTMLRenderer
NewHTMLRenderer is an HTMLRenderer writing to w, unbuffered; ctx renders Node attribute values.
func (*HTMLRenderer) Close ¶ added in v0.5.0
func (h *HTMLRenderer) Close(e *Element) error
func (*HTMLRenderer) Opaque ¶ added in v0.5.0
func (h *HTMLRenderer) Opaque(ctx context.Context, n Node) error
func (*HTMLRenderer) Open ¶ added in v0.5.0
func (h *HTMLRenderer) Open(e *Element) error
func (*HTMLRenderer) Raw ¶ added in v0.5.0
func (h *HTMLRenderer) Raw(html string) error
func (*HTMLRenderer) Text ¶ added in v0.5.0
func (h *HTMLRenderer) Text(s string) error
type Instances ¶
type Instances struct {
// contains filtered or unexported fields
}
Instances holds a generic decorated function's instantiations, each built from its decorators on first use.
type Node ¶ added in v0.5.0
Node is anything that renders HTML: what a JSX expression evaluates to. It is templ.Component itself, so Vuka components are templ components and templ components are Vuka children, with nothing in between.
The Nodes built here (*Element, TextNode, RawHTML, Group, Builder, TryNode, Boundary) are also a tree any Renderer can Walk; any other Node — a templ component, a NodeFunc — is opaque: it can only render HTML. Nodes are immutable values; rendering one concurrently is safe.
func Child ¶ added in v0.5.0
Child is a {expr} child: a Node as is, nil as nothing, an error as its message, a []Node or []any as each element in turn, anything else as Text.
func ErrorBoundary ¶ added in v0.5.0
ErrorBoundary renders children, or — when one of them fails — fallback(err) in place of everything they wrote.
type NodeFunc ¶ added in v0.5.0
type NodeFunc = templ.ComponentFunc
NodeFunc is a function rendering HTML: templ.ComponentFunc.
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option is a value that may be absent. The zero Option is None.
func None ¶
None is an absent value. Vuka code writes plain None; the transpiler fills in T from where the value goes.
func (Option[T]) Unwrap ¶
func (o Option[T]) Unwrap() T
Unwrap is the value; it panics when o is None.
type Param ¶ added in v0.6.0
Param is a parameter of a declared function: its name as written ("" when unnamed) and type.
type Renderer ¶ added in v0.5.0
type Renderer interface {
Open(e *Element) error // before children
Close(e *Element) error // after children (not called for void elements)
Text(s string) error
Raw(html string) error // Safe content
Opaque(ctx context.Context, n Node) error // a component that only renders HTML (templ, NodeFunc)
}
Renderer receives a node tree in document order; Walk drives it. The HTML renderer behind every Node's Render is one; package term is another.
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
Result is a value or an error. It is Ok when it holds no error; the zero Result is Ok with T's zero value.
func (Result[T]) Unwrap ¶
func (r Result[T]) Unwrap() T
Unwrap is the value; it panics when r failed.
type Static ¶ added in v0.2.0
type Static[V any] struct { V V }
Static is the storage of a static field of a generic type: one per instantiation, so Model[User] and Model[Order] each have their own.
type Statics ¶ added in v0.2.0
type Statics struct {
// contains filtered or unexported fields
}
Statics holds one generic static field's instantiations.
type Style ¶ added in v0.5.0
Style is an inline style: style={vuka.Style{"color": "red"}}, rendered by templ as "color:red;" sorted by property, each property and value sanitized.
type TemplComponent ¶ added in v0.6.0
TemplComponent is a component of a .templ file: its name, the generated function (func(params…) templ.Component) and its parameters' names.
func TemplComponents ¶ added in v0.6.0
func TemplComponents(f File) ([]TemplComponent, bool)
TemplComponents are the components of an embedded .templ file, in source order: all of them for a file of the referencing package, the exported ones for a file of a subdirectory's package.
type TryNode ¶ added in v0.5.0
TryNode is a (Node, error) component's result as a child: Err fails the render, else Node renders.
type Type ¶ added in v0.2.0
type Type struct {
Name string // "main.User"
Reflect reflect.Type
Attrs []any // the declaration's typed attributes
New any // the constructor, for a struct; nil otherwise
Fields []Field // the struct's fields
}
Type describes a decorated type, for a type decorator:
func Model(t *vuka.Type) { registry[t.Name] = t.Reflect }
For a struct, New is a constructor taking each dependency field as a parameter (or the type's own static New, when it declares one), which is the shape dependency-injection containers take:
func Component(t *vuka.Type) { providers = append(providers, t.New) }
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
vuka
command
Command vuka transpiles Vuka source to Go and runs the go command over it.
|
Command vuka transpiles Vuka source to Go and runs the go command over it. |
|
vukavet
module
|
|
|
internal
|
|
|
format
Package format formats Vuka source: the Go in it exactly as gofmt does, and its JSX the way Prettier lays out markup, without changing what it renders.
|
Package format formats Vuka source: the Go in it exactly as gofmt does, and its JSX the way Prettier lays out markup, without changing what it renders. |
|
load
Package load finds Vuka packages in a module and supplies what transpiling them needs from the go command: build-context file selection, export data for imports, and the overlay that puts generated files in front of the compiler.
|
Package load finds Vuka packages in a module and supplies what transpiling them needs from the go command: build-context file selection, export data for imports, and the overlay that puts generated files in front of the compiler. |
|
Package templx passes children to templ components.
|
Package templx passes children to templ components. |
|
Package term renders a vuka Node tree as terminal text: headings, paragraphs word-wrapped at a width, lists, tables, quotes, code, links, and inline bold/italic/underline as ANSI styles when Color is on.
|
Package term renders a vuka Node tree as terminal text: headings, paragraphs word-wrapped at a width, lists, tables, quotes, code, links, and inline bold/italic/underline as ANSI styles when Color is on. |
|
Package transpile turns Vuka source into Go.
|
Package transpile turns Vuka source into Go. |
