vendor: github.com/lestrrat-go/httprc/v3 v3.0.2
full diff: https://github.com/lestrrat-go/httprc/compare/v3.0.1...v3.0.2 Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
+4
@@ -1,6 +1,10 @@
|
||||
Changes
|
||||
=======
|
||||
|
||||
v3.0.2 05 Dev 2025
|
||||
* Code changes mainly due to upgraded linter.
|
||||
* github.com/lestrrat-go/option upgraded to v2
|
||||
|
||||
v3.0.1 18 Aug 2025
|
||||
* Refresh() no longer requires the resource to be ready.
|
||||
|
||||
|
||||
+8
-6
@@ -51,6 +51,9 @@ type Client struct {
|
||||
// By default ALL urls are allowed. This may not be suitable for you if
|
||||
// are using this in a production environment. You are encouraged to specify
|
||||
// a whitelist using the `WithWhitelist` option.
|
||||
//
|
||||
// NOTE: In future versions, this function signature should be changed to
|
||||
// return an error to properly handle option parsing failures.
|
||||
func NewClient(options ...NewClientOption) *Client {
|
||||
//nolint:staticcheck
|
||||
var errSink ErrorSink = errsink.NewNop()
|
||||
@@ -63,19 +66,18 @@ func NewClient(options ...NewClientOption) *Client {
|
||||
defaultMaxInterval := DefaultMaxInterval
|
||||
|
||||
numWorkers := DefaultWorkers
|
||||
//nolint:forcetypeassert
|
||||
for _, option := range options {
|
||||
switch option.Ident() {
|
||||
case identHTTPClient{}:
|
||||
httpcl = option.Value().(HTTPClient)
|
||||
_ = option.Value(&httpcl)
|
||||
case identWorkers{}:
|
||||
numWorkers = option.Value().(int)
|
||||
_ = option.Value(&numWorkers)
|
||||
case identErrorSink{}:
|
||||
errSink = option.Value().(ErrorSink)
|
||||
_ = option.Value(&errSink)
|
||||
case identTraceSink{}:
|
||||
traceSink = option.Value().(TraceSink)
|
||||
_ = option.Value(&traceSink)
|
||||
case identWhitelist{}:
|
||||
wl = option.Value().(Whitelist)
|
||||
_ = option.Value(&wl)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -123,11 +123,12 @@ func (c *controller) Add(ctx context.Context, r Resource, options ...AddOption)
|
||||
c.traceSink.Put(ctx, fmt.Sprintf("httprc controller: START Add(%q)", r.URL()))
|
||||
defer c.traceSink.Put(ctx, fmt.Sprintf("httprc controller: END Add(%q)", r.URL()))
|
||||
waitReady := true
|
||||
//nolint:forcetypeassert
|
||||
for _, option := range options {
|
||||
switch option.Ident() {
|
||||
case identWaitReady{}:
|
||||
waitReady = option.(addOption).Value().(bool)
|
||||
if err := option.Value(&waitReady); err != nil {
|
||||
return fmt.Errorf(`httprc.Controller.Add: failed to parse WaitReady option: %w`, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ package httprc
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/lestrrat-go/option"
|
||||
"github.com/lestrrat-go/option/v2"
|
||||
)
|
||||
|
||||
type NewClientOption interface {
|
||||
|
||||
+13
-6
@@ -41,17 +41,24 @@ func NewResource[T any](s string, transformer Transformer[T], options ...NewReso
|
||||
var interval time.Duration
|
||||
minInterval := DefaultMinInterval
|
||||
maxInterval := DefaultMaxInterval
|
||||
//nolint:forcetypeassert
|
||||
for _, option := range options {
|
||||
switch option.Ident() {
|
||||
case identHTTPClient{}:
|
||||
httpcl = option.Value().(HTTPClient)
|
||||
if err := option.Value(&httpcl); err != nil {
|
||||
return nil, fmt.Errorf(`httprc.NewResource: failed to parse HTTPClient option: %w`, err)
|
||||
}
|
||||
case identMinimumInterval{}:
|
||||
minInterval = option.Value().(time.Duration)
|
||||
if err := option.Value(&minInterval); err != nil {
|
||||
return nil, fmt.Errorf(`httprc.NewResource: failed to parse MinimumInterval option: %w`, err)
|
||||
}
|
||||
case identMaximumInterval{}:
|
||||
maxInterval = option.Value().(time.Duration)
|
||||
if err := option.Value(&maxInterval); err != nil {
|
||||
return nil, fmt.Errorf(`httprc.NewResource: failed to parse MaximumInterval option: %w`, err)
|
||||
}
|
||||
case identConstantInterval{}:
|
||||
interval = option.Value().(time.Duration)
|
||||
if err := option.Value(&interval); err != nil {
|
||||
return nil, fmt.Errorf(`httprc.NewResource: failed to parse ConstantInterval option: %w`, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if transformer == nil {
|
||||
@@ -109,7 +116,7 @@ func (r *ResourceBase[T]) Ready(ctx context.Context) error {
|
||||
// returns `A` or `B` depending on the type of the resource. When accessing the
|
||||
// resource through the `httprc.Resource` interface, use this method to obtain the
|
||||
// stored value.
|
||||
func (r *ResourceBase[T]) Get(dst interface{}) error {
|
||||
func (r *ResourceBase[T]) Get(dst any) error {
|
||||
return blackmagic.AssignIfCompatible(dst, r.Resource())
|
||||
}
|
||||
|
||||
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021 lestrrat-go
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
-245
@@ -1,245 +0,0 @@
|
||||
# option
|
||||
|
||||
Base object for the "Optional Parameters Pattern".
|
||||
|
||||
# DESCRIPTION
|
||||
|
||||
The beauty of this pattern is that you can achieve a method that can
|
||||
take the following simple calling style
|
||||
|
||||
```go
|
||||
obj.Method(mandatory1, mandatory2)
|
||||
```
|
||||
|
||||
or the following, if you want to modify its behavior with optional parameters
|
||||
|
||||
```go
|
||||
obj.Method(mandatory1, mandatory2, optional1, optional2, optional3)
|
||||
```
|
||||
|
||||
Instead of the more clunky zero value for optionals style
|
||||
|
||||
```go
|
||||
obj.Method(mandatory1, mandatory2, nil, "", 0)
|
||||
```
|
||||
|
||||
or the equally clunky config object style, which requires you to create a
|
||||
struct with `NamesThatLookReallyLongBecauseItNeedsToIncludeMethodNamesConfig
|
||||
|
||||
```go
|
||||
cfg := &ConfigForMethod{
|
||||
Optional1: ...,
|
||||
Optional2: ...,
|
||||
Optional3: ...,
|
||||
}
|
||||
obj.Method(mandatory1, mandatory2, &cfg)
|
||||
```
|
||||
|
||||
# SYNOPSIS
|
||||
|
||||
Create an "identifier" for the option. We recommend using an unexported empty struct,
|
||||
because
|
||||
|
||||
1. It is uniquely identifiable globally
|
||||
1. Takes minimal space
|
||||
1. Since it's unexported, you do not have to worry about it leaking elsewhere or having it changed by consumers
|
||||
|
||||
```go
|
||||
// an unexported empty struct
|
||||
type identFeatureX struct{}
|
||||
```
|
||||
|
||||
Then define a method to create an option using this identifier. Here we assume
|
||||
that the option will be a boolean option.
|
||||
|
||||
```go
|
||||
// this is optional, but for readability we usually use a wrapper
|
||||
// around option.Interface, or a type alias.
|
||||
type Option
|
||||
func WithFeatureX(v bool) Option {
|
||||
// use the constructor to create a new option
|
||||
return option.New(identFeatureX{}, v)
|
||||
}
|
||||
```
|
||||
|
||||
Now you can create an option, which essentially a two element tuple consisting
|
||||
of an identifier and its associated value.
|
||||
|
||||
To consume this, you will need to create a function with variadic parameters,
|
||||
and iterate over the list looking for a particular identifier:
|
||||
|
||||
```go
|
||||
func MyAwesomeFunc( /* mandatory parameters omitted */, options ...[]Option) {
|
||||
var enableFeatureX bool
|
||||
// The nolint directive is recommended if you are using linters such
|
||||
// as golangci-lint
|
||||
//nolint:forcetypeassert
|
||||
for _, option := range options {
|
||||
switch option.Ident() {
|
||||
case identFeatureX{}:
|
||||
enableFeatureX = option.Value().(bool)
|
||||
// other cases omitted
|
||||
}
|
||||
}
|
||||
if enableFeatureX {
|
||||
....
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# Option objects
|
||||
|
||||
Option objects take two arguments, its identifier and the value it contains.
|
||||
|
||||
The identifier can be anything, but it's usually better to use a an unexported
|
||||
empty struct so that only you have the ability to generate said option:
|
||||
|
||||
```go
|
||||
type identOptionalParamOne struct{}
|
||||
type identOptionalParamTwo struct{}
|
||||
type identOptionalParamThree struct{}
|
||||
|
||||
func WithOptionOne(v ...) Option {
|
||||
return option.New(identOptionalParamOne{}, v)
|
||||
}
|
||||
```
|
||||
|
||||
Then you can call the method we described above as
|
||||
|
||||
```go
|
||||
obj.Method(m1, m2, WithOptionOne(...), WithOptionTwo(...), WithOptionThree(...))
|
||||
```
|
||||
|
||||
Options should be parsed in a code that looks somewhat like this
|
||||
|
||||
```go
|
||||
func (obj *Object) Method(m1 Type1, m2 Type2, options ...Option) {
|
||||
paramOne := defaultValueParamOne
|
||||
for _, option := range options {
|
||||
switch option.Ident() {
|
||||
case identOptionalParamOne{}:
|
||||
paramOne = option.Value().(...)
|
||||
}
|
||||
}
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
The loop requires a bit of boilerplate, and admittedly, this is the main downside
|
||||
of this module. However, if you think you want use the Option as a Function pattern,
|
||||
please check the FAQ below for rationale.
|
||||
|
||||
# Simple usage
|
||||
|
||||
Most of the times all you need to do is to declare the Option type as an alias
|
||||
in your code:
|
||||
|
||||
```go
|
||||
package myawesomepkg
|
||||
|
||||
import "github.com/lestrrat-go/option"
|
||||
|
||||
type Option = option.Interface
|
||||
```
|
||||
|
||||
Then you can start defining options like they are described in the SYNOPSIS section.
|
||||
|
||||
# Differentiating Options
|
||||
|
||||
When you have multiple methods and options, and those options can only be passed to
|
||||
each one the methods, it's hard to see which options should be passed to which method.
|
||||
|
||||
```go
|
||||
func WithX() Option { ... }
|
||||
func WithY() Option { ... }
|
||||
|
||||
// Now, which of WithX/WithY go to which method?
|
||||
func (*Obj) Method1(options ...Option) {}
|
||||
func (*Obj) Method2(options ...Option) {}
|
||||
```
|
||||
|
||||
In this case the easiest way to make it obvious is to put an extra layer around
|
||||
the options so that they have different types
|
||||
|
||||
```go
|
||||
type Method1Option interface {
|
||||
Option
|
||||
method1Option()
|
||||
}
|
||||
|
||||
type method1Option struct { Option }
|
||||
func (*method1Option) method1Option() {}
|
||||
|
||||
func WithX() Method1Option {
|
||||
return &methodOption{option.New(...)}
|
||||
}
|
||||
|
||||
func (*Obj) Method1(options ...Method1Option) {}
|
||||
```
|
||||
|
||||
This way the compiler knows if an option can be passed to a given method.
|
||||
|
||||
# FAQ
|
||||
|
||||
## Why aren't these function-based?
|
||||
|
||||
Using a base option type like `type Option func(ctx interface{})` is certainly one way to achieve the same goal. In this case, you are giving the option itself the ability to "configure" the main object. For example:
|
||||
|
||||
```go
|
||||
type Foo struct {
|
||||
optionaValue bool
|
||||
}
|
||||
|
||||
type Option func(*Foo) error
|
||||
|
||||
func WithOptionalValue(v bool) Option {
|
||||
return Option(func(f *Foo) error {
|
||||
f.optionalValue = v
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func NewFoo(options ...Option) (*Foo, error) {
|
||||
var f Foo
|
||||
for _, o := range options {
|
||||
if err := o(&f); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return &f
|
||||
}
|
||||
```
|
||||
|
||||
This in itself is fine, but we think there are a few problems:
|
||||
|
||||
### 1. It's hard to create a reusable "Option" type
|
||||
|
||||
We create many libraries using this optional pattern. We would like to provide a default base object. However, this function based approach is not reusuable because each "Option" type requires that it has a context-specific input type. For example, if the "Option" type in the previous example was `func(interface{}) error`, then its usability will significantly decrease because of the type conversion.
|
||||
|
||||
This is not to say that this library's approach is better as it also requires type conversion to convert the _value_ of the option. However, part of the beauty of the original function based approach was the ease of its use, and we claim that this significantly decreases the merits of the function based approach.
|
||||
|
||||
### 2. The receiver requires exported fields
|
||||
|
||||
Part of the appeal for a function-based option pattern is by giving the option itself the ability to do what it wants, you open up the possibility of allowing third-parties to create options that do things that the library authors did not think about.
|
||||
|
||||
```go
|
||||
package thirdparty
|
||||
, but when I read drum sheet music, I kind of get thrown off b/c many times it says to hit the bass drum where I feel like it's a snare hit.
|
||||
func WithMyAwesomeOption( ... ) mypkg.Option {
|
||||
return mypkg.Option(func(f *mypkg) error {
|
||||
f.X = ...
|
||||
f.Y = ...
|
||||
f.Z = ...
|
||||
return nil
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
However, for any third party code to access and set field values, these fields (`X`, `Y`, `Z`) must be exported. Basically you will need an "open" struct.
|
||||
|
||||
Exported fields are absolutely no problem when you have a struct that represents data alone (i.e., API calls that refer or change state information) happen, but we think that casually expose fields for a library struct is a sure way to maintenance hell in the future. What happens when you want to change the API? What happens when you realize that you want to use the field as state (i.e. use it for more than configuration)? What if they kept referring to that field, and then you have concurrent code accessing it?
|
||||
|
||||
Giving third parties complete access to exported fields is like handing out a loaded weapon to the users, and you are at their mercy.
|
||||
|
||||
Of course, providing public APIs for everything so you can validate and control concurrency is an option, but then ... it's a lot of work, and you may have to provide APIs _only_ so that users can refer it in the option-configuration phase. That sounds like a lot of extra work.
|
||||
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
package option
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Interface defines the minimum interface that an option must fulfill
|
||||
type Interface interface {
|
||||
// Ident returns the "identity" of this option, a unique identifier that
|
||||
// can be used to differentiate between options
|
||||
Ident() interface{}
|
||||
|
||||
// Value returns the corresponding value.
|
||||
Value() interface{}
|
||||
}
|
||||
|
||||
type pair struct {
|
||||
ident interface{}
|
||||
value interface{}
|
||||
}
|
||||
|
||||
// New creates a new Option
|
||||
func New(ident, value interface{}) Interface {
|
||||
return &pair{
|
||||
ident: ident,
|
||||
value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pair) Ident() interface{} {
|
||||
return p.ident
|
||||
}
|
||||
|
||||
func (p *pair) Value() interface{} {
|
||||
return p.value
|
||||
}
|
||||
|
||||
func (p *pair) String() string {
|
||||
return fmt.Sprintf(`%v(%v)`, p.ident, p.value)
|
||||
}
|
||||
Reference in New Issue
Block a user