Skip to content

Implement a Generator interface #747

Closed
@JP-Ellis

Description

@JP-Ellis

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:v3Relating to the pact.v3 moduledifficulty:hardA task requiring a lot of work and an in-depth understanding of the codebasetype:featureNew feature

    Type

    No type

    Projects

    Status

    ✅ Completed

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions