gojsonata

package
v0.0.0-...-ce092e3 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2019 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package gojsonata provides utility functions to execute JSONata expression for JSON data transformations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddModuleFile

func AddModuleFile(filename string) error

AddModuleFile loads content of a js module file and add the result to cache, so later script evaluation can use this module

func AddModuleSource

func AddModuleSource(key, source string) error

AddModuleSource loads source code of a js module file and add the result to cache, so later script evaluation can use this module

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets.

func CachedModules

func CachedModules() []string

CachedModules returns names of js modules currently loaded in the engine

func CallScript

func CallScript(source, result string) (otto.Value, error)

CallScript evaluates a js script, and return value of specified result var It checks if all required modules are loaded in cache

func CallScriptFile

func CallScriptFile(filename, result string) (otto.Value, error)

CallScriptFile evaluates content of js or json file, and return value of specified result var the file extension must be .js or .json

func MustAsset

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func RestoreAsset

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

func RunScript

func RunScript(source interface{}) (otto.Value, error)

RunScript executes js script and returns the result It checks if all required modules are loaded in cache

func Transform

func Transform(data interface{}, expr string) (interface{}, error)

Transform executes JSONata expression on input JSON data, and returns the transformation result as Go interface{}

Types

type Engine

type Engine struct {
	// it is based on otto
	*otto.Otto
	// contains filtered or unexported fields
}

Engine is modular vm environment

func NewEngine

func NewEngine() *Engine

NewEngine creates a new otto vm instance.

func (*Engine) LoadModule

func (m *Engine) LoadModule(source string) (otto.Value, error)

LoadModule loads source code of a js module. It recursively loads dependent modules required by the source code. All dependent modules' source code should be preloaded in jsdata.go, or loaded before this module. Note: this implementation is based on https://github.com/ddliu/motto/blob/master/module.go

func (*Engine) Require

func (m *Engine) Require(name string) (otto.Value, error)

Require implements js 'require(name)' for modeules. It first check if the module is already in cache, then check if it is a preloaded resource in jsdata.go. It returns error if source code of the specified module is not found. Note: input name may contains file path info, e.g., ./js/mycode.js, but the cache key will use only the file name without suffix, i.e., mycode => value

Jump to

Keyboard shortcuts

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