common

package
v0.31.1 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2021 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(rt *goja.Runtime, v interface{}, ctxPtr *context.Context) map[string]interface{}

Bind the provided value v to the provided runtime

func BindToGlobal

func BindToGlobal(rt *goja.Runtime, data map[string]interface{}) func()

BindToGlobal Binds an object's members to the global scope. Returns a function that un-binds them. Note that this will panic if passed something that isn't a struct; please don't do that.

func FieldName

func FieldName(t reflect.Type, f reflect.StructField) string

FieldName Returns the JS name for an exported struct field. The name is snake_cased, with respect for certain common initialisms (URL, ID, HTTP, etc).

func GetReader added in v0.30.0

func GetReader(data interface{}) (io.Reader, error)

GetReader tries to return an io.Reader value from an exported goja value.

func GetRuntime

func GetRuntime(ctx context.Context) *goja.Runtime

GetRuntime retrieves the attached goja runtime from the given context.

func MethodName

func MethodName(t reflect.Type, m reflect.Method) string

MethodName Returns the JS name for an exported method. The first letter of the method's name is lowercased, otherwise it is unaltered.

func NewRandSource

func NewRandSource() goja.RandSource

NewRandSource is copied from goja's source code: https://github.com/dop251/goja/blob/master/goja/main.go#L44 The returned RandSource is NOT safe for concurrent use: https://golang.org/pkg/math/rand/#NewSource

func Throw

func Throw(rt *goja.Runtime, err error)

Throw a JS error; avoids re-wrapping GoErrors.

func ToBytes added in v0.31.0

func ToBytes(data interface{}) ([]byte, error)

ToBytes tries to return a byte slice from compatible types.

func WithInitEnv added in v0.29.0

func WithInitEnv(ctx context.Context, initEnv *InitEnvironment) context.Context

WithInitEnv attaches the given init environment to the context.

func WithRuntime

func WithRuntime(ctx context.Context, rt *goja.Runtime) context.Context

WithRuntime attaches the given goja runtime to the context.

Types

type FieldNameMapper

type FieldNameMapper struct{}

FieldNameMapper for goja.Runtime.SetFieldNameMapper()

func (FieldNameMapper) FieldName

FieldName is part of the goja.FieldNameMapper interface https://godoc.org/github.com/dop251/goja#FieldNameMapper

func (FieldNameMapper) MethodName

func (FieldNameMapper) MethodName(t reflect.Type, m reflect.Method) string

MethodName is part of the goja.FieldNameMapper interface https://godoc.org/github.com/dop251/goja#FieldNameMapper

type InitContextError added in v0.23.0

type InitContextError string

InitContextError is an error that happened during the a test init context

func NewInitContextError added in v0.23.0

func NewInitContextError(msg string) InitContextError

NewInitContextError returns a new InitContextError with the provided message

func (InitContextError) Error added in v0.23.0

func (i InitContextError) Error() string

func (InitContextError) String added in v0.23.0

func (i InitContextError) String() string

type InitEnvironment added in v0.29.0

type InitEnvironment struct {
	Logger      logrus.FieldLogger
	FileSystems map[string]afero.Fs
	CWD         *url.URL
	// TODO: add RuntimeOptions and other properties, goja sources, etc.
	// ideally, we should leave this as the only data structure necessary for
	// executing the init context for all JS modules
	SharedObjects *SharedObjects
}

InitEnvironment contains properties that can be accessed by Go code executed in the k6 init context. It can be accessed by calling common.GetInitEnv().

func GetInitEnv added in v0.29.0

func GetInitEnv(ctx context.Context) *InitEnvironment

GetInitEnv retrieves the attached init environment struct from the given context.

func (*InitEnvironment) GetAbsFilePath added in v0.29.0

func (ie *InitEnvironment) GetAbsFilePath(filename string) string

GetAbsFilePath should be used to access the FileSystems, since afero has a bug when opening files with relative paths - it caches them from the FS root, not the current working directory... So, if necessary, this method will transform any relative paths into absolute ones, using the CWD.

TODO: refactor? It was copied from https://github.com/loadimpact/k6/blob/c51095ad7304bdd1e82cdb33c91abc331533b886/js/initcontext.go#L211-L222

type SharedObjects added in v0.30.0

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

SharedObjects is a collection of general store for objects to be shared. It is mostly a wrapper around map[string]interface with a lock and stuff. The reason behind not just using sync.Map is that it still needs a lock when we want to only call the function constructor if there is no such key at which point you already need a lock so ...

func NewSharedObjects added in v0.30.0

func NewSharedObjects() *SharedObjects

NewSharedObjects returns a new SharedObjects ready to use

func (*SharedObjects) GetOrCreateShare added in v0.30.0

func (so *SharedObjects) GetOrCreateShare(name string, createCallback func() interface{}) interface{}

GetOrCreateShare returns a shared value with the given name or sets it's value whatever createCallback returns and returns it.

Jump to

Keyboard shortcuts

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