vm

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 6, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

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

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

func MakeSnippet(filename, s string) linter.Snippet

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

func MakeCode

func MakeCode(s string) Code

MakeCode returns a code object from the supplied string.

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.

func New

func New(config Config) VM

New constructs a new VM based on the supplied config. The returned VM interface is safe for concurrent use.

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

func NewCodeVar(name, code string) Var

NewCodeVar returns a variable that has a code value

func NewVar

func NewVar(name, value string) Var

NewVar returns a variable that has a string 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
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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL