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

Nodes with the same value are mixed up #63

Closed
Leonti opened this issue Jan 28, 2020 · 8 comments · Fixed by #69
Closed

Nodes with the same value are mixed up #63

Leonti opened this issue Jan 28, 2020 · 8 comments · Fixed by #69
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Leonti
Copy link

Leonti commented Jan 28, 2020

Hi!
I'm having a weird issue with targeting nodes that I want to replace, given the following snippet:

import scala.xml._
import advxml.syntax.traverse.try_._
import advxml.syntax.transform._
import advxml.instances.transform._
import scala.util._
import cats.implicits._

val doc = XML.loadString("""
<charts>
  <chart>
    <title>chart-0</title>
    <entries>
      <entry idx="0">
        <v>13</v>
      </entry>
    </entries>
  </chart>
  <chart>
    <title>chart-1</title>
    <entries>
      <entry idx="0">
        <v>12</v>
      </entry>
    </entries>
  </chart>  
</charts>
""")

def toEntries(root: NodeSeq): NodeSeq = (root \ "chart" filter hasImmediateChild("title", text(_ == "chart-1"))) \ "entries"

val rule = $(r => (toEntries(r) \ "entry" filter attrs("idx" -> (_ == "0"))) \ "v" ) ==> Replace(_ => <v>11</v>) 

val result: Try[NodeSeq] = doc.transform[Try](rule)

println(result.toOption.get)

It will output this:

<charts>
   <chart>
      <title>chart-0</title>
      <entries>
         <entry idx="0">
            <v>13</v>
         </entry>
      </entries>
   </chart>
   <chart>
      <title>chart-1</title>
      <entries>
         <entry idx="0">
            <v>11</v>
         </entry>
      </entries>
   </chart>
</charts>

https://scastie.scala-lang.org/JRM25U76S6yXpcIV6ZuDug
It correctly found entries of a chart which has chart-1 as a title and then replaced it.

But as soon as I make the original values to be the same number:

val doc = XML.loadString("""
<charts>
  <chart>
    <title>chart-0</title>
    <entries>
      <entry idx="0">
        <v>12</v>
      </entry>
    </entries>
  </chart>
  <chart>
    <title>chart-1</title>
    <entries>
      <entry idx="0">
        <v>12</v>
      </entry>
    </entries>
  </chart>  
</charts>
""")

it will replace the entry under chart-0, not chart-1 as expected:

<charts>
   <chart>
      <title>chart-0</title>
      <entries>
         <entry idx="0">
            <v>11</v>
         </entry>
      </entries>
   </chart>
   <chart>
      <title>chart-1</title>
      <entries>
         <entry idx="0">
            <v>12</v>
         </entry>
      </entries>
   </chart>
</charts>

https://scastie.scala-lang.org/KBkU87D6T4GhNlkDq3rWjQ

Looks like it's finding the element first, so in this case <v>12</v> and then using it for replacement, so if you have 2 identical elements bu with different paths it will replace the first one.

Or maybe I'm not using it correctly?
By they way, apart from this issue the library is a joy to use, thanks!

@geirolz geirolz self-assigned this Jan 30, 2020
@geirolz geirolz modified the milestone: v2.1.0 Jan 30, 2020
@geirolz geirolz added the under investigation Under investigation label Jan 30, 2020
@geirolz
Copy link
Owner

geirolz commented Jan 30, 2020

Hi @Leonti thanks for your feedback :)
I'm investigating about this problem, i let you know as soon as possible.

P.S. Special thanks for scastie examples.

@geirolz geirolz added the bug Something isn't working label Jan 30, 2020
@geirolz
Copy link
Owner

geirolz commented Jan 30, 2020

@SethTisue what do you think about this problem ?

Currently in my XmlTransformer class i create a standard RewriteRule instance and, if parameter is equals to the target(the node to edit) i apply my function. The problem is that the node hasn't an unique ID and the structure is an unidirectional tree(i can't check his parent) so in this case i match the both the nodes.

@geirolz
Copy link
Owner

geirolz commented Jan 31, 2020

I've just opened an issue on scala-xml project for this problem.
scala/scala-xml#403

@geirolz geirolz added this to the v2.1.0 milestone Jan 31, 2020
@geirolz geirolz pinned this issue Jan 31, 2020
@SethTisue
Copy link

SethTisue commented Jan 31, 2020

@geirolz sorry, no opinion, I don't know this API, either in this repo or over in scala-xml

@geirolz geirolz removed the under investigation Under investigation label Feb 18, 2020
@geirolz
Copy link
Owner

geirolz commented Feb 18, 2020

Hi @Leonti finally i've resolved this bug.
I'm going to release a new draft version 2.1.0-RC1, if you what you can try it and give me a feedback , your help is very important for me.

Thanks for the support.

@geirolz geirolz linked a pull request Feb 18, 2020 that will close this issue
@geirolz geirolz unpinned this issue Feb 18, 2020
geirolz added a commit that referenced this issue Feb 18, 2020
@Leonti
Copy link
Author

Leonti commented Feb 24, 2020

Hi @geirolz!
That's awesome news! Thanks a lot!
Sorry for the late reply, didn't have a chance to try it out before.
I like the new interface, where XmlZoom is a case class :)

I see that \\ is not supported - any way I can replace it with existing \, \+, \++, | combinators?

Thanks again,
Leonti

@geirolz
Copy link
Owner

geirolz commented Feb 27, 2020

@Leonti Yes unfortunately currently \\ is not supported, i'm working on it.

  • \ traverse the document down to the immediate child with specified label
  • \+ and \++ is used to combine respectively one and more XmlZoom.
  • | is just an alias to filter method

So, for answer to your question:

any way I can replace it with existing , +, ++, | combinators?

No, the only way to traverse the document currently is using \, you need to "map" the full path to the node you want. I know that this is a regression but i prefered fix a runtime bug , i'm working on a solution to reintroduce \\ the operator.

Is your bug fixed with these changes ?

@Leonti
Copy link
Author

Leonti commented Feb 28, 2020

Hi @geirolz!

Yes, definitely the right call with fixing the bug first and then introducing the missing functionality.

Haven't had a chance to update the library in my code to see if it fixed the bug (I'm using a workaround for now - just making sure all the values are unique).
Will try to get to it next week, but by looking at your code it looks like it's going to fix it :)

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants