gs_dync

package
v1.2.2 Latest Latest
Warning

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

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

Documentation

Overview

Package gs_dync provides dynamic configuration binding and refresh capabilities for Go applications.

This package is built on thread-safe atomic.Value storage with automatic type conversion and supports change listeners, making it suitable for components or services that need to react to configuration updates at runtime.

Key Features:

  • Type-safe and thread-safe encapsulation of configuration values
  • Change notification mechanism using channels
  • Hierarchical key resolution for nested structs and map keys
  • Fine-grained refresh logic that only updates affected objects
  • JSON serialization support for value persistence and transmission

Examples:

Basic value binding:

var v Value[int]
_ = v.onRefresh(conf.Map(map[string]any{"key": 42}), conf.BindParam{Key: "key"})
fmt.Print(v.Value()) // Output: 42

Binding nested structs:

type Config struct {
	Server struct {
		Port Value[int] `value:"${port}"`
	} `value:"${server}"`
}

var cfg Config
_ = p.RefreshField(reflect.ValueOf(&cfg), conf.BindParam{Key: "config"})

Change notification:

listener := v.NewListener()
go func() {
	<-listener.C
	fmt.Print("value changed!")
}()
_ = v.onRefresh(conf.Map(map[string]any{"key": 100}), conf.BindParam{Key: "key"})

This package is ideal for use cases that require hot-reloading of configuration, have complex config structures, or demand reactive behavior to configuration changes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Errors

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

Errors represents a collection of errors.

func (*Errors) Append

func (e *Errors) Append(err error)

Append adds an error to the collection if it is non-nil.

func (*Errors) Error

func (e *Errors) Error() string

Error concatenates all errors into a single string.

func (*Errors) Len

func (e *Errors) Len() int

Len returns the number of errors.

type Listener

type Listener struct {
	C chan struct{}
}

Listener holds a channel to receive notifications.

type Properties

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

Properties manages dynamic properties and refreshable objects.

func New

func New(p conf.Properties) *Properties

New creates and returns a new Properties instance.

func (*Properties) Data

func (p *Properties) Data() conf.Properties

Data returns the current properties.

func (*Properties) ObjectsCount

func (p *Properties) ObjectsCount() int

ObjectsCount returns the number of registered refreshable objects.

func (*Properties) Refresh

func (p *Properties) Refresh(prop conf.Properties) (err error)

Refresh updates the properties and refreshes all bound objects as necessary.

func (*Properties) RefreshField

func (p *Properties) RefreshField(v reflect.Value, param conf.BindParam) error

RefreshField refreshes a field of a bean, optionally registering it as refreshable.

type Value

type Value[T any] struct {
	// contains filtered or unexported fields
}

Value represents a thread-safe object that can dynamically refresh its value.

func (*Value[T]) MarshalJSON

func (r *Value[T]) MarshalJSON() ([]byte, error)

MarshalJSON serializes the stored value as JSON.

func (*Value) NewListener

func (r *Value) NewListener() *Listener

NewListener creates and registers a new listener.

func (*Value[T]) Value

func (r *Value[T]) Value() T

Value retrieves the current value stored in the object. If no value is set, it returns the zero value for the type T.

Jump to

Keyboard shortcuts

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