opt

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 6, 2022 License: GPL-2.0, GPL-2.0-or-later Imports: 10 Imported by: 0

README

Package opt provides exclusive access and parsed input of generic options.

See examples,


© 2022 Platina Systems, Inc. All rights reserved. Use of this source code is governed by this BSD-style LICENSE.

Documentation

Overview

Package opt provides exclusive access and parsed input of generic options.

Example (Struct_init)
text, err := json.MarshalIndent(StructExample{}, "", "  ")
if err != nil {
	fmt.Println(err)
} else {
	os.Stdout.Write(text)
}
Output:

{
  "Scalar": {
    "Bool": false,
    "String": "",
    "Int": 0,
    "Float": 0,
    "Addr": "invalid IP",
    "AddrPort": "invalid AddrPort",
    "Prefix": "invalid Prefix",
    "Duration": "0s",
    "URL": ""
  },
  "Slice": {
    "Ints": [],
    "Floats": [],
    "Strings": [],
    "Prefixes": [],
    "Structs": null
  }
}
Example (Struct_toml)
var x StructExample
_, err := toml.Decode(`
[[opt]]

[scalar]
bool = true
string = "hello world"
int = 789
float = 7.89
addr = "192.168.2.2"
addr_port = "192.168.2.2:80"
prefix = "192.168.2.2/24"
duration = "15s"
url = "https://golang.org/pkg/flag"

[slice]
ints = [ 3, 2, 1 ]
floats = [ 3.3, 2.2, 1.1 ]
strings = [
        "hello world",
        "bonjour le monde",
        "こんにちは世界",
]
prefixes = [
        "192.168.1.1/32",
        "192.168.1.2/32",
        "192.168.1.3/32",
    ]
[[slice.structs]]
number = 123
string = "hello world"
[[slice.structs]]
number = 456
string = "bonjour le monde"
[[slice.structs]]
number = 789
string = "こんにちは世界"
`, &x)
if err != nil {
	fmt.Println(err)
	return
}
text, err := json.MarshalIndent(&x, "", "  ")
if err != nil {
	fmt.Println(err)
} else {
	os.Stdout.Write(text)
}
Output:

{
  "Scalar": {
    "Bool": true,
    "String": "hello world",
    "Int": 789,
    "Float": 7.89,
    "Addr": "192.168.2.2",
    "AddrPort": "192.168.2.2:80",
    "Prefix": "192.168.2.2/24",
    "Duration": "15s",
    "URL": "https://golang.org/pkg/flag"
  },
  "Slice": {
    "Ints": [
      3,
      2,
      1
    ],
    "Floats": [
      3.3,
      2.2,
      1.1
    ],
    "Strings": [
      "hello world",
      "bonjour le monde",
      "こんにちは世界"
    ],
    "Prefixes": [
      "192.168.1.1/32",
      "192.168.1.2/32",
      "192.168.1.3/32"
    ],
    "Structs": [
      {
        "Number": 123,
        "String": "hello world"
      },
      {
        "Number": 456,
        "String": "bonjour le monde"
      },
      {
        "Number": 789,
        "String": "こんにちは世界"
      }
    ]
  }
}
Example (Struct_yaml)
var x StructExample
err := yaml.Unmarshal([]byte(`
scalar:
  bool: true
  string: hello world
  int: 789
  float: 7.89
  addr: 192.168.2.2
  addr_port: 192.168.2.2:80
  prefix: 192.168.2.2/24
  duration: "55s"
  url: https://golang.org/pkg/flag

slice:
  ints:
    - 3
    - 2
    - 1
  floats:
    - 3.3
    - 2.2
    - 1.1
  strings:
    - hello world
    - bonjour le monde
    - こんにちは世界
  prefixes:
    - 192.168.1.1/32
    - 192.168.1.2/32
    - 192.168.1.3/32
  structs:
    - number: 123
      string: hello world
    - number: 456
      string: bonjour le monde
    - number: 789
      string: こんにちは世界
`), &x)
if err != nil {
	fmt.Println(err)
	return
}
text, err := yaml.Marshal(&x)
if err != nil {
	fmt.Println(err)
} else {
	os.Stdout.Write(text)
}
Output:

scalar:
  bool: true
  string: hello world
  int: 789
  float: 7.89
  addr: 192.168.2.2
  addr_port: 192.168.2.2:80
  prefix: 192.168.2.2/24
  duration: 55s
  url: https://golang.org/pkg/flag
slice:
  ints:
  - 3
  - 2
  - 1
  floats:
  - 3.3
  - 2.2
  - 1.1
  strings:
  - hello world
  - bonjour le monde
  - こんにちは世界
  prefixes:
  - 192.168.1.1/32
  - 192.168.1.2/32
  - 192.168.1.3/32
  structs:
  - number: 123
    string: hello world
  - number: 456
    string: bonjour le monde
  - number: 789
    string: こんにちは世界

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Subscribe

