diff --git a/seed/redis/component.go b/seed/redis/component.go index 91b8859..a2bd746 100644 --- a/seed/redis/component.go +++ b/seed/redis/component.go @@ -70,36 +70,63 @@ func (r *SeedComponent) seed(ctx context.Context) error { return err } - err = client.FlushAll(ctx).Err() - if err != nil { + if err = client.FlushAll(ctx).Err(); err != nil { + return err + } + + if err = r.setEntries(ctx, err, client); err != nil { return err } - for _, hashData := range r.config.Data { - var count int - err = client.HSet(ctx, hashData.Key, hashData.Fields).Err() + if err = r.HashSetEntries(ctx, err, client); err != nil { + return err + } + r.logInsertions() + + return nil +} + +func (r *SeedComponent) setEntries(ctx context.Context, err error, client *redis.Client) error { + for _, entry := range r.config.Entries { + err = client.Set(ctx, entry.Key, entry.Value, entry.TTL*time.Second).Err() + + if err != nil { + return err + } + } + return nil +} + +func (r *SeedComponent) HashSetEntries(ctx context.Context, err error, client *redis.Client) error { + for _, hEntry := range r.config.HEntries { + err = client.HSet(ctx, hEntry.Key, hEntry.Values).Err() if err != nil { return err } - if hashData.TTL > 0 { - err = client.Expire(ctx, hashData.Key, time.Duration(hashData.TTL)*time.Second).Err() + if hEntry.TTL > 0 { + err = client.Expire(ctx, hEntry.Key, hEntry.TTL*time.Second).Err() if err != nil { return err } } - count = len(hashData.Fields) - - r.writer.WriteString(fmt.Sprintf( - "inserted %s fields to %s", - r.writer.Color.Green(strconv.Itoa(count)), - r.writer.Color.Green(hashData.Key), - )) } - return nil } +func (r *SeedComponent) logInsertions() { + count := len(r.config.Entries) + hashedCount := len(r.config.HEntries) + + r.writer.WriteString(fmt.Sprintf( + "inserted %s fields to %s and %s fields to %s", + r.writer.Color.Green(strconv.Itoa(count)), + r.writer.Color.Green("Entries"), + r.writer.Color.Green(strconv.Itoa(hashedCount)), + r.writer.Color.Green("Hashed Entries"), + )) +} + func (r *SeedComponent) clientProvider() (*redis.Client, error) { if r.config.ClientProvider != nil { return r.config.ClientProvider() diff --git a/seed/redis/config.go b/seed/redis/config.go index 7a347e9..242fbc8 100644 --- a/seed/redis/config.go +++ b/seed/redis/config.go @@ -4,7 +4,10 @@ package redis -import "github.com/go-redis/redis/v8" +import ( + "github.com/go-redis/redis/v8" + "time" +) // SeedConfig represents the configuration for the redis seed component. type SeedConfig struct { @@ -16,18 +19,23 @@ type SeedConfig struct { // if both ClientProvider and Address are provided, ClientProvider is used. ClientProvider func() (*redis.Client, error) `json:"-"` - // Data - a list of objects, each represents a redis key and its data - Data []*SeedData `json:"data,omitempty"` -} + // Entries - a list of key-value pairs to set in redis + Entries []*Set `json:"entries,omitempty"` -// SeedData represents data for a redis hash. -type SeedData struct { - // Key - the name of the redis key - Key string `json:"key,omitempty"` + // HEntries - a list of key-value pairs to set in redis hashes + HEntries []*HSet `json:"hentries,omitempty"` +} - // Fields - a map of field names and their values to insert using the redis HSet function: - Fields []string `json:"fields,omitempty"` +// Set Represents a key-value pair to set in redis. +type Set struct { + Key string `json:"key,omitempty"` + Value string `json:"value"` + TTL time.Duration `json:"ttl"` +} - // TTL - the time to live for the key in seconds - TTL int `json:"ttl,omitempty"` +// HSet Represents a key-value pair to set in a redis hash. +type HSet struct { + Key string `json:"key"` + Values map[string]string `json:"values"` + TTL time.Duration `json:"ttl"` }