Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
c6b11a8f
Unverified
Commit
c6b11a8f
authored
Dec 14, 2020
by
Kyle Brandt
Committed by
GitHub
Dec 14, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Expressions: support ${my var} syntax (#29819)
parent
e7447c50
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
7 deletions
+38
-7
pkg/expr/mathexp/parse/lex.go
+21
-4
pkg/expr/mathexp/parse/lex_test.go
+13
-0
pkg/expr/mathexp/parse/node.go
+1
-1
pkg/expr/mathexp/parse/parse.go
+3
-2
No files found.
pkg/expr/mathexp/parse/lex.go
View file @
c6b11a8f
...
...
@@ -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
.
IsLette
r
(
r
)
:
case
isVarcha
r
(
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
)
}
...
...
pkg/expr/mathexp/parse/lex_test.go
View file @
c6b11a8f
...
...
@@ -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.
...
...
pkg/expr/mathexp/parse/node.go
View file @
c6b11a8f
...
...
@@ -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
}
...
...
pkg/expr/mathexp/parse/parse.go
View file @
c6b11a8f
...
...
@@ -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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment