Skip to content

Commit e281504

Browse files
fork: modified encoding/xml fork
Tests still need to be fixed and this code needs to be updated to the latest "version" of encoding/xml but it is functional.
1 parent 2d31637 commit e281504

File tree

11 files changed

+9027
-0
lines changed

11 files changed

+9027
-0
lines changed

atom_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2011 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package xml
6+
7+
import "time"
8+
9+
var atomValue = &Feed{
10+
XMLName: Name{"http://www.w3.org/2005/Atom", "feed"},
11+
Title: "Example Feed",
12+
Link: []Link{{Href: "http://example.org/"}},
13+
Updated: ParseTime("2003-12-13T18:30:02Z"),
14+
Author: Person{Name: "John Doe"},
15+
ID: "urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6",
16+
17+
Entry: []Entry{
18+
{
19+
Title: "Atom-Powered Robots Run Amok",
20+
Link: []Link{{Href: "http://example.org/2003/12/13/atom03"}},
21+
ID: "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a",
22+
Updated: ParseTime("2003-12-13T18:30:02Z"),
23+
Summary: NewText("Some text."),
24+
},
25+
},
26+
}
27+
28+
var atomXML = `` +
29+
`<feed xmlns="http://www.w3.org/2005/Atom" updated="2003-12-13T18:30:02Z">` +
30+
`<title>Example Feed</title>` +
31+
`<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>` +
32+
`<link href="http://example.org/"></link>` +
33+
`<author><name>John Doe</name><uri></uri><email></email></author>` +
34+
`<entry>` +
35+
`<title>Atom-Powered Robots Run Amok</title>` +
36+
`<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>` +
37+
`<link href="http://example.org/2003/12/13/atom03"></link>` +
38+
`<updated>2003-12-13T18:30:02Z</updated>` +
39+
`<author><name></name><uri></uri><email></email></author>` +
40+
`<summary>Some text.</summary>` +
41+
`</entry>` +
42+
`</feed>`
43+
44+
func ParseTime(str string) time.Time {
45+
t, err := time.Parse(time.RFC3339, str)
46+
if err != nil {
47+
panic(err)
48+
}
49+
return t
50+
}
51+
52+
func NewText(text string) Text {
53+
return Text{
54+
Body: text,
55+
}
56+
}

example_marshaling_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package xml_test
6+
7+
import (
8+
"github.com/mattrax/Mattrax/pkg/xml"
9+
"fmt"
10+
"log"
11+
"strings"
12+
)
13+
14+
type Animal int
15+
16+
const (
17+
Unknown Animal = iota
18+
Gopher
19+
Zebra
20+
)
21+
22+
func (a *Animal) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
23+
var s string
24+
if err := d.DecodeElement(&s, &start); err != nil {
25+
return err
26+
}
27+
switch strings.ToLower(s) {
28+
default:
29+
*a = Unknown
30+
case "gopher":
31+
*a = Gopher
32+
case "zebra":
33+
*a = Zebra
34+
}
35+
36+
return nil
37+
}
38+
39+
func (a Animal) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
40+
var s string
41+
switch a {
42+
default:
43+
s = "unknown"
44+
case Gopher:
45+
s = "gopher"
46+
case Zebra:
47+
s = "zebra"
48+
}
49+
return e.EncodeElement(s, start)
50+
}
51+
52+
func Example_customMarshalXML() {
53+
blob := `
54+
<animals>
55+
<animal>gopher</animal>
56+
<animal>armadillo</animal>
57+
<animal>zebra</animal>
58+
<animal>unknown</animal>
59+
<animal>gopher</animal>
60+
<animal>bee</animal>
61+
<animal>gopher</animal>
62+
<animal>zebra</animal>
63+
</animals>`
64+
var zoo struct {
65+
Animals []Animal `xml:"animal"`
66+
}
67+
if err := xml.Unmarshal([]byte(blob), &zoo); err != nil {
68+
log.Fatal(err)
69+
}
70+
71+
census := make(map[Animal]int)
72+
for _, animal := range zoo.Animals {
73+
census[animal] += 1
74+
}
75+
76+
fmt.Printf("Zoo Census:\n* Gophers: %d\n* Zebras: %d\n* Unknown: %d\n",
77+
census[Gopher], census[Zebra], census[Unknown])
78+
79+
// Output:
80+
// Zoo Census:
81+
// * Gophers: 3
82+
// * Zebras: 2
83+
// * Unknown: 3
84+
}

example_test.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Copyright 2012 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package xml_test
6+
7+
import (
8+
"github.com/mattrax/Mattrax/pkg/xml"
9+
"fmt"
10+
"os"
11+
)
12+
13+
func ExampleMarshalIndent() {
14+
type Address struct {
15+
City, State string
16+
}
17+
type Person struct {
18+
XMLName xml.Name `xml:"person"`
19+
Id int `xml:"id,attr"`
20+
FirstName string `xml:"name>first"`
21+
LastName string `xml:"name>last"`
22+
Age int `xml:"age"`
23+
Height float32 `xml:"height,omitempty"`
24+
Married bool
25+
Address
26+
Comment string `xml:",comment"`
27+
}
28+
29+
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
30+
v.Comment = " Need more details. "
31+
v.Address = Address{"Hanga Roa", "Easter Island"}
32+
33+
output, err := xml.MarshalIndent(v, " ", " ")
34+
if err != nil {
35+
fmt.Printf("error: %v\n", err)
36+
}
37+
38+
os.Stdout.Write(output)
39+
// Output:
40+
// <person id="13">
41+
// <name>
42+
// <first>John</first>
43+
// <last>Doe</last>
44+
// </name>
45+
// <age>42</age>
46+
// <Married>false</Married>
47+
// <City>Hanga Roa</City>
48+
// <State>Easter Island</State>
49+
// <!-- Need more details. -->
50+
// </person>
51+
}
52+
53+
func ExampleEncoder() {
54+
type Address struct {
55+
City, State string
56+
}
57+
type Person struct {
58+
XMLName xml.Name `xml:"person"`
59+
Id int `xml:"id,attr"`
60+
FirstName string `xml:"name>first"`
61+
LastName string `xml:"name>last"`
62+
Age int `xml:"age"`
63+
Height float32 `xml:"height,omitempty"`
64+
Married bool
65+
Address
66+
Comment string `xml:",comment"`
67+
}
68+
69+
v := &Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
70+
v.Comment = " Need more details. "
71+
v.Address = Address{"Hanga Roa", "Easter Island"}
72+
73+
enc := xml.NewEncoder(os.Stdout)
74+
enc.Indent(" ", " ")
75+
if err := enc.Encode(v); err != nil {
76+
fmt.Printf("error: %v\n", err)
77+
}
78+
79+
// Output:
80+
// <person id="13">
81+
// <name>
82+
// <first>John</first>
83+
// <last>Doe</last>
84+
// </name>
85+
// <age>42</age>
86+
// <Married>false</Married>
87+
// <City>Hanga Roa</City>
88+
// <State>Easter Island</State>
89+
// <!-- Need more details. -->
90+
// </person>
91+
}
92+
93+
// This example demonstrates unmarshaling an XML excerpt into a value with
94+
// some preset fields. Note that the Phone field isn't modified and that
95+
// the XML <Company> element is ignored. Also, the Groups field is assigned
96+
// considering the element path provided in its tag.
97+
func ExampleUnmarshal() {
98+
type Email struct {
99+
Where string `xml:"where,attr"`
100+
Addr string
101+
}
102+
type Address struct {
103+
City, State string
104+
}
105+
type Result struct {
106+
XMLName xml.Name `xml:"Person"`
107+
Name string `xml:"FullName"`
108+
Phone string
109+
Email []Email
110+
Groups []string `xml:"Group>Value"`
111+
Address
112+
}
113+
v := Result{Name: "none", Phone: "none"}
114+
115+
data := `
116+
<Person>
117+
<FullName>Grace R. Emlin</FullName>
118+
<Company>Example Inc.</Company>
119+
<Email where="home">
120+
<Addr>[email protected]</Addr>
121+
</Email>
122+
<Email where='work'>
123+
<Addr>[email protected]</Addr>
124+
</Email>
125+
<Group>
126+
<Value>Friends</Value>
127+
<Value>Squash</Value>
128+
</Group>
129+
<City>Hanga Roa</City>
130+
<State>Easter Island</State>
131+
</Person>
132+
`
133+
err := xml.Unmarshal([]byte(data), &v)
134+
if err != nil {
135+
fmt.Printf("error: %v", err)
136+
return
137+
}
138+
fmt.Printf("XMLName: %#v\n", v.XMLName)
139+
fmt.Printf("Name: %q\n", v.Name)
140+
fmt.Printf("Phone: %q\n", v.Phone)
141+
fmt.Printf("Email: %v\n", v.Email)
142+
fmt.Printf("Groups: %v\n", v.Groups)
143+
fmt.Printf("Address: %v\n", v.Address)
144+
// Output:
145+
// XMLName: xml.Name{Space:"", Local:"Person"}
146+
// Name: "Grace R. Emlin"
147+
// Phone: "none"
148+
// Email: [{home [email protected]} {work [email protected]}]
149+
// Groups: [Friends Squash]
150+
// Address: {Hanga Roa Easter Island}
151+
}

example_text_marshaling_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package xml_test
6+
7+
import (
8+
"github.com/mattrax/Mattrax/pkg/xml"
9+
"fmt"
10+
"log"
11+
"strings"
12+
)
13+
14+
type Size int
15+
16+
const (
17+
Unrecognized Size = iota
18+
Small
19+
Large
20+
)
21+
22+
func (s *Size) UnmarshalText(text []byte) error {
23+
switch strings.ToLower(string(text)) {
24+
default:
25+
*s = Unrecognized
26+
case "small":
27+
*s = Small
28+
case "large":
29+
*s = Large
30+
}
31+
return nil
32+
}
33+
34+
func (s Size) MarshalText() ([]byte, error) {
35+
var name string
36+
switch s {
37+
default:
38+
name = "unrecognized"
39+
case Small:
40+
name = "small"
41+
case Large:
42+
name = "large"
43+
}
44+
return []byte(name), nil
45+
}
46+
47+
func Example_textMarshalXML() {
48+
blob := `
49+
<sizes>
50+
<size>small</size>
51+
<size>regular</size>
52+
<size>large</size>
53+
<size>unrecognized</size>
54+
<size>small</size>
55+
<size>normal</size>
56+
<size>small</size>
57+
<size>large</size>
58+
</sizes>`
59+
var inventory struct {
60+
Sizes []Size `xml:"size"`
61+
}
62+
if err := xml.Unmarshal([]byte(blob), &inventory); err != nil {
63+
log.Fatal(err)
64+
}
65+
66+
counts := make(map[Size]int)
67+
for _, size := range inventory.Sizes {
68+
counts[size] += 1
69+
}
70+
71+
fmt.Printf("Inventory Counts:\n* Small: %d\n* Large: %d\n* Unrecognized: %d\n",
72+
counts[Small], counts[Large], counts[Unrecognized])
73+
74+
// Output:
75+
// Inventory Counts:
76+
// * Small: 3
77+
// * Large: 2
78+
// * Unrecognized: 3
79+
}

0 commit comments

Comments
 (0)