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

chore: made deriving Clone consistent #156

Merged
merged 1 commit into from
Jul 1, 2024

Conversation

CommanderStorm
Copy link
Contributor

This is a followup to #154 (comment)

(I don't have a real usecase for this. Just thought that more consistency can't hurt)

Neo4jImage is ommited, as ContainerState does not implement Clone.

@@ -8,7 +8,7 @@ use testcontainers::{
const NAME: &str = "docker.elastic.co/elasticsearch/elasticsearch";
const TAG: &str = "7.16.1";

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct ElasticSearch {
_priv: (),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

One thing that I found interesting is this pattern vs (for example) pub struct DynamoDb;

Out of curiosity:
Is there a reason for having this unused member variable? ^^

Copy link
Contributor

@DDtKey DDtKey Jul 1, 2024

Choose a reason for hiding this comment

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

Is there a reason for having this unused member variable? ^^

Yes, to provide encapsulation, this allows the image to be extended without breaking future changes.

pub struct DynamoDb; allows you to create an instance simply by using DynamoDb (e.g DynamoDb.start()). Therefore adding a new field will break this usage.
Although ElasticSearch requires the use of new() or default() so that we can safely add new fields.

However, some images are still missing this pattern and should be updated in the future

Copy link
Contributor

@DDtKey DDtKey left a comment

Choose a reason for hiding this comment

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

Thank you very much!

I'd consider this as a feature, we introduce new allowed functionality in general.

(I don't have a real usecase for this. Just thought that more consistency can't hurt)

I think one possible use-case is to spawn several pre-configured instances, something like:

let image = MssqlServer::default().with_sa_password("my_password");
let container_1 = image.clone().start();
let container_2 = image.start()

It's unlikely to be common pattern, but there is no reason to restrict users of doing this.

@DDtKey DDtKey merged commit bccc7ff into testcontainers:main Jul 1, 2024
6 checks passed
@DDtKey
Copy link
Contributor

DDtKey commented Jul 1, 2024

Forgot to change chore to feat, but not that important

Merged, thanks!

@CommanderStorm CommanderStorm deleted the consistent-cloning branch July 2, 2024 04:02
@github-actions github-actions bot mentioned this pull request Jul 7, 2024
DDtKey pushed a commit that referenced this pull request Sep 25, 2024
This PR makes sure that the way structs are constructed is consistent
and encourages the builder pattern.
It is based on this question:
#156 (comment)

For the changelog:

We now require you to use the builder pattern (instead of some structs
being [unit structs](https://doc.rust-lang.org/std/keyword.struct.html))
for all modules.
This ensures that if we add a field in the future that this will not
break your existing code.

This change is breaking for these modules:

| Module | before | after |
|--------|--------|--------|
| `cncf_distribution::CncfDistribution` | `CncfDistribution.start()` |
`CncfDistribution::default().start()` |
| `dynamodb_local::DynamoDb` | `DynamoDb.start()` |
`DynamoDb::default().start()` |
| `elasticmq::ElasticMq` | `ElasticMq.start()` |
`ElasticMq::default().start()` |
| ~`mongo::Mongo`~ (see #143) | ~`Mongo.start()`~ |
~`Mongo::default().start()`~ |
| `kwok::KwokCluster` | `KwokCluster.start()` |
`KwokCluster::default().start()` |
| `rabbitmq::RabbitMq` | `RabbitMq.start()` |
`RabbitMq::default().start()` |
| `redis::stack::RedisStack` | `RedisStack.start()` |
`RedisStack::default().start()` |
| `redis::standalone::Redis` | `Redis.start()` |
`Redis::default().start()` |
| `victoria_metrics::VictoriaMetrics` | `VictoriaMetrics.start()` |
`VictoriaMetrics::default().start()` |
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