jsonnet

package module
v0.0.0-...-4fbcbea Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2019 License: MIT Imports: 7 Imported by: 7

README

jsonnet_cgo

Simple golang cgo wrapper around JSonnet VM.

Everything in libjsonnet.h is covered except the multi-file evaluators.

See jsonnet_test.go for how to use it.

Quick example in golang:

    vm := jsonnet.Make()
    vm.ExtVar("color", "purple")

    x, err := vm.EvaluateSnippet(`Test_Demo`, `"dark " + std.extVar("color")`)

    if err != nil {
            panic(err)
    }
    if x != "\"dark purple\"\n" {
            panic("fail: we got " + x)
    }

    vm.Destroy()

Quick examples with the command line demo program:

$ ( cd jsonnet_main/  ; go  build -x -a )
...
mv $WORK/b001/exe/a.out jsonnet_main
...
$ echo "{ a: 1, b: 2 }"  | jsonnet_main/jsonnet_main /dev/stdin
{
   "a": 1,
   "b": 2
}
$ cat test1.j
{
  shell: "/bin/sh",
  awk: "/usr/bin/awk",
}
$ jsonnet_main/jsonnet_main test1.j
{
   "awk": "/usr/bin/awk",
   "shell": "/bin/sh"
}
$ cat test2.j
local test1 = import "test1.j";

test1 {
  shell: "/bin/csh",
}
$ jsonnet_main/jsonnet_main test2.j
{
   "awk": "/usr/bin/awk",
   "shell": "/bin/csh"
}
$ echo ' std.extVar("a") + "bar" ' | jsonnet_main/jsonnet_main /dev/stdin a=foo
"foobar"

LICENSES

Notice the various LICENSE* files. I cannot offer legal advice, but you might find that the Apache License is the most restrictive.

Most of this code comes from https://github.com/google/jsonnet and is under the Apache License, Version 2.0, January 2004, and our files that match filenames there are under that license.

Notice the third_party/ directory in that distribution. It has json/ and md5/ under their own licences, and our files that match filenames there are under those licenses.

Anything new added here is under an MIT license in the plain LICENSE file.

Documentation

Overview

jsonnet is a simple Go wrapper for the JSonnet VM.

See http://jsonnet.org/

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version() string

Evaluate a file containing Jsonnet code, return a JSON string.

Types

type ImportCallback

type ImportCallback func(base, rel string) (result string, path string, err error)

type JsonValue

type JsonValue struct {
	// contains filtered or unexported fields
}

JsonValue represents a jsonnet JSON object.

func (*JsonValue) ArrayAppend

func (v *JsonValue) ArrayAppend(item *JsonValue)

func (*JsonValue) Extract

func (v *JsonValue) Extract() interface{}

func (*JsonValue) ExtractBool

func (v *JsonValue) ExtractBool() (bool, bool)

func (*JsonValue) ExtractNull

func (v *JsonValue) ExtractNull() bool

ExtractNull returns true iff the value is null

func (*JsonValue) ExtractNumber

func (v *JsonValue) ExtractNumber() (float64, bool)

func (*JsonValue) ExtractString

func (v *JsonValue) ExtractString() (string, bool)

ExtractString returns the string value and true if the value was a string

func (*JsonValue) ObjectAppend

func (v *JsonValue) ObjectAppend(key string, value *JsonValue)

type NativeCallback

type NativeCallback func(args ...*JsonValue) (result *JsonValue, err error)

type VM

type VM struct {
	// contains filtered or unexported fields
}

func Make

func Make() *VM

Create a new Jsonnet virtual machine.

func (*VM) Destroy

func (vm *VM) Destroy()

Complement of Make().

func (*VM) EvaluateFile

func (vm *VM) EvaluateFile(filename string) (string, error)

Evaluate a file containing Jsonnet code, return a JSON string.

func (*VM) EvaluateSnippet

func (vm *VM) EvaluateSnippet(filename, snippet string) (string, error)

Evaluate a string containing Jsonnet code, return a JSON string.

func (*VM) ExtCode

func (vm *VM) ExtCode(key, val string)

Bind a Jsonnet external var to the given Jsonnet code.

func (*VM) ExtVar

func (vm *VM) ExtVar(key, val string)

Bind a Jsonnet external var to the given value.

func (*VM) FormatFile

func (vm *VM) FormatFile(filename string) (string, error)

Format a file containing Jsonnet code, return a JSON string.

func (*VM) FormatIndent

func (vm *VM) FormatIndent(n int)

Indentation level when reformatting (number of spaces)

func (*VM) FormatSnippet

func (vm *VM) FormatSnippet(filename, snippet string) (string, error)

Format a string containing Jsonnet code, return a JSON string.

func (*VM) GcGrowthTrigger

func (vm *VM) GcGrowthTrigger(v float64)

Run the garbage collector after this amount of growth in the number of objects.

func (*VM) GcMinObjects

func (vm *VM) GcMinObjects(v uint)

Set the number of objects required before a garbage collection cycle is allowed.

func (*VM) ImportCallback

func (vm *VM) ImportCallback(f ImportCallback)

Override the callback used to locate imports.

func (*VM) JpathAdd

func (vm *VM) JpathAdd(path string)

Add to the default import callback's library search path.

func (*VM) MaxStack

func (vm *VM) MaxStack(v uint)

Set the maximum stack depth.

func (*VM) MaxTrace

func (vm *VM) MaxTrace(v uint)

Set the number of lines of stack trace to display (0 for all of them).

func (*VM) NativeCallback

func (vm *VM) NativeCallback(name string, params []string, f interface{})

NativeCallback is a helper around NativeCallbackRaw that uses `reflect` to convert argument and result types to/from JsonValue. `f` is expected to be a function that takes argument types supported by `(*JsonValue).Extract` and returns `(x, error)` where `x` is a type supported by `NewJson`.

func (*VM) NativeCallbackRaw

func (vm *VM) NativeCallbackRaw(name string, params []string, f NativeCallback)

func (*VM) NewArray

func (vm *VM) NewArray() *JsonValue

func (*VM) NewBool

func (vm *VM) NewBool(v bool) *JsonValue

func (*VM) NewJson

func (vm *VM) NewJson(value interface{}) *JsonValue

func (*VM) NewNull

func (vm *VM) NewNull() *JsonValue

func (*VM) NewNumber

func (vm *VM) NewNumber(v float64) *JsonValue

func (*VM) NewObject

func (vm *VM) NewObject() *JsonValue

func (*VM) NewString

func (vm *VM) NewString(v string) *JsonValue

func (*VM) StringOutput

func (vm *VM) StringOutput(v bool)

Expect a string as output and don't JSON encode it.

func (*VM) TlaCode

func (vm *VM) TlaCode(key, val string)

Bind a Jsonnet top-level argument to the given Jsonnet code.

func (*VM) TlaVar

func (vm *VM) TlaVar(key, val string)

Bind a Jsonnet top-level argument to the given value.

Directories

Path Synopsis
Command line tool to try evaluating JSonnet.
Command line tool to try evaluating JSonnet.
A program to stress the JSonnet VM and its cgo wrappings.
A program to stress the JSonnet VM and its cgo wrappings.

Jump to

Keyboard shortcuts

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