Comments (7)
Thanks for clarification
from dry-struct.
Monkey-patch fix:
class Dry::Struct
undef_method :to_hash
def to_h
self.class.schema.keys.each_with_object({}) do |key, result|
result[key] = Hashify[self[key]]
end
end
end
But I am not 100% of the implications of it
from dry-struct.
Yeah, I know that, this is how ruby works, they really messed with it IMO. Structs are supposed to work as hashes so this is expected. I wouldn't go with a monkey patch though, you can subclass Dry::Struct
and use it as a base where you need
class MyStruct < Dry::Struct
undef_method :to_hash
end
^also, you don't need to override to_h
, undefining of to_hash
doesn't affect it.
from dry-struct.
You say "Structs are supposed to work as hashes", but I wonder where is this requirement coming from.
In Ruby neither Struct
nor OpenStruct
implements the implicit conversion method to_hash
. Both implement the explicit conversion method #to_h
More on the differences between implicit and explicit conversion methods here.
irb(main):001:0> A = Struct.new(:a, :b)
=> A
irb(main):002:0> def iii(**kwargs)
irb(main):003:1> puts kwargs
irb(main):004:1> end
=> :iii
irb(main):005:0> iii A.new(1,2)
Traceback (most recent call last):
3: from /Users/sevos/.rbenv/versions/2.5.0/bin/irb:11:in `<main>'
2: from (irb):5
1: from (irb):2:in `iii'
ArgumentError (wrong number of arguments (given 1, expected 0))
irb(main):006:0> iii A.new(1,2).to_h
{:a=>1, :b=>2}
=> nil
irb(main):009:0> iii OpenStruct.new(a: 1, b: 2)
Traceback (most recent call last):
3: from /Users/sevos/.rbenv/versions/2.5.0/bin/irb:11:in `<main>'
2: from (irb):9
1: from (irb):2:in `iii'
irb(main):010:0> iii OpenStruct.new(a: 1, b: 2).to_h
{:a=>1, :b=>2}
=> nil
irb(main):007:0>
I am afraid that there may be more cases when the struct may be deconstructed into a hash prematurely or accidentally causing us to lose the information about the type of the data it carries. Please tell me what are the cases you find this behaviour useful? Maybe there is really more good than harm...
Personally I use Dry::Struct
as a base class for business domain entities and it's more of a better-Struct than a better-Hash, so I would prefer not to deconstruct these objects easily. Of course there should be a way to convert it to a hash, when needed.
Thanks for the idea for the base class, but now I worry more about dry-struct compatibility with other libraries, which may deconstruct it accidentally.
from dry-struct.
This is not a requirement, this is a design choice. One quick example is rom
's interface for writing data, it relies on coercions made by Hash[value]
, anything hash-like can be passed in, including structs.
from dry-struct.
For the record, this also just bit me. I was definitely not expecting my entity object to deconstruct into a hash because I had a keyword arg in a composite class initializer.
from dry-struct.
FYI: This does not even require **kwargs
, regular keyword arguments are enough.
def display(*items, color: true)
puts "items: %p color: %p" % [items, color]
end
Calling this with a Dry::Struct
causes: ArgumentError : unknown keywords: item_attribute, item_attribute2, item_attribute_x
There's an issue (that originated from using Dry::Validation :)) at https://bugs.ruby-lang.org/issues/14909 and they suggest "just wait for ruby 3"
from dry-struct.
Related Issues (20)
- Does not create an accessor method when method exists into the mother class HOT 5
- Error while using optional on Dry::Struct HOT 4
- Sum operator not working when default values are defined HOT 4
- Document how sum types works with structs HOT 4
- Instance method name attribute not available HOT 1
- Compose transforms HOT 2
- Subclasses don't recognize properly when attribute described as Types::Array.of(Parent)
- Missing attribute and maybe type HOT 4
- Expanding array item type in generated schema HOT 2
- `define_accessors` eval block consumes nonsensical values
- MissingAttributeError when access optional field with hash syntax
- Assigning nil to attribute returns hash if attribute struct has optional attibutes only HOT 3
- Errors in structs nested inside a Types::Hash raise as `#<NoMethodError: undefined method 'message' for #<String` instead of Dry Errors with the actual message HOT 1
- Struct with optional nested struct requires key in nested struct HOT 2
- [Security] Workflow ci.yml is using vulnerable action actions/checkout
- [Security] Workflow ci.yml is using vulnerable action actions/checkout
- Dry::Struct by including module HOT 1
- Release 1.5.0 breaks rails app boot with eager-loading HOT 8
- Nested optional struct does not inherit `transform_keys(&:to_sym)` HOT 3
- `Dry::Types::ConstraintError` not propagated when originating from inside a `Dry::Types::Map`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dry-struct.