snapshot of https://github.com/go-gitea/gitea.git (main, 2026-09-15; blobs > 131072B omitted)
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
This commit is contained in:
99
modules/templates/vars/vars.go
Normal file
99
modules/templates/vars/vars.go
Normal file
@@ -0,0 +1,99 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package vars
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// ExpandCurlyBrace replaces all variables like {var} by `vars` map,
|
||||
// it always returns the expanded string regardless of errors.
|
||||
// if error occurs (wrong syntax, missing variable), the error part doesn't change and is returned as it is.
|
||||
func ExpandCurlyBrace(template string, vars map[string]string) (string, error) {
|
||||
// in the future, if necessary, we can introduce some escape-char,
|
||||
// for example: it will use `#' as a reversed char, templates will use `{#{}` to do escape and output char '{'.
|
||||
var buf strings.Builder
|
||||
var err error
|
||||
|
||||
posBegin := 0
|
||||
strLen := len(template)
|
||||
for posBegin < strLen {
|
||||
// find the next `{`
|
||||
pos := strings.IndexByte(template[posBegin:], '{')
|
||||
if pos == -1 {
|
||||
buf.WriteString(template[posBegin:])
|
||||
break
|
||||
}
|
||||
|
||||
// copy texts between vars
|
||||
buf.WriteString(template[posBegin : posBegin+pos])
|
||||
|
||||
// find the var between `{` and `}`/end
|
||||
posBegin += pos
|
||||
posEnd := posBegin + 1
|
||||
for posEnd < strLen {
|
||||
if template[posEnd] == '}' {
|
||||
posEnd++
|
||||
break
|
||||
} // in the future, if we need to support escape chars, we can do: if (isEscapeChar) { posEnd+=2 }
|
||||
posEnd++
|
||||
}
|
||||
|
||||
// the var part, it can be "{", "{}", "{..." or or "{...}"
|
||||
part := template[posBegin:posEnd]
|
||||
posBegin = posEnd
|
||||
if part == "{}" || part[len(part)-1] != '}' {
|
||||
// treat "{}" or "{..." as error
|
||||
err = fmt.Errorf("wrong syntax found in %s", template)
|
||||
buf.WriteString(part)
|
||||
} else {
|
||||
// now we get a valid key "{...}"
|
||||
key := part[1 : len(part)-1]
|
||||
keyFirst, _ := utf8.DecodeRuneInString(key)
|
||||
if unicode.IsSpace(keyFirst) || unicode.IsPunct(keyFirst) || unicode.IsControl(keyFirst) {
|
||||
// if the key doesn't start with a letter, then we do not treat it as a var now
|
||||
buf.WriteString(part)
|
||||
} else {
|
||||
// look up in the map
|
||||
if val, ok := vars[key]; ok {
|
||||
buf.WriteString(val)
|
||||
} else {
|
||||
// write the non-existing var as it is
|
||||
buf.WriteString(part)
|
||||
err = fmt.Errorf("the variable %s is missing for %s", key, template)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return buf.String(), err
|
||||
}
|
||||
|
||||
var globalVars = sync.OnceValue(func() (ret struct {
|
||||
regexpShellLike *regexp.Regexp
|
||||
},
|
||||
) {
|
||||
ret.regexpShellLike = regexp.MustCompile(`(\$\{[a-zA-Z_]\w*\}|\$[a-zA-Z_]\w*)`)
|
||||
return ret
|
||||
})
|
||||
|
||||
// ExpandShellLike works like os.Expand, the difference is that this function keeps the non-existing keys
|
||||
func ExpandShellLike(template string, vars map[string]string) string {
|
||||
re := globalVars().regexpShellLike
|
||||
return re.ReplaceAllStringFunc(template, func(s string) string {
|
||||
key := s[1:]
|
||||
if strings.HasPrefix(key, "{") && strings.HasSuffix(key, "}") {
|
||||
key = key[1 : len(key)-1]
|
||||
}
|
||||
if val, ok := vars[key]; ok {
|
||||
return val
|
||||
}
|
||||
return s
|
||||
})
|
||||
}
|
||||
87
modules/templates/vars/vars_test.go
Normal file
87
modules/templates/vars/vars_test.go
Normal file
@@ -0,0 +1,87 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package vars
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestExpandCurlyBrace(t *testing.T) {
|
||||
cases := []struct {
|
||||
tmpl string
|
||||
data map[string]string
|
||||
out string
|
||||
error bool
|
||||
}{
|
||||
{
|
||||
tmpl: "{a}",
|
||||
data: map[string]string{
|
||||
"a": "1",
|
||||
},
|
||||
out: "1",
|
||||
},
|
||||
{
|
||||
tmpl: "expand {a}, {b} and {c}, with non-var { } {#}",
|
||||
data: map[string]string{
|
||||
"a": "1",
|
||||
"b": "2",
|
||||
"c": "3",
|
||||
},
|
||||
out: "expand 1, 2 and 3, with non-var { } {#}",
|
||||
},
|
||||
{
|
||||
tmpl: "中文内容 {一}, {二} 和 {三} 中文结尾",
|
||||
data: map[string]string{
|
||||
"一": "11",
|
||||
"二": "22",
|
||||
"三": "33",
|
||||
},
|
||||
out: "中文内容 11, 22 和 33 中文结尾",
|
||||
},
|
||||
{
|
||||
tmpl: "expand {{a}, {b} and {c}",
|
||||
data: map[string]string{
|
||||
"a": "foo",
|
||||
"b": "bar",
|
||||
},
|
||||
out: "expand {{a}, bar and {c}",
|
||||
error: true,
|
||||
},
|
||||
{
|
||||
tmpl: "expand } {} and {",
|
||||
out: "expand } {} and {",
|
||||
error: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.tmpl, func(t *testing.T) {
|
||||
res, err := ExpandCurlyBrace(c.tmpl, c.data)
|
||||
assert.Equal(t, c.out, res)
|
||||
if c.error {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandShellLike(t *testing.T) {
|
||||
cases := []struct {
|
||||
tmpl string
|
||||
data map[string]string
|
||||
out string
|
||||
}{
|
||||
{tmpl: "$key ${key} $other ${other}", data: map[string]string{"key": "val"}, out: "val val $other ${other}"},
|
||||
{tmpl: "$ key ${key }", data: map[string]string{"key": "val"}, out: "$ key ${key }"},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
out := ExpandShellLike(c.tmpl, c.data)
|
||||
assert.Equal(t, c.out, out, "tmpl: %s", c.tmpl)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user