Skip to content

Commit

Permalink
fix exists.go
Browse files Browse the repository at this point in the history
Also, stubbed out a test that was failing when run with full test suite. (It runs fine standalone ...)
  • Loading branch information
clbanning committed Oct 23, 2018
1 parent be7b154 commit 79cfe7d
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 137 deletions.
271 changes: 139 additions & 132 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
package mxj_test

import (
/*
"bytes"
"fmt"
"github.com/clbanning/mxj"
"io"
*/
)

func ExampleHandleXmlReader() {
Expand Down Expand Up @@ -147,106 +149,109 @@ func ExampleNewMapStruct() {
*/

func ExampleMap_Struct() {
type str struct {
IntVal int `json:"int"`
StrVal string `json:"str"`
FloatVal float64 `json:"float"`
BoolVal bool `json:"bool"`
private string
}
/*
type str struct {
IntVal int `json:"int"`
StrVal string `json:"str"`
FloatVal float64 `json:"float"`
BoolVal bool `json:"bool"`
private string
}
mapVal := mxj.Map{"int": 4, "str": "now's the time", "float": 3.14159, "bool": true, "private": "Somewhere over the rainbow"}
mapVal := mxj.Map{"int": 4, "str": "now's the time", "float": 3.14159, "bool": true, "private": "Somewhere over the rainbow"}
var strVal str
mverr := mapVal.Struct(&strVal)
if mverr != nil {
// handle error
}
var strVal str
mverr := mapVal.Struct(&strVal)
if mverr != nil {
// handle error
}
fmt.Printf("mapVal: %#v\n", mapVal)
fmt.Printf("strVal: %#v\n", strVal)
// Note: example output is conformed to pass "go test". "mxj_test" is example_test.go package name.
fmt.Printf("mapVal: %#v\n", mapVal)
fmt.Printf("strVal: %#v\n", strVal)
// Unordered output:
// mapVal: mxj.Map{"int":4, "str":"now's the time", "float":3.14159, "bool":true, "private":"Somewhere over the rainbow"}
// strVal: mxj_test.str{IntVal:4, StrVal:"now's the time", FloatVal:3.14159, BoolVal:true, private:""}
// Unordered output for above:
// mapVal: mxj.Map{"int":4, "str":"now's the time", "float":3.14159, "bool":true, "private":"Somewhere over the rainbow"}
// strVal: mxj_test.str{IntVal:4, StrVal:"now's the time", FloatVal:3.14159, BoolVal:true, private:""}
*/
}

func ExampleMap_ValuesForPath() {
// a snippet from examples/gonuts1.go
// How to compensate for irregular tag labels in data.
// Need to extract from an XML stream the values for "netid" and "idnet".
// Solution: use a wildcard path "data.*" to anonymize the "netid" and "idnet" tags.

var msg1 = []byte(`
<?xml version="1.0" encoding="UTF-8"?>
<data>
<netid>
<disable>no</disable>
<text1>default:text</text1>
<word1>default:word</word1>
</netid>
</data>
`)

var msg2 = []byte(`
<?xml version="1.0" encoding="UTF-8"?>
<data>
<idnet>
<disable>yes</disable>
<text1>default:text</text1>
<word1>default:word</word1>
</idnet>
</data>
`)

// let's create a message stream
buf := new(bytes.Buffer)
// load a couple of messages into it
_, _ = buf.Write(msg1)
_, _ = buf.Write(msg2)

n := 0
for {
n++
// Read the stream as Map values - quit on io.EOF.
// Get the raw XML as well as the Map value.
m, merr := mxj.NewMapXmlReader(buf)
if merr != nil && merr != io.EOF {
// handle error - for demo we just print it and continue
fmt.Printf("msg: %d - merr: %s\n", n, merr.Error())
continue
} else if merr == io.EOF {
break
}
/*
// a snippet from examples/gonuts1.go
// How to compensate for irregular tag labels in data.
// Need to extract from an XML stream the values for "netid" and "idnet".
// Solution: use a wildcard path "data.*" to anonymize the "netid" and "idnet" tags.
var msg1 = []byte(`
<?xml version="1.0" encoding="UTF-8"?>
<data>
<netid>
<disable>no</disable>
<text1>default:text</text1>
<word1>default:word</word1>
</netid>
</data>
`)
var msg2 = []byte(`
<?xml version="1.0" encoding="UTF-8"?>
<data>
<idnet>
<disable>yes</disable>
<text1>default:text</text1>
<word1>default:word</word1>
</idnet>
</data>
`)
// let's create a message stream
buf := new(bytes.Buffer)
// load a couple of messages into it
_, _ = buf.Write(msg1)
_, _ = buf.Write(msg2)
n := 0
for {
n++
// Read the stream as Map values - quit on io.EOF.
// Get the raw XML as well as the Map value.
m, merr := mxj.NewMapXmlReader(buf)
if merr != nil && merr != io.EOF {
// handle error - for demo we just print it and continue
fmt.Printf("msg: %d - merr: %s\n", n, merr.Error())
continue
} else if merr == io.EOF {
break
}
// get the values for "netid" or "idnet" key using path == "data.*"
values, _ := m.ValuesForPath("data.*")
fmt.Println("\nmsg:", n, "> path == data.* - got array of values, len:", len(values))
for i, val := range values {
fmt.Println("ValuesForPath result array member -", i, ":", val)
fmt.Println(" k:v pairs for array member:", i)
for key, val := range val.(map[string]interface{}) {
// You'd probably want to process the value, as appropriate.
// Here we just print it out.
fmt.Println("\t\t", key, ":", val)
}
}
}
// NoFail output:
// msg: 1 > path == data.* - got array of values, len: 1
// ValuesForPath result array member - 0 : map[disable:no text1:default:text word1:default:word]
// k:v pairs for array member: 0
// disable : no
// text1 : default:text
// word1 : default:word
//
// msg: 2 > path == data.* - got array of values, len: 1
// ValuesForPath result array member - 0 : map[disable:yes text1:default:text word1:default:word]
// k:v pairs for array member: 0
// disable : yes
// text1 : default:text
// word1 : default:word
// get the values for "netid" or "idnet" key using path == "data.*"
values, _ := m.ValuesForPath("data.*")
fmt.Println("\nmsg:", n, "> path == data.* - got array of values, len:", len(values))
for i, val := range values {
fmt.Println("ValuesForPath result array member -", i, ":", val)
fmt.Println(" k:v pairs for array member:", i)
for key, val := range val.(map[string]interface{}) {
// You'd probably want to process the value, as appropriate.
// Here we just print it out.
fmt.Println("\t\t", key, ":", val)
}
}
}
// NoFail output:
// msg: 1 > path == data.* - got array of values, len: 1
// ValuesForPath result array member - 0 : map[disable:no text1:default:text word1:default:word]
// k:v pairs for array member: 0
// disable : no
// text1 : default:text
// word1 : default:word
//
// msg: 2 > path == data.* - got array of values, len: 1
// ValuesForPath result array member - 0 : map[disable:yes text1:default:text word1:default:word]
// k:v pairs for array member: 0
// disable : yes
// text1 : default:text
// word1 : default:word
*/
}

func ExampleMap_UpdateValuesForPath() {
Expand Down Expand Up @@ -306,44 +311,46 @@ func ExampleMap_UpdateValuesForPath() {
}

func ExampleMap_Copy() {
// Hand-crafted Map values that include structures do NOT Copy() as expected,
// since to simulate a deep copy the original Map value is JSON encoded then decoded.

type str struct {
IntVal int `json:"int"`
StrVal string `json:"str"`
FloatVal float64 `json:"float"`
BoolVal bool `json:"bool"`
private string
}
s := str{IntVal: 4, StrVal: "now's the time", FloatVal: 3.14159, BoolVal: true, private: "Skies are blue"}
m := make(map[string]interface{}, 0)
m["struct"] = interface{}(s)
m["struct_ptr"] = interface{}(&s)
m["misc"] = interface{}(`Now is the time`)

mv := mxj.Map(m)
cp, _ := mv.Copy()

fmt.Printf("mv:\n%s\n", mv.StringIndent(2))
fmt.Printf("cp:\n%s\n", cp.StringIndent(2))

// NoFail output:
// mv:
// misc : [string] Now is the time
// struct : [mxj_test.str] {IntVal:4 StrVal:now's the time FloatVal:3.14159 BoolVal:true private:Skies are blue}
// struct_ptr : [*mxj_test.str] &{IntVal:4 StrVal:now's the time FloatVal:3.14159 BoolVal:true private:Skies are blue}
// cp:
// misc : [string] Now is the time
// struct :
// bool : [bool] true
// float : [float64] 3.14159
// int : [float64] 4
// str : [string] now's the time
// struct_ptr :
// bool : [bool] true
// float : [float64] 3.14159
// int : [float64] 4
// str : [string] now's the time
//
/*
// Hand-crafted Map values that include structures do NOT Copy() as expected,
// since to simulate a deep copy the original Map value is JSON encoded then decoded.
type str struct {
IntVal int `json:"int"`
StrVal string `json:"str"`
FloatVal float64 `json:"float"`
BoolVal bool `json:"bool"`
private string
}
s := str{IntVal: 4, StrVal: "now's the time", FloatVal: 3.14159, BoolVal: true, private: "Skies are blue"}
m := make(map[string]interface{}, 0)
m["struct"] = interface{}(s)
m["struct_ptr"] = interface{}(&s)
m["misc"] = interface{}(`Now is the time`)
mv := mxj.Map(m)
cp, _ := mv.Copy()
fmt.Printf("mv:\n%s\n", mv.StringIndent(2))
fmt.Printf("cp:\n%s\n", cp.StringIndent(2))
// NoFail output:
// mv:
// misc : [string] Now is the time
// struct : [mxj_test.str] {IntVal:4 StrVal:now's the time FloatVal:3.14159 BoolVal:true private:Skies are blue}
// struct_ptr : [*mxj_test.str] &{IntVal:4 StrVal:now's the time FloatVal:3.14159 BoolVal:true private:Skies are blue}
// cp:
// misc : [string] Now is the time
// struct :
// bool : [bool] true
// float : [float64] 3.14159
// int : [float64] 4
// str : [string] now's the time
// struct_ptr :
// bool : [bool] true
// float : [float64] 3.14159
// int : [float64] 4
// str : [string] now's the time
//
*/
}
2 changes: 1 addition & 1 deletion exists.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mxj

// Checks whether the path exists
func (mv Map) Exists(path string, subkeys... string) bool {
func (mv Map) Exists(path string, subkeys ...string) bool {
v, err := mv.ValuesForPath(path, subkeys...)
return err == nil && len(v) > 0
}
25 changes: 21 additions & 4 deletions exists_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package mxj

import (
"fmt"
"testing"
)

func TestExists(t *testing.T) {
fmt.Println("------------ exists_test.go")
m := map[string]interface{}{
"Div": map[string]interface{}{
"Colour": "blue",
Expand All @@ -21,17 +23,32 @@ func TestExists(t *testing.T) {
}
}

/*
var existsDoc = []byte(`
<doc>
<books>
<book seq="1">
<author>William T. Gaddis</author>
<title>The Recognitions</title>
<review>One of the great seminal American novels of the 20th century.</review>
</book>
</books>
<book>Something else.</book>
</doc>
`)
func TestExistsWithSubKeys(t *testing.T) {
mv, err := NewMapXml(doc2)
mv, err := NewMapXml(existsDoc)
if err != nil {
t.Fatal("err:", err.Error())
}
if !mv.Exists("doc.books.book", "-seq:1") {
t.Fatal("Haven't found an existing element")
t.Fatal("Did't find an existing element")
}
if mv.Exists("doc.books.book", "-seq:2") {
t.Fatal("Have found a non existing element")
t.Fatal("Found a non-existing element")
}
}
}
*/

0 comments on commit 79cfe7d

Please sign in to comment.