func Subscribe(ch chan<- uintptr)

Subscribe to option change notifications.

Example
var updates int
var n Number[int]
pn := uintptr(unsafe.Pointer(&n))
ch := make(chan uintptr, 4)
Subscribe(ch)
defer Unsubscribe(ch)
go func() {
	defer close(ch)
	for _, mod := range []int{3, 2, 1} {
		n.Store(mod)
	}
}()
for p := range ch {
	if p != pn {
		fmt.Println("mismatch")
	} else {
		updates += 1
	}
}
fmt.Print(updates)
Output:

3

func Unsubscribe

func Unsubscribe(ch chan<- uintptr)

Unsubscribe to option change notifications.

Types

type Addr

type Addr = NetIP[netip.Addr]

type AddrPort

type AddrPort = NetIP[netip.AddrPort]

type AddrPorts

type AddrPorts = NetIPs[netip.AddrPort]

type Addrs

type Addrs = NetIPs[netip.Addr]

type Bool

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

func NewBool

func NewBool(v bool) *Bool

func (Bool) IsBoolFlag

func (opt Bool) IsBoolFlag() bool

func (Bool) MarshalJSON

func (opt Bool) MarshalJSON() ([]byte, error)

func (Bool) MarshalText

func (opt Bool) MarshalText() ([]byte, error)

func (Bool) MarshalYAML

func (opt Bool) MarshalYAML() (interface{}, error)

func (*Bool) Set

func (opt *Bool) Set(s string) error

func (*Bool) Store

func (opt *Bool) Store(v bool) error

func (Bool) String

func (opt Bool) String() string

func (*Bool) UnmarshalJSON

func (opt *Bool) UnmarshalJSON(text []byte) error

func (*Bool) UnmarshalText

func (opt *Bool) UnmarshalText(text []byte) error

func (Bool) Value

func (opt Bool) Value() bool

type Duration

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

func LimitedDuration

func LimitedDuration(v, min, max time.Duration) *Duration
Example
fmt.Print(LimitedDuration(3*time.Second, 1*time.Second, 5*time.Second).
	Store(10 * time.Second))
Output:

10s > max{5s}

func MustParseDuration

func MustParseDuration(s string) *Duration

func NewDuration

func NewDuration(v time.Duration) *Duration

func (Duration) MarshalJSON

func (opt Duration) MarshalJSON() ([]byte, error)

func (Duration) MarshalText

func (opt Duration) MarshalText() ([]byte, error)

func (Duration) MarshalYAML

func (opt Duration) MarshalYAML() (interface{}, error)

func (*Duration) Set

func (opt *Duration) Set(s string) error

func (*Duration) Store

func (opt *Duration) Store(v time.Duration) error

func (Duration) String

func (opt Duration) String() string

func (*Duration) UnmarshalJSON

func (opt *Duration) UnmarshalJSON(text []byte) error

func (*Duration) UnmarshalText

func (opt *Duration) UnmarshalText(text []byte) error

func (Duration) Value

func (opt Duration) Value() time.Duration

type Env

type Env map[string]func(string) error

Associate environment variable names with Opt[T].Set()

Example
var x StructExample
env := Env{
	"BOOL":      x.Scalar.Bool.Set,
	"STRING":    x.Scalar.String.Set,
	"INT":       x.Scalar.Int.Set,
	"FLOAT":     x.Scalar.Float.Set,
	"DURATION":  x.Scalar.Duration.Set,
	"ADDR":      x.Scalar.Addr.Set,
	"ADDR_PORT": x.Scalar.AddrPort.Set,
	"PREFIX":    x.Scalar.Prefix.Set,
	"URL":       x.Scalar.URL.Set,
}
err := env.Set(
	"BOOL",
	"STRING=hello world",
	"INT=321",
	"FLOAT=3.21",
	"ADDR=10.1.1.1",
	"ADDR_PORT=10.1.1.1:80",
	"PREFIX=10.1.1.1/24",
	"DURATION=5m",
	"URL=https://golang.org/pkg/flag",
)
if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(x.Scalar.Bool)
	fmt.Println(x.Scalar.String)
	fmt.Println(x.Scalar.Int)
	fmt.Println(x.Scalar.Float)
	fmt.Println(x.Scalar.Duration)
	fmt.Println(x.Scalar.Addr)
	fmt.Println(x.Scalar.AddrPort)
	fmt.Println(x.Scalar.Prefix)
	fmt.Println(x.Scalar.URL)
}
Output:

true
hello world
321
3.21
5m0s
10.1.1.1
10.1.1.1:80
10.1.1.1/24
https://golang.org/pkg/flag

func (Env) Set

func (env Env) Set(args ...string) error

Set options from os.Environ() or given list of KEY=VALUEs.

type NetIP

type NetIP[T netip.Addr | netip.AddrPort | netip.Prefix] struct {
	// contains filtered or unexported fields
}

func MustParseAddr

func MustParseAddr(s string) *NetIP[netip.Addr]

func MustParseAddrPort

func MustParseAddrPort(s string) *NetIP[netip.AddrPort]

func MustParsePrefix

func MustParsePrefix(s string) *NetIP[netip.Prefix]

func (NetIP[T]) Format

func (opt NetIP[T]) Format(f fmt.State, verb rune)

func (NetIP[T]) MarshalJSON

func (opt NetIP[T]) MarshalJSON() ([]byte, error)

func (NetIP[T]) MarshalText

func (opt NetIP[T]) MarshalText() ([]byte, error)

func (NetIP[T]) MarshalYAML

func (opt NetIP[T]) MarshalYAML() (interface{}, error)

func (*NetIP[T]) Set

func (opt *NetIP[T]) Set(s string) error

func (NetIP[T]) String

func (opt NetIP[T]) String() string

func (*NetIP[T]) UnmarshalJSON

func (opt *NetIP[T]) UnmarshalJSON(text []byte) error

func (*NetIP[T]) UnmarshalText

func (opt *NetIP[T]) UnmarshalText(text []byte) error

func (NetIP[T]) Value

func (opt NetIP[T]) Value() T

type NetIPs

type NetIPs[T netip.Addr | netip.AddrPort | netip.Prefix] struct {
	// contains filtered or unexported fields
}

func NewAddrPorts

func NewAddrPorts(v []netip.AddrPort) *NetIPs[netip.AddrPort]

func NewAddrs

func NewAddrs(v []netip.Addr) *NetIPs[netip.Addr]

func NewPrefixes

func NewPrefixes(v []netip.Prefix) *NetIPs[netip.Prefix]

func (NetIPs[T]) MarshalJSON

func (opt NetIPs[T]) MarshalJSON() ([]byte, error)

func (NetIPs[T]) MarshalYAML

func (opt NetIPs[T]) MarshalYAML() (interface{}, error)

func (*NetIPs[T]) Store

func (opt *NetIPs[T]) Store(v []T) error

func (*NetIPs[T]) UnmarshalTOML

func (opt *NetIPs[T]) UnmarshalTOML(input interface{}) error

func (*NetIPs[T]) UnmarshalYAML

func (opt *NetIPs[T]) UnmarshalYAML(unmarshal func(interface{}) error) error

func (NetIPs[T]) Value

func (opt NetIPs[T]) Value() []T

type Number

type Number[T Numeric] struct {
	// contains filtered or unexported fields
}

func LimitedNumber

func LimitedNumber[T Numeric](v, min, max T) *Number[T]
Example
fmt.Print(LimitedNumber[uint](100, 100, 200).
	Store(201))
Output:

201 > max{200}

func NewNumber

func NewNumber[T Numeric](v T) *Number[T]

func (Number[T]) MarshalJSON

func (opt Number[T]) MarshalJSON() ([]byte, error)

func (Number[T]) MarshalText

func (opt Number[T]) MarshalText() ([]byte, error)

func (Number[T]) MarshalYAML

func (opt Number[T]) MarshalYAML() (interface{}, error)

func (*Number[T]) Set

func (opt *Number[T]) Set(s string) error

func (*Number[T]) Store

func (opt *Number[T]) Store(v T) error

func (Number[T]) String

func (opt Number[T]) String() string

func (*Number[T]) UnmarshalJSON

func (opt *Number[T]) UnmarshalJSON(text []byte) error

func (*Number[T]) UnmarshalText

func (opt *Number[T]) UnmarshalText(text []byte) error

func (Number[T]) Value

func (opt Number[T]) Value() T

type Numbers

type Numbers[T Numeric] struct {
	// contains filtered or unexported fields
}

func NewNumbers

func NewNumbers[T Numeric](v []T) *Numbers[T]

func (Numbers[T]) MarshalJSON

func (opt Numbers[T]) MarshalJSON() ([]byte, error)

func (Numbers[T]) MarshalText

func (opt Numbers[T]) MarshalText() ([]byte, error)

func (Numbers[T]) MarshalYAML

func (opt Numbers[T]) MarshalYAML() (interface{}, error)

func (*Numbers[T]) Store

func (opt *Numbers[T]) Store(v []T) error

func (Numbers[T]) String

func (opt Numbers[T]) String() string

func (*Numbers[T]) UnmarshalTOML

func (opt *Numbers[T]) UnmarshalTOML(input interface{}) error

