Bumps the minor-and-patch group with 8 updates in the / directory: | Package | From | To | | --- | --- | --- | | [github.com/gookit/config/v2](https://github.com/gookit/config) | `2.2.8` | `2.2.9` | | [github.com/jellydator/ttlcache/v3](https://github.com/jellydator/ttlcache) | `3.4.0` | `3.4.1` | | [github.com/kovidgoyal/imaging](https://github.com/kovidgoyal/imaging) | `1.8.21` | `1.8.22` | | [github.com/nats-io/nats-server/v2](https://github.com/nats-io/nats-server) | `2.14.2` | `2.14.3` | | [github.com/onsi/ginkgo/v2](https://github.com/onsi/ginkgo) | `2.31.0` | `2.32.0` | | [github.com/onsi/gomega](https://github.com/onsi/gomega) | `1.42.0` | `1.42.1` | | [github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) | `1.17.1` | `1.18.2` | | [google.golang.org/grpc](https://github.com/grpc/grpc-go) | `1.81.1` | `1.82.0` | Updates `github.com/gookit/config/v2` from 2.2.8 to 2.2.9 - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.8...v2.2.9) Updates `github.com/jellydator/ttlcache/v3` from 3.4.0 to 3.4.1 - [Release notes](https://github.com/jellydator/ttlcache/releases) - [Commits](https://github.com/jellydator/ttlcache/compare/v3.4.0...v3.4.1) Updates `github.com/kovidgoyal/imaging` from 1.8.21 to 1.8.22 - [Release notes](https://github.com/kovidgoyal/imaging/releases) - [Commits](https://github.com/kovidgoyal/imaging/compare/v1.8.21...v1.8.22) Updates `github.com/nats-io/nats-server/v2` from 2.14.2 to 2.14.3 - [Release notes](https://github.com/nats-io/nats-server/releases) - [Changelog](https://github.com/nats-io/nats-server/blob/main/RELEASES.md) - [Commits](https://github.com/nats-io/nats-server/compare/v2.14.2...v2.14.3) Updates `github.com/onsi/ginkgo/v2` from 2.31.0 to 2.32.0 - [Release notes](https://github.com/onsi/ginkgo/releases) - [Changelog](https://github.com/onsi/ginkgo/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/ginkgo/compare/v2.31.0...v2.32.0) Updates `github.com/onsi/gomega` from 1.42.0 to 1.42.1 - [Release notes](https://github.com/onsi/gomega/releases) - [Changelog](https://github.com/onsi/gomega/blob/master/CHANGELOG.md) - [Commits](https://github.com/onsi/gomega/compare/v1.42.0...v1.42.1) Updates `github.com/open-policy-agent/opa` from 1.17.1 to 1.18.2 - [Release notes](https://github.com/open-policy-agent/opa/releases) - [Changelog](https://github.com/open-policy-agent/opa/blob/main/CHANGELOG.md) - [Commits](https://github.com/open-policy-agent/opa/compare/v1.17.1...v1.18.2) Updates `google.golang.org/grpc` from 1.81.1 to 1.82.0 - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](https://github.com/grpc/grpc-go/compare/v1.81.1...v1.82.0) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-version: 2.2.9 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/jellydator/ttlcache/v3 dependency-version: 3.4.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/kovidgoyal/imaging dependency-version: 1.8.22 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/nats-io/nats-server/v2 dependency-version: 2.14.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/onsi/ginkgo/v2 dependency-version: 2.32.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: github.com/onsi/gomega dependency-version: 1.42.1 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: minor-and-patch - dependency-name: github.com/open-policy-agent/opa dependency-version: 1.18.2 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch - dependency-name: google.golang.org/grpc dependency-version: 1.82.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: minor-and-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Roman Perekhod <2403905@gmail.com>
196 lines
4.8 KiB
Go
196 lines
4.8 KiB
Go
package textutil
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/gookit/goutil/maputil"
|
|
"github.com/gookit/goutil/reflects"
|
|
"github.com/gookit/goutil/strutil"
|
|
)
|
|
|
|
// type SimpleAnyFunc func(args ...any) any
|
|
|
|
// StrVarRenderer implements like shell vars renderer
|
|
// 简单的实现类似 php, kotlin, shell 插值变量渲染,表达式解析处理。
|
|
//
|
|
// - var format: $var_name, ${some_var}, ${top.sub_var} ${name|default}
|
|
// - func call: ${func($var_name, 'const string')}
|
|
type StrVarRenderer struct {
|
|
// global variables
|
|
vars map[string]any
|
|
// fallback func for var not exists
|
|
getter FallbackFn
|
|
// var func map. refer the text/template
|
|
//
|
|
// Func allow return 1 or 2 values, if return 2 values, the second value is error.
|
|
rfs map[string]*reflects.FuncX
|
|
}
|
|
|
|
// NewStrVarRenderer create a new StrVarRenderer
|
|
func NewStrVarRenderer() *StrVarRenderer {
|
|
return &StrVarRenderer{
|
|
vars: make(map[string]any),
|
|
rfs: make(map[string]*reflects.FuncX),
|
|
}
|
|
}
|
|
|
|
// SetVars set variables
|
|
func (r *StrVarRenderer) SetVars(vars map[string]any) *StrVarRenderer {
|
|
for k, v := range vars {
|
|
r.vars[k] = v
|
|
}
|
|
return r
|
|
}
|
|
|
|
// SetVar set a variable
|
|
func (r *StrVarRenderer) SetVar(name string, value any) *StrVarRenderer {
|
|
r.vars[name] = value
|
|
return r
|
|
}
|
|
|
|
// SetFunc set a function
|
|
func (r *StrVarRenderer) SetFunc(name string, fn any) *StrVarRenderer {
|
|
r.rfs[name] = reflects.NewFunc(fn).WithEnhanceConv()
|
|
return r
|
|
}
|
|
|
|
// SetFuncMap set function map
|
|
func (r *StrVarRenderer) SetFuncMap(fnMap map[string]func(...any) any) *StrVarRenderer {
|
|
for k, v := range fnMap {
|
|
r.SetFunc(k, v)
|
|
}
|
|
return r
|
|
}
|
|
|
|
// SetFuncs set typed function map
|
|
func (r *StrVarRenderer) SetFuncs(fnMap map[string]any) *StrVarRenderer {
|
|
for k, v := range fnMap {
|
|
r.SetFunc(k, v)
|
|
}
|
|
return r
|
|
}
|
|
|
|
// SetGetter set variable fallback getter
|
|
func (r *StrVarRenderer) SetGetter(getter FallbackFn) *StrVarRenderer {
|
|
r.getter = getter
|
|
return r
|
|
}
|
|
|
|
var (
|
|
// 处理 $var_name 格式
|
|
// - 允许:$1..$N 这样的变量
|
|
// - 也支持 $@, $* 变量
|
|
reS = regexp.MustCompile(`\$(\w[a-zA-Z0-9_]*|[@|*])`)
|
|
// 处理 ${var_name} ${top.sub} ${var|def} 格式
|
|
reQ = regexp.MustCompile(`\$\{([a-zA-Z][a-zA-Z0-9_.]*)(?:\s*\|\s*([^}]+))?\}`)
|
|
// 处理 ${func(...)} 格式
|
|
reFn = regexp.MustCompile(`\$\{([a-zA-Z][a-zA-Z0-9_]*)\(([^}]*)\)\}`)
|
|
)
|
|
|
|
// Render rendering input string with variables
|
|
func (r *StrVarRenderer) Render(input string, vars map[string]any) string {
|
|
vars = maputil.Merge1level(r.vars, vars)
|
|
data := maputil.Map(vars)
|
|
|
|
// 处理 $var_name 格式
|
|
input = r.replaceVars(input, reS, data)
|
|
|
|
if strings.Contains(input, "${") {
|
|
// 处理 ${var.name} 格式
|
|
input = r.replaceVars(input, reQ, data)
|
|
// 处理 ${func(...)} 格式
|
|
input = r.handleFuncCalls(input, reFn, data)
|
|
}
|
|
|
|
return input
|
|
}
|
|
|
|
func (r *StrVarRenderer) handleFuncCalls(input string, re *regexp.Regexp, vars maputil.Map) string {
|
|
return re.ReplaceAllStringFunc(input, func(matched string) string {
|
|
submatch := re.FindStringSubmatch(matched)
|
|
funcName := submatch[1]
|
|
|
|
// 解析参数 并 调用函数
|
|
if fx, ok := r.rfs[funcName]; ok {
|
|
args := r.parseArgs(submatch[2], vars)
|
|
result, err := fx.Call2(args...)
|
|
if err == nil {
|
|
return fmt.Sprint(result)
|
|
}
|
|
}
|
|
|
|
return matched
|
|
})
|
|
}
|
|
|
|
func (r *StrVarRenderer) parseArgs(argsStr string, data maputil.Map) []any {
|
|
if argsStr == "" {
|
|
return []any{}
|
|
}
|
|
|
|
// 简单参数解析,按逗号分割
|
|
args := strings.Split(argsStr, ",")
|
|
results := make([]any, len(args))
|
|
|
|
for i, arg := range args {
|
|
arg = strings.TrimSpace(arg)
|
|
// is var name
|
|
if arg[0] == '$' {
|
|
results[i] = data.Get(arg[1:])
|
|
continue
|
|
}
|
|
|
|
// 常量值:去掉引号
|
|
last := len(arg) - 1
|
|
// strconv.Unquote( arg)
|
|
if (arg[0] == '"' && arg[last] == '"') || (arg[0] == '\'' && arg[last] == '\'') {
|
|
results[i] = arg[1 : len(arg)-1]
|
|
} else if arg == "true" || arg == "false" {
|
|
results[i] = strutil.SafeBool(arg)
|
|
} else if strutil.IsInt(arg) {
|
|
results[i] = strutil.SafeInt(arg)
|
|
} else {
|
|
results[i] = arg
|
|
}
|
|
}
|
|
|
|
return results
|
|
}
|
|
|
|
func (r *StrVarRenderer) replaceVars(input string, re *regexp.Regexp, data maputil.Map) string {
|
|
return re.ReplaceAllStringFunc(input, func(matched string) string {
|
|
var varName, defVal string
|
|
|
|
// format: ${var_name} 提取变量名
|
|
if strings.HasPrefix(matched, "${") {
|
|
submatch := re.FindStringSubmatch(matched)
|
|
varName = submatch[1]
|
|
if len(submatch) > 2 {
|
|
defVal = strings.TrimSpace(submatch[2])
|
|
}
|
|
} else {
|
|
// format: $var_name
|
|
varName = matched[1:]
|
|
}
|
|
|
|
// 从 vars map 获取值,支持嵌套变量名
|
|
if val, ok := data.GetByPath(varName); ok {
|
|
return fmt.Sprint(val)
|
|
}
|
|
|
|
// fallback: 使用 getter fn 获取值
|
|
if r.getter != nil {
|
|
if val, ok := r.getter(varName); ok {
|
|
return fmt.Sprint(val)
|
|
}
|
|
}
|
|
|
|
if len(defVal) > 0 {
|
|
return defVal
|
|
}
|
|
return matched
|
|
})
|
|
}
|