-
Notifications
You must be signed in to change notification settings - Fork 112
/
bulkraw_test.go
113 lines (92 loc) · 2.81 KB
/
bulkraw_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// bulk_test.go - uses Handler and Writer functions to process some streams as a demo.
package mxj
import (
"bytes"
"fmt"
"testing"
)
func TestBulkRawHeader(t *testing.T) {
fmt.Println("\n---------------- bulkraw_test.go ...")
}
// use data from bulk_test.go
var jsonWriterRaw = new(bytes.Buffer)
var xmlWriterRaw = new(bytes.Buffer)
var jsonErrLogRaw = new(bytes.Buffer)
var xmlErrLogRaw = new(bytes.Buffer)
func TestXmlReaderRaw(t *testing.T) {
// create Reader for xmldata
xmlReader := bytes.NewReader(xmldata)
// read XML from Reader and pass Map value with the raw XML to handler
err := HandleXmlReaderRaw(xmlReader, bxmaphandlerRaw, bxerrhandlerRaw)
if err != nil {
t.Fatal("err:", err.Error())
}
// get the JSON
j := make([]byte, jsonWriterRaw.Len())
_, _ = jsonWriterRaw.Read(j)
// get the errors
e := make([]byte, xmlErrLogRaw.Len())
_, _ = xmlErrLogRaw.Read(e)
// print the input
fmt.Println("XmlReaderRaw, xmldata:\n", string(xmldata))
// print the result
fmt.Println("XmlReaderRaw, result :\n", string(j))
// print the errors
fmt.Println("XmlReaderRaw, errors :\n", string(e))
}
func bxmaphandlerRaw(m Map, raw []byte) bool {
j, err := m.JsonIndent("", " ", true)
if err != nil {
return false
}
_, _ = jsonWriterRaw.Write(j)
// put in a NL to pretty up printing the Writer
_, _ = jsonWriterRaw.Write([]byte("\n"))
return true
}
func bxerrhandlerRaw(err error, raw []byte) bool {
// write errors to file
_, _ = xmlErrLogRaw.Write([]byte(err.Error()))
_, _ = xmlErrLogRaw.Write([]byte("\n")) // pretty up
_, _ = xmlErrLogRaw.Write(raw)
_, _ = xmlErrLogRaw.Write([]byte("\n")) // pretty up
return true
}
func TestJsonReaderRaw(t *testing.T) {
jsonReader := bytes.NewReader(jsondata)
// read all the JSON
err := HandleJsonReaderRaw(jsonReader, bjmaphandlerRaw, bjerrhandlerRaw)
if err != nil {
t.Fatal("err:", err.Error())
}
// get the XML
x := make([]byte, xmlWriterRaw.Len())
_, _ = xmlWriterRaw.Read(x)
// get the errors
e := make([]byte, jsonErrLogRaw.Len())
_, _ = jsonErrLogRaw.Read(e)
// print the input
fmt.Println("JsonReaderRaw, jsondata:\n", string(jsondata))
// print the result
fmt.Println("JsonReaderRaw, result :\n", string(x))
// print the errors
fmt.Println("JsonReaderRaw, errors :\n", string(e))
}
func bjmaphandlerRaw(m Map, raw []byte) bool {
x, err := m.XmlIndent(" ", " ")
if err != nil {
return false
}
_, _ = xmlWriterRaw.Write(x)
// put in a NL to pretty up printing the Writer
_, _ = xmlWriterRaw.Write([]byte("\n"))
return true
}
func bjerrhandlerRaw(err error, raw []byte) bool {
// write errors to file
_, _ = jsonErrLogRaw.Write([]byte(err.Error()))
_, _ = jsonErrLogRaw.Write([]byte("\n")) // pretty up, Error() from json.Unmarshal !NL
_, _ = jsonErrLogRaw.Write(raw)
_, _ = jsonErrLogRaw.Write([]byte("\n")) // pretty up
return true
}