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

Element nesting depth ignored #74

Open
felixbuenemann opened this issue Sep 3, 2015 · 5 comments
Open

Element nesting depth ignored #74

felixbuenemann opened this issue Sep 3, 2015 · 5 comments

Comments

@felixbuenemann
Copy link

I have the following case:

<root>
  <foo id="1">
    <bar>
      <meta>
        <value id="a"/>
        <value id="b"/>
      </meta>
    </bar>
    <meta>
      <value id="x"/>
      <value id="y"/>
    </meta>
  </foo>
  <foo id="2">
    <meta>
      <value id="z"/>
    </meta>
  </foo>
</root>

And the following mapping:

class Root
  include SAXMachine
  elements :foo, as: :children, class: Foo
end

class Meta
  include SAXMachine
  elements :values, as: :values, class: String
end

class Foo
  include SAXMachine
  element :meta, class: Meta
end

Now if I get metaof foo with id 1, I get the meta node of bar instead of the meta node of foo.

How do I solve this?

Maybe it would be great to have a depth constraint, so I could define something like:

class Foo
  include SAXMachine
  element :meta, class: Meta, depth: 1
  element :other, class: Other, depth: ->(depth) { depth > 2 && depth < 5 }
end
@felixbuenemann felixbuenemann changed the title Element depth ignored Element nesting depth ignored Sep 3, 2015
@krasnoukhov
Copy link
Collaborator

Hey, thanks for reporting. Would you mind writing a failing test so we can address this properly? Thanks.

@felixbuenemann
Copy link
Author

Sure, I'm on the road during the week, but I should be able to come up with a test this weekend.

@felixbuenemann
Copy link
Author

The specs for "parsing a tree without a collection class" and "with element deeper inside the xml structure" in sax_document_spec.rb added in 68c445f specifically assert the current behavior, so I can't really write a sensible failing spec.

Any ideas?

@krasnoukhov
Copy link
Collaborator

Thanks for looking it up. Do you see any way of improving the things without introducing a breaking change?

@felixbuenemann
Copy link
Author

One idea is the depth constraint mentioned above. Another idea would be a within constraint:

class Foo
  include SAXMachine
  element :meta, class: Meta, within: '.'        # only children of current element
  elements :value, as: :values, within: './meta' # match within meta children of current element
  elements :meta, as: :bar_metas, within: 'bar'  # match meta inside any bar descendants
end

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

No branches or pull requests

2 participants