Documentation
¶
Overview ¶
Package fmtstring resolves Open Job Description (OpenJD) format strings.
OpenJD templates embed format-string references delimited by double braces, for example "{{ Param.FrameStart }}" or "{{Param.FrameStart}}" (surrounding whitespace inside the braces is trimmed, so the two forms are equivalent). Text outside the braces is literal and copied verbatim. There is no backslash escape in OpenJD: every "{{" begins a reference, and a "}}" with no preceding "{{" is literal text.
A reference names a dotted OpenJD identifier such as Param.Foo, RawParam.Foo, Task.Param.Frame, Task.RawParam.Frame, Task.File.script, Env.File.env, Session.WorkingDirectory, Session.HasPathMappingRules, or Session.PathMappingRulesFile. Each dot-separated segment is an OpenJD identifier matching [A-Za-z_][A-Za-z0-9_]*.
The package is a leaf with no dependencies on other internal packages, because both the server (at job submit/expansion time) and the worker (at task-run time) use it. It does not hardcode the available variable namespaces; instead callers supply a Scope that maps fully-qualified names (for example "Task.Param.Frame") to values.
The two core operations are Resolve (substitute every reference) and References (list the names a string refers to without resolving them).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoneRef ¶ added in v0.3.0
LoneRef reports the body when input is exactly one reference with no surrounding text, which is section 1.3.2's condition for a format string to inherit its field's target type rather than being converted to a string.
Surrounding whitespace counts as text: " {{X}}" is a string whose value begins with a space, not a transparent reference.
func References ¶
References returns the distinct variable names referenced by input, in order of first appearance. It returns a *MalformedError if input contains a syntactically invalid reference.
func Resolve ¶
Resolve replaces every "{{ name }}" reference in input with the value returned by scope.Lookup for the trimmed name. Literal text outside braces is copied verbatim.
Resolve returns a *MalformedError if input contains a syntactically invalid reference, and a *UnresolvedError naming every distinct unknown variable if any reference names a variable the scope does not provide.
Types ¶
type MalformedError ¶
type MalformedError struct {
// Ref is the offending reference text, including the surrounding braces
// when they are present (for an unclosed reference it is the text from
// "{{" to the end of the input).
Ref string
// Reason describes why the reference is malformed.
Reason string
}
MalformedError is returned when an input contains a syntactically invalid format-string reference: an unclosed "{{", an empty or whitespace-only name, or a name that is not a valid dotted OpenJD identifier.
func (*MalformedError) Error ¶
func (e *MalformedError) Error() string
type MapScope ¶
MapScope is a Scope backed by a plain map from fully-qualified variable name to value.
type Scope ¶
Scope resolves a fully-qualified OpenJD variable name (for example "Task.Param.Frame") to its string value. Lookup reports ok=false when the name is not present in the scope; callers must distinguish a missing name from a name whose value is the empty string.
func MultiScope ¶
MultiScope composes scopes into a single Scope. Lookup consults the scopes in the order given and returns the value from the first scope that contains the name, so earlier scopes take precedence over later ones.
type Segment ¶ added in v0.3.0
type Segment struct {
// Literal is the text, when IsRef is false.
Literal string
// Ref is the trimmed reference body, when IsRef is true.
Ref string
// IsRef distinguishes the two. A zero Segment is an empty literal.
IsRef bool
}
Segment is one piece of a format string: either a run of literal text or a reference body.
The body is reported verbatim, trimmed of surrounding whitespace but otherwise unjudged. This package does not know what a reference body means: under the base specification it is a dotted identifier (validName), and with the EXPR extension it is an arbitrary expression, and keeping that decision in the caller is what lets one scanner serve both.
func Segments ¶ added in v0.3.0
Segments splits input into literal runs and reference bodies, in order.
It returns a *MalformedError on an unclosed or empty reference, sharing the underlying scanner with Resolve and References (parse and parseRaw both call scan), so all three agree on where a reference starts and ends. They differ on whether a given body is well-formed: Segments reports a body like "Param.1bad" unjudged, where Resolve and References reject it via parse's validName check. That judgment is left to the caller, per the type's doc comment.
type UnresolvedError ¶
type UnresolvedError struct {
Names []string
}
UnresolvedError is returned by Resolve when one or more references name variables that the Scope does not provide. Names lists every distinct unknown variable in order of first appearance.
func (*UnresolvedError) Error ¶
func (e *UnresolvedError) Error() string