Documentation
¶
Index ¶
- Constants
- type Marshaler
- type Model
- func (mdl *Model) Cur(pK, pV *interface{}) bool
- func (mdl *Model) Data() ([]interface{}, map[string]int, map[int]string)
- func (mdl *Model) Delete(key interface{}) error
- func (mdl *Model) Filter(callback func(std.Value) std.Model) std.Model
- func (mdl *Model) Get(key interface{}) (std.Value, error)
- func (mdl *Model) GetID() interface{}
- func (mdl *Model) GetType() std.ModelType
- func (mdl *Model) Has(key interface{}) bool
- func (mdl *Model) Lock()
- func (mdl *Model) Map(callback func(std.Value) std.Model) std.Model
- func (mdl *Model) MarshalJSON() ([]byte, error)
- func (mdl *Model) MarshalModel() ([]byte, error)
- func (mdl *Model) Merge(model std.Model) error
- func (mdl *Model) Next(pK, pV *interface{}) bool
- func (mdl *Model) Prev(pK, pV *interface{}) bool
- func (mdl *Model) Push(value interface{}) error
- func (mdl *Model) Reduce(callback func(std.Value) bool) std.Value
- func (mdl *Model) Reset()
- func (mdl *Model) Reverse()
- func (mdl *Model) Seek(pos interface{}) error
- func (mdl *Model) Set(key interface{}, value interface{}) error
- func (mdl *Model) SetData(data interface{}) error
- func (mdl *Model) SetID(id interface{})
- func (mdl *Model) SetType(typ std.ModelType) error
- func (mdl *Model) Sort(flag std.SortFlag) error
- func (mdl *Model) UnmarshalJSON(jsn []byte) error
- func (mdl *Model) UnmarshalModel() ([]byte, error)
- type Unmarshaler
- type Value
- func (val *Value) Bool() (bool, error)
- func (val *Value) Float() (float64, error)
- func (val *Value) Float32() (float32, error)
- func (val *Value) Float64() (float64, error)
- func (val *Value) Int() (int, error)
- func (val *Value) List() ([]std.Value, error)
- func (val *Value) Map() (map[string]std.Value, error)
- func (val *Value) Model() (std.Model, error)
- func (val *Value) String() (string, error)
- func (val *Value) Value() interface{}
Examples ¶
Constants ¶
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
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 ¶
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 ¶
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 ¶
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) Filter ¶
Filter filters elements of the data using a callback function and returns the result.
func (*Model) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*Model) MarshalModel ¶
MarshalModel implements Marshaler.
func (*Model) Next ¶
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 ¶
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) Reduce ¶
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) Set ¶
Set stores a value in the internal data store. All values must be identified by key.
func (*Model) SetData ¶
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 ¶
SetType sets the model type. If any data is stored in this model, this property becomes read-only.
func (*Model) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
func (*Model) UnmarshalModel ¶
UnmarshalModel implements Marshaler.
type Unmarshaler ¶
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 ¶
Bool returns the boolean representation of the value of this node, or an error if the type conversion is not possible.
func (*Value) Float ¶
Float returns the float64 representation of the value of this node, or an error if the type conversion is not possible.
func (*Value) Float32 ¶
Float32 returns the float32 representation of the value of this node, or an error if the type conversion is not possible.
func (*Value) Float64 ¶
Float64 returns the float64 representation of the value of this node, or an error if the type conversion is not possible.
func (*Value) Int ¶
Int returns the int representation of the value of this node, or an error if the type conversion is not possible.
func (*Value) List ¶
List returns the array of Values stored in this node, or an error if the type conversion is not possible.
func (*Value) Map ¶
Map returns the map[string]Value data stored in this node, or an error if the type conversion is not possible.
func (*Value) Model ¶
Model returns the Model stored at this node, or an error if the value does not implement Model.