Skip to content

Commit

Permalink
Merge pull request #43 from K-Phoen/fix-variables
Browse files Browse the repository at this point in the history
Fix variables definition
  • Loading branch information
K-Phoen authored Feb 9, 2020
2 parents 642c457 + f3eb3fa commit 9102e1f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {
"95th": "95",
"99th": "99",
}),
constant.Default("80th"),
constant.Default("80"),
),
grabana.VariableAsCustom(
"vX",
Expand Down
29 changes: 28 additions & 1 deletion variable/constant/constant.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package constant

import (
"strings"

"github.com/grafana-tools/sdk"
)

Expand All @@ -10,9 +12,30 @@ type Option func(constant *Constant)
// ValuesMap represent a "label" to "value" map of options for a constant variable.
type ValuesMap map[string]string

func (values ValuesMap) asQuery() string {
valuesList := make([]string, 0, len(values))

for _, value := range values {
valuesList = append(valuesList, value)
}

return strings.Join(valuesList, ",")
}

func (values ValuesMap) labelFor(value string) string {
for label, val := range values {
if val == value {
return label
}
}

return value
}

// Constant represents a "constant" templated variable.
type Constant struct {
Builder sdk.TemplateVar
values ValuesMap
}

// New creates a new "constant" templated variable.
Expand All @@ -39,14 +62,18 @@ func Values(values ValuesMap) Option {
Value: value,
})
}

constant.values = values
constant.Builder.Query = values.asQuery()
}
}

// Default sets the default value of the variable.
func Default(value string) Option {
return func(constant *Constant) {
constant.Builder.Current = sdk.Current{
Text: value,
Text: constant.values.labelFor(value),
Value: value,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions variable/constant/constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestDefaultValueCanBeSet(t *testing.T) {
panel := New("const", Default("99th"))

req.Equal("99th", panel.Builder.Current.Text)
req.Equal("99th", panel.Builder.Current.Value)
}

func TestLabelCanBeHidden(t *testing.T) {
Expand Down
65 changes: 46 additions & 19 deletions variable/custom/custom.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package custom

import (
"strings"

"github.com/grafana-tools/sdk"
)

Expand All @@ -10,80 +12,105 @@ type Option func(constant *Custom)
// ValuesMap represent a "label" to "value" map of options for a custom variable.
type ValuesMap map[string]string

func (values ValuesMap) asQuery() string {
valuesList := make([]string, 0, len(values))

for _, value := range values {
valuesList = append(valuesList, value)
}

return strings.Join(valuesList, ",")
}

func (values ValuesMap) labelFor(value string) string {
for label, val := range values {
if val == value {
return label
}
}

return value
}

// Custom represents a "custom" templated variable.
type Custom struct {
Builder sdk.TemplateVar
values ValuesMap
}

// New creates a new "custom" templated variable.
func New(name string, options ...Option) *Custom {
constant := &Custom{Builder: sdk.TemplateVar{
custom := &Custom{Builder: sdk.TemplateVar{
Name: name,
Label: name,
Type: "custom",
}}

for _, opt := range options {
opt(constant)
opt(custom)
}

return constant
return custom
}

// Values sets the possible values for the variable.
func Values(values ValuesMap) Option {
return func(constant *Custom) {
return func(custom *Custom) {
for label, value := range values {
constant.Builder.Options = append(constant.Builder.Options, sdk.Option{
custom.Builder.Options = append(custom.Builder.Options, sdk.Option{
Text: label,
Value: value,
})
}

custom.values = values
custom.Builder.Query = values.asQuery()
}
}

// Default sets the default value of the variable.
func Default(value string) Option {
return func(constant *Custom) {
constant.Builder.Current = sdk.Current{
Text: value,
return func(custom *Custom) {
custom.Builder.Current = sdk.Current{
Text: custom.values.labelFor(value),
Value: value,
}
}
}

// Label sets the label of the variable.
func Label(label string) Option {
return func(constant *Custom) {
constant.Builder.Label = label
return func(custom *Custom) {
custom.Builder.Label = label
}
}

// HideLabel ensures that this variable's label will not be displayed.
func HideLabel() Option {
return func(constant *Custom) {
constant.Builder.Hide = 1
return func(custom *Custom) {
custom.Builder.Hide = 1
}
}

// Hide ensures that the variable will not be displayed.
func Hide() Option {
return func(constant *Custom) {
constant.Builder.Hide = 2
return func(custom *Custom) {
custom.Builder.Hide = 2
}
}

// Multi allows several values to be selected.
func Multi() Option {
return func(constant *Custom) {
constant.Builder.Multi = true
return func(custom *Custom) {
custom.Builder.Multi = true
}
}

// IncludeAll adds an option to allow all values to be selected.
func IncludeAll() Option {
return func(constant *Custom) {
constant.Builder.IncludeAll = true
constant.Builder.Options = append(constant.Builder.Options, sdk.Option{
return func(custom *Custom) {
custom.Builder.IncludeAll = true
custom.Builder.Options = append(custom.Builder.Options, sdk.Option{
Text: "All",
Value: "$__all",
})
Expand Down
3 changes: 2 additions & 1 deletion variable/interval/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func Values(values ValuesList) Option {
func Default(value string) Option {
return func(interval *Interval) {
interval.Builder.Current = sdk.Current{
Text: value,
Text: value,
Value: value,
}
}
}
Expand Down

0 comments on commit 9102e1f

Please sign in to comment.