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

Implement a Generator interface #747

Open
Tracked by #396
JP-Ellis opened this issue Aug 2, 2024 · 1 comment
Open
Tracked by #396

Implement a Generator interface #747

JP-Ellis opened this issue Aug 2, 2024 · 1 comment
Labels
area:v3 Relating to the pact.v3 module difficulty:hard A task requiring a lot of work and an in-depth understanding of the codebase type:feature New feature

Comments

@JP-Ellis
Copy link
Contributor

JP-Ellis commented Aug 2, 2024

Summary

Provide a Pythonic interface for creating generators.

Motivation

The Pact specification supports generators for most aspects of an interaction. These generators allow for values to be dynamic which can be particularly useful for values such as unique IDs, or datetimes.

At present, functions such as with_body or with_matching_rules only take strings or byte arrays which are passed as-is to the underlying FFI. As a result, while generators are technically supported, the end user is responsible for writing out the generators in a JSON string. This is clearly undesirable.

Proposed Implementation

General Considerations

The proposed changes would apply to all functions that can support generators, whether it be the body, headers, metadata, etc. The specific might change slightly based on context, but the general idea should remain the same.

There should also be consistency between using with_body(...) with a generators, and with_generators(...). That is, a user should be able to straightforward refactor from one to the other. Similarly, adapting a generator from with_body to with_header should be equally straightforward.

Generator Constructor

The specific implementation might depend a bit on what is done for the matching rules in #746, but I would want to follow a similar pattern. That is:

  • Expose a number of simple functions which can be used to create a matching rule on the fly:
    • type, min, max, timestamp, time, date, etc.
  • Have these functions return a Generator object.
    • It should be transparently supported within the with_body function and friends.
    • It should overload the & operator to allow for logical and. This would allow for the creation of more generators such as type(42) & min(0) & max(100) for "a random integer between 0 and 100 inclusive".

Types

Unlike the matching rules, the types supported by generators is a bit more restricted and needs to be more properly informed by the underlying capabilities of the Rust library.

References

Note

To be filled out.

@JP-Ellis JP-Ellis changed the title Implement a interface Implement a Generator interface Aug 2, 2024
@JP-Ellis JP-Ellis added type:feature New feature area:v3 Relating to the pact.v3 module difficulty:hard A task requiring a lot of work and an in-depth understanding of the codebase labels Aug 2, 2024
@JP-Ellis
Copy link
Contributor Author

JP-Ellis commented Aug 2, 2024

If possible, I would like to first get some eyes across the proposed implementation to hopefully "get it right" the first time around.

Tagging @YOU54F, @mefellows, @valkolovos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:v3 Relating to the pact.v3 module difficulty:hard A task requiring a lot of work and an in-depth understanding of the codebase type:feature New feature
Projects
Status: 🚀 To-Do
Development

No branches or pull requests

1 participant