Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict method arguments by model getters #99

Open
vladfaust opened this issue May 4, 2019 · 0 comments
Open

Restrict method arguments by model getters #99

vladfaust opened this issue May 4, 2019 · 0 comments
Labels
idea Just an idea

Comments

@vladfaust
Copy link
Member

Just an idea.

module Onyx::SQL
  macro restrict(t, model, &block)
    {% for exp in block.body.expressions %}
      {% unless exp.is_a?(Call) && exp.name == "getter" %}
        {% raise "Must be getter" %}
      {% end %}

      {% found = false %}

      {% for method in t.methods %}
        {% if method.name == exp.args.first.var &&
                method.return_type == exp.args.first.type %}
          {% found = true %}
        {% end %}
      {% end %}

      {% raise "#{model} is restricted to have `#{exp.args.first.var}` getter with return type `#{exp.args.first.type}`" unless found %}
    {% end %}
  end
end

class User
  property id : Int32?
  property name : String?

  def initialize(@id = nil, @name = nil)
  end
end

def foo(user : T) forall T
  Onyx::SQL.restrict({{T}}, User) do
    getter id : Int32
    getter name : String?
  end

  pp user
end

record UserObject, id : Int32, name : String?

# foo(UserObject.new(42, nil)) # OK
foo(User.new(42, nil)) # User is restricted to have `id` getter with return type `Int32`
@vladfaust vladfaust added the idea Just an idea label May 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idea Just an idea
Projects
None yet
Development

No branches or pull requests

1 participant