forked from johannesboyne/gofakes3
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go1.17 backward compatibility support
- Loading branch information
Showing
20 changed files
with
9,593 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2011 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package xml | ||
|
||
import "time" | ||
|
||
var atomValue = &Feed{ | ||
XMLName: Name{"http://www.w3.org/2005/Atom", "feed"}, | ||
Title: "Example Feed", | ||
Link: []Link{{Href: "http://example.org/"}}, | ||
Updated: ParseTime("2003-12-13T18:30:02Z"), | ||
Author: Person{Name: "John Doe"}, | ||
ID: "urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6", | ||
|
||
Entry: []Entry{ | ||
{ | ||
Title: "Atom-Powered Robots Run Amok", | ||
Link: []Link{{Href: "http://example.org/2003/12/13/atom03"}}, | ||
ID: "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a", | ||
Updated: ParseTime("2003-12-13T18:30:02Z"), | ||
Summary: NewText("Some text."), | ||
}, | ||
}, | ||
} | ||
|
||
var atomXML = `` + | ||
`<feed xmlns="http://www.w3.org/2005/Atom" updated="2003-12-13T18:30:02Z">` + | ||
`<title>Example Feed</title>` + | ||
`<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>` + | ||
`<link href="http://example.org/"></link>` + | ||
`<author><name>John Doe</name><uri></uri><email></email></author>` + | ||
`<entry>` + | ||
`<title>Atom-Powered Robots Run Amok</title>` + | ||
`<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>` + | ||
`<link href="http://example.org/2003/12/13/atom03"></link>` + | ||
`<updated>2003-12-13T18:30:02Z</updated>` + | ||
`<author><name></name><uri></uri><email></email></author>` + | ||
`<summary>Some text.</summary>` + | ||
`</entry>` + | ||
`</feed>` | ||
|
||
func ParseTime(str string) time.Time { | ||
t, err := time.Parse(time.RFC3339, str) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return t | ||
} | ||
|
||
func NewText(text string) Text { | ||
return Text{ | ||
Body: text, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2018 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package xml_test | ||
|
||
import ( | ||
"encoding/xml" | ||
"fmt" | ||
"log" | ||
"strings" | ||
) | ||
|
||
type Animal int | ||
|
||
const ( | ||
Unknown Animal = iota | ||
Gopher | ||
Zebra | ||
) | ||
|
||
func (a *Animal) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { | ||
var s string | ||
if err := d.DecodeElement(&s, &start); err != nil { | ||
return err | ||
} | ||
switch strings.ToLower(s) { | ||
default: | ||
*a = Unknown | ||
case "gopher": | ||
*a = Gopher | ||
case "zebra": | ||
*a = Zebra | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a Animal) MarshalXML(e *xml.Encoder, start xml.StartElement) error { | ||
var s string | ||
switch a { | ||
default: | ||
s = "unknown" | ||
case Gopher: | ||
s = "gopher" | ||
case Zebra: | ||
s = "zebra" | ||
} | ||
return e.EncodeElement(s, start) | ||
} | ||
|
||
func Example_customMarshalXML() { | ||
blob := ` | ||
<animals> | ||
<animal>gopher</animal> | ||
<animal>armadillo</animal> | ||
<animal>zebra</animal> | ||
<animal>unknown</animal> | ||
<animal>gopher</animal> | ||
<animal>bee</animal> | ||
<animal>gopher</animal> | ||
<animal>zebra</animal> | ||
</animals>` | ||
var zoo struct { | ||
Animals []Animal `xml:"animal"` | ||
} | ||
if err := xml.Unmarshal([]byte(blob), &zoo); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
census := make(map[Animal]int) | ||
for _, animal := range zoo.Animals { | ||
census[animal] += 1 | ||
} | ||
|
||
fmt.Printf("Zoo Census:\n* Gophers: %d\n* Zebras: %d\n* Unknown: %d\n", | ||
census[Gopher], census[Zebra], census[Unknown]) | ||
|
||
// Output: | ||
// Zoo Census: | ||
// * Gophers: 3 | ||
// * Zebras: 2 | ||
// * Unknown: 3 | ||
} |
Oops, something went wrong.