settings

package
v0.9.4 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2025 License: MIT Imports: 18 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CombineJSONFiles

func CombineJSONFiles(files fs.FS) ([]byte, error)

CombineJSONFiles reads a set of JSON config files and combines them in to one file. The expected inputs are:

  • global.json
  • org/*.json
  • owner/*.json
  • workflow/*.json

The directory and file names translate to keys in the JSON structure, while the file extensions are discarded. For example: owner/0x1234.json:Foo.Bar becomes owner.0x1234.Foo.Bar

func CombineTOMLFiles

func CombineTOMLFiles(files fs.FS) ([]byte, error)

CombineTOMLFiles reads a set of TOML config files and combines them in to one file. The expected inputs are:

  • global.toml
  • org/*.toml
  • owner/*.toml
  • workflow/*.toml

The directory and file names translate to keys in the TOML structure, while the file extensions are discarded. For example: owner/0x1234.toml:Foo.Bar becomes owner.0x1234.Foo.Bar

func InitConfig

func InitConfig(a any) error

InitConfig accepts a pointer to a config struct and iterates over all the fields, initializing any Setting fields with a full-qualified, dot-separated key. Every field must either be a Setting or a nested struct following the same rules.

Types

type Getter

type Getter interface {
	// GetScoped returns the value for key, including checking for any Scope based overrides up to scope.
	// The tenant for each scope is based on the contexts.CRE in the ctx.
	GetScoped(ctx context.Context, scope Scope, key string) (value string, err error)
}

Getter fetches scoped settings by key.

func NewJSONGetter

func NewJSONGetter(b []byte) (Getter, error)

NewJSONGetter returns a static Getter backed by the given JSON. TODO https://smartcontract-it.atlassian.net/browse/CAPPL-775 NewJSONRegistry with polling & subscriptions

func NewTOMLGetter

func NewTOMLGetter(b []byte) (Getter, error)

NewTOMLGetter returns a static Getter backed by the given TOML. TODO https://smartcontract-it.atlassian.net/browse/CAPPL-775 NewTOMLRegistry with polling & subscriptions

type Registry

type Registry interface {
	Getter
	// SubscribeScoped returns a channel for updates as an efficient alternative to polling.
	// Stop must be closed to terminate the subscription.
	SubscribeScoped(ctx context.Context, scope Scope, key string) (updates <-chan Update[string], stop func())
}

Registry extends Getter with subscriptions.

type Scope

type Scope int

Scope represents the levels at which settings can be overridden and applied.

const (
	ScopeGlobal Scope = iota
	ScopeOrg
	ScopeOwner
	ScopeWorkflow
)

func ParseScope

func ParseScope(scope string) (Scope, error)

func (Scope) IsTenantRequired

func (s Scope) IsTenantRequired() bool

func (Scope) RoundCRE

func (s Scope) RoundCRE(c contexts.CRE) contexts.CRE

RoundCRE returns a modified CRE with out-of-scope tenants omitted.

func (Scope) String

func (s Scope) String() string

func (Scope) Value

func (s Scope) Value(ctx context.Context) string

Value gets the tenant for this scope from ctx, or return empty string if not set.

type Setting

type Setting[T any] struct {
	Key          string
	DefaultValue T
	Scope        Scope
	Parse        func(string) (T, error) `json:"-" toml:"-"`
	Unit         string
}

Setting holds a key, default value, and parsing function for a particular setting. Use Setting.GetOrDefault with a Getter to look up settings. Use Setting.Subscribe with a Registry to have updates pushed over a channel.

func Duration

func Duration(defaultValue time.Duration) Setting[time.Duration]

func Float32

func Float32(defaultValue float32) Setting[float32]

func Float64

func Float64(defaultValue float64) Setting[float64]

func Int

func Int(defaultValue int) Setting[int]

func Int16

func Int16(defaultValue int16) Setting[int16]

func Int32

func Int32(defaultValue int32) Setting[int32]

func Int64

func Int64(defaultValue int64) Setting[int64]

func Int8

func Int8(defaultValue int8) Setting[int8]

func MarshaledText

func MarshaledText[T encoding.TextUnmarshaler](defaultValue T) Setting[T]

func NewSetting

func NewSetting[T any](defaultValue T, parse func(string) (T, error)) Setting[T]

func Rate

func Rate(defaultLimit rate.Limit, defaultBurst int) Setting[config.Rate]

func Size

func Size(defaultValue config.Size) Setting[config.Size]

func URL

func URL(defaultValue *url.URL) Setting[*url.URL]

func Uint16

func Uint16(defaultValue uint16) Setting[uint16]

func Uint32

func Uint32(defaultValue uint32) Setting[uint32]

func Uint64

func Uint64(defaultValue uint64) Setting[uint64]

func Uint8

func Uint8(defaultValue uint8) Setting[uint8]

func (*Setting[T]) GetOrDefault

func (s *Setting[T]) GetOrDefault(ctx context.Context, g Getter) (value T, err error)

GetOrDefault gets the setting from the Getter for the given Scope, or returns the default value with an error.

func (Setting[T]) MarshalText

func (s Setting[T]) MarshalText() ([]byte, error)

func (*Setting[T]) Subscribe

func (s *Setting[T]) Subscribe(ctx context.Context, r Registry) (<-chan Update[T], func())

func (*Setting[T]) UnmarshalText added in v0.9.4

func (s *Setting[T]) UnmarshalText(b []byte) (err error)

type Update

type Update[T any] struct {
	Value T
	Err   error
}

Directories

Path Synopsis
Package cresettings contains configurable settings definitions for nodes in the CRE.
Package cresettings contains configurable settings definitions for nodes in the CRE.
Package limits helps enforce request-scoped, multi-tenant limits with three kinds of Limiter:
Package limits helps enforce request-scoped, multi-tenant limits with three kinds of Limiter:

Jump to

Keyboard shortcuts

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