Skip to content

Commit

Permalink
Merge branch '13_generate_go_structs_with_camelCase'
Browse files Browse the repository at this point in the history
Merged in branch as it is complete.
  • Loading branch information
gnewton committed Jun 16, 2017
2 parents a22e555 + 9b995f4 commit aecafe8
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
25 changes: 12 additions & 13 deletions chidley.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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++ {
Expand Down
8 changes: 4 additions & 4 deletions extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

var nameMapper = map[string]string{
"-": "_",
".": "_dot_",
"-": "Hyphen",
".": "Dot",
}

var DiscoveredOrder = 0
Expand Down Expand Up @@ -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
Expand All @@ -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 += " "
}
Expand Down
6 changes: 2 additions & 4 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion printGoStructVisitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func makeAnnotation(annotationId string, spaceTag string, useSpaceTag bool, useS

if useSpaceTagInName {
if spaceTag != "" {
annotation = annotation + spaceTag + "__"
annotation = annotation + spaceTag
}
}

Expand Down
47 changes: 42 additions & 5 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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\"`"
}
}

Expand All @@ -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 {
Expand All @@ -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)
}

0 comments on commit aecafe8

Please sign in to comment.