From 22577eeefee04e43c82ac3be9c044ec7245206cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Sat, 23 Dec 2023 15:14:34 +0100 Subject: [PATCH] Add a DefaultAll() option to custom variables --- variable/custom/custom.go | 11 ++++++++++- variable/custom/custom_test.go | 9 +++++++++ variable/query/query.go | 6 ++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/variable/custom/custom.go b/variable/custom/custom.go index 547cad72..1fcad72b 100644 --- a/variable/custom/custom.go +++ b/variable/custom/custom.go @@ -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) @@ -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) { diff --git a/variable/custom/custom_test.go b/variable/custom/custom_test.go index 8486b146..910d7b48 100644 --- a/variable/custom/custom_test.go +++ b/variable/custom/custom_test.go @@ -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) +} diff --git a/variable/query/query.go b/variable/query/query.go index 7cdff9e2..586e2fb0 100644 --- a/variable/query/query.go +++ b/variable/query/query.go @@ -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) @@ -139,7 +141,7 @@ func IncludeAll() Option { query.Builder.IncludeAll = true query.Builder.Options = append(query.Builder.Options, sdk.Option{ Text: "All", - Value: "$__all", + Value: All, }) } } @@ -147,7 +149,7 @@ func IncludeAll() Option { // 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} } }