model

package module
v0.0.0-...-91b10cf Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2018 License: BSD-3-Clause Imports: 8 Imported by: 0

README

model

BSD-2-Clause WIP Build status Coverage status Go Report Card Github issues Github pull requests GoDoc

Documentation

Index

Examples

Constants

View Source
const (
	// InvalidIndex - The specified index does not exist.
	InvalidIndex errors.Code = iota + 1000

	// InvalidIndexType - The specified index data type is invalid for this
	// model.
	InvalidIndexType

	// InvalidMethodContext - The requested method is not valid in the
	// current context. E.g. the Push() method on hash models.
	InvalidMethodContext

	// ReadOnlyProperty - An attempt was made to modify a read-only property.
	ReadOnlyProperty

	// InvalidDataSet - An attempt was made to store a data set that is
	// with the model type
	InvalidDataSet
)

Internal errors

View Source
const (
	// Dict defines a dictionary model type.
	Dict modelType = iota
	// List defines a list model type.
	List
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Marshaler

type Marshaler interface {
	MarshalModel() ([]byte, error)
}

Marshaler is the interface implemented by types that can serialize themselves into a static byte array.

type Model

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

Model defines the model data structure.

func New

func New(modelType std.ModelType) *Model

New returns a new std.Model.

Example
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/bdlm/model"
	"github.com/bdlm/std"

	log "github.com/sirupsen/logrus"
)

func main() {
	mdl := model.New(std.ModelTypeHash)
	json.Unmarshal(
		[]byte(`{"key1":"value1","key2":2,"key3":["one","two","three"],"key4":{"k1":"v1","k2":"v2"}}`),
		&mdl,
	)
	var key, val interface{}
	for mdl.Next(&key, &val) {
		if "key3" == key.(string) || "key4" == key.(string) {
			var k2, v2 interface{}
			var m2 std.Model
			m2, _ = val.(std.Value).Model()
			if nil == m2 {
				data, hash, index := mdl.Data()
				log.Debugf("\n\n\ndata: %v\nhash: %v\nindex: %v\n\n\n", data, hash, index)
				os.Exit(1)
			}

			fmt.Println(key)
			for m2.(std.Iterator).Next(&k2, &v2) {
				fmt.Println("   ", k2, v2.(std.Value).Value())
			}
		} else {
			fmt.Println(key, val.(std.Value).Value())
		}
	}

}
Output:

key1 value1
key2 2
key3
    0 one
    1 two
    2 three
key4
    k1 v1
    k2 v2

func (*Model) Cur

func (mdl *Model) Cur(pK, pV *interface{}) bool

Cur implements std.Iterator.

Cur reads the key and value at the current cursor postion into pK and pV respectively. Cur will return false if no iteration has begun, including following calls to Reset.

func (*Model) Data

func (mdl *Model) Data() ([]interface{}, map[string]int, map[int]string)

Data returns the current data set and indexes.

func (*Model) Delete

func (mdl *Model) Delete(key interface{}) error

Delete removes a value from this model.

func (*Model) Filter

func (mdl *Model) Filter(callback func(std.Value) std.Model) std.Model

Filter filters elements of the data using a callback function and returns the result.

func (*Model) Get

func (mdl *Model) Get(key interface{}) (std.Value, error)

Get returns the specified data value in this model.

func (*Model) GetID

func (mdl *Model) GetID() interface{}

GetID returns returns this model's id.

func (*Model) GetType

func (mdl *Model) GetType() std.ModelType

GetType returns the model type.

func (*Model) Has

func (mdl *Model) Has(key interface{}) bool

Has tests to see of a specified data element exists in this model.

func (*Model) Lock

func (mdl *Model) Lock()

Lock marks this model as read-only.

func (*Model) Map

func (mdl *Model) Map(callback func(std.Value) std.Model) std.Model

Map applies a callback to all elements in this model and returns the result.

func (*Model) MarshalJSON

func (mdl *Model) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*Model) MarshalModel

func (mdl *Model) MarshalModel() ([]byte, error)

MarshalModel implements Marshaler.

func (*Model) Merge

func (mdl *Model) Merge(model std.Model) error

Merge merges data from any Model into this Model.

