Skip to content

Commit

Permalink
Export config for replacement function
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Rebhan <[email protected]>
  • Loading branch information
srebhan committed Jun 16, 2023
1 parent e4e3a43 commit 1eee7e9
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,36 @@ type SubstituteFunc func(string, Mapping) (string, bool, error)

// ReplacementFunc is a user-supplied function that is apply to the matching
// substring. Returns the value as a string and an error.
type ReplacementFunc func(string, Mapping, config) (string, error)
type ReplacementFunc func(string, Mapping, *Config) (string, error)

type config struct {
type Config struct {
pattern *regexp.Regexp
substituteFunc SubstituteFunc
replacementFunc ReplacementFunc
logging bool
}

type Option func(*config)
type Option func(*Config)

func WithPattern(pattern *regexp.Regexp) Option {
return func(cfg *config) {
return func(cfg *Config) {
cfg.pattern = pattern
}
}

func WithSubstitutionFunction(subsFunc SubstituteFunc) Option {
return func(cfg *config) {
return func(cfg *Config) {
cfg.substituteFunc = subsFunc
}
}

func WithReplacementFunction(replacementFunc ReplacementFunc) Option {
return func(cfg *config) {
return func(cfg *Config) {
cfg.replacementFunc = replacementFunc
}
}

func WithoutLogging(cfg *config) {
func WithoutLogging(cfg *Config) {
cfg.logging = false
}

Expand All @@ -112,7 +112,7 @@ func WithoutLogging(cfg *config) {
func SubstituteWithOptions(template string, mapping Mapping, options ...Option) (string, error) {
var returnErr error

cfg := &config{
cfg := &Config{
pattern: defaultPattern,
replacementFunc: DefaultReplacementFunc,
logging: true,
Expand All @@ -122,7 +122,7 @@ func SubstituteWithOptions(template string, mapping Mapping, options ...Option)
}

result := cfg.pattern.ReplaceAllStringFunc(template, func(substring string) string {
replacement, err := cfg.replacementFunc(substring, mapping, *cfg)
replacement, err := cfg.replacementFunc(substring, mapping, cfg)
if err != nil {
// Add the template for template errors
var tmplErr *InvalidTemplateError
Expand All @@ -143,7 +143,7 @@ func SubstituteWithOptions(template string, mapping Mapping, options ...Option)
return result, returnErr
}

func DefaultReplacementFunc(substring string, mapping Mapping, cfg config) (string, error) {
func DefaultReplacementFunc(substring string, mapping Mapping, cfg *Config) (string, error) {
pattern := cfg.pattern
subsFunc := cfg.substituteFunc
if subsFunc == nil {
Expand Down

0 comments on commit 1eee7e9

Please sign in to comment.