-
Notifications
You must be signed in to change notification settings - Fork 161
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
Tokenizer crash when passing an array of hashes - Add support for "key[0].foo" #208
Comments
I think I'm making a little bit of progress. I realized that array indexes and hash keys are basically the same thing (inspired by JavaScript), so that might make it easier to implement. I added an experimental test case for describe Dentaku::FlatHash do
let(:expressions) do
{
string: 'foo',
hash: {
string: 'bar',
number: 12,
hash: {
string: 'baz'
},
array: ['foo']
},
array: ['foo'],
array_hash: [
{ string: 'foo' },
{ string: 'bar' },
'baz',
]
}
end
it 'flattens a hash with nested hashes and arrays' do
flattened_hash = Dentaku::FlatHash.from_hash(expressions)
expect(flattened_hash).to eq(
:string => "foo",
:"hash.string" => "bar",
:"hash.number" => 12,
:"hash.hash.string" => "baz",
:"hash.array.0" => "foo",
:"array.0" => "foo",
:"array_hash.0.string" => "foo",
:"array_hash.1.string" => "bar",
:"array_hash.2" => "baz",
)
end
end And the code: module Dentaku
class FlatHash
def self.from_hash(h, key = [], acc = {})
case h
when Hash
h.each { |k, v| from_hash(v, key + [k], acc) }
when Array
h.each_with_index do |el, i|
from_hash(el, key + [i], acc)
end
else
acc.update(key => h)
end
flatten_keys(acc)
end The only problem is that this breaks many other things to do with arrays. I also added another test case for
It looks like Dentaku doesn't support Would it be possible to add support for hashes inside arrays? I have one case in my app where it's actually currently crashing because of this. Fortunately this case doesn't rely on the dentaku formulas at the moment, so I can ignore the error for now and disable the "formula" feature here, but it would be great if this could be supported! |
Hmmm, this is a tricky one. I think the root cause of the problem is that stored values in hashes and arrays are accessed differently. Hashes are flattened, and we get the value from "outside" by referencing the flattened key, while arrays are stored, and we can get individual values "inside" of Dentaku. I'll give it some thought and see if I can find a good solution. |
Edit: I tried it out and it fails the same way. 😞 Sorry for the noise. |
Hi @rubysolo, no worries! Also no hurry at all, but I'm definitely still interested if this might be possible in the future. Thanks! |
Hello, I've been using dentaku for a few years, and it's been really great! Thanks for all your work on it!
I just ran into a crash where I need to process some formulas inside an array of hashes. (My case is that I have an array of fields in a table, where each hash represents a row in the table.)
I've been using a fork that just includes this one commit with an "expand" option: DocSpring@458ddd1
Here's the branch in my fork, where I've added a failing test case with this "array of hashes" case: https://github.com/DocSpring/dentaku/tree/array_of_hashes
Failing test:
Failure error message:
(This test was previously passing when it didn't include the "percents" array.)
I can see that an array of strings is supported (
discounts: ["0.4 * 2", "0.3 * 2"],
), but it doesn't seem to handle the array of hashes.I'm not quite sure where to start with this, so I was wondering if you might have any idea about how to handle this case in the tokenizer?
Thanks for your time, and no problem at all if you don't have time to look into this! (I know it's a very obscure case!)
The text was updated successfully, but these errors were encountered: