Skip to content

Commit

Permalink
gofmt -w
Browse files Browse the repository at this point in the history
  • Loading branch information
andeya committed Jan 6, 2017
1 parent 7f969ea commit b9b2f5c
Show file tree
Hide file tree
Showing 10 changed files with 839 additions and 848 deletions.
21 changes: 10 additions & 11 deletions config/xml/x2j/goofy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestGoofy(t *testing.T) {
var doc = `<xml><tag one="1" pi="3.1415962535" bool="true"/><tagJR key="value"/></xml>`
type goofy struct {
S string
S string
Sp *string
}
g := new(goofy)
Expand All @@ -19,21 +19,21 @@ func TestGoofy(t *testing.T) {

m, err := DocToMap(doc)
if err != nil {
fmt.Println("err:",err.Error())
fmt.Println("err:", err.Error())
return
}

m["goofyVal"] = interface{}(g)
m["byteVal"] = interface{}([]byte(`the aid of their country`))
m["nilVal"] = interface{}(nil)

fmt.Println("\nTestGoofy ... MapToDoc:",m)
fmt.Println("\nTestGoofy ... MapToDoc:", m)
var v []byte
v,err = json.Marshal(m)
v, err = json.Marshal(m)
if err != nil {
fmt.Println("err:",err.Error())
fmt.Println("err:", err.Error())
}
fmt.Println("v:",string(v))
fmt.Println("v:", string(v))

type goofier struct {
G *goofy
Expand All @@ -46,11 +46,10 @@ func TestGoofy(t *testing.T) {
gg.N = nil
m["goofierVal"] = interface{}(gg)

fmt.Println("\nTestGoofier ... MapToDoc:",m)
v,err = json.Marshal(m)
fmt.Println("\nTestGoofier ... MapToDoc:", m)
v, err = json.Marshal(m)
if err != nil {
fmt.Println("err:",err.Error())
fmt.Println("err:", err.Error())
}
fmt.Println("v:",string(v))
fmt.Println("v:", string(v))
}

3 changes: 0 additions & 3 deletions config/xml/x2j/reader2j.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func ToJsonIndent(rdr io.Reader, recast ...bool) (string, error) {
return string(b), nil
}


// ReaderValuesFromTagPath - io.Reader version of ValuesFromTagPath()
func ReaderValuesFromTagPath(rdr io.Reader, path string, getAttrs ...bool) ([]interface{}, error) {
var a bool
Expand All @@ -101,5 +100,3 @@ func ReaderValuesForTag(rdr io.Reader, tag string) ([]interface{}, error) {

return ValuesForKey(m, tag), nil
}


51 changes: 25 additions & 26 deletions config/xml/x2j/reader2j_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,76 @@ import (
var doc = `<entry><vars><foo>bar</foo><foo2><hello>world</hello></foo2></vars></entry>`

func TestToTree(t *testing.T) {
fmt.Println("\nToTree - Read doc:",doc)
fmt.Println("\nToTree - Read doc:", doc)
rdr := bytes.NewBufferString(doc)
n,err := ToTree(rdr)
n, err := ToTree(rdr)
if err != nil {
fmt.Println("err:",err.Error())
fmt.Println("err:", err.Error())
}
fmt.Println(n.WriteTree())
}

func TestToMap(t *testing.T) {
fmt.Println("\nToMap - Read doc:",doc)
fmt.Println("\nToMap - Read doc:", doc)
rdr := bytes.NewBufferString(doc)
m,err := ToMap(rdr)
m, err := ToMap(rdr)
if err != nil {
fmt.Println("err:",err.Error())
fmt.Println("err:", err.Error())
}
fmt.Println(WriteMap(m))
}

func TestToJson(t *testing.T) {
fmt.Println("\nToJson - Read doc:",doc)
fmt.Println("\nToJson - Read doc:", doc)
rdr := bytes.NewBufferString(doc)
s,err := ToJson(rdr)
s, err := ToJson(rdr)
if err != nil {
fmt.Println("err:",err.Error())
fmt.Println("err:", err.Error())
}
fmt.Println("json:",s)
fmt.Println("json:", s)
}

func TestToJsonIndent(t *testing.T) {
fmt.Println("\nToJsonIndent - Read doc:",doc)
fmt.Println("\nToJsonIndent - Read doc:", doc)
rdr := bytes.NewBufferString(doc)
s,err := ToJsonIndent(rdr)
s, err := ToJsonIndent(rdr)
if err != nil {
fmt.Println("err:",err.Error())
fmt.Println("err:", err.Error())
}
fmt.Println("json:",s)
fmt.Println("json:", s)
}

func TestBulkParser(t *testing.T) {
s := doc + `<this><is>an</err>`+ doc
fmt.Println("\nBulkParser (with error) - Read doc:",s)
s := doc + `<this><is>an</err>` + doc
fmt.Println("\nBulkParser (with error) - Read doc:", s)
rdr := bytes.NewBufferString(s)
err := XmlMsgsFromReader(rdr,phandler,ehandler)
err := XmlMsgsFromReader(rdr, phandler, ehandler)
if err != nil {
fmt.Println("reader terminated:",err.Error())
fmt.Println("reader terminated:", err.Error())
}
}

func phandler(m map[string]interface{}) bool {
fmt.Println("phandler m:",m)
fmt.Println("phandler m:", m)
return true
}

func ehandler(err error) bool {
fmt.Println("ehandler err:",err.Error())
fmt.Println("ehandler err:", err.Error())
return true
}

func TestBulkParserToJson(t *testing.T) {
s := doc + `<this><is>an</err>`+ doc
fmt.Println("\nBulkParser (with error) - Read doc:",s)
s := doc + `<this><is>an</err>` + doc
fmt.Println("\nBulkParser (with error) - Read doc:", s)
rdr := bytes.NewBufferString(s)
err := XmlMsgsFromReaderAsJson(rdr,phandlerj,ehandler)
err := XmlMsgsFromReaderAsJson(rdr, phandlerj, ehandler)
if err != nil {
fmt.Println("reader terminated:",err.Error())
fmt.Println("reader terminated:", err.Error())
}
}

func phandlerj(s string) bool {
fmt.Println("phandlerj s:",s)
fmt.Println("phandlerj s:", s)
return true
}

17 changes: 8 additions & 9 deletions config/xml/x2j/x2j.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
One useful function is:
- Unmarshal(doc []byte, v interface{}) error
- Unmarshal(doc []byte, v interface{}) error
where v is a pointer to a variable of type 'map[string]interface{}', 'string', or
any other type supported by xml.Unmarshal().
To retrieve a value for specific tag use:
To retrieve a value for specific tag use:
- DocValue(doc, path string, attrs ...string) (interface{},error)
- DocValue(doc, path string, attrs ...string) (interface{},error)
- MapValue(m map[string]interface{}, path string, attr map[string]interface{}, recast ...bool) (interface{}, error)
The 'path' argument is a period-separated tag hierarchy - also known as dot-notation.
It is the program's responsibility to cast the returned value to the proper type; possible
types are the normal JSON unmarshaling types: string, float64, bool, []interface, map[string]interface{}.
It is the program's responsibility to cast the returned value to the proper type; possible
types are the normal JSON unmarshaling types: string, float64, bool, []interface, map[string]interface{}.
To retrieve all values associated with a tag occurring anywhere in the XML document use:
Expand All @@ -31,7 +31,7 @@
Returned values should be one of map[string]interface, []interface{}, or string.
All the values assocated with a tag-path that may include one or more wildcard characters -
All the values assocated with a tag-path that may include one or more wildcard characters -
'*' - can also be retrieved using:
- ValuesFromTagPath(doc, path string, getAttrs ...bool) ([]interface{}, error)
Expand All @@ -42,7 +42,7 @@
NOTE: care should be taken when using "*" at the end of a path - i.e., "books.book.*". See
the x2jpath_test.go case on how the wildcard returns all key values and collapses list values;
the same message structure can load a []interface{} or a map[string]interface{} (or an interface{})
the same message structure can load a []interface{} or a map[string]interface{} (or an interface{})
value for a tag.
See the test cases in "x2jpath_test.go" and programs in "example" subdirectory for more.
Expand Down Expand Up @@ -83,7 +83,7 @@ import (
// ...
// x2j.X2jCharsetReader = charset.NewReader
// s, err := x2j.DocToJson(doc)
var X2jCharsetReader func(charset string, input io.Reader)(io.Reader, error)
var X2jCharsetReader func(charset string, input io.Reader) (io.Reader, error)

type Node struct {
dup bool // is member of a list
Expand Down Expand Up @@ -653,4 +653,3 @@ func ByteDocToTree(doc []byte) (*Node, error) {

return n, nil
}

27 changes: 13 additions & 14 deletions config/xml/x2j/x2j_bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
// Note: phandler() and ehandler() calls are blocking, so reading and processing of messages is serialized.
// This means that you can stop reading the file on error or after processing a particular message.
// To have reading and handling run concurrently, pass arguments to a go routine in handler and return true.
func XmlMsgsFromFileAsJson(fname string, phandler func(string)(bool), ehandler func(error)(bool), recast ...bool) error {
func XmlMsgsFromFileAsJson(fname string, phandler func(string) bool, ehandler func(error) bool, recast ...bool) error {
var r bool
if len(recast) == 1 {
r = recast[0]
Expand All @@ -36,26 +36,26 @@ func XmlMsgsFromFileAsJson(fname string, phandler func(string)(bool), ehandler f
return fherr
}
defer fh.Close()
buf := make([]byte,fi.Size())
_, rerr := fh.Read(buf)
buf := make([]byte, fi.Size())
_, rerr := fh.Read(buf)
if rerr != nil {
return rerr
}
doc := string(buf)

// xml.Decoder doesn't properly handle whitespace in some doc
// see songTextString.xml test case ...
reg,_ := regexp.Compile("[ \t\n\r]*<")
doc = reg.ReplaceAllString(doc,"<")
// see songTextString.xml test case ...
reg, _ := regexp.Compile("[ \t\n\r]*<")
doc = reg.ReplaceAllString(doc, "<")
b := bytes.NewBufferString(doc)

for {
s, serr := XmlBufferToJson(b,r)
s, serr := XmlBufferToJson(b, r)
if serr != nil && serr != io.EOF {
if ok := ehandler(serr); !ok {
// caused reader termination
return serr
}
}
}
if s != "" {
if ok := phandler(s); !ok {
Expand All @@ -72,13 +72,13 @@ func XmlMsgsFromFileAsJson(fname string, phandler func(string)(bool), ehandler f
// XmlBufferToJson - process XML message from a bytes.Buffer
// 'b' is the buffer
// Optional argument 'recast' coerces values to float64 or bool where possible.
func XmlBufferToJson(b *bytes.Buffer,recast ...bool) (string,error) {
func XmlBufferToJson(b *bytes.Buffer, recast ...bool) (string, error) {
var r bool
if len(recast) == 1 {
r = recast[0]
}

n,err := XmlBufferToTree(b)
n, err := XmlBufferToTree(b)
if err != nil {
return "", err
}
Expand All @@ -99,19 +99,19 @@ func XmlBufferToJson(b *bytes.Buffer,recast ...bool) (string,error) {
// Note: phandler() and ehandler() calls are blocking, so reading and processing of messages is serialized.
// This means that you can stop reading the file on error or after processing a particular message.
// To have reading and handling run concurrently, pass arguments to a go routine in handler and return true.
func XmlMsgsFromReaderAsJson(rdr io.Reader, phandler func(string)(bool), ehandler func(error)(bool), recast ...bool) error {
func XmlMsgsFromReaderAsJson(rdr io.Reader, phandler func(string) bool, ehandler func(error) bool, recast ...bool) error {
var r bool
if len(recast) == 1 {
r = recast[0]
}

for {
s, serr := ToJson(rdr,r)
s, serr := ToJson(rdr, r)
if serr != nil && serr != io.EOF {
if ok := ehandler(serr); !ok {
// caused reader termination
return serr
}
}
}
if s != "" {
if ok := phandler(s); !ok {
Expand All @@ -124,4 +124,3 @@ func XmlMsgsFromReaderAsJson(rdr io.Reader, phandler func(string)(bool), ehandle
}
return nil
}

Loading

0 comments on commit b9b2f5c

Please sign in to comment.