func (*Numbers[T]) UnmarshalYAML

func (opt *Numbers[T]) UnmarshalYAML(unmarshal func(interface{}) error) error

func (Numbers[T]) Value

func (opt Numbers[T]) Value() []T

type Numeric

type Numeric interface {
	~float32 | ~float64 |
		~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Prefix

type Prefix = NetIP[netip.Prefix]

type Prefixes

type Prefixes = NetIPs[netip.Prefix]

type String

type String[T ~string] struct {
	// contains filtered or unexported fields
}

func Alias

func Alias[T ~string](name T, aka ...T) *String[T]

Alias returns a string option that assures a new string matches either primary name or one of the aliasws before it's update.

Example
fmt.Print(Alias[string]("Thomas", "Tom", "Tommy").Set("Tommey"))
Output:

"Tommey" invalid

func NewString

func NewString[T ~string](v T) *String[T]

func (String[T]) MarshalJSON

func (opt String[T]) MarshalJSON() ([]byte, error)

func (String[T]) MarshalText

func (opt String[T]) MarshalText() ([]byte, error)

func (String[T]) MarshalYAML

func (opt String[T]) MarshalYAML() (interface{}, error)

func (*String[T]) Set

func (opt *String[T]) Set(s string) error

func (*String[T]) Store

func (opt *String[T]) Store(v T) error

func (String[T]) String

func (opt String[T]) String() string

func (*String[T]) UnmarshalJSON

func (opt *String[T]) UnmarshalJSON(text []byte) error

func (*String[T]) UnmarshalText

func (opt *String[T]) UnmarshalText(text []byte) error

func (String[T]) Value

func (opt String[T]) Value() T

type Strings

type Strings[T ~string] struct {
	// contains filtered or unexported fields
}

func NewStrings

func NewStrings[T ~string](v []T) *Strings[T]

func (Strings[T]) MarshalJSON

func (opt Strings[T]) MarshalJSON() ([]byte, error)

func (Strings[T]) MarshalText

func (opt Strings[T]) MarshalText() ([]byte, error)

func (Strings[T]) MarshalYAML

func (opt Strings[T]) MarshalYAML() (interface{}, error)

func (*Strings[T]) Store

func (opt *Strings[T]) Store(v []T) error

func (Strings[T]) String

func (opt Strings[T]) String() string

func (*Strings[T]) UnmarshalTOML

func (opt *Strings[T]) UnmarshalTOML(input interface{}) error

func (*Strings[T]) UnmarshalYAML

func (opt *Strings[T]) UnmarshalYAML(unmarshal func(interface{}) error) error

func (Strings[T]) Value

func (opt Strings[T]) Value() []T

type Time

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

func LimitedTime

func LimitedTime(v, min, max time.Time) *Time
Example
fmt.Print(MustParseLimitedTime(
	"2006-01-02T15:04:05Z",
	"2006-01-02T12:00:00Z",
	"2006-01-02T17:00:00Z",
).UnmarshalText([]byte(
	"2006-01-02T17:00:01Z")))
Output:

too late

func MustParseLimitedTime

func MustParseLimitedTime(sv, smin, smax string) *Time

func MustParseTime

func MustParseTime(s string) *Time

func NewTime

func NewTime(v time.Time) *Time

func (Time) MarshalJSON

func (opt Time) MarshalJSON() ([]byte, error)

func (Time) MarshalText

func (opt Time) MarshalText() ([]byte, error)

func (Time) MarshalYAML

func (opt Time) MarshalYAML() (interface{}, error)

func (*Time) Set

func (opt *Time) Set(s string) error

func (*Time) Store

func (opt *Time) Store(v time.Time) error

func (Time) String

func (opt Time) String() string

func (*Time) UnmarshalJSON

func (opt *Time) UnmarshalJSON(text []byte) error

func (*Time) UnmarshalText

func (opt *Time) UnmarshalText(text []byte) error

func (Time) Value

func (opt Time) Value() time.Time

type URL

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

func MustParseURL

func MustParseURL(s string) *URL

func (URL) MarshalJSON

func (opt URL) MarshalJSON() ([]byte, error)

func (URL) MarshalText

func (opt URL) MarshalText() ([]byte, error)

func (URL) MarshalYAML

func (opt URL) MarshalYAML() (interface{}, error)

func (*URL) Set

func (opt *URL) Set(s string) error

func (URL) String

func (opt URL) String() string

func (*URL) UnmarshalJSON

func (opt *URL) UnmarshalJSON(text []byte) error

func (*URL) UnmarshalText

func (opt *URL) UnmarshalText(text []byte) error

func (URL) Value

func (opt URL) Value() url.URL

Jump to

Keyboard shortcuts

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