Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve anonymous type handling #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions gentests/_testgen/testgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ func genXSDTests(cfg xsdgen.Config, data []byte, pkg string) (code, tests *ast.F
xmltree.MarshalIndent(inputTree, "", " "))
}
`, params).Decl()

if err != nil {
return nil, nil, err
}
Expand All @@ -186,10 +185,10 @@ type Element struct {
Name, Type xml.Name
}

func topLevelElements(root *xmltree.Element) []Element {
func topLevelElements(root *xmltree.Element) []*Element {
const schemaNS = "http://www.w3.org/2001/XMLSchema"

result := make([]Element, 0)
result := make([]*Element, 0)
root = &xmltree.Element{Scope: root.Scope, Children: []xmltree.Element{*root}}
for _, schema := range root.Search(schemaNS, "schema") {
tns := schema.Attr("", "targetNamespace")
Expand Down
4 changes: 2 additions & 2 deletions xmltree/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Equal(a, b *Element) bool {
return equal(a, b, 0)
}

type byName []Element
type byName []*Element

func (l byName) Len() int { return len(l) }
func (l byName) Less(i, j int) bool {
Expand All @@ -37,7 +37,7 @@ func equal(a, b *Element, depth int) bool {
sort.Sort(byName(a.Children))
sort.Sort(byName(b.Children))
for i := range a.Children {
if !equal(&a.Children[i], &b.Children[i], depth+1) {
if !equal(a.Children[i], b.Children[i], depth+1) {
return false
}
}
Expand Down
10 changes: 5 additions & 5 deletions xmltree/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func ExampleElement_SearchFunc() {
}

func ExampleUnmarshal() {
var input = []byte(`<mediawiki xml:lang="en">
input := []byte(`<mediawiki xml:lang="en">
<page>
<title>Page title</title>
<restrictions>edit=sysop:move=sysop</restrictions>
Expand Down Expand Up @@ -178,7 +178,7 @@ func ExampleUnmarshal() {
}

func ExampleMarshal() {
var input = []byte(`<?xml version="1.0" encoding="UTF-8"?>
input := []byte(`<?xml version="1.0" encoding="UTF-8"?>
<toc>
<chapter-list>
<chapter>
Expand All @@ -200,7 +200,7 @@ func ExampleMarshal() {
</chapter-list>
</toc>`)

var chapters []xmltree.Element
var chapters []*xmltree.Element
root, err := xmltree.Parse(input)
if err != nil {
log.Fatal(err)
Expand All @@ -211,7 +211,7 @@ func ExampleMarshal() {
el.Content = child.Content
}
el.Children = nil
chapters = append(chapters, *el)
chapters = append(chapters, el)
}
root.Children = chapters
fmt.Printf("%s\n", xmltree.MarshalIndent(root, "", " "))
Expand All @@ -226,7 +226,7 @@ func ExampleMarshal() {
}

func ExampleMarshalNested() {
var input = []byte(`<?xml version="1.0" encoding="UTF-8"?>
input := []byte(`<?xml version="1.0" encoding="UTF-8"?>
<toc>
<level1>
<level2>
Expand Down
4 changes: 2 additions & 2 deletions xmltree/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (e *encoder) encode(el, parent *Element, visited map[*Element]struct{}) err
}
for i := range el.Children {
visited[el] = struct{}{}
if err := e.encode(&el.Children[i], el, visited); err != nil {
if err := e.encode(el.Children[i], el, visited); err != nil {
return err
}
delete(visited, el)
Expand Down Expand Up @@ -139,7 +139,7 @@ func (e *encoder) encodeOpenTag(el *Element, scope Scope, depth int) error {
io.WriteString(e.w, e.indent)
}
}
var tag = struct {
tag := struct {
*Element
NS []xml.Name
}{Element: el, NS: scope.ns}
Expand Down
6 changes: 3 additions & 3 deletions xmltree/xmltree.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Element struct {
// end tags. Uses the underlying byte array passed to Parse.
Content []byte
// Sub-elements contained within this element.
Children []Element
Children []*Element
}

// Attr gets the value of the first attribute whose name matches the
Expand Down Expand Up @@ -262,7 +262,7 @@ walk:
for scanner.scan() {
switch tok := scanner.tok.(type) {
case xml.StartElement:
child := Element{StartElement: tok.Copy(), Scope: el.Scope}
child := &Element{StartElement: tok.Copy(), Scope: el.Scope}
if err := child.parse(scanner, data, depth+1); err != nil {
return err
}
Expand All @@ -284,7 +284,7 @@ walk:
// immediately.
func (el *Element) walk(fn walkFunc) error {
for i := 0; i < len(el.Children); i++ {
fn(&el.Children[i])
fn(el.Children[i])
}
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions xmltree/xmltree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ func TestModification(t *testing.T) {
root := parseDoc(t, from)
// Remove any non-<li> children from all <ul> elements
// in the document.
valid := make([]Element, 0, len(root.Children))
valid := make([]*Element, 0, len(root.Children))
for _, p := range root.Search("", "li") {
t.Logf("%#v", *p)
valid = append(valid, *p)
valid = append(valid, p)
}
root.Children = valid
if s := root.String(); s != to {
Expand All @@ -249,7 +249,7 @@ func TestModification(t *testing.T) {
func TestStringPreserveNS(t *testing.T) {
root := parseDoc(t, exampleDoc)
var doc []byte
var descent = 4
descent := 4
for _, el := range root.Flatten() {
descent--
if descent <= 0 {
Expand Down
Loading