Skip to content

Commit

Permalink
Added some convenience methods for casting Result's to a string, numb…
Browse files Browse the repository at this point in the history
…er, or NodeSet value.
  • Loading branch information
ChrisTrenkamp committed Apr 20, 2024
1 parent 203914e commit fd8f24b
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 50 deletions.
82 changes: 82 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,88 @@ func ExampleExec() {
// Output: This is an XML node.
}

func ExampleExecAsString() {
xml := `
<root>
<a>This is the first node.</a>
<a>This is the second node.</a>
</root>
`

xpath := xsel.MustBuildExpr(`/root/a`)
cursor, _ := xsel.ReadXml(bytes.NewBufferString(xml))
result, _ := xsel.ExecAsString(cursor, &xpath)

// Be careful when returning the string result of NodeSet's.
// Only the first node's string value will be returned.
// If you want the string value of all node's, use ExecAsNodeset.

fmt.Println(result)
// Output: This is the first node.
}

func ExampleExecAsNumber() {
xml := `
<root>
<a>3.14</a>
<a>9001</a>
</root>
`

xpath := xsel.MustBuildExpr(`/root/a`)
cursor, _ := xsel.ReadXml(bytes.NewBufferString(xml))
result, _ := xsel.ExecAsNumber(cursor, &xpath)

// Be careful when returning the number result of NodeSet's.
// Only the first node's value value will be returned.

fmt.Println(result)
// Output: 3.14
}

func ExampleExecAsNodeset() {
xml := `
<root>
<a>This is the first node.</a>
<a>This is the second node.</a>
</root>
`

xpath := xsel.MustBuildExpr(`/root/a`)
cursor, _ := xsel.ReadXml(bytes.NewBufferString(xml))
result, _ := xsel.ExecAsNodeset(cursor, &xpath)

for _, i := range result {
fmt.Println(xsel.GetCursorString(i))
}

// Output: This is the first node.
// This is the second node.
}

func ExampleExecAsNodeset_subqueries() {
xml := `
<root>
<a><b>Some text inbetween b and c. <c>A descendant c element.</c></b></a>
<a><d>A d element.</d><c>A c element.</c></a>
</root>
`

aQuery := xsel.MustBuildExpr(`/root/a`)
rootCursor, _ := xsel.ReadXml(bytes.NewBufferString(xml))
aElements, _ := xsel.ExecAsNodeset(rootCursor, &aQuery)

cQuery := xsel.MustBuildExpr(`.//c`)

for i, a := range aElements {
cElements, _ := xsel.ExecAsNodeset(a, &cQuery)
fmt.Printf("%d: %s\n", i, cElements.String())
}

// Output: 0: A descendant c element.
// 1: A c element.
}

