-
-
Notifications
You must be signed in to change notification settings - Fork 501
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
feat: use an interface for container customization in modules #1042
feat: use an interface for container customization in modules #1042
Conversation
✅ Deploy Preview for testcontainers-go ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
* main: Add support for LocalStack v2 (testcontainers#994) docs: document tc_host inside the networking section (testcontainers#1041)
|
||
// transfer options to the config | ||
|
||
if bucketCustomizer, ok := opt.(bucketCustomizer); ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fbiville this is how I'm transferring the functional option state to the container config
type serviceCustomizer struct { | ||
enabledService Service | ||
} | ||
|
||
for _, service := range enabledServices { | ||
for _, port := range service.ports { | ||
exposedPorts = append(exposedPorts, port+"/tcp") | ||
} | ||
func (c serviceCustomizer) Customize(req *testcontainers.GenericContainerRequest) { | ||
for _, port := range c.enabledService.ports { | ||
req.ExposedPorts = append(req.ExposedPorts, port+"/tcp") | ||
} | ||
} | ||
|
||
// withService creates a serviceCustomizer for the given service. | ||
// It's private to prevent users from creating other services than the Analytics and Eventing services. | ||
func withService(service Service) serviceCustomizer { | ||
return serviceCustomizer{ | ||
enabledService: service, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fbiville this is how I'm creating a custom implementation of a customizer for services, holding a state for the services to enable, and adding a Customize request method for the customizer which modifies the container request.
The services, because they are not part of the request, need to be transferred to the config above.
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
- Define container options for the module. We consider that a best practice for the options is to return a function that returns a modified `testcontainers.GenericContainerRequest` type, and for that, the library already provides with a `testcontainers.CustomizeRequestOption` type representing this function signature. | ||
- Define container options for the module leveraging the `testcontainers.ContainerCustomizer` interface, that has one single method: `Customize(req *GenericContainerRequest)`. | ||
- We consider that a best practice for the options is define a function using the `With` prefix, that returns a function returning a modified `testcontainers.GenericContainerRequest` type. For that, the library already provides with a `testcontainers.CustomizeRequestOption` type implementing the `ContainerCustomizer` interface, and we encourage you use this type for creating your own customizer functions. | ||
- At the same time, you could need to create your own container customizers for your module. Make sure they implement the `testcontainers.ContainerCustomizer` interface. Defining your own customizer functions is useful when you need to transfer certain state that is not present at the `ContainerRequest` to the container, possibly using an intermediate Config struct. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fbiville explained here in the docs
What does this PR do?
This PR leverages a new interface for container customization:
ContainerCustomizer
, which has one single method:Customize
.Therefore we are adding a polymorphic behaviour for customization, which opens the gate to module authors to create an in-module struct implementing the interface and pass it as a customizer.
As a result, we are updating all existing modules to follow this design.
Why is it important?
Accepting interfaces as options allow users to define their own customizers, making it more flexible the creation of modules.
Related issues