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

Issue with the struct generated #20

Closed
singhaniashrinkhala opened this issue Jun 16, 2017 · 5 comments
Closed

Issue with the struct generated #20

singhaniashrinkhala opened this issue Jun 16, 2017 · 5 comments

Comments

@singhaniashrinkhala
Copy link

singhaniashrinkhala commented Jun 16, 2017

`package main

import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)

type Query struct {
rpc Rpc_reply xml:"urn:ietf:params:xml:ns:netconf:base:1.0 rpc-reply,omitempty" json:"rpc-reply,omitempty"
}
type Addr_tag_list struct {
Addr_tag Addr_tag xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr,omitempty" json:"addr,omitempty"
Pref_tag Pref_tag xml:"urn:ietf:params:xml:ns:netconf:base:1.0 pref,omitempty" json:"pref,omitempty"
Tag Tag xml:"urn:ietf:params:xml:ns:netconf:base:1.0 tag,omitempty" json:"tag,omitempty"
Type_tag Type_tag xml:"urn:ietf:params:xml:ns:netconf:base:1.0 type,omitempty" json:"type,omitempty"
VpcPeer_tag VpcPeer_tag xml:"urn:ietf:params:xml:ns:netconf:base:1.0 vpcPeer,omitempty" json:"vpcPeer,omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Addr-list,omitempty" json:"Addr-list,omitempty"
}

type Dom_list struct {
If_items If_items xml:"urn:ietf:params:xml:ns:netconf:base:1.0 if-items,omitempty" json:"if-items,omitempty"
Name_tag Name_tag xml:"urn:ietf:params:xml:ns:netconf:base:1.0 name,omitempty" json:"name,omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Dom-list,omitempty" json:"Dom-list,omitempty"
}

type If_list struct {
Addr_items_tag Addr_items_tag xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr-items,omitempty" json:"addr-items,omitempty"
Id_tag Id_tag xml:"urn:ietf:params:xml:ns:netconf:base:1.0 id,omitempty" json:"id,omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 If-list,omitempty" json:"If-list,omitempty"
}

type Systemtag struct {
Ipv4_items Ipv4_items xml:"urn:ietf:params:xml:ns:netconf:base:1.0 ipv4-items,omitempty" json:"ipv4-items,omitempty"
Test_name Test_name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 test-name,omitempty" json:"test-name,omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 System,omitempty" json:"System,omitempty"
}

type Addr_tag struct {
Text string xml:",chardata" json:",omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr,omitempty" json:"addr,omitempty"
}

type Addr_items_tag struct {
Addr_tag_list Addr_tag_list xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Addr-list,omitempty" json:"Addr-list,omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr-items,omitempty" json:"addr-items,omitempty"
}

type Data_tag struct {
Systemtag Systemtag xml:"urn:ietf:params:xml:ns:netconf:base:1.0 System,omitempty" json:"System,omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 data,omitempty" json:"data,omitempty"
}

type Dom_items struct {
Dom_list []Dom_list xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Dom-list,omitempty" json:"Dom-list,omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 dom-items,omitempty" json:"dom-items,omitempty"
}

type Id_tag struct {
Text string xml:",chardata" json:",omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 id,omitempty" json:"id,omitempty"
}

type If_items struct {
If_list []If_list xml:"urn:ietf:params:xml:ns:netconf:base:1.0 If-list,omitempty" json:"If-list,omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 if-items,omitempty" json:"if-items,omitempty"
}

type Inst_items struct {
Dom_items Dom_items xml:"urn:ietf:params:xml:ns:netconf:base:1.0 dom-items,omitempty" json:"dom-items,omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 inst-items,omitempty" json:"inst-items,omitempty"
}

type Ipv4_items struct {
Inst_items Inst_items xml:"urn:ietf:params:xml:ns:netconf:base:1.0 inst-items,omitempty" json:"inst-items,omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 ipv4-items,omitempty" json:"ipv4-items,omitempty"
}

type Name_tag struct {
Text string xml:",chardata" json:",omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 name,omitempty" json:"name,omitempty"
}

type Pref_tag struct {
Text bool xml:",chardata" json:",omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 pref,omitempty" json:"pref,omitempty"
}

type Rpc_reply struct {
Attr_message_id string xml:" message-id,attr" json:",omitempty"
Attr_xmlns string xml:" xmlns,attr" json:",omitempty"
Data_tag Data_tag xml:"urn:ietf:params:xml:ns:netconf:base:1.0 data,omitempty" json:"data,omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 rpc-reply,omitempty" json:"rpc-reply,omitempty"
}

type Tag struct {
Text bool xml:",chardata" json:",omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 tag,omitempty" json:"tag,omitempty"
}

type Type_tag struct {
Text string xml:",chardata" json:",omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 type,omitempty" json:"type,omitempty"
}

type VpcPeer_tag struct {
Text string xml:",chardata" json:",omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 vpcPeer,omitempty" json:"vpcPeer,omitempty"
}

type Test_name struct {
Text string xml:",chardata" json:",omitempty"
XMLName xml.Name xml:"urn:ietf:params:xml:ns:netconf:base:1.0 test-name,omitempty" json:"test-name,omitempty"
}

func main() {
xmlFile, err := os.Open("interface.xml")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer xmlFile.Close()

b, _ := ioutil.ReadAll(xmlFile)

var q Query
err2 := xml.Unmarshal(b, &q)
if err2 != nil {
	fmt.Printf("error: %v", err2)
	return
}
fmt.Println(q)
//#2fmt.Printf("%#v",q)
 fmt.Printf("%s",q.rpc.Attr_message_id);

}`

