fieldcollection

package
v2.25.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2024 License: Apache-2.0 Imports: 9 Imported by: 2

Documentation

Overview

Package fieldcollection contains a map[string]any with accessor methods to derive them into different formats

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrValueNotSet signalizes the value does not exist in the map
	ErrValueNotSet = errors.New("specified value not found")
	// ErrValueMismatch signalizes the value has a different data type
	ErrValueMismatch = errors.New("specified value has different format")
)

Functions

func MustHaveNoUnknowFields added in v2.24.0

func MustHaveNoUnknowFields(f *FieldCollection, validateStore *FieldCollection) error

MustHaveNoUnknowFields validates no fields are present which are not previously allow-listed through CanHaveField or MustHaveField and therefore should be put as the last ValidateOpt

Types

type FieldCollection

type FieldCollection struct {
	// contains filtered or unexported fields
}

FieldCollection holds a map with integrated locking and can therefore used in multiple Go-routines concurrently

func FieldCollectionFromData

func FieldCollectionFromData(data map[string]any) *FieldCollection

FieldCollectionFromData is a wrapper around NewFieldCollection and SetFromData

func NewFieldCollection

func NewFieldCollection() *FieldCollection

NewFieldCollection creates a new FieldCollection with empty data store

func (*FieldCollection) Bool

func (f *FieldCollection) Bool(name string) (bool, error)

Bool tries to read key name as bool

func (*FieldCollection) CanBool

func (f *FieldCollection) CanBool(name string) bool

CanBool tries to read key name as bool and checks whether error is nil

func (*FieldCollection) CanDuration

func (f *FieldCollection) CanDuration(name string) bool

CanDuration tries to read key name as time.Duration and checks whether error is nil

func (*FieldCollection) CanFloat64 added in v2.25.0

func (f *FieldCollection) CanFloat64(name string) bool

CanFloat64 tries to read key name as float64 and checks whether error is nil

func (*FieldCollection) CanInt64

func (f *FieldCollection) CanInt64(name string) bool

CanInt64 tries to read key name as int64 and checks whether error is nil

func (*FieldCollection) CanString

func (f *FieldCollection) CanString(name string) bool

CanString tries to read key name as string and checks whether error is nil

func (*FieldCollection) CanStringSlice added in v2.24.0

func (f *FieldCollection) CanStringSlice(name string) bool

CanStringSlice tries to read key name as []string and checks whether error is nil

func (*FieldCollection) Clone

func (f *FieldCollection) Clone() *FieldCollection

Clone is a wrapper around n.SetFromData(o.Data())

func (*FieldCollection) Data

func (f *FieldCollection) Data() map[string]any

Data creates a map-copy of the data stored inside the FieldCollection

func (*FieldCollection) Duration

func (f *FieldCollection) Duration(name string) (time.Duration, error)

Duration tries to read key name as time.Duration

func (*FieldCollection) Expect

func (f *FieldCollection) Expect(keys ...string) error

Expect takes a list of keys and returns an error with all non-found names

func (*FieldCollection) Float64 added in v2.25.0

func (f *FieldCollection) Float64(name string) (float64, error)

Float64 tries to read key name as float64

func (*FieldCollection) Get added in v2.24.0

func (f *FieldCollection) Get(name string) (any, error)

Get retrieves the value of a key as "any" type or returns an error in case the field is not set

func (*FieldCollection) HasAll

func (f *FieldCollection) HasAll(keys ...string) bool

HasAll takes a list of keys and returns whether all of them exist inside the FieldCollection

func (*FieldCollection) Int64

func (f *FieldCollection) Int64(name string) (int64, error)

Int64 tries to read key name as int64

func (*FieldCollection) Keys added in v2.24.0

func (f *FieldCollection) Keys() (keys []string)

Keys returns a list of all known keys

func (*FieldCollection) MarshalJSON

func (f *FieldCollection) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaller interface

func (*FieldCollection) MarshalYAML

func (f *FieldCollection) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaller interface

func (*FieldCollection) MustBool

func (f *FieldCollection) MustBool(name string, defVal *bool) bool

MustBool is a wrapper around Bool and panics if an error was returned

func (*FieldCollection) MustDuration

func (f *FieldCollection) MustDuration(name string, defVal *time.Duration) time.Duration

MustDuration is a wrapper around Duration and panics if an error was returned

func (*FieldCollection) MustFloat64 added in v2.25.0

func (f *FieldCollection) MustFloat64(name string, defVal *float64) float64

MustFloat64 is a wrapper around Float64 and panics if an error was returned

func (*FieldCollection) MustInt64

func (f *FieldCollection) MustInt64(name string, defVal *int64) int64

MustInt64 is a wrapper around Int64 and panics if an error was returned

func (*FieldCollection) MustString

func (f *FieldCollection) MustString(name string, defVal *string) string

MustString is a wrapper around String and panics if an error was returned

func (*FieldCollection) MustStringSlice added in v2.24.0

func (f *FieldCollection) MustStringSlice(name string, defVal *[]string) []string

MustStringSlice is a wrapper around StringSlice and panics if an error was returned

func (*FieldCollection) Set

func (f *FieldCollection) Set(key string, value any)

Set sets a single key to specified value

func (*FieldCollection) SetFromData

func (f *FieldCollection) SetFromData(data map[string]any)

SetFromData takes a map of data and copies all data into the FieldCollection

func (*FieldCollection) String

func (f *FieldCollection) String(name string) (string, error)

String tries to read key name as string

func (*FieldCollection) StringSlice

func (f *FieldCollection) StringSlice(name string) ([]string, error)

StringSlice tries to read key name as []string

func (*FieldCollection) UnmarshalJSON

func (f *FieldCollection) UnmarshalJSON(raw []byte) error

UnmarshalJSON implements json.Unmarshaller interface

func (*FieldCollection) UnmarshalYAML

func (f *FieldCollection) UnmarshalYAML(unmarshal func(any) error) error

UnmarshalYAML implements yaml.Unmarshaller interface

func (*FieldCollection) ValidateSchema added in v2.24.0

func (f *FieldCollection) ValidateSchema(opts ...ValidateOpt) error

ValidateSchema can be used to validate the contents of the FieldCollection by passing in field definitions which may be there or must be there and to check whether there are no surplus fields

type SchemaField added in v2.24.0

type SchemaField struct {
	// Name of the field to validate
	Name string
	// If set to true the field must i.e. not be "" for a string field
	NonEmpty bool
	// The expected type of the field
	Type SchemaFieldType
}

SchemaField defines how a field is expected to be

type SchemaFieldType added in v2.24.0

type SchemaFieldType uint64

SchemaFieldType is a collection of known field types for which can be checked

const (
	SchemaFieldTypeAny SchemaFieldType = iota
	SchemaFieldTypeBool
	SchemaFieldTypeDuration
	SchemaFieldTypeFloat64
	SchemaFieldTypeInt64
	SchemaFieldTypeString
	SchemaFieldTypeStringSlice
)

Collection of known field types for which can be checked

type ValidateOpt added in v2.24.0

type ValidateOpt func(f, validateStore *FieldCollection) error

ValidateOpt is a validation function to be executed during the validation call

func CanHaveField added in v2.24.0

func CanHaveField(field SchemaField) ValidateOpt

CanHaveField validates the type of the field if it exists and puts the field to the allow-list for MustHaveNoUnknowFields

func MustHaveField added in v2.24.0

func MustHaveField(field SchemaField) ValidateOpt

MustHaveField validates the type of the field and puts the field to the allow-list for MustHaveNoUnknowFields

Jump to

Keyboard shortcuts

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