func ExampleWithNS() {
xml := `
<root xmlns="http://some.namespace.com">
Expand Down
60 changes: 30 additions & 30 deletions exec/contextfn_comparisons.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func execEqualityExprEqual(context *exprContext, expr *grammar.Grammar) error {
if leftNodeSetOk && rightNodeSetOk {
for _, leftNode := range leftNodeSet {
for _, rightNode := range rightNodeSet {
if getCursorString(leftNode) == getCursorString(rightNode) {
if GetCursorString(leftNode) == GetCursorString(rightNode) {
context.result = Bool(true)
return nil
}
Expand All @@ -72,7 +72,7 @@ func execEqualityExprEqual(context *exprContext, expr *grammar.Grammar) error {

if leftNumberOk && rightNodeSetOk {
for _, rightNode := range rightNodeSet {
if leftNumber == Number(getStringNumber(getCursorString(rightNode))) {
if leftNumber == Number(getStringNumber(GetCursorString(rightNode))) {
context.result = Bool(true)
return nil
}
Expand All @@ -86,7 +86,7 @@ func execEqualityExprEqual(context *exprContext, expr *grammar.Grammar) error {

if leftNodeSetOk && rightNumberOk {
for _, leftNode := range leftNodeSet {
if Number(getStringNumber(getCursorString(leftNode))) == rightNumber {
if Number(getStringNumber(GetCursorString(leftNode))) == rightNumber {
context.result = Bool(true)
return nil
}
Expand All @@ -100,7 +100,7 @@ func execEqualityExprEqual(context *exprContext, expr *grammar.Grammar) error {

if leftStringOk && rightNodeSetOk {
for _, rightNode := range rightNodeSet {
if leftString == String(getCursorString(rightNode)) {
if leftString == String(GetCursorString(rightNode)) {
context.result = Bool(true)
return nil
}
Expand All @@ -114,7 +114,7 @@ func execEqualityExprEqual(context *exprContext, expr *grammar.Grammar) error {

if leftNodeSetOk && rightStringOk {
for _, leftNode := range leftNodeSet {
if String(getCursorString(leftNode)) == rightString {
if String(GetCursorString(leftNode)) == rightString {
context.result = Bool(true)
return nil
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func execEqualityExprNotEqual(context *exprContext, expr *grammar.Grammar) error
if leftNodeSetOk && rightNodeSetOk {
for _, leftNode := range leftNodeSet {
for _, rightNode := range rightNodeSet {
if getCursorString(leftNode) != getCursorString(rightNode) {
if GetCursorString(leftNode) != GetCursorString(rightNode) {
context.result = Bool(true)
return nil
}
Expand All @@ -180,7 +180,7 @@ func execEqualityExprNotEqual(context *exprContext, expr *grammar.Grammar) error

if leftNumberOk && rightNodeSetOk {
for _, rightNode := range rightNodeSet {
if leftNumber != Number(getStringNumber(getCursorString(rightNode))) {
if leftNumber != Number(getStringNumber(GetCursorString(rightNode))) {
context.result = Bool(true)
return nil
}
Expand All @@ -194,7 +194,7 @@ func execEqualityExprNotEqual(context *exprContext, expr *grammar.Grammar) error

if leftNodeSetOk && rightNumberOk {
for _, leftNode := range leftNodeSet {
if Number(getStringNumber(getCursorString(leftNode))) != rightNumber {
if Number(getStringNumber(GetCursorString(leftNode))) != rightNumber {
context.result = Bool(true)
return nil
}
Expand All @@ -208,7 +208,7 @@ func execEqualityExprNotEqual(context *exprContext, expr *grammar.Grammar) error

if leftStringOk && rightNodeSetOk {
for _, rightNode := range rightNodeSet {
if leftString != String(getCursorString(rightNode)) {
if leftString != String(GetCursorString(rightNode)) {
context.result = Bool(true)
return nil
}
Expand All @@ -222,7 +222,7 @@ func execEqualityExprNotEqual(context *exprContext, expr *grammar.Grammar) error

if leftNodeSetOk && rightStringOk {
for _, leftNode := range leftNodeSet {
if String(getCursorString(leftNode)) != rightString {
if String(GetCursorString(leftNode)) != rightString {
context.result = Bool(true)
return nil
}
Expand Down Expand Up @@ -273,7 +273,7 @@ func execRelationalExprLessThan(context *exprContext, expr *grammar.Grammar) err
if leftNodeSetOk && rightNodeSetOk {
for _, leftNode := range leftNodeSet {
for _, rightNode := range rightNodeSet {
if getCursorString(leftNode) < getCursorString(rightNode) {
if GetCursorString(leftNode) < GetCursorString(rightNode) {
context.result = Bool(true)
return nil
}
Expand All @@ -288,7 +288,7 @@ func execRelationalExprLessThan(context *exprContext, expr *grammar.Grammar) err

if leftNumberOk && rightNodeSetOk {
for _, rightNode := range rightNodeSet {
if leftNumber < Number(getStringNumber(getCursorString(rightNode))) {
if leftNumber < Number(getStringNumber(GetCursorString(rightNode))) {
context.result = Bool(true)
return nil
}
Expand All @@ -302,7 +302,7 @@ func execRelationalExprLessThan(context *exprContext, expr *grammar.Grammar) err

if leftNodeSetOk && rightNumberOk {
for _, leftNode := range leftNodeSet {
if Number(getStringNumber(getCursorString(leftNode))) < rightNumber {
if Number(getStringNumber(GetCursorString(leftNode))) < rightNumber {
context.result = Bool(true)
return nil
}
Expand All @@ -316,7 +316,7 @@ func execRelationalExprLessThan(context *exprContext, expr *grammar.Grammar) err

if leftStringOk && rightNodeSetOk {
for _, rightNode := range rightNodeSet {
if leftString < String(getCursorString(rightNode)) {
if leftString < String(GetCursorString(rightNode)) {
context.result = Bool(true)
return nil
}
Expand All @@ -330,7 +330,7 @@ func execRelationalExprLessThan(context *exprContext, expr *grammar.Grammar) err

if leftNodeSetOk && rightStringOk {
for _, leftNode := range leftNodeSet {
if String(getCursorString(leftNode)) < rightString {
if String(GetCursorString(leftNode)) < rightString {
context.result = Bool(true)
return nil
}
Expand All @@ -357,7 +357,7 @@ func execRelationalExprLessThanOrEqual(context *exprContext, expr *grammar.Gramm
if leftNodeSetOk && rightNodeSetOk {
for _, leftNode := range leftNodeSet {
for _, rightNode := range rightNodeSet {
if getCursorString(leftNode) <= getCursorString(rightNode) {
if GetCursorString(leftNode) <= GetCursorString(rightNode) {
context.result = Bool(true)
return nil
}
Expand All @@ -372,7 +372,7 @@ func execRelationalExprLessThanOrEqual(context *exprContext, expr *grammar.Gramm

if leftNumberOk && rightNodeSetOk {
for _, rightNode := range rightNodeSet {
if leftNumber <= Number(getStringNumber(getCursorString(rightNode))) {
if leftNumber <= Number(getStringNumber(GetCursorString(rightNode))) {
context.result = Bool(true)
return nil
}
Expand All @@ -386,7 +386,7 @@ func execRelationalExprLessThanOrEqual(context *exprContext, expr *grammar.Gramm

if leftNodeSetOk && rightNumberOk {
for _, leftNode := range leftNodeSet {
if Number(getStringNumber(getCursorString(leftNode))) <= rightNumber {
if Number(getStringNumber(GetCursorString(leftNode))) <= rightNumber {
context.result = Bool(true)
return nil
}
Expand All @@ -400,7 +400,7 @@ func execRelationalExprLessThanOrEqual(context *exprContext, expr *grammar.Gramm

if leftStringOk && rightNodeSetOk {
for _, rightNode := range rightNodeSet {
if leftString <= String(getCursorString(rightNode)) {
if leftString <= String(GetCursorString(rightNode)) {
context.result = Bool(true)
return nil
}
Expand All @@ -414,7 +414,7 @@ func execRelationalExprLessThanOrEqual(context *exprContext, expr *grammar.Gramm

if leftNodeSetOk && rightStringOk {
for _, leftNode := range leftNodeSet {
if String(getCursorString(leftNode)) <= rightString {
if String(GetCursorString(leftNode)) <= rightString {
context.result = Bool(true)
return nil
}
Expand All @@ -441,7 +441,7 @@ func execRelationalExprGreaterThan(context *exprContext, expr *grammar.Grammar)
if leftNodeSetOk && rightNodeSetOk {
for _, leftNode := range leftNodeSet {
for _, rightNode := range rightNodeSet {
if getCursorString(leftNode) > getCursorString(rightNode) {
if GetCursorString(leftNode) > GetCursorString(rightNode) {
context.result = Bool(true)
return nil
}
Expand All @@ -456,7 +456,7 @@ func execRelationalExprGreaterThan(context *exprContext, expr *grammar.Grammar)

if leftNumberOk && rightNodeSetOk {
for _, rightNode := range rightNodeSet {
if leftNumber > Number(getStringNumber(getCursorString(rightNode))) {
if leftNumber > Number(getStringNumber(GetCursorString(rightNode))) {
context.result = Bool(true)
return nil
}
Expand All @@ -470,7 +470,7 @@ func execRelationalExprGreaterThan(context *exprContext, expr *grammar.Grammar)

if leftNodeSetOk && rightNumberOk {
for _, leftNode := range leftNodeSet {
if Number(getStringNumber(getCursorString(leftNode))) > rightNumber {
if Number(getStringNumber(GetCursorString(leftNode))) > rightNumber {
context.result = Bool(true)
return nil
}
Expand All @@ -484,7 +484,7 @@ func execRelationalExprGreaterThan(context *exprContext, expr *grammar.Grammar)

if leftStringOk && rightNodeSetOk {
for _, rightNode := range rightNodeSet {
if leftString > String(getCursorString(rightNode)) {
if leftString > String(GetCursorString(rightNode)) {
context.result = Bool(true)
return nil
}
Expand All @@ -498,7 +498,7 @@ func execRelationalExprGreaterThan(context *exprContext, expr *grammar.Grammar)

if leftNodeSetOk && rightStringOk {
for _, leftNode := range leftNodeSet {
if String(getCursorString(leftNode)) > rightString {
if String(GetCursorString(leftNode)) > rightString {
context.result = Bool(true)
return nil
}
Expand All @@ -525,7 +525,7 @@ func execRelationalExprGreaterThanOrEqual(context *exprContext, expr *grammar.Gr
if leftNodeSetOk && rightNodeSetOk {
for _, leftNode := range leftNodeSet {
for _, rightNode := range rightNodeSet {
if getCursorString(leftNode) >= getCursorString(rightNode) {
if GetCursorString(leftNode) >= GetCursorString(rightNode) {
context.result = Bool(true)
return nil
}
Expand All @@ -540,7 +540,7 @@ func execRelationalExprGreaterThanOrEqual(context *exprContext, expr *grammar.Gr

if leftNumberOk && rightNodeSetOk {
for _, rightNode := range rightNodeSet {
if leftNumber >= Number(getStringNumber(getCursorString(rightNode))) {
if leftNumber >= Number(getStringNumber(GetCursorString(rightNode))) {
context.result = Bool(true)
return nil
}
Expand All @@ -554,7 +554,7 @@ func execRelationalExprGreaterThanOrEqual(context *exprContext, expr *grammar.Gr

if leftNodeSetOk && rightNumberOk {
for _, leftNode := range leftNodeSet {
if Number(getStringNumber(getCursorString(leftNode))) >= rightNumber {
if Number(getStringNumber(GetCursorString(leftNode))) >= rightNumber {
context.result = Bool(true)
return nil
}
Expand All @@ -568,7 +568,7 @@ func execRelationalExprGreaterThanOrEqual(context *exprContext, expr *grammar.Gr

if leftStringOk && rightNodeSetOk {
for _, rightNode := range rightNodeSet {
if leftString >= String(getCursorString(rightNode)) {
if leftString >= String(GetCursorString(rightNode)) {
context.result = Bool(true)
return nil
}
Expand All @@ -582,7 +582,7 @@ func execRelationalExprGreaterThanOrEqual(context *exprContext, expr *grammar.Gr

if leftNodeSetOk && rightStringOk {
for _, leftNode := range leftNodeSet {
if String(getCursorString(leftNode)) >= rightString {
if String(GetCursorString(leftNode)) >= rightString {
context.result = Bool(true)
return nil
}
Expand Down
Loading

0 comments on commit fd8f24b

Please sign in to comment.