Documentation
¶
Overview ¶
Package replacer provides a string Replacement mechanism with custom Replacement functions and keywords.
Package replacer provides a string Replacement mechanism with custom Replacement functions and keywords.
Index ¶
- Constants
- func FileReplacer(path string, replacements map[string]string) ([]byte, error)
- func ObjectReplacer(v any, replacements map[string]string) error
- func Replace(content []byte, replacements map[string]string) []byte
- func ReplaceString(content string, replacements map[string]string) string
- type Match
- type MatchSetting
- type Matcher
- type ReplaceFunc
- type Replacement
- type Replacer
- type Setting
Constants ¶
const ( // DefaultStartKeyword defines the default keyword. DefaultStartKeyword = "${" DefaultHostStartKeyword = "@{" DefaultMatchStartKeyword = "@" DefaultEndKeyword = "}" DefaultHostEndKeyword = ":" DefaultMatchEndKeyword = DefaultHostEndKeyword )
Variables ¶
This section is empty.
Functions ¶
func FileReplacer ¶
FileReplacer opens a file, replaces occurrences of ${name} with values from the map, and returns the result as []byte.
func ObjectReplacer ¶ added in v0.0.56
ObjectReplacer replaces occurrences of ${name} with values from the map
Types ¶
type Match ¶ added in v0.0.55
type Match struct {
// contains filtered or unexported fields
}
type MatchSetting ¶
type MatchSetting = func(*Match)
func WithMatchEnd ¶
func WithMatchEnd(end string) MatchSetting
WithMatchEnd returns a new Matcher instance with replacer end keyword.
func WithMatchFold ¶
func WithMatchFold(fold bool) MatchSetting
WithMatchFold returns a new Matcher instance with case-insensitive matching.
func WithMatchHostMap ¶
func WithMatchHostMap(hosts map[string]string) MatchSetting
WithMatchHostMap Parse the name and return the corresponding IP address
func WithMatchHosts ¶
func WithMatchHosts(list []string, sep string) MatchSetting
WithMatchHosts Parse the name and return the corresponding IP address
func WithMatchSta ¶
func WithMatchSta(sta string) MatchSetting
WithMatchSta returns a new Matcher instance with replacer start keyword.
type ReplaceFunc ¶
ReplaceFunc is a function type that accepts a string and returns a replaced string.
type Replacement ¶ added in v0.0.55
type Replacement struct {
// contains filtered or unexported fields
}
Replacement struct implements the Replacer interface, storing Replacement hooks and the current keyword.
func New ¶
func New(ss ...Setting) *Replacement
New returns a new Replacer instance with default settings.
func NewHost ¶
func NewHost(ss ...Setting) *Replacement
func (Replacement) Replace ¶ added in v0.0.55
func (r Replacement) Replace(content []byte, replacements map[string]string) []byte
Replace within the Replacement struct iterates through the values map, applying custom hooks and replacing placeholders found in the source string.
func (Replacement) ReplaceString ¶ added in v0.0.55
func (r Replacement) ReplaceString(content string, replacements map[string]string) string
ReplaceString replaces substrings within the provided content string using the provided key-value pairs.
type Replacer ¶
type Replacer interface {
Replace(content []byte, replacements map[string]string) []byte // Replaces substrings based on provided key-value pairs.
ReplaceString(content string, replacements map[string]string) string
}
Replacer interface defines methods for setting keywords and performing replacements.
type Setting ¶
type Setting = func(*Replacement)
Setting is the setting of replacer.
func WithFold ¶
func WithFold() Setting
WithFold returns a new Replacer instance with string case folding.
func WithHook ¶
func WithHook(hook ReplaceFunc) Setting
WithHook returns a new Replacer instance with the specified hooks.
func WithKeyword ¶
WithKeyword returns a new Replacer instance with the specified keyword.