From 606668f323984f40718c4fe86137a0d92d7c3172 Mon Sep 17 00:00:00 2001 From: "Kevin.Falting" Date: Wed, 13 Dec 2023 16:59:14 -0600 Subject: [PATCH] rename interimValue to proposedValue --- conf.go | 4 ++-- confhandler/default.go | 10 +++++----- confhandler/environmentvariable.go | 6 +++--- confhandler/flag.go | 6 +++--- confhandler/required.go | 12 ++++++------ confhandler/rsa.go | 8 ++++---- stronf/field_test.go | 14 +++++++------- stronf/handler.go | 28 ++++++++++++++-------------- stronf/handler_test.go | 30 +++++++++++++++--------------- 9 files changed, 59 insertions(+), 59 deletions(-) diff --git a/conf.go b/conf.go index 2fa3f5b..8959801 100644 --- a/conf.go +++ b/conf.go @@ -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 diff --git a/confhandler/default.go b/confhandler/default.go index 873b4e2..0390620 100644 --- a/confhandler/default.go +++ b/confhandler/default.go @@ -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 { diff --git a/confhandler/environmentvariable.go b/confhandler/environmentvariable.go index 4ccb6ea..784459e 100644 --- a/confhandler/environmentvariable.go +++ b/confhandler/environmentvariable.go @@ -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 diff --git a/confhandler/flag.go b/confhandler/flag.go index 46ec11a..482d784 100644 --- a/confhandler/flag.go +++ b/confhandler/flag.go @@ -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 { @@ -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 diff --git a/confhandler/required.go b/confhandler/required.go index 6072d3b..efb3486 100644 --- a/confhandler/required.go +++ b/confhandler/required.go @@ -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 } diff --git a/confhandler/rsa.go b/confhandler/rsa.go index bf77aef..fed6184 100644 --- a/confhandler/rsa.go +++ b/confhandler/rsa.go @@ -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) diff --git a/stronf/field_test.go b/stronf/field_test.go index 3f0a410..7633688 100644 --- a/stronf/field_test.go +++ b/stronf/field_test.go @@ -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") } @@ -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 as the - // initial interimValue - return interimValue, nil + // initial proposedValue + return proposedValue, nil } expect := "i'm a default value" @@ -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 } @@ -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 } @@ -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 } diff --git a/stronf/handler.go b/stronf/handler.go index b782749..794e605 100644 --- a/stronf/handler.go +++ b/stronf/handler.go @@ -5,16 +5,16 @@ 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) @@ -22,26 +22,26 @@ 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 diff --git a/stronf/handler_test.go b/stronf/handler_test.go index a85f5ae..bd077e4 100644 --- a/stronf/handler_test.go +++ b/stronf/handler_test.go @@ -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) @@ -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 }), ) @@ -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 }), ) @@ -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 }), @@ -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 }), ) @@ -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 }),