property

package
v0.0.0-...-4214274 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2021 License: MIT Imports: 3 Imported by: 0

README

Property

Property is a package that allows users to store either configurations for handlers or metadata for Payloads in a generic way.

Property

A property is a simple struct that contains a Name for the property, a Value (interface{}) and a Description.
There is also a required boolean which is used to make sure that the property is filled with correct information before running a handler.

Since the value field is a interface we can assign any value here. Note that when using the property you have to type asset or use the built in type asserts in the property.

Configuration

Instead of having many Propertys in a handler its easier to stack them together in a sort of manager. The configuration struct is used to properly manage properties.

It allows you to add, set values, remove and validate properties in bulk instead of one by one.
It also avoids running duplicate properties.

Usage

    // First you need to create a Configuration
    p := property.NewConfiguration()
    // Then you need to Add Properties along with a description and requirement
    // this is usually done by the Handler itself
    p.AddProperty("someConfig", "description", false)
    // You can then set the value of the property
    mywantedPropert := []string{"this", "is", "my", "splice"}
    p.SetProperty("someConfig", mywantedPropert)

    p.GetProperty("someConfig").StringSplice()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	//ErrWrongPropertyType is cast when the Property is of another type
	ErrWrongPropertyType = errors.New("the property is not of this data type")
	//ErrNoSuchProperty is when trying to assign value to Property that does not exist
	ErrNoSuchProperty = errors.New("the property ur trying to set the value for does not exist")
)

Functions

This section is empty.

Types

type Configuration

type Configuration struct {
	Properties []*Property `json:"properties" yaml:"properties"`
	sync.Mutex `json:"-" yaml:"-"`
}

Configuration is used to store Cfg for Actions and metadata for Payloads

func NewConfiguration

func NewConfiguration() *Configuration

NewConfiguration will initialize a configuration properly to avoid nil pointers

func (*Configuration) AddProperty

func (a *Configuration) AddProperty(name, description string, requierd bool)

AddProperty is used to add an Property to the PropertyContainer

func (*Configuration) GetProperty

func (a *Configuration) GetProperty(name string) *Property

GetProperty is used to extract an property from the Configuration

func (*Configuration) RemoveProperty

func (a *Configuration) RemoveProperty(name string)

RemoveProperty will remove any added properties

func (*Configuration) SetProperty

func (a *Configuration) SetProperty(name string, value interface{}) error

SetProperty will extract properties from the map,

func (*Configuration) ValidateProperties

func (a *Configuration) ValidateProperties() (bool, []string)

ValidateProperties will make sure that all properties are actually there that is required

type Property

type Property struct {
	Name        string      `json:"name" yaml:"name"`
	Value       interface{} `json:"value" yaml:"value"`
	Description string      `json:"description" yaml:"description"`
	Required    bool        `json:"required" yaml:"required"`
	Valid       bool        `json:"valid" yaml:"valid"`
}

Property is a value holder used by Actions to handle Configs

func (*Property) Bool

func (p *Property) Bool() (bool, error)

Bool is used to return the value as a boolean

func (*Property) Int

func (p *Property) Int() (int, error)

Int will reutnr the value as int

func (*Property) Int64

func (p *Property) Int64() (int64, error)

Int64 resemblance of the property is returned

func (*Property) IsValid

func (p *Property) IsValid() bool

IsValid is used to control if Required is true, then Value cannot be nil, if it is it will return False

func (*Property) MapWithSlice

func (p *Property) MapWithSlice() (map[string][]string, error)

MapWithSlice will return a Map that holds a slice of strings

func (*Property) String

func (p *Property) String() string

String Will return the Value as string

func (*Property) StringMap

func (p *Property) StringMap() (map[string]string, error)

StringMap is used to return the value as a map[string]string

func (*Property) StringSplice

func (p *Property) StringSplice() ([]string, error)

StringSplice is used to get the value as a stringslice

Jump to

Keyboard shortcuts

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