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

Adding new functions to Node #54

Open
suntong opened this issue Mar 17, 2021 · 3 comments
Open

Adding new functions to Node #54

suntong opened this issue Mar 17, 2021 · 3 comments

Comments

@suntong
Copy link

suntong commented Mar 17, 2021

Basically, all functions defined in

xmlquery/node_test.go

Lines 11 to 69 in e73954f

func findRoot(n *Node) *Node {
if n == nil {
return nil
}
for ; n.Parent != nil; n = n.Parent {
}
return n
}
func findNode(root *Node, name string) *Node {
node := root.FirstChild
for {
if node == nil || node.Data == name {
break
}
node = node.NextSibling
}
return node
}
func childNodes(root *Node, name string) []*Node {
var list []*Node
node := root.FirstChild
for {
if node == nil {
break
}
if node.Data == name {
list = append(list, node)
}
node = node.NextSibling
}
return list
}
func testNode(t *testing.T, n *Node, expected string) {
if n.Data != expected {
t.Fatalf("expected node name is %s,but got %s", expected, n.Data)
}
}
func testAttr(t *testing.T, n *Node, name, expected string) {
for _, attr := range n.Attr {
if attr.Name.Local == name && attr.Value == expected {
return
}
}
t.Fatalf("not found attribute %s in the node %s", name, n.Data)
}
func testValue(t *testing.T, val, expected interface{}) {
if val == expected {
return
}
if reflect.DeepEqual(val, expected) {
return
}
t.Fatalf("expected value is %+v, but got %+v", expected, val)
}

are necessary for me for my node navigation & checking, and I'd like to move them into node and expose them,

while changing (in node.go alone)

  • testNode to func IsNode(n *Node, expected string) bool
  • testAttr to func HasAttr(n *Node, expected string) bool and
  • testValue to func HasValue(val, expected interface{}) bool

OK?

@zhengchun
Copy link
Contributor

It makes no sense to add IsNode, HasAttr and HasValue to node.go. Node already have InnerText() and SelectAttr() to help finish these logical operation.

@suntong
Copy link
Author

suntong commented Mar 18, 2021

OK. I'll leave them alone.

How about the rest of the functions then?

@suntong
Copy link
Author

suntong commented Mar 21, 2021

Ping

How about the rest of the functions?

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