Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion fixtures/chromedata.wsdl
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
<simpleType name="DriveTrain">
<restriction base="string">
<enumeration value="Front Wheel Drive" />
<enumeration value="Front Wheel Drive+" />
<enumeration value="Rear Wheel Drive" />
<enumeration value="All Wheel Drive" />
<enumeration value="Four Wheel Drive" />
Expand Down Expand Up @@ -1169,4 +1170,4 @@
<soap:address location="https://services.chromedata.com:443/Description/7a" />
</port>
</service>
</definitions>
</definitions>
17 changes: 12 additions & 5 deletions gowsdl.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ var reservedWords = map[string]string{
"var": "var_",
}

var reservedCharacters = map[rune]string{
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you already pointed out this will work only for specific cases. The idea of custom mapping sounds like a better solution.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I don't really expect this to be accepted :-)

'_': "_",
'+': "Plus",
}

// Replaces Go reserved keywords to avoid compilation issues
func replaceReservedWords(identifier string) string {
value := reservedWords[identifier]
Expand All @@ -358,14 +363,16 @@ func replaceReservedWords(identifier string) string {

// Normalizes value to be used as a valid Go identifier, avoiding compilation issues
func normalize(value string) string {
mapping := func(r rune) rune {
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' {
return r
normalized := ""
for _, r := range value {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
normalized += string(r)
} else if replacement, found := reservedCharacters[r]; found {
normalized += replacement
}
return -1
}

return strings.Map(mapping, value)
return normalized
}

func goString(s string) string {
Expand Down
1 change: 1 addition & 0 deletions gowsdl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func TestEnumerationsGeneratedCorrectly(t *testing.T) {
}
}
enumStringTest(t, "chromedata.wsdl", "DriveTrainFrontWheelDrive", "DriveTrain", "Front Wheel Drive")
enumStringTest(t, "chromedata.wsdl", "DriveTrainFrontWheelDrivePlus", "DriveTrain", "Front Wheel Drive+")
enumStringTest(t, "vboxweb.wsdl", "SettingsVersionV1_14", "SettingsVersion", "v1_14")

}
Expand Down