pact-reference icon indicating copy to clipboard operation
pact-reference copied to clipboard

pact_consumer: `each_like!` and nested structs

Open emk opened this issue 8 years ago • 2 comments

The PR #18 contains an each_like! macro which mimics Ruby's ArrayLike matcher. This generates a #{path}[*].* rule:

      def handle_array_like array_like, path, match_type
        record_rule "#{path}", 'min' => array_like.min
        record_match_type_rule "#{path}[*].*", 'type'
        recurse array_like.contents, "#{path}[*]", :array_like
      end

@bethesque argues that the #{path}[*].* rule is unnecessary.

We could remove that rule from Rust, but then we'd want to add a fields_like! macro so that you could write:

// This is an incredibly common Rust use case when generating JSON from domain objects.
each_like!(fields_like!(strip_null_fields(json!(DomainObject::factory()))

The each_like! would generate "#{path}" => { "matches": "type" }, and the fields_like! would generate the "#{path}[*].*" => { "matches": "type" } that each_like! generates now. Maybe fields_like! should also include a build-in strip_null_fields?

This is trivial to implement; so it's purely a design question. What do people think?

emk avatar Oct 12 '17 14:10 emk

See also https://github.com/pact-foundation/pact-js/issues/108

Note that each_like! will probably need to be renamed if it no longer generates any rules about how to match the contents. Tricky. Best to talk to other maintainers about the right way to represent this.

CC @valerie-roske @bethesque @mefellows, in case you want to provide input.

emk avatar Oct 12 '17 16:10 emk

It does indicate how to match the contents, because the matchers cascade from the parents down to the children, and the min/max length attributes don't make sense for a hash.

I'm ok with doing parent[*] too. I just can't remember how jvm does it.

bethesque avatar Oct 12 '17 21:10 bethesque