dynConfigs

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"
	"github.com/adverax/configs"
	"github.com/adverax/fetchers/maps/maps"
	"time"
)

type MyConfig struct {
	BaseConfig
	Name     String  `config:"name"`
	Interval Integer `config:"interval"`
	StartAt  Time    `config:"start_at"`
}

func DefaultConfig() *MyConfig {
	return &MyConfig{
		Name: NewString("unknown"),
	}
}

func main() {
	// This example demonstrates how to use dynamic loading configs.
	//
	// First, create source:
	source := maps.Engine{
		"name":     "My App",
		"interval": int64(10),
		"start_at": time.Now().Format(time.RFC3339),
	}

	// Then create loader:
	loader, err := configs.NewBuilder().
		WithSource(source).
		WithDistinct(true).
		Build()
	if err != nil {
		panic(err)
	}

	// Then load initial configuration:
	config := DefaultConfig()
	Init(config)
	err = loader.Load(config)
	if err != nil {
		panic(err)
	}

	// Then create watcher:
	watcher, err := NewWatcherBuilder().
		WithConfig(config).
		WithLoader(loader).
		WithInterval(10 * time.Second).
		WithNewConfig(func() Config {
			return DefaultConfig()
		}).
		Build()
	if err != nil {
		panic(err)
	}

	// Start watcher:
	watcher.Start()
	defer watcher.Close()

	// Now you can use dynamic config.
	usingConfig(context.Background(), config)

}

func usingConfig(ctx context.Context, config *MyConfig) {
	// For example, print it:
	name, _ := config.Name.Get(ctx)
	interval, _ := config.Interval.Get(ctx)
	fmt.Println(name, interval)
}
Output:

My App 10

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrRequiredFieldNewConfig = errors.New("newConfig is required")
	ErrRequiredFieldLoader    = errors.New("loader is required")
	ErrRequiredFieldConfig    = errors.New("config is required")
)
View Source
var TimeFormat = time.RFC3339

Functions

func Assign

func Assign(dst, src Config)

Assign copies the values from the source structure to the destination structure.

Types

type BaseConfig

type BaseConfig struct {
	sync.RWMutex
}

type Boolean

type Boolean interface {
	Get(ctx context.Context) (bool, error)
}

type BooleanField

type BooleanField struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewBoolean

func NewBoolean(value bool) *BooleanField

func (*BooleanField) Fetch

func (that *BooleanField) Fetch(_ context.Context) (bool, error)

func (*BooleanField) Get

func (that *BooleanField) Get(ctx context.Context) (bool, error)

func (*BooleanField) Init

func (that *BooleanField) Init(c Config)

func (*BooleanField) Let

func (that *BooleanField) Let(_ context.Context, value bool) error

func (*BooleanField) Set

func (that *BooleanField) Set(ctx context.Context, value bool) error

func (*BooleanField) String

func (that *BooleanField) String() string

type BooleanTypeHandler

type BooleanTypeHandler struct {
	configs.BooleanTypeHandler
}

func (*BooleanTypeHandler) New

func (that *BooleanTypeHandler) New(conf Config) interface{}

type Clonable

type Clonable interface {
	Clone() interface{}
}

type Config

type Config interface {
	Lock()
	Unlock()
	RLock()
	RUnlock()
}

func Init

func Init(c Config) Config

Init initializes the structure with pointers to the custom types.

type Duration

type Duration interface {
	Get(ctx context.Context) (time.Duration, error)
}

type DurationField

type DurationField struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewDuration

func NewDuration(value time.Duration) *DurationField

func (*DurationField) Fetch

func (that *DurationField) Fetch(ctx context.Context) (time.Duration, error)

func (*DurationField) Get

func (that *DurationField) Get(ctx context.Context) (time.Duration, error)

func (*DurationField) Init

func (that *DurationField) Init(c Config)

func (*DurationField) Let

func (that *DurationField) Let(ctx context.Context, value time.Duration) error

func (*DurationField) Set

func (that *DurationField) Set(ctx context.Context, value time.Duration) error

func (*DurationField) String

func (that *DurationField) String() string

type DurationTypeHandler

type DurationTypeHandler struct {
	configs.DurationTypeHandler
}

func (*DurationTypeHandler) New

func (that *DurationTypeHandler) New(conf Config) interface{}

type Float

type Float interface {
	Get(ctx context.Context) (float64, error)
}

type FloatField

type FloatField struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewFloat

func NewFloat(value float64) *FloatField

func (*FloatField) Fetch

func (that *FloatField) Fetch(ctx context.Context) (float64, error)

func (*FloatField) Get

func (that *FloatField) Get(ctx context.Context) (float64, error)

func (*FloatField) Init

func (that *FloatField) Init(c Config)

func (*FloatField) Let

func (that *FloatField) Let(ctx context.Context, value float64) error

func (*FloatField) Set

func (that *FloatField) Set(ctx context.Context, value float64) error

func (*FloatField) String

func (that *FloatField) String() string

type FloatTypeHandler

type FloatTypeHandler struct {
	configs.FloatTypeHandler
}

func (*FloatTypeHandler) New