The structs above were generated using Chidley, I have removed the Chi prefix and the pointers.
When I try printing anything, its just a blank. Only #2 gives me the XML structure. Rest all blank.

Sample of my interface.xml file:

<?xml version="1.0" encoding="UTF-8"?> <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1"> <data> <System> <test-name>##WORK##</test-name> <ipv4-items> <inst-items> <dom-items> <Dom-list> <name>default</name> <if-items> <If-list> <id>eth5/8</id> <addr-items> <Addr-list> <addr>1.1.1.1</addr> <pref>1</pref> <tag>0</tag> <type>primary</type> <vpcPeer>0.0.0.0</vpcPeer> </Addr-list> </addr-items> </If-list> </if-items> </Dom-list> <Dom-list> <name>management</name> <if-items> <If-list> <id>eth5/8</id> <addr-items> <Addr-list> <addr>11.11.11.11</addr> <pref>1</pref> <tag>0</tag> <type>primary</type> <vpcPeer>0.0.0.0</vpcPeer> </Addr-list> </addr-items> </If-list> </if-items> </Dom-list> </dom-items> </inst-items> </ipv4-items> </System> </data> </rpc-reply>

@gnewton
Copy link
Owner

gnewton commented Jun 16, 2017

Hello,

I did not have any problem with the XML file you supplied, above.

