storage

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2017 License: MIT Imports: 4 Imported by: 0

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

Examples

Constants

View Source
const (
	STRING        DataType = "STRING"
	STRINGSLICE            = "STRINGARRAY"
	STRINGPOINTER          = "STRINGPOINTER"
	STRINGTABLE            = "STRINGTABLE"
	INT                    = "INT"
	INTPOINTER             = "INTPOINTER"
	FLOAT64                = "FLOAT64"
	OPTION                 = "OPTION"
	FUNCPOINTER            = "FUNCPOINTER"
)

Variables

View Source
var ErrKeyNotFound = errors.New("The given key was not found inside storage!")

ErrKeyNotFound is returned by GetWithErrors() when the key is not found

View Source
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 New

func New() *Data

NewStorage returns a pointer to a new empty storage

func (*Data) Get

func (d *Data) Get(key string) interface{}

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) GetKeys

func (d *Data) GetKeys() []string

GetKeys returns all the keys a slice of strings

func (*Data) GetType

func (d *Data) GetType(key string) (DataType, error)

GetType returns the DataType of a key. If there is no value availiable with the key an error is returned.

func (*Data) GetWithErrors

func (d *Data) GetWithErrors(key string) (interface{}, error)

GetWithErrors get a value and returns an error. Because of the api there is a Get() and a GetWithErrors() function.

func (*Data) Marshal

func (d *Data) Marshal() ([]byte, error)

Marshal the storage into json format

func (*Data) Remove

func (d *Data) Remove(key string) bool

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

func (d *Data) Set(key string, i interface{}) error

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

func (*Data) SetData

func (d *Data) SetData(data *Data) error

SetData takes a storage and sets all the values. This also works for all the pointers. That function is used, when the storage is unmarshaled input. Just exported values are set new.

func (*Data) Unmarshal

func (d *Data) Unmarshal(b []byte) error

Unmarshal a slice of bytes into the data

type DataType

type DataType string

type Option

type Option struct {
	Option string
	Values []string
}

Option holds the one option of a element

Jump to

Keyboard shortcuts

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