func (that *FloatTypeHandler) New(conf Config) interface{}

type Integer

type Integer interface {
	Get(ctx context.Context) (int64, error)
}

type IntegerField

type IntegerField struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewInteger

func NewInteger(value int64) *IntegerField

func (*IntegerField) Fetch

func (that *IntegerField) Fetch(ctx context.Context) (int64, error)

func (*IntegerField) Get

func (that *IntegerField) Get(ctx context.Context) (int64, error)

func (*IntegerField) Init

func (that *IntegerField) Init(c Config)

func (*IntegerField) Let

func (that *IntegerField) Let(ctx context.Context, value int64) error

func (*IntegerField) Set

func (that *IntegerField) Set(ctx context.Context, value int64) error

func (*IntegerField) String

func (that *IntegerField) String() string

type IntegerTypeHandler

type IntegerTypeHandler struct {
	configs.IntegerTypeHandler
}

func (*IntegerTypeHandler) New

func (that *IntegerTypeHandler) New(conf Config) interface{}

type Loader

type Loader interface {
	Load(config interface{}) error
}

type Logger

type Logger interface {
	WithError(err error) Logger
	Error(msg string)
}

type String

type String interface {
	Get(ctx context.Context) (string, error)
}

type StringField

type StringField struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewString

func NewString(value string) *StringField

func (*StringField) Fetch

func (that *StringField) Fetch(ctx context.Context) (string, error)

func (*StringField) Get

func (that *StringField) Get(ctx context.Context) (string, error)

func (*StringField) Init

func (that *StringField) Init(c Config)

func (*StringField) Let

func (that *StringField) Let(ctx context.Context, value string) error

func (*StringField) Set

func (that *StringField) Set(ctx context.Context, value string) error

func (*StringField) String

func (that *StringField) String() string

type StringTypeHandler

type StringTypeHandler struct {
	configs.StringTypeHandler
}

func (*StringTypeHandler) New

func (that *StringTypeHandler) New(conf Config) interface{}

type Strings

type Strings interface {
	Get(ctx context.Context) ([]string, error)
}

type StringsField

type StringsField struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewStrings

func NewStrings(value []string) *StringsField

func (*StringsField) Fetch

func (that *StringsField) Fetch(ctx context.Context) ([]string, error)

func (*StringsField) Get

func (that *StringsField) Get(ctx context.Context) ([]string, error)

func (*StringsField) Init

func (that *StringsField) Init(c Config)

func (*StringsField) Let

func (that *StringsField) Let(ctx context.Context, value []string) error

func (*StringsField) Set

func (that *StringsField) Set(ctx context.Context, value []string) error

func (*StringsField) String

func (that *StringsField) String() string

type StringsTypeHandler

type StringsTypeHandler struct {
	configs.StringsTypeHandler
}

func (*StringsTypeHandler) New

func (that *StringsTypeHandler) New(conf Config) interface{}

type Time

type Time interface {
	Get(ctx context.Context) (time.Time, error)
}

type TimeField

type TimeField struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewTime

func NewTime(value time.Time) *TimeField

func (*TimeField) Fetch

func (that *TimeField) Fetch(ctx context.Context) (time.Time, error)

func (*TimeField) Get

func (that *TimeField) Get(ctx context.Context) (time.Time, error)

func (*TimeField) Import

func (that *TimeField) Import(ctx context.Context, value interface{}) error

func (*TimeField) Init

func (that *TimeField) Init(c Config)

func (*TimeField) Let

func (that *TimeField) Let(ctx context.Context, value time.Time) error

func (*TimeField) Set

func (that *TimeField) Set(ctx context.Context, value time.Time) error

func (*TimeField) String

func (that *TimeField) String() string

type TimeTypeHandler

type TimeTypeHandler struct {
	configs.TimeTypeHandler
}

func (*TimeTypeHandler) New

func (that *TimeTypeHandler) New(conf Config) interface{}

type TypeHandler

type TypeHandler interface {
	configs.TypeHandler
	New(conf Config) interface{}
}

type Watcher

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

func (*Watcher) Close

func (that *Watcher) Close()

func (*Watcher) Serve

func (that *Watcher) Serve()

func (*Watcher) Start

func (that *Watcher) Start()

type WatcherBuilder

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

func NewWatcherBuilder

func NewWatcherBuilder() *WatcherBuilder

func (*WatcherBuilder) Build

func (that *WatcherBuilder) Build() (*Watcher, error)

func (*WatcherBuilder) WithConfig

func (that *WatcherBuilder) WithConfig(config Config) *WatcherBuilder

func (*WatcherBuilder) WithInterval

func (that *WatcherBuilder) WithInterval(interval time.Duration) *WatcherBuilder

func (*WatcherBuilder) WithLoader

func (that *WatcherBuilder) WithLoader(loader Loader) *WatcherBuilder

func (*WatcherBuilder) WithLogger

func (that *WatcherBuilder) WithLogger(logger Logger) *WatcherBuilder

func (*WatcherBuilder) WithNewConfig

func (that *WatcherBuilder) WithNewConfig(newConfig func() Config) *WatcherBuilder

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