[me@machine tmp]$ git clone https://github.com/gnewton/chidley.git
Cloning into 'chidley'...
remote: Counting objects: 449, done.        
remote: Total 449 (delta 0), reused 0 (delta 0), pack-reused 449        
Receiving objects: 100% (449/449), 58.11 MiB | 1.46 MiB/s, done.
Resolving deltas: 100% (283/283), done.
Checking connectivity... done.
[me@machine tmp]$ cd chidley/
[me@machine chidley]$ go build
[me@machine chidley]$ mkdir mmm
[me@machine chidley]$ ./chidley -W /home/gnewton/gocode/src/github.com/gnewton/chidley/foo.xml >mmm/main.go
[me@machine chidley]$ cd mmm
[me@machine mmm]$ go build
[me@machine mmm]$ ./mmm
Usage of ./mmm:
  -c	Count each instance of XML tags
  -f string
    	XML file or URL to read in (default "/home/gnewton/gocode/src/github.com/gnewton/chidley/foo.xml")
  -h	Usage
  -j	Convert to JSON
  -s	Stream XML by using XML elements one down from the root tag. Good for huge XML files (see http://blog.davidsingleton.org/parsing-huge-xml-files-with-go/
  -x	Convert to XML
[me@machine mmm]$ ./mmm -j
{
 "Attr_message_id": "1",
 "Attr_xmlns": "urn:ietf:params:xml:ns:netconf:base:1.0",
 "data": {
  "System": {
   "ipv4-items": {
    "inst-items": {
     "dom-items": {
      "Dom-list": [
       {
        "if-items": {
         "If-list": {
          "addr-items": {
           "Addr-list": {
            "addr": {
             "Text": "1.1.1.1",
             "addr": {
              "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "Local": "addr"
             }
            },
            "pref": {
             "Text": "1",
             "pref": {
              "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "Local": "pref"
             }
            },
            "tag": {
             "Text": "0",
             "tag": {
              "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "Local": "tag"
             }
            },
            "type": {
             "Text": "primary",
             "type": {
              "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "Local": "type"
             }
            },
            "vpcPeer": {
             "Text": "0.0.0.0",
             "vpcPeer": {
              "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "Local": "vpcPeer"
             }
            },
            "Addr-list": {
             "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
             "Local": "Addr-list"
            }
           },
           "addr-items": {
            "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
            "Local": "addr-items"
           }
          },
          "id": {
           "Text": "eth5/8",
           "id": {
            "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
            "Local": "id"
           }
          },
          "If-list": {
           "Space": "urn:ietf:param

s:xml:ns:netconf:base:1.0",
           "Local": "If-list"
          }
         },
         "if-items": {
          "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
          "Local": "if-items"
         }
        },
        "name": {
         "Text": "default",
         "name": {
          "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
          "Local": "name"
         }
        },
        "Dom-list": {
         "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
         "Local": "Dom-list"
        }
       },
       {
        "if-items": {
         "If-list": {
          "addr-items": {
           "Addr-list": {
            "addr": {
             "Text": "11.11.11.11",
             "addr": {
              "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "Local": "addr"
             }
            },
            "pref": {
             "Text": "1",
             "pref": {
              "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "Local": "pref"
             }
            },
            "tag": {
             "Text": "0",
             "tag": {
              "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "Local": "tag"
             }
            },
            "type": {
             "Text": "primary",
             "type": {
              "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "Local": "type"
             }
            },
            "vpcPeer": {
             "Text": "0.0.0.0",
             "vpcPeer": {
              "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
              "Local": "vpcPeer"
             }
            },
            "Addr-list": {
             "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
             "Local": "Addr-list"
            }
           },
           "addr-items": {
            "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
            "Local": "addr-items"
           }
          },
          "id": {
           "Text": "eth5/8",
           "id": {
            "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
            "Local": "id"
           }
          },
          "If-list": {
           "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
           "Local": "If-list"
          }
         },
         "if-items": {
          "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
          "Local": "if-items"
         }
        },
        "name": {
         "Text": "management",
         "name": {
          "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
          "Local": "name"
         }
        },
        "Dom-list": {
         "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
         "Local": "Dom-list"
        }
       }
      ],
      "dom-items": {
       "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
       "Local": "dom-items"
      }
     },
     "inst-items": {
      "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
      "Local": "inst-items"
     }
    },
    "ipv4-items": {
     "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
     "Local": "ipv4-items"
    }
   },
   "test-name": {
    "Text": "##WORK##",
    "test-name": {
     "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
     "Local": "test-name"
    }
   },
   "System": {
    "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
    "Local": "System"
   }
  },
  "data": {
   "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
   "Local": "data"
  }
 },
 "rpc-reply": {
  "Space": "urn:ietf:params:xml:ns:netconf:base:1.0",
  "Local": "rpc-reply"
 }
}
[me@machine mmm]$ 

Are you using the latest version of chidley?

Here is 'mmm' (Go code generated from your XML by chidley):



package main

/////////////////////////////////////////////////////////////////
//Code generated by chidley https://github.com/gnewton/chidley //
/////////////////////////////////////////////////////////////////

import (
	"bufio"
	"compress/bzip2"
	"compress/gzip"
	"encoding/json"
	"encoding/xml"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"runtime"
	"strings"
)

const (
	JsonOut = iota
	XmlOut
	CountAll
)

var toJson bool = false
var toXml bool = false
var oneLevelDown bool = false
var countAll bool = false
var musage bool = false

var uniqueFlags = []*bool{
	&toJson,
	&toXml,
	&countAll}

var filename = "/home/gnewton/gocode/src/github.com/gnewton/chidley/foo.xml"



func init() {
	flag.BoolVar(&toJson, "j", toJson, "Convert to JSON")
	flag.BoolVar(&toXml, "x", toXml, "Convert to XML")
	flag.BoolVar(&countAll, "c", countAll, "Count each instance of XML tags")
	flag.BoolVar(&oneLevelDown, "s", oneLevelDown, "Stream XML by using XML elements one down from the root tag. Good for huge XML files (see http://blog.davidsingleton.org/parsing-huge-xml-files-with-go/")
	flag.BoolVar(&musage, "h", musage, "Usage")
	flag.StringVar(&filename, "f", filename, "XML file or URL to read in")
}

var out int = -1

var counters map[string]*int

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	flag.Parse()

	if musage {
		flag.Usage()
		return
	}

	numSetBools, outFlag := numberOfBoolsSet(uniqueFlags)
	if numSetBools == 0 {
		flag.Usage()
                return
	}

	if numSetBools != 1 {
		flag.Usage()
		log.Fatal("Only one of ", uniqueFlags, " can be set at once")
	}

	reader, xmlFile, err := genericReader(filename)
	if err != nil {
		log.Fatal(err)
		return
	}

	decoder := xml.NewDecoder(reader)
	counters = make(map[string]*int)
	for {
		token, _ := decoder.Token()
		if token == nil {
			break
		}
		switch se := token.(type) {
		case xml.StartElement:
			handleFeed(se, decoder, outFlag)
		}
	}
        if xmlFile != nil{
	    defer xmlFile.Close()
        }
	if countAll {
		for k, v := range counters {
			fmt.Println(*v, k)
		}
	}
}

