Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add redis seed #19

Merged
merged 8 commits into from
May 30, 2024
Merged

add redis seed #19

merged 8 commits into from
May 30, 2024

Conversation

etrpx
Copy link
Contributor

@etrpx etrpx commented May 28, 2024

No description provided.

@etrpx etrpx requested a review from avivpxi May 28, 2024 13:26

// buildRedisSeed is a builder function that constructs a new Redis seed component.
// It takes a byte slice of JSON data as input.
// The function attempts to parse the JSON data into a mongo.SeedConfig struct, which defines the configuration
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mongo.SeedConfig -> redis.SeedConfig

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// It takes a byte slice of JSON data as input.
// The function attempts to parse the JSON data into a mongo.SeedConfig struct, which defines the configuration
// for a Redis seed component. If the JSON data is successfully parsed, it then uses this configuration
// to instantiate and return a new MongoDB seed component via the redis.NewSeedComponent function.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MongoDB -> Redis

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// SeedData represents data for a redis hash.
type SeedData struct {
// Key - the name of the redis key
Key string `json:"key,omitempty"`
Copy link
Collaborator

@avivpxi avivpxi May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my only comment on the config is that this structure will only let us support HSet, or at least block the name Key for this specific use.
What do you think about something like this:

// SeedConfig represents the configuration for the redis seed component.
type SeedConfig struct {
	// Address - a valid redis server address to connect to
	Address string `json:"address,omitempty"`

	// ClientProvider - can be used as an alternative to Address, provides a redis client to use.
	// available only via code, not available in config files.
	// if both ClientProvider and Address are provided, ClientProvider is used.
	ClientProvider func() (*redis.Client, error) `json:"-"`

	SetKeys  []*Set  `json:"set_keys"`
	HSetKeys []*HSet `json:"hset_keys"`
}

type Set struct {
	Key   string        `json:"key"`
	Value string        `json:"value"`
	TTL   time.Duration `json:"ttl"`
}

type HSet struct {
	Key    string            `json:"key"`
	Values map[string]string `json:"values"`
	TTL    time.Duration     `json:"ttl"`
}

this will allow us to add future support for other data types as well without breaking anything
WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

Copy link
Contributor Author

@etrpx etrpx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avivpxi , please review


// buildRedisSeed is a builder function that constructs a new Redis seed component.
// It takes a byte slice of JSON data as input.
// The function attempts to parse the JSON data into a mongo.SeedConfig struct, which defines the configuration
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// It takes a byte slice of JSON data as input.
// The function attempts to parse the JSON data into a mongo.SeedConfig struct, which defines the configuration
// for a Redis seed component. If the JSON data is successfully parsed, it then uses this configuration
// to instantiate and return a new MongoDB seed component via the redis.NewSeedComponent function.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// SeedData represents data for a redis hash.
type SeedData struct {
// Key - the name of the redis key
Key string `json:"key,omitempty"`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

return nil
}

func (r *SeedComponent) HashSetEntries(ctx context.Context, err error, client *redis.Client) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be private


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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

entry.TTL*time.Second is a mistake, it's already a duration - multipying by time.Second will just multiply the duration by 1000^3 (source code)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

return err
}
if hEntry.TTL > 0 {
err = client.Expire(ctx, hEntry.Key, hEntry.TTL*time.Second).Err()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same duration comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@etrpx etrpx requested a review from avivpxi May 29, 2024 11:16
@etrpx etrpx merged commit 7c70f10 into main May 30, 2024
4 checks passed
@etrpx etrpx deleted the redis-seed branch May 30, 2024 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants