1package mxj
2
3import (
4	"bytes"
5	"fmt"
6	"io"
7	"testing"
8)
9
10func TestNewMapHeader(t *testing.T) {
11	fmt.Println("\n----------------  newmap_test.go ...")
12}
13
14func TestNewMap(t *testing.T) {
15	j := []byte(`{ "A":"this", "B":"is", "C":"a", "D":"test" }`)
16	fmt.Println("j:", string(j))
17
18	m, _ := NewMapJson(j)
19	fmt.Printf("m: %#v\n", m)
20
21	fmt.Println("\n", `eval - m.NewMap("A:AA", "B:BB", "C:cc", "D:help")`)
22	n, err := m.NewMap("A:AA", "B:BB", "C:cc", "D:help")
23	if err != nil {
24		fmt.Println("err:", err.Error())
25	}
26	j, _ = n.Json()
27	fmt.Println("n.Json():", string(j))
28	x, _ := n.Xml()
29	fmt.Println("n.Xml():\n", string(x))
30	x, _ = n.XmlIndent("", "  ")
31	fmt.Println("n.XmlIndent():\n", string(x))
32
33	fmt.Println("\n", `eval - m.NewMap("A:AA.A", "B:AA.B", "C:AA.B.cc", "D:hello.help")`)
34	n, err = m.NewMap("A:AA.A", "B:AA.B", "C:AA.B.cc", "D:hello.help")
35	if err != nil {
36		fmt.Println("err:", err.Error())
37	}
38	j, _ = n.Json()
39	fmt.Println("n.Json():", string(j))
40	x, _ = n.Xml()
41	fmt.Println("n.Xml():\n", string(x))
42	x, _ = n.XmlIndent("", "  ")
43	fmt.Println("n.XmlIndent():\n", string(x))
44
45	var keypairs = []string{"A:xml.AA", "B:xml.AA.hello.again", "C:xml.AA", "D:xml.AA.hello.help"}
46	fmt.Println("\n", `eval - m.NewMap keypairs:`, keypairs)
47	n, err = m.NewMap(keypairs...)
48	if err != nil {
49		fmt.Println("err:", err.Error())
50	}
51	j, _ = n.Json()
52	fmt.Println("n.Json():", string(j))
53	x, _ = n.Xml()
54	fmt.Println("n.Xml():\n", string(x))
55	x, _ = n.XmlIndent("", "  ")
56	fmt.Println("n.XmlIndent():\n", string(x))
57}
58
59// Need to normalize from an XML stream the values for "netid" and "idnet".
60// Solution: make everything "netid"
61// Demo how to re-label a key using mv.NewMap()
62
63var msg1 = []byte(`
64<?xml version="1.0" encoding="UTF-8"?>
65<data>
66    <netid>
67        <disable>no</disable>
68        <text1>default:text</text1>
69        <word1>default:word</word1>
70    </netid>
71</data>
72`)
73
74var msg2 = []byte(`
75<?xml version="1.0" encoding="UTF-8"?>
76<data>
77    <idnet>
78        <disable>yes</disable>
79        <text1>default:text</text1>
80        <word1>default:word</word1>
81    </idnet>
82</data>
83`)
84
85func TestNetId(t *testing.T) {
86	// let's create a message stream
87	buf := new(bytes.Buffer)
88	// load a couple of messages into it
89	_, _ = buf.Write(msg1)
90	_, _ = buf.Write(msg2)
91
92	n := 0
93	for {
94		n++
95		// read the stream as Map values - quit on io.EOF
96		m, raw, merr := NewMapXmlReaderRaw(buf)
97		if merr != nil && merr != io.EOF {
98			// handle error - for demo we just print it and continue
99			fmt.Printf("msg: %d - merr: %s\n", n, merr.Error())
100			continue
101		} else if merr == io.EOF {
102			break
103		}
104
105		// the first keypair retains values if data correct
106		// the second keypair relabels "idnet" to "netid"
107		n, _ := m.NewMap("data.netid", "data.idnet:data.netid")
108		x, _ := n.XmlIndent("", "  ")
109
110		fmt.Println("original value:", string(raw))
111		fmt.Println("new value:")
112		fmt.Println(string(x))
113	}
114}
115