diff --git a/chidley.go b/chidley.go index c30e5a2..f0cb8d6 100644 --- a/chidley.go +++ b/chidley.go @@ -18,14 +18,15 @@ import ( ) var DEBUG = false -var progress = false -var attributePrefix = "Attr_" -var structsToStdout = false +var attributePrefix = "Attr" +var codeGenConvert = false +var classicStructNamesWithUnderscores = false var nameSpaceInJsonName = false var prettyPrint = false -var codeGenConvert = false +var progress = false var readFromStandardIn = false var sortByXmlOrder = false +var structsToStdout = true var codeGenDir = "codegen" var codeGenFilename = "CodeGenStructs.go" @@ -64,11 +65,13 @@ var outputs = []*bool{ func init() { + flag.BoolVar(&classicStructNamesWithUnderscores, "C", classicStructNamesWithUnderscores, "Structs have underscores instead of CamelCase; how chidley used to produce output; includes name spaces (see -n)") + flag.BoolVar(&DEBUG, "d", DEBUG, "Debug; prints out much information") flag.BoolVar(&addDbMetadata, "B", addDbMetadata, "Add database metadata to created Go structs") flag.BoolVar(&sortByXmlOrder, "X", sortByXmlOrder, "Sort output of structs in Go code by order encounered in source XML (default is alphabetical order)") flag.BoolVar(&codeGenConvert, "W", codeGenConvert, "Generate Go code to convert XML to JSON or XML (latter useful for validation) and write it to stdout") - flag.BoolVar(&nameSpaceInJsonName, "n", nameSpaceInJsonName, "Use the XML namespace prefix as prefix to JSON name; prefix followed by 2 underscores (__)") + flag.BoolVar(&nameSpaceInJsonName, "n", nameSpaceInJsonName, "Use the XML namespace prefix as prefix to JSON name") flag.BoolVar(&prettyPrint, "p", prettyPrint, "Pretty-print json in generated code (if applicable)") flag.BoolVar(&progress, "r", progress, "Progress: every 50000 input tags (elements)") flag.BoolVar(&readFromStandardIn, "c", readFromStandardIn, "Read XML from standard input") @@ -87,6 +90,10 @@ func init() { func handleParameters() error { flag.Parse() + if codeGenConvert || writeJava { + structsToStdout = false + } + numBoolsSet := countNumberOfBoolsSet(outputs) if numBoolsSet > 1 { log.Print(" ERROR: Only one of -W -J -X -V -c can be set") @@ -357,14 +364,6 @@ func indent(d int) string { return indent } -func capitalizeFirstLetter(s string) string { - return strings.ToUpper(s[0:1]) + s[1:] -} - -func lowerFirstLetter(s string) string { - return strings.ToLower(s[0:1]) + s[1:] -} - func countNumberOfBoolsSet(a []*bool) int { counter := 0 for i := 0; i < len(a); i++ { diff --git a/extractor.go b/extractor.go index 1b10ca8..98e81fa 100644 --- a/extractor.go +++ b/extractor.go @@ -9,8 +9,8 @@ import ( ) var nameMapper = map[string]string{ - "-": "_", - ".": "_dot_", + "-": "Hyphen", + ".": "Dot", } var DiscoveredOrder = 0 @@ -132,7 +132,7 @@ func handleTokens(tChannel chan xml.Token, ex *Extractor, handleTokensDoneChanne if DEBUG { log.Printf("EndElement: %+v\n", element) log.Printf("[[" + thisNode.tempCharData + "]]") - log.Printf("Char is empty: ", isJustSpacesAndLinefeeds(thisNode.tempCharData)) + log.Println("Char is empty: ", isJustSpacesAndLinefeeds(thisNode.tempCharData)) } if !thisNode.hasCharData && !isJustSpacesAndLinefeeds(thisNode.tempCharData) { thisNode.hasCharData = true @@ -159,7 +159,7 @@ func handleTokens(tChannel chan xml.Token, ex *Extractor, handleTokensDoneChanne } func space(n int) string { - s := strconv.Itoa(n) + ":" + s := strconv.Itoa(n) + "Space" for i := 0; i < n; i++ { s += " " } diff --git a/node.go b/node.go index fec9a06..474fdd5 100644 --- a/node.go +++ b/node.go @@ -48,7 +48,7 @@ func (n *Node) makeName() string { } func (n *Node) makeType(prefix string, suffix string) string { - return capitalizeFirstLetter(makeTypeGeneric(n.name, n.spaceTag, prefix, suffix, false)) + return capitalizeFirstLetter(makeTypeGeneric(n.name, n.spaceTag, prefix, suffix, true)) } func (n *Node) makeJavaType(prefix string, suffix string) string { @@ -79,9 +79,7 @@ func (n *Node) popParent() *Node { func makeTypeGeneric(name string, space string, prefix string, suffix string, capitalizeName bool) string { spaceTag := "" - if space != "" { - spaceTag = space + "_" - } + if capitalizeName { name = capitalizeFirstLetter(name) } diff --git a/printGoStructVisitor.go b/printGoStructVisitor.go index 576cb89..c9f2939 100644 --- a/printGoStructVisitor.go +++ b/printGoStructVisitor.go @@ -128,7 +128,7 @@ func makeAnnotation(annotationId string, spaceTag string, useSpaceTag bool, useS if useSpaceTagInName { if spaceTag != "" { - annotation = annotation + spaceTag + "__" + annotation = annotation + spaceTag } } diff --git a/util.go b/util.go index 4fd4dca..e3ac887 100644 --- a/util.go +++ b/util.go @@ -32,9 +32,21 @@ func genericReader(filename string) (io.Reader, *os.File, error) { func cleanName(name string) string { for old, new := range nameMapper { - name = strings.Replace(name, old, new, -1) + parts := strings.Split(name, old) + if len(parts) == 1 { + continue + } else { + name = "" + l := len(parts) + for i := 0; i < l; i++ { + name += capitalizeFirstLetter(parts[i]) + if i+1 < l { + name += new + } + } + } } - return name + return capitalizeFirstLetter(name) } func findType(nti *NodeTypeInfo, useType bool) string { @@ -83,10 +95,12 @@ func makeAttributes(lineChannel chan string, attributes []*FQN, nameSpaceTagMap spaceTag, ok := nameSpaceTagMap[space] if ok && spaceTag != "" { - spaceTag = spaceTag + "_" + spaceTag = spaceTag + "Space" + } else { + spaceTag = space } - lineChannel <- "\t" + attributePrefix + spaceTag + cleanName(name) + " string `xml:\"" + space + " " + name + ",attr\" json:\",omitempty\"`" + lineChannel <- "\t" + attributePrefix + capitalizeFirstLetter(spaceTag) + cleanName(name) + " string `xml:\"" + space + " " + name + ",attr\" json:\",omitempty\"`" } } @@ -111,7 +125,7 @@ func nk(n *Node) string { } func nks(space, name string) string { - return space + "___" + name + return space + "NS" + name } func getFullPath(filename string) string { @@ -125,3 +139,26 @@ func getFullPath(filename string) string { } return file.Name() } + +type alterer func(string) string + +func alterFirstLetter(s string, f alterer) string { + switch len(s) { + case 0: + return s + case 1: + v := f(s[0:1]) + return v + + default: + return f(s[0:1]) + s[1:] + } +} + +func capitalizeFirstLetter(s string) string { + return alterFirstLetter(s, strings.ToUpper) +} + +func lowerFirstLetter(s string) string { + return alterFirstLetter(s, strings.ToLower) +}