func handleFeed(se xml.StartElement, decoder *xml.Decoder, outFlag *bool) {
	if outFlag == &countAll {
		incrementCounter(se.Name.Space, se.Name.Local)
	} else {
                if !oneLevelDown{
        		if se.Name.Local == "rpc-reply" && se.Name.Space == "urn:ietf:params:xml:ns:netconf:base:1.0" {
	        	      var item Chirpc_reply
			      decoder.DecodeElement(&item, &se)
			      switch outFlag {
			      case &toJson:
				      writeJson(item)
			      case &toXml:
				      writeXml(item)
			      }
		      }
                }else{
                   
        		if se.Name.Local == "data" && se.Name.Space == "urn:ietf:params:xml:ns:netconf:base:1.0" {
	        	      var item Chidata
			      decoder.DecodeElement(&item, &se)
			      switch outFlag {
			      case &toJson:
				      writeJson(item)
			      case &toXml:
				      writeXml(item)
			      }
		      }
                   
               }
	}
}

func makeKey(space string, local string) string {
	if space == "" {
		space = "_"
	}
	return space + ":" + local
}

func incrementCounter(space string, local string) {
	key := makeKey(space, local)

	counter, ok := counters[key]
	if !ok {
		n := 1
		counters[key] = &n
	} else {
		newv := *counter + 1
		counters[key] = &newv
	}
}

func writeJson(item interface{}) {
	b, err := json.MarshalIndent(item, "", " ")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(b))
}

func writeXml(item interface{}) {
	output, err := xml.MarshalIndent(item, "  ", "    ")
	if err != nil {
		fmt.Printf("error: %v\n", err)
	}
	os.Stdout.Write(output)
}

func genericReader(filename string) (io.Reader, *os.File, error) {
	if filename == "" {
		return bufio.NewReader(os.Stdin), nil, nil
	}
	file, err := os.Open(filename)
	if err != nil {
		return nil, nil, err
	}
	if strings.HasSuffix(filename, "bz2") {
		return bufio.NewReader(bzip2.NewReader(bufio.NewReader(file))), file, err
	}

	if strings.HasSuffix(filename, "gz") {
		reader, err := gzip.NewReader(bufio.NewReader(file))
		if err != nil {
			return nil, nil, err
		}
		return bufio.NewReader(reader), file, err
	}
	return bufio.NewReader(file), file, err
}

func numberOfBoolsSet(a []*bool) (int, *bool) {
	var setBool *bool
	counter := 0
	for i := 0; i < len(a); i++ {
		if *a[i] {
			counter += 1
			setBool = a[i]
		}
	}
	return counter, setBool
}


///////////////////////////
/// structs
///////////////////////////

type Chiroot struct {
	Chirpc_reply *Chirpc_reply `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 rpc-reply,omitempty" json:"rpc-reply,omitempty"`
}

type ChiAddr_list struct {
	Chiaddr *Chiaddr `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr,omitempty" json:"addr,omitempty"`
	Chipref *Chipref `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 pref,omitempty" json:"pref,omitempty"`
	Chitag *Chitag `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 tag,omitempty" json:"tag,omitempty"`
	Chitype *Chitype `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 type,omitempty" json:"type,omitempty"`
	ChivpcPeer *ChivpcPeer `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 vpcPeer,omitempty" json:"vpcPeer,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Addr-list,omitempty" json:"Addr-list,omitempty"`
}

type ChiDom_list struct {
	Chiif_items *Chiif_items `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 if-items,omitempty" json:"if-items,omitempty"`
	Chiname *Chiname `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 name,omitempty" json:"name,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Dom-list,omitempty" json:"Dom-list,omitempty"`
}

type ChiIf_list struct {
	Chiaddr_items *Chiaddr_items `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr-items,omitempty" json:"addr-items,omitempty"`
	Chiid *Chiid `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 id,omitempty" json:"id,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 If-list,omitempty" json:"If-list,omitempty"`
}

type ChiSystem struct {
	Chiipv4_items *Chiipv4_items `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 ipv4-items,omitempty" json:"ipv4-items,omitempty"`
	Chitest_name *Chitest_name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 test-name,omitempty" json:"test-name,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 System,omitempty" json:"System,omitempty"`
}

type Chiaddr struct {
	Text string `xml:",chardata" json:",omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr,omitempty" json:"addr,omitempty"`
}

type Chiaddr_items struct {
	ChiAddr_list *ChiAddr_list `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Addr-list,omitempty" json:"Addr-list,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr-items,omitempty" json:"addr-items,omitempty"`
}

type Chidata struct {
	ChiSystem *ChiSystem `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 System,omitempty" json:"System,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 data,omitempty" json:"data,omitempty"`
}

type Chidom_items struct {
	ChiDom_list []*ChiDom_list `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Dom-list,omitempty" json:"Dom-list,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 dom-items,omitempty" json:"dom-items,omitempty"`
}

type Chiid struct {
	Text string `xml:",chardata" json:",omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 id,omitempty" json:"id,omitempty"`
}

type Chiif_items struct {
	ChiIf_list *ChiIf_list `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 If-list,omitempty" json:"If-list,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 if-items,omitempty" json:"if-items,omitempty"`
}

type Chiinst_items struct {
	Chidom_items *Chidom_items `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 dom-items,omitempty" json:"dom-items,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 inst-items,omitempty" json:"inst-items,omitempty"`
}

type Chiipv4_items struct {
	Chiinst_items *Chiinst_items `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 inst-items,omitempty" json:"inst-items,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 ipv4-items,omitempty" json:"ipv4-items,omitempty"`
}

type Chiname struct {
	Text string `xml:",chardata" json:",omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 name,omitempty" json:"name,omitempty"`
}

type Chipref struct {
	Text string `xml:",chardata" json:",omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 pref,omitempty" json:"pref,omitempty"`
}

type Chirpc_reply struct {
	Attr_message_id string `xml:" message-id,attr"  json:",omitempty"`
	Attr_xmlns string `xml:" xmlns,attr"  json:",omitempty"`
	Chidata *Chidata `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 data,omitempty" json:"data,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 rpc-reply,omitempty" json:"rpc-reply,omitempty"`
}

type Chitag struct {
	Text string `xml:",chardata" json:",omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 tag,omitempty" json:"tag,omitempty"`
}

type Chitest_name struct {
	Text string `xml:",chardata" json:",omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 test-name,omitempty" json:"test-name,omitempty"`
}

type Chitype struct {
	Text string `xml:",chardata" json:",omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 type,omitempty" json:"type,omitempty"`
}

type ChivpcPeer struct {
	Text string `xml:",chardata" json:",omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 vpcPeer,omitempty" json:"vpcPeer,omitempty"`
}


///////////////////////////

Thanks,
Glen

@singhaniashrinkhala
Copy link
Author

singhaniashrinkhala commented Jun 16, 2017

@gnewton

For the xml file: https://expirebox.com/download/abc77ad1602fa9c6c835dc1f15beac81.html

The struct file that is created, if I save that as a go file <open_config_struct.go> where package is main, I get two errors, one the root being redeclared, which I fix by renaming the second root.

./open_config_struct.go:3151: Root redeclared in this block
previous declaration at ./open_config_struct.go:3

