Documentation ¶
Overview ¶
Package jsonnet implements a parser and evaluator for jsonnet.
Jsonnet is a domain specific configuration language that helps you define JSON data. Jsonnet lets you compute fragments of JSON within the structure, bringing the same benefit to structured data that templating languages bring to plain text.
See http://jsonnet.org/ for a full language description and tutorial.
Index ¶
- func SnippetToAST(filename string, snippet string) (ast.Node, error)
- func Version() string
- type ColorFormatter
- type Contents
- type ErrorFormatter
- type FileImporter
- type Importer
- type LineReader
- type MemoryImporter
- type NativeFunction
- type Reader
- type RuntimeError
- type VM
- func (vm *VM) Evaluate(node ast.Node) (val string, err error)
- func (vm *VM) EvaluateAnonymousSnippet(filename string, snippet string) (json string, formattedErr error)
- func (vm *VM) EvaluateAnonymousSnippetMulti(filename string, snippet string) (files map[string]string, formattedErr error)
- func (vm *VM) EvaluateAnonymousSnippetStream(filename string, snippet string) (docs []string, formattedErr error)
- func (vm *VM) EvaluateFile(filename string) (json string, formattedErr error)
- func (vm *VM) EvaluateFileMulti(filename string) (files map[string]string, formattedErr error)
- func (vm *VM) EvaluateFileStream(filename string) (docs []string, formattedErr error)
- func (vm *VM) EvaluateMulti(node ast.Node) (output map[string]string, err error)
- func (vm *VM) EvaluateSnippet(filename string, snippet string) (json string, formattedErr error)deprecated
- func (vm *VM) EvaluateSnippetMulti(filename string, snippet string) (files map[string]string, formattedErr error)deprecated
- func (vm *VM) EvaluateSnippetStream(filename string, snippet string) (docs []string, formattedErr error)deprecated
- func (vm *VM) EvaluateStream(node ast.Node) (output []string, err error)
- func (vm *VM) ExtCode(key string, val string)
- func (vm *VM) ExtNode(key string, node ast.Node)
- func (vm *VM) ExtReset()
- func (vm *VM) ExtVar(key string, val string)
- func (vm *VM) FindDependencies(importedFrom string, importedPaths []string) ([]string, error)
- func (vm *VM) ImportAST(importedFrom, importedPath string) (contents ast.Node, foundAt string, err error)
- func (vm *VM) ImportData(importedFrom, importedPath string) (contents string, foundAt string, err error)
- func (vm *VM) Importer(i Importer)
- func (vm *VM) NativeFunction(f *NativeFunction)
- func (vm *VM) ResolveImport(importedFrom, importedPath string) (foundAt string, err error)
- func (vm *VM) SetTraceOut(traceOut io.Writer)
- func (vm *VM) TLACode(key string, val string)
- func (vm *VM) TLANode(key string, node ast.Node)
- func (vm *VM) TLAReset()
- func (vm *VM) TLAVar(key string, val string)
- type YAMLReader
- type YAMLToJSONDecoder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SnippetToAST ¶
SnippetToAST parses a snippet and returns the resulting AST.
Types ¶
type ColorFormatter ¶
ColorFormatter represents a function that writes to the terminal using color.
type Contents ¶
type Contents struct {
// contains filtered or unexported fields
}
Contents is a representation of imported data. It is a simple byte wrapper, which makes it easier to enforce the caching policy.
func MakeContents ¶
MakeContents creates Contents from a string.
func MakeContentsRaw ¶
MakeContentsRaw creates Contents from (possibly non-utf8) []byte data.
type ErrorFormatter ¶
type ErrorFormatter interface { // Format static, runtime, and unexpected errors prior to printing them. Format(err error) string // Set the the maximum length of stack trace before cropping. SetMaxStackTraceSize(size int) // Set the color formatter for the location color. SetColorFormatter(color ColorFormatter) }
An ErrorFormatter formats errors with stacktraces and color.
type FileImporter ¶
type FileImporter struct { JPaths []string // contains filtered or unexported fields }
FileImporter imports data from the filesystem.
type Importer ¶
type Importer interface { // Import fetches data from a given path. It may be relative // to the file where we do the import. What "relative path" // means depends on the importer. // // It is required that: // a) for given (importedFrom, importedPath) the same // (contents, foundAt) are returned on subsequent calls. // b) for given foundAt, the contents are always the same // // It is recommended that if there are multiple locations that // need to be probed (e.g. relative + multiple library paths) // then all results of all attempts will be cached separately, // both nonexistence and contents of existing ones. // FileImporter may serve as an example. // // IMPORTANT: The passed importedFrom might be "" (an empty string). // It means that the import is coming from an ad-hoc snippet, e.g. // code passed on the command line, read from stdin or passed as // a snippet to execute. Importer may have a "default" path to use // in such case or it may only allow absolute imports from such // "anonymous locations". // // Importing the same file multiple times must be a cheap operation // and shouldn't involve copying the whole file - the same buffer // should be returned. Import(importedFrom, importedPath string) (contents Contents, foundAt string, err error) }
An Importer imports data from a path. TODO(sbarzowski) caching of errors (may require breaking changes)
type LineReader ¶
type LineReader struct {
// contains filtered or unexported fields
}
LineReader reads single lines.
func (*LineReader) Read ¶
func (r *LineReader) Read() ([]byte, error)
Read returns a single line (with '\n' ended) from the underlying reader. An error is returned iff there is an error with the underlying reader.
type MemoryImporter ¶
MemoryImporter "imports" data from an in-memory map.
type NativeFunction ¶
type NativeFunction struct { Name string Func func([]interface{}) (interface{}, error) Params ast.Identifiers }
NativeFunction represents a function implemented in Go.
type RuntimeError ¶
type RuntimeError struct { Msg string StackTrace []traceFrame }
RuntimeError is an error discovered during evaluation of the program
func (RuntimeError) Error ¶
func (err RuntimeError) Error() string
type VM ¶
type VM struct { MaxStack int ErrorFormatter ErrorFormatter StringOutput bool // contains filtered or unexported fields }
VM is the core interpreter and is the touchpoint used to parse and execute Jsonnet.
func (*VM) Evaluate ¶
Evaluate evaluates a Jsonnet program given by an Abstract Syntax Tree and returns serialized JSON as string. TODO(sbarzowski) perhaps is should return JSON in standard Go representation
func (*VM) EvaluateAnonymousSnippet ¶
func (vm *VM) EvaluateAnonymousSnippet(filename string, snippet string) (json string, formattedErr error)
EvaluateAnonymousSnippet evaluates a string containing Jsonnet code, return a JSON string.
The filename parameter is only used for error messages.
func (*VM) EvaluateAnonymousSnippetMulti ¶
func (vm *VM) EvaluateAnonymousSnippetMulti(filename string, snippet string) (files map[string]string, formattedErr error)
EvaluateAnonymousSnippetMulti evaluates a string containing Jsonnet code to key-value pairs. The keys are field name strings and the values are JSON strings.
The filename parameter is only used for error messages.
func (*VM) EvaluateAnonymousSnippetStream ¶
func (vm *VM) EvaluateAnonymousSnippetStream(filename string, snippet string) (docs []string, formattedErr error)
EvaluateAnonymousSnippetStream evaluates a string containing Jsonnet code to an array. The array is returned as an array of JSON strings.
The filename parameter is only used for error messages.
func (*VM) EvaluateFile ¶
EvaluateFile evaluates Jsonnet code in a file and returns a JSON string.
The importer is used to fetch the contents of the file.
func (*VM) EvaluateFileMulti ¶
EvaluateFileMulti evaluates Jsonnet code in a file to key-value pairs. The keys are field name strings and the values are JSON strings.
The importer is used to fetch the contents of the file.
func (*VM) EvaluateFileStream ¶
EvaluateFileStream evaluates Jsonnet code in a file to an array. The array is returned as an array of JSON strings.
The importer is used to fetch the contents of the file.
func (*VM) EvaluateMulti ¶
EvaluateMulti evaluates a Jsonnet program given by an Abstract Syntax Tree and returns key-value pairs. The keys are strings and the values are JSON strigns (serialized JSON).
func (*VM) EvaluateSnippet
deprecated
EvaluateSnippet evaluates a string containing Jsonnet code, return a JSON string.
The filename parameter is used for resolving relative imports and for errors messages.
Deprecated: Use EvaluateFile or EvaluateAnonymousSnippet instead.
func (*VM) EvaluateSnippetMulti
deprecated
func (vm *VM) EvaluateSnippetMulti(filename string, snippet string) (files map[string]string, formattedErr error)
EvaluateSnippetMulti evaluates a string containing Jsonnet code to key-value pairs. The keys are field name strings and the values are JSON strings.
The filename parameter is used for resolving relative imports and for errors messages.
Deprecated: Use EvaluateFileMulti or EvaluateAnonymousSnippetMulti instead.
func (*VM) EvaluateSnippetStream
deprecated
func (vm *VM) EvaluateSnippetStream(filename string, snippet string) (docs []string, formattedErr error)
EvaluateSnippetStream evaluates a string containing Jsonnet code to an array. The array is returned as an array of JSON strings.
The filename parameter is used for resolving relative imports and for errors messages.
Deprecated: Use EvaluateFileStream or EvaluateAnonymousSnippetStream instead.
func (*VM) EvaluateStream ¶
EvaluateStream evaluates a Jsonnet program given by an Abstract Syntax Tree and returns an array of JSON strings.
func (*VM) ExtReset ¶
func (vm *VM) ExtReset()
ExtReset rests all external variables registered for this VM.
func (*VM) FindDependencies ¶
FindDependencies returns a sorted array of unique transitive dependencies (via import/importstr/importbin) from all the given `importedPaths` which are themselves excluded from the returned array. The `importedPaths` are parsed as if they were imported from a Jsonnet file located at `importedFrom`.
func (*VM) ImportAST ¶
func (vm *VM) ImportAST(importedFrom, importedPath string) (contents ast.Node, foundAt string, err error)
ImportAST fetches the Jsonnet AST just as if it was imported from a Jsonnet file located at `importedFrom`. It shares the cache with the actual evaluation.
func (*VM) ImportData ¶
func (vm *VM) ImportData(importedFrom, importedPath string) (contents string, foundAt string, err error)
ImportData fetches the data just as if it was imported from a Jsonnet file located at `importedFrom`. It shares the cache with the actual evaluation.
func (*VM) NativeFunction ¶
func (vm *VM) NativeFunction(f *NativeFunction)
NativeFunction registers a native function.
func (*VM) ResolveImport ¶
ResolveImport finds the actual path where the imported file can be found. It will cache the contents of the file immediately as well, to avoid the possibility of the file disappearing after being checked.
func (*VM) SetTraceOut ¶
SetTraceOut sets the output stream for the builtin function std.trace().
type YAMLReader ¶
type YAMLReader struct {
// contains filtered or unexported fields
}
YAMLReader reads YAML
func NewYAMLReader ¶
func NewYAMLReader(r *bufio.Reader) *YAMLReader
NewYAMLReader creates a new YAMLReader
type YAMLToJSONDecoder ¶
type YAMLToJSONDecoder struct {
// contains filtered or unexported fields
}
YAMLToJSONDecoder decodes YAML documents from an io.Reader by separating individual documents. It first converts the YAML body to JSON, then unmarshals the JSON.
func NewYAMLToJSONDecoder ¶
func NewYAMLToJSONDecoder(r io.Reader) *YAMLToJSONDecoder
NewYAMLToJSONDecoder decodes YAML documents from the provided stream in chunks by converting each document (as defined by the YAML spec) into its own chunk, converting it to JSON via yaml.YAMLToJSON, and then passing it to json.Decoder.
func (*YAMLToJSONDecoder) Decode ¶
func (d *YAMLToJSONDecoder) Decode(into interface{}) error
Decode reads a YAML document as JSON from the stream or returns an error. The decoding rules match json.Unmarshal, not yaml.Unmarshal.
func (*YAMLToJSONDecoder) IsStream ¶
func (d *YAMLToJSONDecoder) IsStream() bool
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package ast provides AST nodes and ancillary structures and algorithms.
|
Package ast provides AST nodes and ancillary structures and algorithms. |
cmd
|
|
internal/cmd
Package cmd provides utilities for parsing and handling command line arguments.
|
Package cmd provides utilities for parsing and handling command line arguments. |
Package formatter is what powers jsonnetfmt, a Jsonnet formatter.
|
Package formatter is what powers jsonnetfmt, a Jsonnet formatter. |
internal
|
|
dump
Package dump can dump a Go data structure to Go source file, so that it can be statically embedded into other code.
|
Package dump can dump a Go data structure to Go source file, so that it can be statically embedded into other code. |
errors
Package errors provides internal representation of errors.
|
Package errors provides internal representation of errors. |
formatter
Package formatter provides API for producing pretty-printed source from AST.
|
Package formatter provides API for producing pretty-printed source from AST. |
parser
Package parser reads Jsonnet files and parses them into AST nodes.
|
Package parser reads Jsonnet files and parses them into AST nodes. |
pass
Package pass provides a visitor framework for source code analysis and transformation.
|
Package pass provides a visitor framework for source code analysis and transformation. |
program
Package program provides API for AST pre-processing (desugaring, static analysis).
|
Package program provides API for AST pre-processing (desugaring, static analysis). |
testutils
Package testutils provides general testing utilities.
|
Package testutils provides general testing utilities. |
Package linter analyses Jsonnet code for code "smells".
|
Package linter analyses Jsonnet code for code "smells". |
internal/common
Package common provides utilities to be used in multiple linter subpackages.
|
Package common provides utilities to be used in multiple linter subpackages. |
internal/traversal
Package traversal provides relatively lightweight checks which can all fit within one traversal of the AST.
|
Package traversal provides relatively lightweight checks which can all fit within one traversal of the AST. |
internal/types
Package types provides type inference functionality.
|
Package types provides type inference functionality. |
internal/variables
Package variables allows collecting the information about how variables are used.
|
Package variables allows collecting the information about how variables are used. |
Package toolutils includes several utilities handy for use in code analysis tools
|
Package toolutils includes several utilities handy for use in code analysis tools |