func (*Model) Next

func (mdl *Model) Next(pK, pV *interface{}) bool

Next implements std.Iterator.

Next moves the cursor forward one position before reading the key and value at the cursor position into pK and pV respectively. If data is available at that position and was written to pK and pV then Next returns true, else false to signify the end of the data and resets the cursor postion to the beginning of the data set (-1).

func (*Model) Prev

func (mdl *Model) Prev(pK, pV *interface{}) bool

Prev implements std.Iterator.

Prev moves the cursor backward one position before reading the key and value at the cursor position into pK and pV respectively. If data is available at that position and was written to pK and pV then Prev returns true, else false to signify the beginning of the data.

func (*Model) Push

func (mdl *Model) Push(value interface{}) error

Push a value to the end of the internal data store.

func (*Model) Reduce

func (mdl *Model) Reduce(callback func(std.Value) bool) std.Value

Reduce iteratively reduces the data to a single value using a callback function and returns the result.

func (*Model) Reset

func (mdl *Model) Reset()

Reset implements std.Iterator.

Reset sets the iterator cursor position.

func (*Model) Reverse

func (mdl *Model) Reverse()

Reverse reverses the order of the data store.

func (*Model) Seek

func (mdl *Model) Seek(pos interface{}) error

Seek implements std.Iterator.

Seek sets the iterator cursor position.

func (*Model) Set

func (mdl *Model) Set(key interface{}, value interface{}) error

Set stores a value in the internal data store. All values must be identified by key.

func (*Model) SetData

func (mdl *Model) SetData(data interface{}) error

SetData replaces the current data stored in the model with the provided data.

func (*Model) SetID

func (mdl *Model) SetID(id interface{})

SetID sets this Model's identifier property.

func (*Model) SetType

func (mdl *Model) SetType(typ std.ModelType) error

SetType sets the model type. If any data is stored in this model, this property becomes read-only.

func (*Model) Sort

func (mdl *Model) Sort(flag std.SortFlag) error

Sort sorts the model data.

func (*Model) UnmarshalJSON

func (mdl *Model) UnmarshalJSON(jsn []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Model) UnmarshalModel

func (mdl *Model) UnmarshalModel() ([]byte, error)

UnmarshalModel implements Marshaler.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalModel(bytes []byte) error
}

Unmarshaler is the interface implemented by Models that can unmarshal a serialized description of themselves. The input can be assumed to be a valid encoding of a Model value. UnmarshalModel must copy the data if it wishes to retain the data after returning.

By convention, to approximate the behavior of similar functionality in other packges, Unmarshalers implement UnmarshalModel([]byte("null")) as a no-op.

type Value

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

Value implements github.com/bdlm/std/Value.

func (*Value) Bool

func (val *Value) Bool() (bool, error)

Bool returns the boolean representation of the value of this node, or an error if the type conversion is not possible.

func (*Value) Float

func (val *Value) Float() (float64, error)

Float returns the float64 representation of the value of this node, or an error if the type conversion is not possible.

func (*Value) Float32

func (val *Value) Float32() (float32, error)

Float32 returns the float32 representation of the value of this node, or an error if the type conversion is not possible.

func (*Value) Float64

func (val *Value) Float64() (float64, error)

Float64 returns the float64 representation of the value of this node, or an error if the type conversion is not possible.

func (*Value) Int

func (val *Value) Int() (int, error)

Int returns the int representation of the value of this node, or an error if the type conversion is not possible.

func (*Value) List

func (val *Value) List() ([]std.Value, error)

List returns the array of Values stored in this node, or an error if the type conversion is not possible.

func (*Value) Map

func (val *Value) Map() (map[string]std.Value, error)

Map returns the map[string]Value data stored in this node, or an error if the type conversion is not possible.

func (*Value) Model

func (val *Value) Model() (std.Model, error)

Model returns the Model stored at this node, or an error if the value does not implement Model.

func (*Value) String

func (val *Value) String() (string, error)

String returns the boolean representation of the value, or an error if the type conversion is not possible.

func (*Value) Value

func (val *Value) Value() interface{}

Value returns the untyped value.

Jump to

Keyboard shortcuts

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