Skip to content

Commit

Permalink
Fixed multi-hyphen sanitization. (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
abice authored Apr 19, 2019
1 parent 9b3947d commit fd4b3b6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions example/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ Green = 33 // Green starts with 33
// blue-green
// red-orange
// yellow_green
// red-orange-blue
// )
type Color int
7 changes: 6 additions & 1 deletion example/color_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions generator/.snapshots/generator-TestExampleFile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
([]string) (len=1266) {
([]string) (len=1272) {
(string) (len=28) "// Code generated by go-enum",
(string) (len=15) "// DO NOT EDIT!",
(string) "",
Expand Down Expand Up @@ -595,9 +595,11 @@
(string) (len=27) "\tColorWithComment3BlueGreen",
(string) (len=72) "\t// ColorWithComment3RedOrange is a ColorWithComment3 of type Red-Orange",
(string) (len=27) "\tColorWithComment3RedOrange",
(string) (len=81) "\t// ColorWithComment3RedOrangeBlue is a ColorWithComment3 of type Red-Orange-Blue",
(string) (len=31) "\tColorWithComment3RedOrangeBlue",
(string) (len=1) ")",
(string) "",
(string) (len=85) "const _ColorWithComment3Name = \"BlackWhiteRedGreenBluegreyyellowblue-greenred-orange\"",
(string) (len=100) "const _ColorWithComment3Name = \"BlackWhiteRedGreenBluegreyyellowblue-greenred-orangered-orange-blue\"",
(string) "",
(string) (len=39) "var _ColorWithComment3Names = []string{",
(string) (len=29) "\t_ColorWithComment3Name[0:5],",
Expand All @@ -609,6 +611,7 @@
(string) (len=31) "\t_ColorWithComment3Name[26:32],",
(string) (len=31) "\t_ColorWithComment3Name[32:42],",
(string) (len=31) "\t_ColorWithComment3Name[42:52],",
(string) (len=31) "\t_ColorWithComment3Name[52:67],",
(string) (len=1) "}",
(string) "",
(string) (len=88) "// ColorWithComment3Names returns a list of possible string values of ColorWithComment3.",
Expand All @@ -628,6 +631,7 @@
(string) (len=35) "\t36: _ColorWithComment3Name[26:32],",
(string) (len=35) "\t37: _ColorWithComment3Name[32:42],",
(string) (len=35) "\t38: _ColorWithComment3Name[42:52],",
(string) (len=35) "\t39: _ColorWithComment3Name[52:67],",
(string) (len=1) "}",
(string) "",
(string) (len=44) "// String implements the Stringer interface.",
Expand Down Expand Up @@ -657,6 +661,8 @@
(string) (len=52) "\tstrings.ToLower(_ColorWithComment3Name[32:42]): 37,",
(string) (len=52) "\t_ColorWithComment3Name[42:52]: 38,",
(string) (len=52) "\tstrings.ToLower(_ColorWithComment3Name[42:52]): 38,",
(string) (len=52) "\t_ColorWithComment3Name[52:67]: 39,",
(string) (len=52) "\tstrings.ToLower(_ColorWithComment3Name[52:67]): 39,",
(string) (len=1) "}",
(string) "",
(string) (len=77) "// ParseColorWithComment3 attempts to convert a string to a ColorWithComment3",
Expand Down
9 changes: 7 additions & 2 deletions generator/.snapshots/generator-TestNoPrefixExampleFile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
([]string) (len=985) {
([]string) (len=990) {
(string) (len=28) "// Code generated by go-enum",
(string) (len=15) "// DO NOT EDIT!",
(string) "",
Expand Down Expand Up @@ -467,9 +467,11 @@
(string) (len=10) "\tBlueGreen",
(string) (len=55) "\t// RedOrange is a ColorWithComment3 of type Red-Orange",
(string) (len=10) "\tRedOrange",
(string) (len=64) "\t// RedOrangeBlue is a ColorWithComment3 of type Red-Orange-Blue",
(string) (len=14) "\tRedOrangeBlue",
(string) (len=1) ")",
(string) "",
(string) (len=85) "const _ColorWithComment3Name = \"BlackWhiteRedGreenBluegreyyellowblue-greenred-orange\"",
(string) (len=100) "const _ColorWithComment3Name = \"BlackWhiteRedGreenBluegreyyellowblue-greenred-orangered-orange-blue\"",
(string) "",
(string) (len=57) "var _ColorWithComment3Map = map[ColorWithComment3]string{",
(string) (len=33) "\t0: _ColorWithComment3Name[0:5],",
Expand All @@ -481,6 +483,7 @@
(string) (len=35) "\t36: _ColorWithComment3Name[26:32],",
(string) (len=35) "\t37: _ColorWithComment3Name[32:42],",
(string) (len=35) "\t38: _ColorWithComment3Name[42:52],",
(string) (len=35) "\t39: _ColorWithComment3Name[52:67],",
(string) (len=1) "}",
(string) "",
(string) (len=44) "// String implements the Stringer interface.",
Expand Down Expand Up @@ -510,6 +513,8 @@
(string) (len=52) "\tstrings.ToLower(_ColorWithComment3Name[32:42]): 37,",
(string) (len=52) "\t_ColorWithComment3Name[42:52]: 38,",
(string) (len=52) "\tstrings.ToLower(_ColorWithComment3Name[42:52]): 38,",
(string) (len=52) "\t_ColorWithComment3Name[52:67]: 39,",
(string) (len=52) "\tstrings.ToLower(_ColorWithComment3Name[52:67]): 39,",
(string) (len=1) "}",
(string) "",
(string) (len=77) "// ParseColorWithComment3 attempts to convert a string to a ColorWithComment3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
([]string) (len=985) {
([]string) (len=990) {
(string) (len=28) "// Code generated by go-enum",
(string) (len=15) "// DO NOT EDIT!",
(string) "",
Expand Down Expand Up @@ -467,9 +467,11 @@
(string) (len=10) "\tBlueGreen",
(string) (len=55) "\t// RedOrange is a ColorWithComment3 of type Red-Orange",
(string) (len=10) "\tRedOrange",
(string) (len=64) "\t// RedOrangeBlue is a ColorWithComment3 of type Red-Orange-Blue",
(string) (len=14) "\tRedOrangeBlue",
(string) (len=1) ")",
(string) "",
(string) (len=85) "const _ColorWithComment3Name = \"BlackWhiteRedGreenBluegreyyellowblue-greenred-orange\"",
(string) (len=100) "const _ColorWithComment3Name = \"BlackWhiteRedGreenBluegreyyellowblue-greenred-orangered-orange-blue\"",
(string) "",
(string) (len=57) "var _ColorWithComment3Map = map[ColorWithComment3]string{",
(string) (len=33) "\t0: _ColorWithComment3Name[0:5],",
Expand All @@ -481,6 +483,7 @@
(string) (len=35) "\t36: _ColorWithComment3Name[26:32],",
(string) (len=35) "\t37: _ColorWithComment3Name[32:42],",
(string) (len=35) "\t38: _ColorWithComment3Name[42:52],",
(string) (len=35) "\t39: _ColorWithComment3Name[52:67],",
(string) (len=1) "}",
(string) "",
(string) (len=44) "// String implements the Stringer interface.",
Expand Down Expand Up @@ -510,6 +513,8 @@
(string) (len=52) "\tstrings.ToLower(_ColorWithComment3Name[32:42]): 37,",
(string) (len=52) "\t_ColorWithComment3Name[42:52]: 38,",
(string) (len=52) "\tstrings.ToLower(_ColorWithComment3Name[42:52]): 38,",
(string) (len=52) "\t_ColorWithComment3Name[52:67]: 39,",
(string) (len=52) "\tstrings.ToLower(_ColorWithComment3Name[52:67]): 39,",
(string) (len=1) "}",
(string) "",
(string) (len=77) "// ParseColorWithComment3 attempts to convert a string to a ColorWithComment3",
Expand Down
1 change: 1 addition & 0 deletions generator/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Green = 33 // Green starts with 33
// yellow
// blue-green // blue-green comment
// red-orange
// red-orange-blue
// )
type ColorWithComment3 int

Expand Down
4 changes: 3 additions & 1 deletion generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,16 @@ func sanitizeValue(value string) string {
}

// Loop through all the runes and remove any that aren't valid.
for i, r := range name {
for i := 0; i < len(name); i++ {
r := rune(name[i])
if !(unicode.IsLetter(r) || unicode.IsNumber(r) || r == '_') {
if i < len(name) {
name = name[:i] + name[i+1:]
} else {
// At the end of the string, take off the last char
name = name[:i-1]
}
i--
}
}

Expand Down

0 comments on commit fd4b3b6

Please sign in to comment.