From 0f229274e68c6adc26e0dc72c5bd22cc80a00fdd Mon Sep 17 00:00:00 2001 From: Juan Calderon-Perez Date: Sat, 9 Sep 2023 23:27:16 -0400 Subject: [PATCH] Add support for ParseURL to rueidis driver --- rueidis/README.md | 11 +++++++++++ rueidis/config.go | 10 ++++++++++ rueidis/rueidis.go | 22 ++++++++++++++++++++++ rueidis/rueidis_test.go | 22 ++++++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/rueidis/README.md b/rueidis/README.md index 1134c12d..06fb4a43 100644 --- a/rueidis/README.md +++ b/rueidis/README.md @@ -61,6 +61,11 @@ store := rueidis.New(rueidis.Config{ TLSConfig: nil, }) +// Initialize using Rueidis URL +store := rueidis.New(rueidis.Config{ + URL: "redis://localhost:6379", +}) + // Initialize Rueidis Cluster Client store := rueidis.New(rueidis.Config{ InitAddress: []string{":6379", ":6380"}, @@ -105,6 +110,12 @@ type Config struct { // Optional. Default is "" ClientName string + // URL standard format Redis URL. If this is set all other config options, InitAddress, Username, Password, ClientName, and SelectDB have no effect. + // + // Example: redis://:@localhost:6379/ + // Optional. Default is "" + URL string + // SelectDB to be selected after connecting to the server. // // Optional. Default is 0 diff --git a/rueidis/config.go b/rueidis/config.go index 96125585..85680c99 100644 --- a/rueidis/config.go +++ b/rueidis/config.go @@ -24,6 +24,12 @@ type Config struct { // Optional. Default is "" ClientName string + // URL standard format Redis URL. If this is set all other config options, InitAddress, Username, Password, ClientName, and SelectDB have no effect. + // + // Example: redis://:@localhost:6379/ + // Optional. Default is "" + URL string + // SelectDB to be selected after connecting to the server. // // Optional. Default is 0 @@ -100,6 +106,7 @@ var ConfigDefault = Config{ Username: "", Password: "", ClientName: "", + URL: "", SelectDB: 0, InitAddress: []string{"127.0.0.1:6379"}, TLSConfig: nil, @@ -138,6 +145,9 @@ func configDefault(config ...Config) Config { if userConfig.ClientName != "" { cfg.ClientName = userConfig.ClientName } + if userConfig.URL != "" { + cfg.URL = userConfig.URL + } if userConfig.SelectDB != 0 { cfg.SelectDB = userConfig.SelectDB } diff --git a/rueidis/rueidis.go b/rueidis/rueidis.go index 9c0dd411..1fe7c472 100644 --- a/rueidis/rueidis.go +++ b/rueidis/rueidis.go @@ -24,6 +24,28 @@ func New(config ...Config) *Storage { var db rueidis.Client cacheTTL = cfg.CacheTTL + // Parse the URL and update config values accordingly + if cfg.URL != "" { + // This will panic if parsing URL fails + options := rueidis.MustParseURL(cfg.URL) + + // Update the config values with the parsed URL values + cfg.InitAddress = options.InitAddress + cfg.Username = options.Username + cfg.Password = options.Password + cfg.SelectDB = options.SelectDB + + // Update ClientName if returned + if cfg.ClientName == "" && options.ClientName != "" { + cfg.ClientName = options.ClientName + } + + // Update TLSConfig if returned + if cfg.TLSConfig == nil && options.TLSConfig != nil { + cfg.TLSConfig = options.TLSConfig + } + } + // Update config values accordingly and start new Client db, err := rueidis.NewClient(rueidis.ClientOption{ Username: cfg.Username, diff --git a/rueidis/rueidis_test.go b/rueidis/rueidis_test.go index 59ac1f5d..933424cb 100644 --- a/rueidis/rueidis_test.go +++ b/rueidis/rueidis_test.go @@ -187,6 +187,28 @@ func Test_Rueidis_With_HostPort(t *testing.T) { require.Nil(t, store.Close()) } +func Test_Rueidis_With_URL(t *testing.T) { + store := New(Config{ + URL: "redis://localhost:6379", + }) + + var ( + key = "bruce" + val = []byte("wayne") + ) + + err := store.Set(key, val, 0) + require.NoError(t, err) + + result, err := store.Get(key) + require.NoError(t, err) + require.Equal(t, val, result) + + err = store.Delete(key) + require.NoError(t, err) + require.Nil(t, store.Close()) +} + func Test_Rueidis_Cluster(t *testing.T) { store := New(Config{ InitAddress: []string{