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

Fix info extension for maybe macro #471

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

santiagodoldan
Copy link

@santiagodoldan santiagodoldan commented Nov 7, 2023

Fixes info extension for maybe macro, it introduces nullable flag to represent whether a key is nullable or not.

    schema = Dry::Schema.JSON do
      required(:email).filled(:string)
      optional(:age).maybe(:integer)
    end

BEFORE

irb(main):030> schema.info
/usr/local/bundle/ruby/3.2.0/gems/dry-schema-1.13.3/lib/dry/schema/extensions/info/schema_compiler.rb:46:in `public_send': undefined method `visit_not' for #<Dry::Schema::Info::SchemaCompiler:0x00007fc01e0ce780 @keys={:email=>{:required=>true, :type=>"string"}, :age=>{:required=>false}}> (NoMethodError)

          public_send(:"visit_#{meth}", rest, opts)
          ^^^^^^^^^^^
Did you mean?  visit_and
               visit_set
               visit_key

AFTER

irb(main):156> schema.info
=> {:keys=>{:email=>{:required=>true, :nullable=>false, :type=>"string"}, :age=>{:required=>false, :nullable=>true, :type=>"integer"}}}

@santiagodoldan santiagodoldan force-pushed the add-maybe-support-for-schema-info branch from 90d830c to ca16f29 Compare November 8, 2023 12:39
@santiagodoldan
Copy link
Author

@solnic could you let me know if my changes make sense? it seems you implemented this class and it would be nice to understand if my changes are aligned with your initial idea, thanks 💯

@santiagodoldan
Copy link
Author

@solnic Let me know if I need to apply any changes here, I'm not sure if what I did is aligned with your initial approach.

Comment on lines +90 to 94
def visit_not(_node, opts = EMPTY_HASH)
key = opts[:key]

keys[key][:nullable] = true
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't have time for a deeper look but this analysis seems very shallow. You cannot deduce a key is nullable from merely using not, this is too general. Of course, the tests pass but it's only because there are too few of them. I think you ought to analyze in visit_predicate, this is the place where you can detect if it was the maybe macro.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do that 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants