Skip to content

Make schema matching easier #18

@chancancode

Description

@chancancode

Proposed API:

jexp = match({
  id: match(Fixnum).capture_as(:user_id),
  username: match(String).and(->(username){ /a-z0-9/i =~ username }),
  first_name: String,
  last_name: String,
  full_name: ->(full_name, json){ full_name == "#{json['first_name']} #{json['last_name']}" },
  password: match(nothing),
  homepage: /\Ahttps?\:\/\/.*\z/i,
  created_at: match.not(nil),
  updated_at: match.not(nil),
  posts: match({
    id: Fixnum,
    author_id: :user_id,
    title: String,
    tags: match(String).repeated(0..Infinity).or(nil)
  }).fuzzy.repeated(0..Infinity)
})

some_json.should match_json_expression(jexp)

jexp[:user_id] # => 1
  1. match (perhaps need a better name, not sure if this conflicts with other gems) is a factory method that always returns a JsonExpressions::Matcher. Passing a JsonExpressions::Matcher to match is a no-op, otherwise it would wrap that with the appropriate subclass of JsonExpression::Matcher.
  2. JsonExpressions::Matcher defines #and, #or which does what you would expect
  3. JsonExpressions::Matcher also defines #repeated which takes an integer or a integer range which turns its receiver into a JsonExpression::RepetitionMatcher and does what you would expect
  4. anything returns an instance of JsonExpressions::WildcardMatcher which matches anything (including nil), which is basically
  5. nothing returns an instance of `JsonExpression::NothingMatcher, which asserts that key doesn't exists
  6. Calling match, JsonExpressions::Matcher#and and JsonExpressions::Matcher#or with no arguments returns a JsonExpressions::NotProxy which defines a #not method, and that wraps its argument in a JsonExpressions::NotMatcher which negates its results, and then passes this to the original receiver + method
  7. JsonExpression::ArrayMatcher, JsonExpression::ObjectMatcher defines #fuzzy, #exact, #ordered and #unordered, so no more monkey-patching Array and Hash
  8. This would probably be a backwards-incompatible rewrite, We are pre-1.0 after all, so I get to do whatever I want :)

Would like some feedback on this, especially on the naming (fuzzy/exact vs forgiving/strict, etc) and any use cases that I missed.

*Also, please let me know if you expect the match method to conflict with a library you are using today. I'm also open to other suggestions for the top level method that we expose/inject (pattern, expression, expr?)

No ETA. This is probably gonna take some time and tbh it won't be on the top of my priority list for the next while, but I'll do what I can. But now that the spec is here, if you really want it now you can always implement this spec yourself :) (If you do, please pick a different name to avoid confusion)

Thank you for sticking around after I ignored you pull request for so long! (To be clear, I did not abandon the gem – I've been actively using it on Rails 4 / Ruby 2.0 myself, and I'll fix any bugs that prevents it from working correctly, I just haven't been needing these schema oriented functionalities.)

/cc @pda @tdumitrescu @martinstreicher @iangreenleaf @matthew-bb @sricc

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions