Skip to content

Commit

Permalink
Added experimental (and for now, default) ordering that is in the ord…
Browse files Browse the repository at this point in the history
…er the Xml element is encountered in the input XML
  • Loading branch information
gnewton committed Jul 24, 2016
1 parent c920147 commit 23a77cd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
20 changes: 20 additions & 0 deletions chidley.go
Expand Up @@ -394,7 +394,27 @@ func printChildrenChildren(node *Node) {
}
}

// Order Xml is encountered
func printStructs(v *PrintGoStructVisitor) {
orderNodes := make(map[int]*Node)
var order []int

for k := range v.alreadyVisitedNodes {
nodeOrder := v.alreadyVisitedNodes[k].discoveredOrder
orderNodes[nodeOrder] = v.alreadyVisitedNodes[k]
order = append(order, nodeOrder)
}
sort.Ints(order)
fmt.Println(order)

for o := range order {
fmt.Println(o)
print(v, orderNodes[o])
}
}

// Alphabetical order
func printStructs2(v *PrintGoStructVisitor) {
var keys []string
for k := range v.alreadyVisitedNodes {
keys = append(keys, k)
Expand Down
6 changes: 5 additions & 1 deletion extractor.go
Expand Up @@ -13,6 +13,8 @@ var nameMapper = map[string]string{
".": "_dot_",
}

var DiscoveredOrder = 0

type Extractor struct {
globalTagAttributes map[string]([]*FQN)
globalTagAttributesMap map[string]bool
Expand Down Expand Up @@ -50,7 +52,7 @@ func (ex *Extractor) extract() error {
token, err := decoder.Token()
if err != nil {
if err.Error() == "EOF" {
// OK
// OK
break
}
log.Println(err)
Expand Down Expand Up @@ -195,6 +197,8 @@ func (ex *Extractor) handleStartElement(startElement xml.StartElement, thisNode
child, ok = ex.globalNodeMap[key]
if !ok {
child = new(Node)
DiscoveredOrder += 1
child.discoveredOrder = DiscoveredOrder
ex.globalNodeMap[key] = child
spaceTag, _ := ex.nameSpaceTagMap[space]
child.initialize(name, space, spaceTag, thisNode)
Expand Down
23 changes: 12 additions & 11 deletions node.go
Expand Up @@ -5,17 +5,18 @@ import (
)

type Node struct {
name string
space string
spaceTag string
parent *Node
parents []*Node
children map[string]*Node
childCount map[string]int
repeats bool
nodeTypeInfo *NodeTypeInfo
hasCharData bool
tempCharData string
name string
space string
spaceTag string
parent *Node
parents []*Node
children map[string]*Node
childCount map[string]int
repeats bool
nodeTypeInfo *NodeTypeInfo
hasCharData bool
tempCharData string
discoveredOrder int
}

type NodeVisitor interface {
Expand Down
2 changes: 2 additions & 0 deletions printGoStructVisitor.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"sort"
"strconv"
)

type PrintGoStructVisitor struct {
Expand Down Expand Up @@ -47,6 +48,7 @@ func (v *PrintGoStructVisitor) Visit(node *Node) bool {

func print(v *PrintGoStructVisitor, node *Node) {
attributes := v.globalTagAttributes[nk(node)]
v.lineChannel <- "// discoveredOrder: " + strconv.Itoa(node.discoveredOrder)
v.lineChannel <- "type " + node.makeType(namePrefix, nameSuffix) + " struct {"
makeAttributes(v.lineChannel, attributes, v.nameSpaceTagMap)
v.printInternalFields(node)
Expand Down

0 comments on commit 23a77cd

Please sign in to comment.