config

package module
v0.0.0-...-4782861 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Created = "created"
	Updated = "updated"
	Deleted = "deleted"
)
View Source
const (
	// Error code prefixes
	ErrCodeNotFound     = "CFG-404" // Not found errors
	ErrCodeInvalidInput = "CFG-400" // Invalid input errors
	ErrCodeInternal     = "CFG-500" // Internal service errors
	ErrCodeConnection   = "CFG-503" // Connection-related errors
	ErrCodePermission   = "CFG-403" // Permission denied errors
)

Error codes for better categorization of errors

Variables

View Source
var (
	// ErrNotFound indicates that the requested configuration was not found
	ErrNotFound = errors.New("configuration not found")

	// ErrInvalidPath indicates that the provided path is invalid
	ErrInvalidPath = errors.New("invalid configuration path")

	// ErrInvalidValue indicates that the provided value is invalid
	ErrInvalidValue = errors.New("invalid configuration value")

	// ErrNodeHasChildren indicates that the node cannot be deleted because it has children
	ErrNodeHasChildren = errors.New("cannot delete node with children")

	// ErrInvalidStructPtr indicates that the provided struct pointer is invalid
	ErrInvalidStructPtr = errors.New("invalid struct pointer")

	// ErrNilHandler indicates that a nil handler was provided
	ErrNilHandler = errors.New("handler cannot be nil")

	// ErrSubscriptionNotFound indicates that the subscription ID was not found
	ErrSubscriptionNotFound = errors.New("subscription not found")

	// ErrConnectionFailed indicates that the connection to ZooKeeper failed
	ErrConnectionFailed = errors.New("failed to connect to ZooKeeper")
)

Functions

func FormatError

func FormatError(code string, err error, context string) error

FormatError creates a standardized error message with a code prefix

func IsConnectionError

func IsConnectionError(err error) bool

IsConnectionError checks if an error is a connection error

func IsInvalidInput

func IsInvalidInput(err error) bool

IsInvalidInput checks if an error is an invalid input error

func IsNotFound

func IsNotFound(err error) bool

IsNotFound checks if an error is a not found error

Types

type ConfigChangeEvent

type ConfigChangeEvent struct {
	Path       string      `json:"path"`
	OldValue   ConfigValue `json:"old_value,omitempty"`
	NewValue   ConfigValue `json:"new_value,omitempty"`
	ChangeType string      `json:"change_type"`
	Timestamp  time.Time   `json:"timestamp"`
}

type ConfigChangeHandler

type ConfigChangeHandler func(event ConfigChangeEvent)

type ConfigService

type ConfigService interface {
	Get(ctx context.Context, path string, watch ...bool) (ConfigValue, error)
	Set(ctx context.Context, path string, value any, watch ...bool) error
	Delete(ctx context.Context, path string) error
	Exists(ctx context.Context, path string) (bool, error)
	List(ctx context.Context, path string) ([]string, error)

	Subscribe(ctx context.Context, path string, handler ConfigChangeHandler) (string, error)
	Unsubscribe(subscriptionID string) error

	GetEffective(ctx context.Context, path string) (ConfigValue, error)

	SetBatch(ctx context.Context, configs map[string]any, watch ...bool) error
	GetBatch(ctx context.Context, paths []string, watch ...bool) (map[string]ConfigValue, error)

	Export(ctx context.Context, rootPath string) (map[string]ConfigValue, error)
	Import(ctx context.Context, configs map[string]ConfigValue, watch ...bool) error

	BindStructWithCallback(ctx context.Context, path string, structPtr any, callback func(any)) (*ConfigStructBinding, error)
	UnbindStruct(binding *ConfigStructBinding) error
	ReloadStruct(ctx context.Context, binding *ConfigStructBinding) error

	SetFromStruct(ctx context.Context, path string, structPtr any) error

	Close() error
}

func NewConfigService

func NewConfigService(zkServers []string, opts ...Option) (ConfigService, error)

NewConfigService creates a new configuration service

type ConfigStructBinding

type ConfigStructBinding struct {
	Path           string
	StructPtr      any
	UpdateCallback func(any)
	// contains filtered or unexported fields
}

type ConfigValue

type ConfigValue struct {
	Type  string `json:"type"`
	Value any    `json:"value"`
}

type Option

type Option func(*ServiceOptions)

Option is a function type for applying options to ServiceOptions

func WithBasePath

func WithBasePath(basePath string) Option

WithBasePath sets the base path in ZooKeeper

func WithDelimiter

func WithDelimiter(delimiter string) Option

WithDelimiter sets the delimiter for hierarchical keys

func WithEnvironment

func WithEnvironment(env string) Option

WithEnvironment sets the environment

func WithNamespace

func WithNamespace(namespace string) Option

WithNamespace sets the namespace

type ServiceOptions

type ServiceOptions struct {
	BasePath    string // Base path in ZooKeeper
	Delimiter   string // Delimiter used for hierarchical keys
	Environment string // Environment (e.g., "dev", "prod")
	Namespace   string // Namespace (e.g., "ecommerce")
}

ServiceOptions contains configuration options for the config service

Jump to

Keyboard shortcuts

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