Skip to content
This repository has been archived by the owner on Jun 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #21 from sprin/fix_import_names
Browse files Browse the repository at this point in the history
Ensure import names are valid Go identifiers
  • Loading branch information
metaleap committed May 13, 2016
2 parents 9f0dd6c + 841deb2 commit 56ab80f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
21 changes: 20 additions & 1 deletion elemmakepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"path"
"strings"
"unicode"

"github.com/go-utils/ustr"

Expand Down Expand Up @@ -516,7 +517,7 @@ func (me *Import) makePkg(bag *PkgBag) {
me.hasElemAnnotation.makePkg(bag)
for k, v := range bag.Schema.XMLNamespaces {
if v == me.Namespace {
impName = k
impName = safeIdentifier(k)
break
}
}
Expand Down Expand Up @@ -836,6 +837,24 @@ func sfmt(s string, a ...interface{}) string {
return fmt.Sprintf(s, a...)
}

// For any rune, return a rune that is a valid in an identifier
func coerceToIdentifierRune(ch rune) rune {
if !unicode.IsLetter(ch) && !unicode.IsNumber(ch) {
return '_'
}
return ch
}

// Take any string and convert it to a valid identifier
// Appends an underscore if the first rune is a number
func safeIdentifier(s string) string {
s = strings.Map(coerceToIdentifierRune, s)
if unicode.IsNumber([]rune(s)[0]) {
s = fmt.Sprint("_", s)
}
return s
}

func subMakeElem(bag *PkgBag, td *declType, el *Element, done map[string]bool, parentMaxOccurs xsdt.Long, anns ...*Annotation) {
var elCache map[string]string
anns = append(anns, el.Annotation)
Expand Down
1 change: 1 addition & 0 deletions makepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ func (me *PkgBag) resolveQnameRef(ref, pref string, noUsageRec *string) string {
}
if pos := strings.Index(ref, ":"); pos > 0 {
impName, ns = ref[:pos], me.Schema.XMLNamespaces[ref[:pos]]
impName = safeIdentifier(impName)
ref = ref[(pos + 1):]
}
if ns == xsdNamespaceUri {
Expand Down

0 comments on commit 56ab80f

Please sign in to comment.