Documentation
¶
Overview ¶
Package playground evaluates expressions for the language playground, using the same entry points a gomplate caller uses so what the playground shows is what production does.
A host embeds this to give its own authors a playground over its own language: Options carries the CEL options and template functions the host registers, so the catalogue, the highlighting and the evaluator all agree with what that binary can actually run.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EvalError ¶
type EvalError struct {
Message string `json:"message"`
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
}
EvalError carries a message and, where the compiler reports one, a source position so the editor can place a marker on the offending token.
type Example ¶
type Example struct {
Name string `json:"name"`
Language Language `json:"language"`
Source string `json:"source"`
Input string `json:"input"`
}
Example is one sample an author can load into the playground.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler serves the playground API.
It carries no authentication of its own, deliberately. /api/eval runs arbitrary expressions with whatever Options grants them: in a host whose functions reach a database or a repository, that is arbitrary execution against real data. Mount it behind the same authorization as any other query endpoint. Handler.Mux satisfies http.Handler, so echo hosts wrap it with echo.WrapHandler inside an already-authenticated group.
func NewHandler ¶
NewHandler builds the API over a freshly extracted spec, so a running playground reflects the current binary rather than a stale generated file.
The spec is extracted once, against context.Background(): Options.CelEnvs is a per-request factory because a function's *binding* closes over a request's context, but the declarations it registers -- the names, overloads and types the editor completes from -- are the same for every request.
type Options ¶
type Options struct {
// CelEnvs are layered onto gomplate's own CEL options, per evaluation.
CelEnvs func(context.Context) []cel.EnvOption
// Functions are exposed to both CEL and go templates. Note gomplate's
// constraint: a CEL-visible entry must be a `func() any`; anything else
// belongs in CelEnvs.
Functions func(context.Context) map[string]any
// Examples are the samples the playground offers to load.
Examples []Example
// Timeout bounds one evaluation. Zero means no bound.
//
// It bounds the *response*, not the work: gomplate honours no context
// deadline while evaluating -- there is no cel.ContextEval and no deadline
// check in RunTemplateContext -- so a runaway expression keeps its
// goroutine after the caller has been answered. That is still the right
// trade for a shared endpoint, where a hung request is the worse failure,
// but it is not cancellation and should not be mistaken for it.
Timeout time.Duration
}
Options configure a playground for one host.
The two function fields are shaped as factories rather than plain slices to match how hosts already register: duty keeps `map[string]func(Context) cel.EnvOption`, because a function like `catalog.query` closes over the database handle it queries through.
type Request ¶
type Request struct {
Language Language `json:"language"`
Source string `json:"source"`
// Input is the evaluation environment, as YAML or JSON. JSON is valid YAML,
// so one parser covers both.
Input string `json:"input,omitempty"`
// LeftDelim and RightDelim override the go-template delimiters. Both must
// be set together.
LeftDelim string `json:"leftDelim,omitempty"`
RightDelim string `json:"rightDelim,omitempty"`
}
Request is one evaluation.
type Response ¶
type Response struct {
// Result is the value rendered as a string, as a gomplate caller sees it.
Result string `json:"result"`
// Value is the native result, so the playground can show typed JSON rather
// than a stringified value.
Value any `json:"value,omitempty"`
// Type names the Go type of Value, which is what makes CEL's int/uint/
// double distinction visible.
Type string `json:"type,omitempty"`
// Error is set when evaluation failed. Result is empty in that case.
Error *EvalError `json:"error,omitempty"`
// DurationMs is wall-clock evaluation time.
DurationMs float64 `json:"durationMs"`
}
Response is the result of an evaluation.