This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate the module in favor of github.com/godogx/expandvars
- Loading branch information
Showing
14 changed files
with
40 additions
and
686 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
// Package expandog provides functionalities to expand variables in cucumber steps. | ||
// | ||
// Deprecated: Use github.com/godogx/expandvars instead. | ||
package expandog |
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 |
---|---|---|
@@ -1,100 +1,37 @@ | ||
package expandog | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"github.com/godogx/expandvars" | ||
) | ||
|
||
const varDelimiter = "$" | ||
|
||
// ErrUnsupportedExpander indicates that the provided expander is not supported. | ||
var ErrUnsupportedExpander = errors.New("unsupported expander") | ||
// | ||
// Deprecated: Use expandvars.ErrUnsupportedExpander instead. | ||
var ErrUnsupportedExpander = expandvars.ErrUnsupportedExpander | ||
|
||
// EnvExpander expands variables using env vars. | ||
var EnvExpander Expander = os.ExpandEnv | ||
// | ||
// Deprecated: Use expandvars.EnvExpander instead. | ||
var EnvExpander = expandvars.EnvExpander | ||
|
||
// Pairs is a pair of old and new to be replaced. | ||
type Pairs = map[string]string | ||
// | ||
// Deprecated: Use expandvars.Pairs instead. | ||
type Pairs = expandvars.Pairs | ||
|
||
// Expander expands the variables in a string. | ||
type Expander func(string) string | ||
// | ||
// Deprecated: Use expandvars.Expander instead. | ||
type Expander = expandvars.Expander | ||
|
||
// Replacer replace string. | ||
type Replacer interface { | ||
Replace(string) string | ||
} | ||
// | ||
// Deprecated: Use expandvars.Replacer instead. | ||
type Replacer = expandvars.Replacer | ||
|
||
// BeforeScenario expands variables from a provider that will be called only once before every scenario. | ||
// | ||
// Deprecated: Use expandvars.BeforeScenario instead. | ||
func BeforeScenario(provide func() Pairs) func() Expander { | ||
return func() Expander { | ||
return mapExpander(provide()) | ||
} | ||
} | ||
|
||
func placeholder(name string) string { | ||
return fmt.Sprintf("%s%s", varDelimiter, name) | ||
} | ||
|
||
// runtimeExpander expands variables from a provider. | ||
func runtimeExpander(provide func() Pairs) Expander { | ||
return func(s string) string { | ||
return mapExpander(provide())(s) | ||
} | ||
} | ||
|
||
// mapExpander initiates a new variable expander from a map of values. | ||
func mapExpander(pairs Pairs) Expander { | ||
oldNew := make([]string, 0, 2*len(pairs)) | ||
|
||
for k, v := range pairs { | ||
oldNew = append(oldNew, placeholder(k), v) | ||
} | ||
|
||
return strings.NewReplacer(oldNew...).Replace | ||
} | ||
|
||
func chainExpanders(expanders ...interface{}) Expander { | ||
l := make([]Expander, 0, len(expanders)) | ||
|
||
for _, e := range expanders { | ||
l = append(l, newExpander(e)) | ||
} | ||
|
||
return func(s string) string { | ||
for _, expand := range l { | ||
s = expand(s) | ||
} | ||
|
||
return s | ||
} | ||
} | ||
|
||
func newExpander(e interface{}) Expander { | ||
switch e := e.(type) { | ||
case Pairs: | ||
return mapExpander(e) | ||
|
||
case func() Pairs: | ||
return runtimeExpander(e) | ||
|
||
case func() Expander: | ||
return e() | ||
|
||
case Replacer: | ||
return e.Replace | ||
|
||
case Expander: | ||
return e | ||
|
||
case func(string) string: | ||
return e | ||
} | ||
|
||
panic(fmt.Errorf("%w: got %T", ErrUnsupportedExpander, e)) | ||
} | ||
|
||
func doExpand(expand Expander, s string) string { | ||
return expand(s) | ||
return expandvars.BeforeScenario(provide) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.