Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
829 views
in Technique[技术] by (71.8m points)

karate - Is there a simple match for objects containing array where the array content order doesn't matter?

I was trying to compare objects that contain arrays when I don't care about the order of keys or the order of array contents. Contains deep is close to what I need except that I need to use it both ways around to ensure there are no missing elements in the data. Ideally I want a version of == that is order-insensitive. Is there is a better option to what I have below?

    * def sample = { user: ['read', 'write', 'append'], public: ['read', 'append'] }
    * def test = { public: ['append', 'read'], user: ['write', 'read', 'append'] }
    * match sample contains deep test
    * match test contains deep sample
question from:https://stackoverflow.com/questions/65938339/is-there-a-simple-match-for-objects-containing-array-where-the-array-content-ord

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use 2 contains only. Yes we haven't designed a deep contains, it is rarely needed.

* def sample = { user: ['read', 'write', 'append'], public: ['read', 'append'] }
* match sample.public contains only ['append', 'read']
* match sample.user contains only ['write', 'read', 'append']

For completeness, although this may be overkill unless you want to re-use schema pieces, you can do this:

* def user = ['write', 'read', 'append']
* def public = ['append', 'read']

* def sample = { user: ['read', 'write', 'append'], public: ['read', 'append'] }
* match sample == { user: '#(^^user)', public: '#(^^public)' }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...