The like matcher is for this purpose: <https://git...
# pact-swift
m
🙌 1
l
Thank you Matt! At least the contract generation step runs with like/somethingLike. Thank you that gives me something I can work with.
👍 1
Oh it looks like Pact is unable to apply the rules correctly though. In my example above, if we use
like
(
somethingLike
in SwiftPact) 1. Pact will try to match it as an object
Copy code
Matchers.somethingLike([
  "field1": Matchers.somethingLike("aaa"),
  "field2": Matchers.somethingLike(111)
])
Error: wanted key "field1" but found key "abc" 2. If I try to match like this, Pact will try to match it as an array
Copy code
Matchers.somethingLike([
  [
  "field1": Matchers.somethingLike("aaa"),
  "field2": Matchers.somethingLike(111)
  ]
])
Error: type mismatch - wanted List but got dictionary
m
can you please elaborate?
Something wrong with my brain today, I swear I was in the python channel!
apologies for the link to python
can you please share your setup?
I’m guessing you’re not accounting for the top level
abc
and
xyz
keys
Matchers can be nested infinitely
l
No worries and thank you for jumping in! My question is given my example of service response, is there a way to assert the types of the items in the dictionary without knowing the dictionary keys?
Copy code
/endpoint1/dictionary returns
{
  "abc": { "field1": "value1", "field2": 123 },
  "xyz": { "field1": "value2", "field2": 456 }
}
In my case, "abc" and "xyz" would be the keys. If I don't know them, is it possible to iterate on the "values" of the dictionary while ignoring the keys? The reason is in my use case, keys can be added or removed at will, but I want to assert that, whenever present, the items in the dictionary have the fields I need.
m
right
there is a matcher called eachKeyLike available in the latest pact, but not sure if Swift has supported that
👍 1
💡 1
it’s like array like, except for dictionaries of unknown keys.
I don’t know the swift interface well enough, so check the docs (or source code)
🙏 1
l
Oh ok. Yeah I think that's not yet in SwiftPact. I'll keep an eye out for
eachKeyLike
if it's added in future releases. That's good info
I tried using
eachLike
but it seems specific for arrays 👍
m
yes, it’s for arrays
swift should be able to support it, because it’s on the latest core, so it’s probably not a lot of work if you’re up for creating a PR
☝️ 1
👍 1
m
Indeed it’s on the latest core (a week ago). It should be very easy to create a new matcher. Hopefully docs are clear. Guidelines on how to contribute: https://github.com/surpher/PactSwift/blob/main/CONTRIBUTING.md Base
Matcher
(and look at implementation of some of the existing ones for inspiration. Don’t forget about the tests!): https://github.com/surpher/PactSwift/blob/main/Sources/Matchers/Matcher.swift The nastiest of things (due to a Swift bug and still wanting to support macOS 10.x) is you have to specifically handle the matcher in `PactBuilder.swift`: https://github.com/surpher/PactSwift/blob/main/Sources/PactBuilder.swift#L95
👍 1
If headspace doesn’t allow it (I totally get it!). I might be able look into it in the next couple of days.
🙏 1
l
Hi @Marko (IttyBittyApps / pact-swift) I wanted to file a ticket to better define the problem we've been discussing in this thread. Does this story description makes sense to you ?
Sorry, this is a second version (the one I meant to post)
pact-github.md
👍 1
Thank you, sir! I'll post the issue so we can collaborate 🙏
m
l
Looks really good Marko! thanks for the extra research on the matching rules. That's one spot where I was clueless.
m
Run a spike with your provider to validate it really works. I written a couple of tests but I’m tight on time. If it’s good I’ll release a new version.
l
Sorry for the delay Marko, I got sidetracked because I moved to a different project. I'll try to make time to validate it soon. 🙏
m
Any progress verifying the
EachKeyLike
matcher implementation @Luiz Ramos?
l
Let me DM you
👍 1