<#128 Is there a way to use expressions into gener...
# pact-swift
g
#128 Is there a way to use expressions into generators? New issue created by marcelinoagile Hi, I'm using your library and it works like a charm. The problem is that now I'm trying to add "dynamic" dates to the pact and I can't find the way to do it. Given a pact where the response should be:
Copy code
{
  "id": "321",
  "start": "today +1 day @ 6 o'clock pm"
}
... my backend colleague is adding the following object to the body using the Pacts Java library:
object.datetimeExpression("start", "today +1 day @ 6 o'clock pm", "yyyy-MM-dd'T'HH:mm'Z'");
And the datetime expression generates:
Copy code
"generators": {
 "body": {
  "$.start": {
   "type": "DateTime",
   "format": "yyyy-MM-dd'T'HH:mm:ss'Z'",
   "expression": "today +1 day @ 6 o'clock pm"
  }
 }
}
But unfortunately I don't find the way to do it using this library. Could you help me? Thank you in advance. DiUS/pact-consumer-swift
a
Hello, I'm the backend colleague šŸ™‚. As you answered to this issue, on backend and Android side we are using v3 spec, We are studiying migrating the apple lib to the new one that supports v3, but what we didn`t locate in its documentation is if we could use expressions like the noted in the issue.
Copy code
"today +1 day @ 6 o'clock pm"
If it is possible to use this kind of expression, could you, please, give us some examples of its usage? Thanks a lot!
m
If you’re wanting to put that expression into the pact file, I don’t know if that is possible. @uglyog I don’t believe that’s possible in Pact JVM. Can you do that?
m
No. I just tried this out and
PactSwift
passes the
expression
into the DateTime example generator. But
pact-rust
only spits out the
type
and
format
- just like the spec v3 says. The way I can see this making sense is to have the ā€œexpressionā€ (or really just a provided
Date
object) used for the consumer tests. But goes against best practices where Pact would be used to test values instead of validating structure and types.
There’s a branch now that exposes a `ExampleGenerator.DateTime(date: Date, format: String)`that will use the provided date converted to
String
using the provided format for the consumer test. Instead of the existing
ExampleGenerator.RandomDateTime(format: String)
that returns a formatted date for consumer tests. But I don’t think there’s a declarative way of defining
Date
objects though in Swift where you could just say ā€œsome day in the future added by 3 hours and 4 millisecondsā€. You’ll need to use
Calendar
and create the
Date
object by adding date components.
I’d be very confused if I saw a:
Copy code
{ 
  "startDate": "some words describing a date" // instead of exemplary "2023-02-13" 
}
so then I can’t use
Decodable
on to convert into
Date
object… šŸ˜…
Tracking this feature request in pact rust-core at https://github.com/pact-foundation/pact-reference/issues/180
šŸ‘ 2
@Marcelino @Ardiel PactSwift should be supporting the datetime expressions now on branch
feature/generator/datetime-expression
(link: https://github.com/surpher/PactSwift/tree/feature/generator/datetime-expression) Have it a spin and let me know if it works as expected. If it does I’ll release a new version.
Example usage:
Copy code
.willRespondWith(
	status: 201,
	body: [
		"start": ExampleGenerator.DateTimeExpression(expression: "@ next hour", format: dateFormat)
	]
)
m
Hi @Marko (IttyBittyApps / pact-swift) I’ll give it a try ASAP. Thank you very much!
m
@Marcelino if you don’t mind leave a comment on the PR for this if it fits the requirements. PR: https://github.com/surpher/PactSwift/pull/84 I’ll then prepare a new version to release.
m
Sure @Marko (IttyBittyApps / pact-swift) I’m still testing
m
DateTimeExpression
now available in
PactSwift v0.12.0
m
Thank you very much @Marko (IttyBittyApps / pact-swift)
m
No worries. Thanks for your PR. I completely missed that rather obvious one šŸ¤·ā€ā™‚ļø
šŸ‘ 1