mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Expressions: support ${my var} syntax (#29819)
This commit is contained in:
parent
e7447c5067
commit
c6b11a8f90
@ -288,9 +288,29 @@ func lexFunc(l *lexer) stateFn {
|
||||
|
||||
func lexVar(l *lexer) stateFn {
|
||||
hasChar := false
|
||||
if l.peek() == '{' {
|
||||
_ = l.next()
|
||||
for {
|
||||
switch r := l.next(); {
|
||||
case r == '}':
|
||||
if !hasChar {
|
||||
return l.errorf("incomplete variable")
|
||||
}
|
||||
l.emit(itemVar)
|
||||
return lexItem
|
||||
case r == eof:
|
||||
return l.errorf("unterminated variable missing closing }")
|
||||
case isVarchar(r) || isSpace(r):
|
||||
hasChar = true
|
||||
default:
|
||||
return l.errorf("unsupported variable character")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for {
|
||||
switch r := l.next(); {
|
||||
case unicode.IsLetter(r):
|
||||
case isVarchar(r):
|
||||
hasChar = true
|
||||
// absorb
|
||||
default:
|
||||
@ -321,9 +341,6 @@ func isSpace(r rune) bool {
|
||||
return unicode.IsSpace(r)
|
||||
}
|
||||
|
||||
// isVarchar should maybe be used in place of unicode is letter above,
|
||||
// but do not want to modify it at this time, so adding lint exception.
|
||||
// nolint:unused,deadcode
|
||||
func isVarchar(r rune) bool {
|
||||
return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r)
|
||||
}
|
||||
|
@ -97,6 +97,16 @@ var lexTests = []lexTest{
|
||||
{itemNumber, 0, "1.2e-4"},
|
||||
tEOF,
|
||||
}},
|
||||
{"curly brace var", "${My Var}", []item{
|
||||
{itemVar, 0, "${My Var}"},
|
||||
tEOF,
|
||||
}},
|
||||
{"curly brace var plus 1", "${My Var} + 1", []item{
|
||||
{itemVar, 0, "${My Var}"},
|
||||
tPlus,
|
||||
{itemNumber, 0, "1"},
|
||||
tEOF,
|
||||
}},
|
||||
{"number plus var", "1 + $A", []item{
|
||||
{itemNumber, 0, "1"},
|
||||
tPlus,
|
||||
@ -113,6 +123,9 @@ var lexTests = []lexTest{
|
||||
{"invalid var", "$", []item{
|
||||
{itemError, 0, "incomplete variable"},
|
||||
}},
|
||||
{"invalid curly var", "${adf sd", []item{
|
||||
{itemError, 0, "unterminated variable missing closing }"},
|
||||
}},
|
||||
}
|
||||
|
||||
// collect gathers the emitted items into a slice.
|
||||
|
@ -88,7 +88,7 @@ func (t NodeType) String() string {
|
||||
type VarNode struct {
|
||||
NodeType
|
||||
Pos
|
||||
Name string // Without the $
|
||||
Name string // Without the $ or {}
|
||||
Text string // Raw
|
||||
}
|
||||
|
||||
|
@ -300,8 +300,9 @@ func (t *Tree) v() Node {
|
||||
func (t *Tree) Var() (v *VarNode) {
|
||||
token := t.next()
|
||||
varNoPrefix := strings.TrimPrefix(token.val, "$")
|
||||
t.VarNames = append(t.VarNames, varNoPrefix)
|
||||
return newVar(token.pos, varNoPrefix, token.val)
|
||||
varNoBraces := strings.TrimSuffix(strings.TrimPrefix(varNoPrefix, "{"), "}")
|
||||
t.VarNames = append(t.VarNames, varNoBraces)
|
||||
return newVar(token.pos, varNoBraces, token.val)
|
||||
}
|
||||
|
||||
// Func parses a FuncNode.
|
||||
|
Loading…
Reference in New Issue
Block a user