However, I get one more error:

./open_config_struct.go:757: undefined: xml in xml.Name

This is for type ChiadminSt. I am clueless why this error is coming up.

Thanks

@gnewton
Copy link
Owner

gnewton commented Jun 17, 2017

I tried your XML.

It turns out this is 2 bugs. One easy to fix, one hard to fix (requires breaking changes to chidley):

1 - chidley creates a top level, called ChiRoot. In the case of your XML, you are using the root tag also:
Line 53330:
<root>primary</root>

so chidley transforms this into

type ChiRoot struct {
   Text string ``xml:",chardata" json:",omitempty"
   XMLName  xml.Name ``xml:"urn:ietf:params:xml:ns:netconf:base:1.0 root,omitempty" json:"root,omitempty"`
}

which collides with the chidley base struct, ChiRoot.

So to avoid such collisions, I have renamed the tag chidley uses for the root base, so this now produces for the base:

type ChiChidleyRoot314159 struct {
	ChiRpcHyphenReply *ChiRpcHyphenReply `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 rpc-reply,omitempty" json:"rpc-reply,omitempty"`
}

2 - The second bug is a little more complicated. If you compile the code generated by the above, you get this error:

$ go build
# github.com/gnewton/chidley/mmm
./main.go:1800: ChiEventHyphenItems redeclared in this block
    previous declaration at ./main.go:366
$

Looking into your XML, it turns out you have two different event items (the 'Hyphen' is added by chidley as Go does not accept hyphens) in struct names, differing only with their capitalization:

Line 14990:

        <Event-items>
          <Event-list>
            <event>event-loopback</event>
            <adminSt>enabled</adminSt>
          </Event-list>
          .
          .
          .

and Line 53026:

        <event-items>
          <Event-list>
            <num>5</num>
            <description>INFORMATION(5)</description>
            <log>yes</log>
            <owner>PMON@INFO</owner>
            <trap>public</trap>
          </Event-list>
          <Event-list>
          .
          .
          .

chidley makes these into camel case, causing a name collision as the difference in the capitalization is lost in this transformation.

For your issue, you can just go in and either change the XML or change the generated code.

From the chidley perspective, this is a major bug: XML with tags that only differ in the capitalization of their tags' first letters will also be impacted this bug.

My plan is to make a breaking change to chidley, with a new full version release. In the new version, no capitalization changes will be made to the names of the XML when transforming them to the struct names.
So, what was previously:

type ChiAddrHyphenList struct {
	ChiAddr *ChiAddr `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr,omitempty" json:"addr,omitempty"`
	ChiPref *ChiPref `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 pref,omitempty" json:"pref,omitempty"`
	ChiTag *ChiTag `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 tag,omitempty" json:"tag,omitempty"`
	ChiType *ChiType `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 type,omitempty" json:"type,omitempty"`
	ChiVpcPeer *ChiVpcPeer `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 vpcPeer,omitempty" json:"vpcPeer,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Addr-list,omitempty" json:"Addr-list,omitempty"`
}

would become:

type ChiaddrHyphenlist struct {
	Chiaddr *Chiaddr `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 addr,omitempty" json:"addr,omitempty"`
	Chipref *Chipref `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 pref,omitempty" json:"pref,omitempty"`
	Chitag *Chitag `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 tag,omitempty" json:"tag,omitempty"`
	Chitype *Chitype `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 type,omitempty" json:"type,omitempty"`
	Chivpcpeer *Chivpcpeer `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 vpcPeer,omitempty" json:"vpcPeer,omitempty"`
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:netconf:base:1.0 Addr-list,omitempty" json:"Addr-list,omitempty"`
}

Note that the Hyphen replaces a - so can remain capitalized, and the XMLName does not come from the source XML, so is also OK as-is.

If have fixed the first bug in master, and will open an issue to make the breaking change.

@singhaniashrinkhala
Copy link
Author

Hi @gnewton , I was able to fix the second issue as well, same way as you mentioned, renaming it.
However, the issue that I have posted is not this. If you save the file as a go file and run it, you will get the following error, which doesn't make sense to me:

./open_config_struct.go:757: undefined: xml in xml.Name

This is for type ChiadminSt struct.

@singhaniashrinkhala
Copy link
Author

@gnewton : nvm. The issue was something not to do with Chidley. Thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants