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
38c15540
Commit
38c15540
authored
Oct 24, 2018
by
Johannes Schill
Committed by
Marcus Efraimsson
Oct 24, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move the variable regex to constants to make sure we use the same reg… (#13801)
parent
758a5ecf
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
18 deletions
+42
-18
public/app/features/templating/specs/variable.test.ts
+15
-0
public/app/features/templating/template_srv.ts
+2
-7
public/app/features/templating/variable.ts
+25
-11
No files found.
public/app/features/templating/specs/variable.test.ts
View file @
38c15540
...
...
@@ -22,6 +22,11 @@ describe('containsVariable', () => {
expect
(
contains
).
toBe
(
true
);
});
it
(
'should find it with [[var:option]] syntax'
,
()
=>
{
const
contains
=
containsVariable
(
'this.[[test:csv]].filters'
,
'test'
);
expect
(
contains
).
toBe
(
true
);
});
it
(
'should find it when part of segment'
,
()
=>
{
const
contains
=
containsVariable
(
'metrics.$env.$group-*'
,
'group'
);
expect
(
contains
).
toBe
(
true
);
...
...
@@ -36,6 +41,16 @@ describe('containsVariable', () => {
const
contains
=
containsVariable
(
'asd'
,
'asd2.$env'
,
'env'
);
expect
(
contains
).
toBe
(
true
);
});
it
(
'should find it with ${var} syntax'
,
()
=>
{
const
contains
=
containsVariable
(
'this.${test}.filters'
,
'test'
);
expect
(
contains
).
toBe
(
true
);
});
it
(
'should find it with ${var:option} syntax'
,
()
=>
{
const
contains
=
containsVariable
(
'this.${test:csv}.filters'
,
'test'
);
expect
(
contains
).
toBe
(
true
);
});
});
});
...
...
public/app/features/templating/template_srv.ts
View file @
38c15540
import
kbn
from
'app/core/utils/kbn'
;
import
_
from
'lodash'
;
import
{
variableRegex
}
from
'app/features/templating/variable'
;
function
luceneEscape
(
value
)
{
return
value
.
replace
(
/
([\!\*\+\-\=
<>
\s\&\|\(\)\[\]\{\}\^\~\?\:\\/
"
])
/g
,
'
\\
$1'
);
...
...
@@ -8,13 +9,7 @@ function luceneEscape(value) {
export
class
TemplateSrv
{
variables
:
any
[];
/*
* This regex matches 3 types of variable reference with an optional format specifier
* \$(\w+) $var1
* \[\[([\s\S]+?)(?::(\w+))?\]\] [[var2]] or [[var2:fmt2]]
* \${(\w+)(?::(\w+))?} ${var3} or ${var3:fmt3}
*/
private
regex
=
/
\$(\w
+
)
|
\[\[([\s\S]
+
?)(?:
:
(\w
+
))?\]\]
|
\$
{
(\w
+
)(?:
:
(\w
+
))?
}/g
;
private
regex
=
variableRegex
;
private
index
=
{};
private
grafanaVariables
=
{};
private
builtIns
=
{};
...
...
public/app/features/templating/variable.ts
View file @
38c15540
import
kbn
from
'app/core/utils/kbn'
;
import
{
assignModelProperties
}
from
'app/core/utils/model_utils'
;
/*
* This regex matches 3 types of variable reference with an optional format specifier
* \$(\w+) $var1
* \[\[([\s\S]+?)(?::(\w+))?\]\] [[var2]] or [[var2:fmt2]]
* \${(\w+)(?::(\w+))?} ${var3} or ${var3:fmt3}
*/
export
const
variableRegex
=
/
\$(\w
+
)
|
\[\[([\s\S]
+
?)(?:
:
(\w
+
))?\]\]
|
\$
{
(\w
+
)(?:
:
(\w
+
))?
}/g
;
// Helper function since lastIndex is not reset
export
const
variableRegexExec
=
(
variableString
:
string
)
=>
{
variableRegex
.
lastIndex
=
0
;
return
variableRegex
.
exec
(
variableString
);
};
export
interface
Variable
{
setValue
(
option
);
updateOptions
();
...
...
@@ -14,15 +27,16 @@ export let variableTypes = {};
export
{
assignModelProperties
};
export
function
containsVariable
(...
args
:
any
[])
{
let
variableName
=
args
[
args
.
length
-
1
];
let
str
=
args
[
0
]
||
''
;
for
(
let
i
=
1
;
i
<
args
.
length
-
1
;
i
++
)
{
str
+=
' '
+
args
[
i
]
||
''
;
}
const
variableName
=
args
[
args
.
length
-
1
];
const
variableString
=
args
.
slice
(
0
,
-
1
).
join
(
' '
);
const
matches
=
variableString
.
match
(
variableRegex
);
const
isMatchingVariable
=
matches
!==
null
?
matches
.
find
(
match
=>
{
const
varMatch
=
variableRegexExec
(
match
);
return
varMatch
!==
null
&&
varMatch
.
indexOf
(
variableName
)
>
-
1
;
})
:
false
;
variableName
=
kbn
.
regexEscape
(
variableName
);
const
findVarRegex
=
new
RegExp
(
'
\\
$('
+
variableName
+
')(?:
\\
W|$)|
\\
[
\\
[('
+
variableName
+
')
\\
]
\\
]'
,
'g'
);
const
match
=
findVarRegex
.
exec
(
str
);
return
match
!==
null
;
return
!!
isMatchingVariable
;
}
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