schemer

package module
v0.0.28 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: MIT Imports: 10 Imported by: 3

README

schemer

Data serialization library

Benchmark

Here is the benchmark result of the JavaScript VM and the transformer.

$ go test -bench . --benchmem -cpu 4,8,16
goos: darwin
goarch: amd64
pkg: github.com/BrobridgeOrg/schemer/runtime/goja
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
BenchmarkJavaScriptVM-4                	 4894088	       243.6 ns/op	     104 B/op	       3 allocs/op
BenchmarkJavaScriptVM-8                	 4795806	       247.2 ns/op	     104 B/op	       3 allocs/op
BenchmarkJavaScriptVM-16               	 5060337	       244.3 ns/op	     104 B/op	       3 allocs/op
BenchmarkTransformer-4                 	   72811	     15771 ns/op	    6984 B/op	     171 allocs/op
BenchmarkTransformer-8                 	   76226	     15830 ns/op	    6984 B/op	     171 allocs/op
BenchmarkTransformer-16                	   78670	     15990 ns/op	    6984 B/op	     171 allocs/op
BenchmarkTransformer_PassThrough-4     	 1329184	       889.8 ns/op	     408 B/op	      10 allocs/op
BenchmarkTransformer_PassThrough-8     	 1359368	       907.0 ns/op	     408 B/op	      10 allocs/op
BenchmarkTransformer_PassThrough-16    	 1348467	       888.5 ns/op	     408 B/op	      10 allocs/op
PASS
ok  	github.com/BrobridgeOrg/schemer/runtime/goja	15.254s

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidTypeDefinition    = errors.New("Invalid type definition")
	ErrInvalidFieldsDefinition  = errors.New("Invalid fields definition")
	ErrInvalidNotNullDefinition = errors.New("Invalid notNull definition")
	ErrInvalidArraySubtype      = errors.New("Array type requires subtype")
)
View Source
var (
	ErrInvalidType = fmt.Errorf("Invalid type")
)
View Source
var ValueTypes = map[string]ValueType{
	"string": TYPE_STRING,
	"binary": TYPE_BINARY,
	"int":    TYPE_INT64,
	"uint":   TYPE_UINT64,
	"float":  TYPE_FLOAT64,
	"bool":   TYPE_BOOLEAN,
	"time":   TYPE_TIME,
	"array":  TYPE_ARRAY,
	"map":    TYPE_MAP,
	"any":    TYPE_ANY,
}

Functions

func Unmarshal added in v0.0.4

func Unmarshal(data map[string]interface{}, s *Schema) error

func UnmarshalDefinition

func UnmarshalDefinition(data interface{}, d *Definition) error

func UnmarshalJSON added in v0.0.4

func UnmarshalJSON(source []byte, s *Schema) error

func WithRuntime added in v0.0.22

func WithRuntime(runtime Runtime) func(*Transformer)

Types

type Definition

type Definition struct {
	Schema  *Schema
	Type    ValueType
	Subtype *Definition
	Info    interface{}
	NotNull bool
}

func NewDefinition

func NewDefinition(t ValueType) *Definition

type RawDefinition

type RawDefinition struct {
	Type    ValueType
	Subtype *RawDefinition
	Fields  map[string]*RawDefinition
	NotNull bool
	Props   map[string]interface{}
}

func NewRawDefinition added in v0.0.17

func NewRawDefinition() *RawDefinition

type Record

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

func NewRecord

func NewRecord(schema *Schema, raw map[string]interface{}) *Record

func (*Record) GetValue

func (r *Record) GetValue(valuePath string) *Value

type Runtime added in v0.0.22

type Runtime interface {
	SetEnv(value map[string]interface{})
	LoadScript(script string) error
	Compile(script string) error
	Execute(sourceSchema *Schema, data map[string]interface{}) ([]map[string]interface{}, error)
}

type Schema

type Schema struct {
	Fields map[string]*Definition
}

func NewSchema

func NewSchema() *Schema

func (*Schema) GetDefinition

func (s *Schema) GetDefinition(valuePath string) *Definition

func (s *Schema) parsePath(fullPath string) []string {

	quoted := false
	elements := strings.FieldsFunc(fullPath, func(r rune) bool {

		if r == '"' {
			quoted = !quoted

			// Ignore
			return true
		}

		return !quoted && r == '.'
	})

	parts := make([]string, len(elements))
	for i, element := range elements {
		parts[i] = element
	}

	return parts
}

func (*Schema) Normalize

func (s *Schema) Normalize(data map[string]interface{}) map[string]interface{}

func (*Schema) Scan

func (s *Schema) Scan(data map[string]interface{}) *Record

type Schemer

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

func NewSchemer

func NewSchemer() *Schemer

type Transformer

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

func NewTransformer

func NewTransformer(source *Schema, dest *Schema, opts ...TransformerOpt) *Transformer

func (*Transformer) GetDestinationSchema added in v0.0.10

func (t *Transformer) GetDestinationSchema() *Schema

func (*Transformer) GetSourceSchema added in v0.0.10

func (t *Transformer) GetSourceSchema() *Schema

func (*Transformer) Reset added in v0.0.26

func (t *Transformer) Reset()

func (*Transformer) SetDestinationSchema added in v0.0.4

func (t *Transformer) SetDestinationSchema(schema *Schema)

func (*Transformer) SetScript

func (t *Transformer) SetScript(script string) error

func (*Transformer) SetSourceSchema added in v0.0.4

func (t *Transformer) SetSourceSchema(schema *Schema)

func (*Transformer) Transform

func (t *Transformer) Transform(env map[string]interface{}, input map[string]interface{}) ([]map[string]interface{}, error)

type TransformerOpt added in v0.0.22

type TransformerOpt func(*Transformer)

type Value

type Value struct {
	Definition *Definition
	Data       interface{}
}

func NewValue

func NewValue(def *Definition) *Value

type ValueType

type ValueType int32
const (
	TYPE_BOOLEAN ValueType = 0
	TYPE_BINARY  ValueType = 1
	TYPE_STRING  ValueType = 2
	TYPE_UINT64  ValueType = 3
	TYPE_INT64   ValueType = 4
	TYPE_FLOAT64 ValueType = 5
	TYPE_ARRAY   ValueType = 6
	TYPE_MAP     ValueType = 7
	TYPE_TIME    ValueType = 8
	TYPE_NULL    ValueType = 9
	TYPE_ANY     ValueType = 10
)

Directories

Path Synopsis
runtime

Jump to

Keyboard shortcuts

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