settings

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 0 Imported by: 24

README

Settings

This is a simple example of using generics in Go.

Example

type Setting = settings.Setting[Serialize]

func WithValue(value string) Setting {
	return func(o *Serialize) {
		o.Value = value
	}
}

type Serialize struct {
	Value    string
}

var (
	defaultSerialize = Serialize{
		Value:    "hello",
}

func NewSerialize(ts ...Setting) *Serialize {
	serialize := settings.Apply(&defaultSerialize, ts)
	return serialize
}

func main() {
	serialize = NewSerialize()
	fmt.Println(serialize.Value)
	// Output: hello
	serialize := NewSerialize(WithValue("world"))
	fmt.Println(serialize.Value)
	// Output: world
}

Documentation

Overview

Package settings implements the functions, types, and interfaces for the module.

Package settings implements the functions, types, and interfaces for the module.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[S any](s *S, fs []func(*S)) *S

Apply is apply settings

func ApplyAny

func ApplyAny[S any](s *S, fs []interface{}) *S

ApplyAny Applies a set of setting functions to a struct. These Settings functions can be ApplyFunc[S] or func(*S), or objects that implement the ApplySetting interface.

func ApplyDefaults added in v0.1.2

func ApplyDefaults[S any](s *S, fs []func(*S)) *S

ApplyDefaults applies the given settings and default settings to the provided value.

It first applies the given settings using the Apply function, then checks if the value implements the Defaulter interface. If it does, it calls the ApplyDefaults method to apply the default settings.

func ApplyDefaultsOr added in v0.1.2

func ApplyDefaultsOr[S any](s *S, fs ...func(*S)) *S

ApplyDefaultsOr applies the given settings and default settings to the provided value.

It is a convenience wrapper around ApplyDefaults that accepts a variable number of setting functions.

func ApplyDefaultsOrZero added in v0.1.2

func ApplyDefaultsOrZero[S any](fs ...func(*S)) *S

ApplyDefaultsOrZero applies the given settings and default settings to a zero value of the type.

It creates a zero value of the type, then calls ApplyDefaults to apply the given settings and default settings.

func ApplyOr

func ApplyOr[S any](s *S, fs ...func(*S)) *S

ApplyOr is an apply settings with defaults

func ApplyOrZero

func ApplyOrZero[S any](fs ...func(*S)) *S

ApplyOrZero is an apply settings with defaults

Types

type ApplyFunc

type ApplyFunc[S any] func(*S)

ApplyFunc is a ApplyFunc function for Apply

func (ApplyFunc[S]) Apply

func (s ApplyFunc[S]) Apply(v *S)

type ApplySetting

type ApplySetting[S any] interface {
	Apply(v *S)
}

type Defaulter added in v0.1.2

type Defaulter interface {
	// ApplyDefaults applies the default settings to the implementing type.
	ApplyDefaults()
}

Defaulter is an interface that provides a method to apply default settings.

type Setting

type Setting[S any] interface {
	func(*S) | ApplyFunc[S]
}

Jump to

Keyboard shortcuts

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