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

define_accessors eval block consumes nonsensical values #169

Open
Slotos opened this issue May 10, 2021 · 0 comments
Open

define_accessors eval block consumes nonsensical values #169

Slotos opened this issue May 10, 2021 · 0 comments

Comments

@Slotos
Copy link

Slotos commented May 10, 2021

Describe the bug

Dry::Struct::ClassInterface#define_accessors naively consumes attribute value in eval block.

If the name contains a single space, second word becomes a parameter.
If argument contains special symbols, SyntaxError is raised.

To Reproduce

require 'dry-types'
require 'dry-struct'

module Types
  include Dry.Types()
end

class Line < Dry::Struct
  attribute :'test string', Types::String  
end

Line.new('test string': '42').test # => ArgumentError: wrong number of arguments (given 0, expected 1)

class Handlebar < Dry::Struct
  attribute :'{test}', Types::String.optional.default(nil)
end # => raises SyntaxError

Expected behavior

Direct accessor methods are only created if the attribute name is a valid method name.
By default, a specific error is raised for names that don't fit the criteria.
Option to skip accessor creation is available.

My environment

  • Affects my production application: NO
  • Ruby version: 2.6.6, 2.7.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant