Documentation ¶
Overview ¶
Package vm exposes the capabilities of a Jsonnet VM as a narrow interface. The returned implementation is automatically set up with the native functions and importers that qbec supports and is safe for concurrent use.
Example ¶
package main import ( "fmt" "github.com/splunk/qbec/vm" ) func main() { jvm := vm.New(vm.Config{}) code := ` function (str, num) { foo: str, bar: num, baz: std.extVar('baz'), } ` vs := vm.VariableSet{}. WithTopLevelVars( vm.NewVar("str", "hello"), vm.NewCodeVar("num", "10"), ). WithVars( vm.NewVar("baz", "world"), ) out, err := jvm.EvalCode("inline-code.jsonnet", vm.MakeCode(code), vs) if err != nil { panic(err) } fmt.Println(out) }
Output: { "bar": 10, "baz": "world", "foo": "hello" }
Index ¶
- func ConfigProviderFromVariables(vs VariableSet) datasource.ConfigProvider
- func CreateDataSources(input []string, cp datasource.ConfigProvider) (sources []datasource.DataSource, closer io.Closer, _ error)
- func MakeSnippet(filename, s string) linter.Snippet
- type Code
- type Config
- type VM
- type Var
- type VariableSet
- func (vs VariableSet) HasTopLevelVar(name string) bool
- func (vs VariableSet) HasVar(name string) bool
- func (vs VariableSet) TopLevelVars() []Var
- func (vs VariableSet) Vars() []Var
- func (vs VariableSet) WithTopLevelVars(add ...Var) VariableSet
- func (vs VariableSet) WithVars(add ...Var) VariableSet
- func (vs VariableSet) WithoutTopLevel() VariableSet
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConfigProviderFromVariables ¶ added in v0.14.8
func ConfigProviderFromVariables(vs VariableSet) datasource.ConfigProvider
ConfigProviderFromVariables returns a simple config provider for data sources based on a static set of variables that will be defined for the VM.
func CreateDataSources ¶ added in v0.14.8
func CreateDataSources(input []string, cp datasource.ConfigProvider) (sources []datasource.DataSource, closer io.Closer, _ error)
CreateDataSources returns the data source implementations for the supplied URIs. It also returns an io.Closer that should be called at the point when the data sources are no longer in use. It guarantees that the returned closer will be non-nil even when there are errors.
func MakeSnippet ¶ added in v0.15.0
MakeSnippet returns a linter Snippet from the supplied filename and the code string
Types ¶
type Code ¶
type Code struct {
// contains filtered or unexported fields
}
Code wraps string to distinguish it from string file names
type Config ¶
type Config struct { LibPaths []string // library paths DataSources []datasource.DataSource // data sources }
Config is the configuration of the VM
type VM ¶
type VM interface { // EvalFile evaluates the supplied file initializing the VM with the supplied variables // and returns its output as a JSON string. EvalFile(file string, v VariableSet) (string, error) // EvalCode evaluates the supplied code initializing the VM with the supplied variables // and returns its output as a JSON string. EvalCode(diagnosticFile string, code Code, v VariableSet) (string, error) // LintCode uses the jsonnet linter to lint the code and returns any errors LintCode(linter.Snippet) error }
VM provides a narrow interface to the capabilities of a jsonnet VM.
type Var ¶
type Var struct { Name string // contains filtered or unexported fields }
Var is an opaque variable to be initialized for the jsonnet VM
func NewCodeVar ¶
NewCodeVar returns a variable that has a code value
type VariableSet ¶
type VariableSet struct {
// contains filtered or unexported fields
}
VariableSet is an immutable set of variables to be registered with a jsonnet VM
func (VariableSet) HasTopLevelVar ¶
func (vs VariableSet) HasTopLevelVar(name string) bool
HasTopLevelVar returns true if the specified TLA variable is defined.
func (VariableSet) HasVar ¶
func (vs VariableSet) HasVar(name string) bool
HasVar returns true if the specified external variable is defined.
func (VariableSet) TopLevelVars ¶
func (vs VariableSet) TopLevelVars() []Var
TopLevelVars returns the string top-level variables defined for this variable set.
func (VariableSet) Vars ¶
func (vs VariableSet) Vars() []Var
Vars returns the names of all variables defined for this variable set.
func (VariableSet) WithTopLevelVars ¶
func (vs VariableSet) WithTopLevelVars(add ...Var) VariableSet
WithTopLevelVars returns a variable set with additional top-level string variables in its environment.
func (VariableSet) WithVars ¶
func (vs VariableSet) WithVars(add ...Var) VariableSet
WithVars returns a variable set with additional variables in its environment.
func (VariableSet) WithoutTopLevel ¶
func (vs VariableSet) WithoutTopLevel() VariableSet
WithoutTopLevel returns a variable set that does not have any top level variables set.
Directories ¶
Path | Synopsis |
---|---|
Package datasource declares the data source interface.
|
Package datasource declares the data source interface. |
internal
|
|
ds/exec
Package exec provides a data source implementation that can execute external commands and return its standard output for import or importstr use.
|
Package exec provides a data source implementation that can execute external commands and return its standard output for import or importstr use. |
ds/factory
Package factory provides a mechanism to create data sources from URLs with custom schemes.
|
Package factory provides a mechanism to create data sources from URLs with custom schemes. |
ds/helm3
Package helm3 provides a data source implementation that can extract k8s objects out of helm3 charts
|
Package helm3 provides a data source implementation that can extract k8s objects out of helm3 charts |
Package vmutil exposes specific functions used in the native implementation of the VM for general purpose use.
|
Package vmutil exposes specific functions used in the native implementation of the VM for general purpose use. |