Skip to content

Commit

Permalink
Merge pull request #247 from K-Phoen/custom-var/default-all
Browse files Browse the repository at this point in the history
Add a DefaultAll() option to custom variables
  • Loading branch information
K-Phoen authored Dec 23, 2023
2 parents d030ef1 + 22577ee commit db29bd7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
11 changes: 10 additions & 1 deletion variable/custom/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/K-Phoen/sdk"
)

const All = "$__all"

// Option represents an option that can be used to configure a custom variable.
type Option func(constant *Custom)

Expand Down Expand Up @@ -116,11 +118,18 @@ func IncludeAll() Option {
custom.Builder.IncludeAll = true
custom.Builder.Options = append(custom.Builder.Options, sdk.Option{
Text: "All",
Value: "$__all",
Value: All,
})
}
}

// DefaultAll selects "All" values by default.
func DefaultAll() Option {
return func(custom *Custom) {
custom.Builder.Current = sdk.Current{Text: &sdk.StringSliceString{Value: []string{"All"}, Valid: true}, Value: All}
}
}

// AllValue define the value used when selecting the "All" option.
func AllValue(value string) Option {
return func(custom *Custom) {
Expand Down
9 changes: 9 additions & 0 deletions variable/custom/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,12 @@ func TestAllValueCanBeOverriden(t *testing.T) {

req.Equal(".*", panel.Builder.AllValue)
}

func TestAnAllValuesCanBeTheDefault(t *testing.T) {
req := require.New(t)

panel := New("", DefaultAll())

req.Equal([]string{"All"}, panel.Builder.Current.Text.Value)
req.Equal("$__all", panel.Builder.Current.Value)
}
6 changes: 4 additions & 2 deletions variable/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/K-Phoen/sdk"
)

const All = "$__all"

// Option represents an option that can be used to configure a query.
type Option func(constant *Query)

Expand Down Expand Up @@ -139,15 +141,15 @@ func IncludeAll() Option {
query.Builder.IncludeAll = true
query.Builder.Options = append(query.Builder.Options, sdk.Option{
Text: "All",
Value: "$__all",
Value: All,
})
}
}

// DefaultAll selects "All" values by default.
func DefaultAll() Option {
return func(query *Query) {
query.Builder.Current = sdk.Current{Text: &sdk.StringSliceString{Value: []string{"All"}, Valid: true}, Value: "$__all"}
query.Builder.Current = sdk.Current{Text: &sdk.StringSliceString{Value: []string{"All"}, Valid: true}, Value: All}
}
}

Expand Down

0 comments on commit db29bd7

Please sign in to comment.