-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make mutators a full-fledged interface (#94)
- Loading branch information
Showing
4 changed files
with
141 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright The envconfig Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package envconfig | ||
|
||
import "context" | ||
|
||
type Mutator interface { | ||
EnvMutate(ctx context.Context, originalKey, resolvedKey, originalValue, currentValue string) (newValue string, stop bool, err error) | ||
} | ||
|
||
// MutatorFunc is a function that mutates a given value before it is passed | ||
// along for processing. This is useful if you want to mutate the environment | ||
// variable value before it's converted to the proper type. | ||
// | ||
// - `originalKey` is the unmodified environment variable name as it was defined | ||
// on the struct. | ||
// | ||
// - `resolvedKey` is the fully-resolved environment variable name, which may | ||
// include prefixes or modifications from processing. When there are | ||
// no modifications, this will be equivalent to `originalKey`. | ||
// | ||
// - `originalValue` is the unmodified environment variable's value before any | ||
// mutations were run. | ||
// | ||
// - `currentValue` is the currently-resolved value, which may have been | ||
// modified by previous mutators and may be modified in the future by | ||
// subsequent mutators in the stack. | ||
// | ||
// It returns the new value, a boolean which indicates whether future mutations | ||
// in the stack should be applied, and any errors that occurred. | ||
type MutatorFunc func(ctx context.Context, originalKey, resolvedKey, originalValue, currentValue string) (newValue string, stop bool, err error) | ||
|
||
func (m MutatorFunc) EnvMutate(ctx context.Context, originalKey, resolvedKey, originalValue, currentValue string) (newValue string, stop bool, err error) { | ||
return m(ctx, originalKey, resolvedKey, originalValue, currentValue) | ||
} | ||
|
||
// LegacyMutatorFunc is a helper that eases the transition from the previous | ||
// MutatorFunc signature. It wraps the previous-style mutator function and | ||
// returns a new one. Since the former mutator function had less data, this is | ||
// inherently lossy. | ||
// | ||
// DEPRECATED: Change type signatures to [MutatorFunc] instead. | ||
func LegacyMutatorFunc(fn func(ctx context.Context, key, value string) (string, error)) MutatorFunc { | ||
return func(ctx context.Context, originalKey, resolvedKey, originalValue, currentValue string) (newValue string, stop bool, err error) { | ||
v, err := fn(ctx, originalKey, currentValue) | ||
return v, true, err | ||
} | ||
} |