Documentation ¶
Overview ¶
Package storage defines a object, which can be exported and imported as a JSON object. The data is stored with the type information, so inside Go the type logic can be used.
Index ¶
- Constants
- Variables
- type Data
- func (d *Data) Get(key string) interface{}
- func (d *Data) GetKeys() []string
- func (d *Data) GetType(key string) (DataType, error)
- func (d *Data) GetWithErrors(key string) (interface{}, error)
- func (d *Data) Marshal() ([]byte, error)
- func (d *Data) Remove(key string) bool
- func (d *Data) Set(key string, i interface{}) error
- func (d *Data) SetData(data *Data) error
- func (d *Data) Unmarshal(b []byte) error
- type DataType
- type Option
Examples ¶
Constants ¶
const ( STRING DataType = "STRING" STRINGSLICE = "STRINGARRAY" STRINGPOINTER = "STRINGPOINTER" STRINGTABLE = "STRINGTABLE" INT = "INT" INTPOINTER = "INTPOINTER" FLOAT64 = "FLOAT64" OPTION = "OPTION" FUNCPOINTER = "FUNCPOINTER" )
Variables ¶
var ErrKeyNotFound = errors.New("The given key was not found inside storage!")
ErrKeyNotFound is returned by GetWithErrors() when the key is not found
var ErrTypeNotSupported = errors.New("Type is not supported!")
ErrTypeNotSupported is returned by Set() when the given type could not be stored.
Functions ¶
This section is empty.
Types ¶
type Data ¶
type Data struct { // Values is a map which know the internal DataType every key value // pair is exported here. Values map[string]DataType `json:"values"` // Data conatains all the data, which can be exported Data map[string]interface{} `json:"data"` // contains filtered or unexported fields }
Data is the type were everything is stored and which can be used for Marshaling to json. Every entry uses a unique key, which is a string.
func (*Data) Get ¶
Get enables a simple api, which just returns the value if no value is found nil is returned. If it is clear which type the value is it is possible to set the type when calling that function.
i := store.Get("myInt").(int)
This usage is dangerous, because it can cause a runtime error, when the found value is another type then expected. But it enables a very simple API, so if you use it be sure that the type you are getting is really fix To be sure at this point the function GetWithErrors() should be used.
func (*Data) GetType ¶
GetType returns the DataType of a key. If there is no value availiable with the key an error is returned.
func (*Data) GetWithErrors ¶
GetWithErrors get a value and returns an error. Because of the api there is a Get() and a GetWithErrors() function.
func (*Data) Remove ¶
Remove just deletes the key from the storage. True is returned when something could be removed. If the key not exists inside the storage nothing could be deleted, so false is returned.
func (*Data) Set ¶
Set is used to set the data of a type. Everytime a new value is set the type of the newest type is used.
Example ¶
store := New() store.Set("myString", "this is my string") store.Set("myInt", 1234) i := store.Get("myInt").(int) fmt.Println(i * 2)
Output: 2468