Skip to content

Commit

Permalink
rename interimValue to proposedValue
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinfalting committed Dec 13, 2023
1 parent 63d61c2 commit 606668f
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 59 deletions.
4 changes: 2 additions & 2 deletions conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func New[T any](opts ...confOptionFunc) (*Conf[T], error) {
}

// Handle will call all of the configured handlers on a single field.
func (c *Conf[T]) Handle(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
func (c *Conf[T]) Handle(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
handler := stronf.CombineHandlers(c.Handlers...)
return handler.Handle(ctx, field, interimValue)
return handler.Handle(ctx, field, proposedValue)
}

// Parse will walk every field and nested field in the provided struct appling
Expand Down
10 changes: 5 additions & 5 deletions confhandler/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import (
// It's typically best just before the Required handler.
type Default struct{}

func (h Default) Handle(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
if interimValue != nil {
return interimValue, nil
func (h Default) Handle(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
if proposedValue != nil {
return proposedValue, nil
}

if !field.IsZero() {
return interimValue, nil
return proposedValue, nil
}

defaultVal, ok := field.LookupTag("conf", "default")
if !ok {
return interimValue, nil
return proposedValue, nil
}

if len(defaultVal) == 0 {
Expand Down
6 changes: 3 additions & 3 deletions confhandler/environmentvariable.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ type EnvironmentVariable struct{}

var _ stronf.Handler = EnvironmentVariable{}

func (ev EnvironmentVariable) Handle(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
func (ev EnvironmentVariable) Handle(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
environmentVariable, ok := field.LookupTag("conf", "env")
if !ok {
return interimValue, nil
return proposedValue, nil
}

val, ok := os.LookupEnv(environmentVariable)
if !ok {
return interimValue, nil
return proposedValue, nil
}

return val, nil
Expand Down
6 changes: 3 additions & 3 deletions confhandler/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ func NewFlag[T any](fset *flag.FlagSet) (*Flag, error) {

var _ stronf.Handler = (*Flag)(nil)

func (f *Flag) Handle(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
func (f *Flag) Handle(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
flagName, ok := field.LookupTag("conf", "flag")
if !ok {
return interimValue, nil
return proposedValue, nil
}

if err := f.Parse(); err != nil {
Expand All @@ -137,7 +137,7 @@ func (f *Flag) Handle(ctx context.Context, field stronf.Field, interimValue any)

valueFn, ok := f.parsedFlags[flagName]
if !ok {
return interimValue, nil
return proposedValue, nil
}

return valueFn(), nil
Expand Down
12 changes: 6 additions & 6 deletions confhandler/required.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ import (
// last handler.
type Required struct{}

func (h Required) Handle(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
if interimValue != nil && reflect.ValueOf(interimValue).IsZero() {
return interimValue, nil
func (h Required) Handle(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
if proposedValue != nil && reflect.ValueOf(proposedValue).IsZero() {
return proposedValue, nil
}

if !field.IsZero() {
return interimValue, nil
return proposedValue, nil
}

_, required := field.LookupTag("conf", "required")

if required && interimValue == nil {
if required && proposedValue == nil {
return nil, fmt.Errorf("structconf: required field %s is not set", field.Name())
}

return interimValue, nil
return proposedValue, nil
}
8 changes: 4 additions & 4 deletions confhandler/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ var _ stronf.Handler = (*RSAHandler)(nil)
// Handle applies the RSA decryption process to a given field value. It checks
// for a "secret://" prefix on a string value and decrypts the value if found.
// If a previous handler has attempted to set a value, represented by the
// interimValue, this handler will decrypt the interimValue, otherwise it will
// proposedValue, this handler will decrypt the proposedValue, otherwise it will
// decrypt the field value.
func (r *RSAHandler) Handle(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
func (r *RSAHandler) Handle(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
val := field.Value()
if interimValue != nil {
val = interimValue
if proposedValue != nil {
val = proposedValue
}

ciphertext, ok := val.(string)
Expand Down
14 changes: 7 additions & 7 deletions stronf/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestField_Parse(t *testing.T) {
})

t.Run("should return an error if handler returns error and not update the field", func(t *testing.T) {
var handler stronf.HandlerFunc = func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
var handler stronf.HandlerFunc = func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
return nil, errors.New("an error")
}

Expand Down Expand Up @@ -210,10 +210,10 @@ func TestField_Parse(t *testing.T) {
})

t.Run("should not override struct value if handler returns nil", func(t *testing.T) {
var handler stronf.HandlerFunc = func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
var handler stronf.HandlerFunc = func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
// This additionally ensures that Field_Parse is passing in <nil> as the
// initial interimValue
return interimValue, nil
// initial proposedValue
return proposedValue, nil
}

expect := "i'm a default value"
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestField_Parse(t *testing.T) {

t.Run("should set value returned by handler", func(t *testing.T) {
expect := "i'm a value returned by the handler"
var handler stronf.HandlerFunc = func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
var handler stronf.HandlerFunc = func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
return expect, nil
}

Expand Down Expand Up @@ -279,7 +279,7 @@ func TestField_Parse(t *testing.T) {
})

t.Run("should error if value returned by handler is wrong type", func(t *testing.T) {
var handler stronf.HandlerFunc = func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
var handler stronf.HandlerFunc = func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
return 55, nil
}

Expand Down Expand Up @@ -323,7 +323,7 @@ func TestField_Parse(t *testing.T) {
t.Fatal("failed to Parse:", err)
}

var handler stronf.HandlerFunc = func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
var handler stronf.HandlerFunc = func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
return []byte(expectTimeString), nil
}

Expand Down
28 changes: 14 additions & 14 deletions stronf/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,43 @@ import (
)

// HandlerFunc is a function type that implements the [Handler] interface. It
// takes a [context.Context], a [Field], and a interimValue as arguments. The
// returned value is the new interimValue for the [Field], or an error. For more
// information on how interimValue is used, see the the [Handler] interface.
type HandlerFunc func(ctx context.Context, field Field, interimValue any) (any, error)
// takes a [context.Context], a [Field], and a proposedValue as arguments. The
// returned value is the new proposedValue for the [Field], or an error. For more
// information on how proposedValue is used, see the the [Handler] interface.
type HandlerFunc func(ctx context.Context, field Field, proposedValue any) (any, error)

// Handle allows [HandlerFunc] to implement the [Handler] interface. This method
// executes the [HandlerFunc] with the provided [context.Context], [Field], and
// interimValue.
func (h HandlerFunc) Handle(ctx context.Context, field Field, interimValue any) (any, error) {
return h(ctx, field, interimValue)
// proposedValue.
func (h HandlerFunc) Handle(ctx context.Context, field Field, proposedValue any) (any, error) {
return h(ctx, field, proposedValue)
}

var _ Handler = (HandlerFunc)(nil)

// Handler represents a contract for processing a [Field]'s proposed value in the
// context of configuration.
type Handler interface {
// Handle method accepts a context, a Field, and an interimValue. It returns
// an updated interimValue, or an error. The handler is responsible for
// returning a new interimValue or the one it recieved.
// Handle method accepts a context, a Field, and an proposedValue. It returns
// an updated proposedValue, or an error. The handler is responsible for
// returning a new proposedValue or the one it recieved.
Handle(context.Context, Field, any) (any, error)
}

// CombineHandlers will return a handler that calls each handler in the order
// they are provided. The last handler takes precedence over the one before it.
func CombineHandlers(handlers ...Handler) Handler {
var combinedHandlerFunc HandlerFunc = func(ctx context.Context, field Field, interimValue any) (any, error) {
var combinedHandlerFunc HandlerFunc = func(ctx context.Context, field Field, proposedValue any) (any, error) {
for _, h := range handlers {
result, err := h.Handle(ctx, field, interimValue)
result, err := h.Handle(ctx, field, proposedValue)
if err != nil {
return nil, err
}

interimValue = result
proposedValue = result
}

return interimValue, nil
return proposedValue, nil
}

return combinedHandlerFunc
Expand Down
30 changes: 15 additions & 15 deletions stronf/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestCombineHandlers(t *testing.T) {

// Test with one handler
t.Run("one handler", func(t *testing.T) {
h := stronf.CombineHandlers(stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
h := stronf.CombineHandlers(stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
return "one", nil
}))
result, err := h.Handle(context.Background(), stronf.Field{}, nil)
Expand All @@ -38,10 +38,10 @@ func TestCombineHandlers(t *testing.T) {
// Test with two handlers
t.Run("two handlers", func(t *testing.T) {
h := stronf.CombineHandlers(
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
return "first", nil
}),
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
return "second", nil
}),
)
Expand All @@ -58,10 +58,10 @@ func TestCombineHandlers(t *testing.T) {
t.Run("error from handler", func(t *testing.T) {
expectedError := errors.New("handler error")
h := stronf.CombineHandlers(
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
return nil, expectedError
}),
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
return "should not be called", nil
}),
)
Expand All @@ -71,15 +71,15 @@ func TestCombineHandlers(t *testing.T) {
}
})

// Test the result of one handler is passed into the interimValue of the next
// Test the result of one handler is passed into the proposedValue of the next
t.Run("pass interim value", func(t *testing.T) {
h := stronf.CombineHandlers(
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
return "first", nil
}),
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
if interimValue != "first" {
t.Errorf("Expected 'first', got %v", interimValue)
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
if proposedValue != "first" {
t.Errorf("Expected 'first', got %v", proposedValue)
}
return "second", nil
}),
Expand All @@ -93,13 +93,13 @@ func TestCombineHandlers(t *testing.T) {
}
})

// Test the final interimValue is returned
// Test the final proposedValue is returned
t.Run("final interim value", func(t *testing.T) {
h := stronf.CombineHandlers(
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
return "first", nil
}),
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
return "final", nil
}),
)
Expand All @@ -116,11 +116,11 @@ func TestCombineHandlers(t *testing.T) {
t.Run("handler order", func(t *testing.T) {
var order []string
h := stronf.CombineHandlers(
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
order = append(order, "first")
return nil, nil
}),
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, interimValue any) (any, error) {
stronf.HandlerFunc(func(ctx context.Context, field stronf.Field, proposedValue any) (any, error) {
order = append(order, "second")
return nil, nil
}),
Expand Down

0 comments on commit 606668f

Please sign in to comment.