storage

package
v0.0.0-...-ba60b02 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2017 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package storage defines the API over interfaces, which are needed to store data.

Index

Constants

View Source
const (
	TypeString      = "string"
	TypeInt         = "int"
	TypeBool        = "bool"
	TypeSliceString = "list"
)

Variables

View Source
var ErrFieldNotSupported = errors.New("Field is not supported!")

ErrFieldNotSupported is used, when the field is not inside the txt storage.

View Source
var ErrTypeNotSupported = errors.New("Type is not supported!")

ErrTypeNotSupported is used when the type of the interface is not able to stored inside the txt.

View Source
var ErrWrongType = errors.New("Input has the wrong type!")

ErrWrongType is used when the given type does not match to the fieldID.

Functions

func LoadTxt

func LoadTxt(filename string, txt *Txt) error

LoadTxt loads data from a file into a Storager

func WriteTxt

func WriteTxt(txt *Txt, filename string) error

WriteTxt writes a Storager to a file

Types

type Field

type Field struct {
	// Key is a unique identifier for the field
	Key string `yaml:"key"`
	// Name of the field, does not have to be unique
	Name string `yaml:"name"`
	// Type of the field must match with the defined types
	// inside of the Data type.
	Type string `yaml:"type"`
	// Render can be set, when there should be a different
	// field inside the gui. For example a string could be rendered
	// as a textarea
	Render string `yaml:"render"`
	// The name of a group the field is used.
	Group string `yaml:"group"`
	// Items are ordered inside the group.
	Order int `yaml:"order"`
}

Field defines all fields for an item.

func (*Field) ID

func (f *Field) ID() string

type Fields

type Fields []Field

func LoadFields

func LoadFields(fpath string) (Fields, error)

func (Fields) Len

func (f Fields) Len() int

func (Fields) Less

func (f Fields) Less(i, j int) bool

func (Fields) Swap

func (f Fields) Swap(i, j int)

type Getter

type Getter interface {
	Get(fieldID string) (interface{}, bool)
	GetInt(fieldID string) (int, bool)
	GetString(fieldID string) (string, bool)
	GetBool(fieldID string) (bool, bool)
}

Getter is used to get data out of the storage

type Setter

type Setter interface {
	Set(fieldID string, value interface{}) error
}

Setter sets the data of a field

type StoragerS

type StoragerS interface {
	Setter
	Getter
	//AddField(Field) error
	GetFields() (Fields, error)
	Marshal() ([]byte, error)
	Unmarshal([]byte) error
}

Storager combines the interfaces

type Txt

type Txt struct {
	Fields       []Field             `json:"fields"`
	Strings      map[string]string   `json:"string"`
	Integers     map[string]int      `json:"int"`
	Bools        map[string]bool     `json:"bool"`
	SliceStrings map[string][]string `json:"list"`
}

Txt is a txt based storager

func NewTxtStorage

func NewTxtStorage() *Txt

NewTxtStorage creates an empty Txt storage.

func (*Txt) AddField

func (txt *Txt) AddField(f Field) error

AddField adds a field to the storage

func (*Txt) Clean

func (txt *Txt) Clean()

Clean removes all values which are not defined as field.

func (*Txt) Get

func (txt *Txt) Get(fieldID string) (interface{}, bool)

Get returns a value from the storage

func (*Txt) GetBool

func (txt *Txt) GetBool(fieldID string) (bool, bool)

func (*Txt) GetField

func (txt *Txt) GetField(fieldID string) (Field, bool)

GetField returns the field

func (*Txt) GetFields

func (txt *Txt) GetFields() (Fields, error)

GetFields returns all fields

func (*Txt) GetInt

func (txt *Txt) GetInt(fieldID string) (int, bool)

func (*Txt) GetSliceString

func (txt *Txt) GetSliceString(fieldID string) ([]string, bool)

func (*Txt) GetString

func (txt *Txt) GetString(fieldID string) (string, bool)

GetString returns the field value if it is a string

func (*Txt) Marshal

func (txt *Txt) Marshal() ([]byte, error)

Marshal is used for a json output

func (*Txt) Set

func (txt *Txt) Set(fieldID string, value interface{}) error

Set the value of a field

func (*Txt) Unmarshal

func (txt *Txt) Unmarshal(b []byte) error

Unmarshal uses json to set all the data into the Txt instance.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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