Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty string if out of range #65

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
11 changes: 10 additions & 1 deletion enumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import "fmt"

// Arguments to format are:
//
// [1]: type name
const stringNameToValueMethod = `// %[1]sString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
Expand All @@ -19,6 +20,7 @@ func %[1]sString(s string) (%[1]s, error) {
`

// Arguments to format are:
//
// [1]: type name
const stringValuesMethod = `// %[1]sValues returns all values of the enum
func %[1]sValues() []%[1]s {
Expand All @@ -27,6 +29,7 @@ func %[1]sValues() []%[1]s {
`

// Arguments to format are:
//
// [1]: type name
const stringsMethod = `// %[1]sStrings returns a slice of all String values of the enum
func %[1]sStrings() []string {
Expand All @@ -37,6 +40,7 @@ func %[1]sStrings() []string {
`

// Arguments to format are:
//
// [1]: type name
const stringBelongsMethodLoop = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise
func (i %[1]s) IsA%[1]s() bool {
Expand All @@ -50,6 +54,7 @@ func (i %[1]s) IsA%[1]s() bool {
`

// Arguments to format are:
//
// [1]: type name
const stringBelongsMethodSet = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise
func (i %[1]s) IsA%[1]s() bool {
Expand All @@ -59,6 +64,7 @@ func (i %[1]s) IsA%[1]s() bool {
`

// Arguments to format are:
//
// [1]: type name
const altStringValuesMethod = `func (%[1]s) Values() []string {
return %[1]sStrings()
Expand Down Expand Up @@ -144,6 +150,7 @@ func (g *Generator) printNamesSlice(runs [][]Value, typeName string, runsThresho
}

// Arguments to format are:
//
// [1]: type name
const jsonMethods = `
// MarshalJSON implements the json.Marshaler interface for %[1]s
Expand All @@ -169,6 +176,7 @@ func (g *Generator) buildJSONMethods(runs [][]Value, typeName string, runsThresh
}

// Arguments to format are:
//
// [1]: type name
const textMethods = `
// MarshalText implements the encoding.TextMarshaler interface for %[1]s
Expand All @@ -189,11 +197,12 @@ func (g *Generator) buildTextMethods(runs [][]Value, typeName string, runsThresh
}

// Arguments to format are:
//
// [1]: type name
const yamlMethods = `
// MarshalYAML implements a YAML Marshaler for %[1]s
func (i %[1]s) MarshalYAML() (interface{}, error) {
return i.String(), nil
return i.string()
}

// UnmarshalYAML implements a YAML Unmarshaler for %[1]s
Expand Down
3 changes: 2 additions & 1 deletion sql.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

// Arguments to format are:
//
// [1]: type name
const valueMethod = `func (i %[1]s) Value() (driver.Value, error) {
return i.String(), nil
return i.string()
}
`

Expand Down
51 changes: 34 additions & 17 deletions stringer.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,28 +774,40 @@ func (g *Generator) buildOneRun(runs [][]Value, typeName string) {
}

// Arguments to format are:
// [1]: type name
// [2]: size of index element (8 for uint8 etc.)
// [3]: less than zero check (for signed types)
const stringOneRun = `func (i %[1]s) String() string {
//
// [1]: type name
// [2]: size of index element (8 for uint8 etc.)
// [3]: less than zero check (for signed types)
const stringOneRun = `func (i %[1]s) string() (string, error) {
if %[3]si >= %[1]s(len(_%[1]sIndex)-1) {
return fmt.Sprintf("%[1]s(%%d)", i)
return "", fmt.Errorf("value is out of enum range")
}
return _%[1]sName[_%[1]sIndex[i]:_%[1]sIndex[i+1]]
return _%[1]sName[_%[1]sIndex[i]:_%[1]sIndex[i+1]], nil
}

func (i %[1]s) String() string {
val, _ := i.string()
return val
}
`

// Arguments to format are:
// [1]: type name
// [2]: lowest defined value for type, as a string
// [3]: size of index element (8 for uint8 etc.)
// [4]: less than zero check (for signed types)
const stringOneRunWithOffset = `func (i %[1]s) String() string {
//
// [1]: type name
// [2]: lowest defined value for type, as a string
// [3]: size of index element (8 for uint8 etc.)
// [4]: less than zero check (for signed types)
const stringOneRunWithOffset = `func(i %[1]s) string() (string, error) {
i -= %[2]s
if %[4]si >= %[1]s(len(_%[1]sIndex)-1) {
return fmt.Sprintf("%[1]s(%%d)", i + %[2]s)
return "", fmt.Errorf("value is out of enum range")
}
return _%[1]sName[_%[1]sIndex[i] : _%[1]sIndex[i+1]]
return _%[1]sName[_%[1]sIndex[i] : _%[1]sIndex[i+1]], nil
}

func (i %[1]s) String() string {
val, _ := i.string()
return val
}
`

Expand All @@ -820,7 +832,7 @@ func (g *Generator) buildMultipleRuns(runs [][]Value, typeName string) {
typeName, i, typeName, i, typeName, i)
}
g.Printf("\tdefault:\n")
g.Printf("\t\treturn fmt.Sprintf(\"%s(%%d)\", i)\n", typeName)
g.Printf("\t\treturn \"\"\n")
g.Printf("\t}\n")
g.Printf("}\n")
}
Expand Down Expand Up @@ -861,10 +873,15 @@ func (g *Generator) buildNoOpOrderChangeDetect(runs [][]Value, typeName string)
}

// Argument to format is the type name.
const stringMap = `func (i %[1]s) String() string {
const stringMap = `func (i %[1]s) string() (string, error) {
if str, ok := _%[1]sMap[i]; ok {
return str
return str, nil
}
return fmt.Sprintf("%[1]s(%%d)", i)
return "", fmt.Errorf("value is out of enum range")
}

func (i %[1]s) String() string {
str, _ := i.string()
return str
}
`
4 changes: 2 additions & 2 deletions testdata/day.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func main() {
ck(Friday, "Friday")
ck(Saturday, "Saturday")
ck(Sunday, "Sunday")
ck(-127, "Day(-127)")
ck(127, "Day(127)")
ck(-127, "")
ck(127, "")
ckDayString(Sunday, "Sunday")
ckDayString(Sunday, "sunday")

Expand Down
11 changes: 8 additions & 3 deletions testdata/day.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ var _DayIndex = [...]uint8{0, 6, 13, 22, 30, 36, 44, 50}

const _DayLowerName = "mondaytuesdaywednesdaythursdayfridaysaturdaysunday"

func (i Day) String() string {
func (i Day) string() (string, error) {
if i < 0 || i >= Day(len(_DayIndex)-1) {
return fmt.Sprintf("Day(%d)", i)
return "", fmt.Errorf("value is out of enum range")
}
return _DayName[_DayIndex[i]:_DayIndex[i+1]]
return _DayName[_DayIndex[i]:_DayIndex[i+1]], nil
}

func (i Day) String() string {
val, _ := i.string()
return val
}

func (Day) Values() []string {
Expand Down
11 changes: 8 additions & 3 deletions testdata/dayTrimAndPrefix.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ var _DayIndex = [...]uint8{0, 11, 23, 37, 50, 61, 74, 85}

const _DayLowerName = "nightmondaynighttuesdaynightwednesdaynightthursdaynightfridaynightsaturdaynightsunday"

func (i Day) String() string {
func (i Day) string() (string, error) {
if i < 0 || i >= Day(len(_DayIndex)-1) {
return fmt.Sprintf("Day(%d)", i)
return "", fmt.Errorf("value is out of enum range")
}
return _DayName[_DayIndex[i]:_DayIndex[i+1]]
return _DayName[_DayIndex[i]:_DayIndex[i+1]], nil
}

func (i Day) String() string {
val, _ := i.string()
return val
}

// An "invalid array index" compiler error signifies that the constant values have changed.
Expand Down
11 changes: 8 additions & 3 deletions testdata/dayWithLinecomment.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ var _DayIndex = [...]uint8{0, 5, 12, 21, 29, 36, 44, 50}

const _DayLowerName = "lunestuesdaywednesdaythursdayviernessaturdaysunday"

func (i Day) String() string {
func (i Day) string() (string, error) {
if i < 0 || i >= Day(len(_DayIndex)-1) {
return fmt.Sprintf("Day(%d)", i)
return "", fmt.Errorf("value is out of enum range")
}
return _DayName[_DayIndex[i]:_DayIndex[i+1]]
return _DayName[_DayIndex[i]:_DayIndex[i+1]], nil
}

func (i Day) String() string {
val, _ := i.string()
return val
}

// An "invalid array index" compiler error signifies that the constant values have changed.
Expand Down
11 changes: 8 additions & 3 deletions testdata/dayWithPrefix.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ var _DayIndex = [...]uint8{0, 9, 19, 31, 42, 51, 62, 71}

const _DayLowerName = "daymondaydaytuesdaydaywednesdaydaythursdaydayfridaydaysaturdaydaysunday"

func (i Day) String() string {
func (i Day) string() (string, error) {
if i < 0 || i >= Day(len(_DayIndex)-1) {
return fmt.Sprintf("Day(%d)", i)
return "", fmt.Errorf("value is out of enum range")
}
return _DayName[_DayIndex[i]:_DayIndex[i+1]]
return _DayName[_DayIndex[i]:_DayIndex[i+1]], nil
}

func (i Day) String() string {
val, _ := i.string()
return val
}

// An "invalid array index" compiler error signifies that the constant values have changed.
Expand Down
10 changes: 5 additions & 5 deletions testdata/gap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ const (
)

func main() {
ck(0, "Gap(0)")
ck(1, "Gap(1)")
ck(0, "")
ck(1, "")
ck(Two, "Two")
ck(Three, "Three")
ck(4, "Gap(4)")
ck(4, "")
ck(Five, "Five")
ck(Six, "Six")
ck(Seven, "Seven")
ck(Eight, "Eight")
ck(Nine, "Nine")
ck(10, "Gap(10)")
ck(10, "")
ck(Eleven, "Eleven")
ck(12, "Gap(12)")
ck(12, "")
}

func ck(gap Gap, str string) {
Expand Down
2 changes: 1 addition & 1 deletion testdata/gap.golden
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (i Gap) String() string {
case i == 11:
return _GapName_2
default:
return fmt.Sprintf("Gap(%d)", i)
return ""
}
}

Expand Down
4 changes: 2 additions & 2 deletions testdata/num.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ const (
)

func main() {
ck(-3, "Num(-3)")
ck(-3, "")
ck(m_2, "m_2")
ck(m_1, "m_1")
ck(m0, "m0")
ck(m1, "m1")
ck(m2, "m2")
ck(3, "Num(3)")
ck(3, "")
}

func ck(num Num, str string) {
Expand Down
11 changes: 8 additions & 3 deletions testdata/num.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ var _NumIndex = [...]uint8{0, 3, 6, 8, 10, 12}

const _NumLowerName = "m_2m_1m0m1m2"

func (i Num) String() string {
func (i Num) string() (string, error) {
i -= -2
if i < 0 || i >= Num(len(_NumIndex)-1) {
return fmt.Sprintf("Num(%d)", i+-2)
return "", fmt.Errorf("value is out of enum range")
}
return _NumName[_NumIndex[i]:_NumIndex[i+1]]
return _NumName[_NumIndex[i]:_NumIndex[i+1]], nil
}

func (i Num) String() string {
val, _ := i.string()
return val
}

func (Num) Values() []string {
Expand Down
2 changes: 1 addition & 1 deletion testdata/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
ck(Two, "Two")
ck(Three, "Three")
ck(AnotherOne, "One")
ck(127, "Number(127)")
ck(127, "")
}

func ck(num Number, str string) {
Expand Down
11 changes: 8 additions & 3 deletions testdata/offset.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ var _NumberIndex = [...]uint8{0, 3, 6, 11}

const _NumberLowerName = "onetwothree"

func (i Number) String() string {
func (i Number) string() (string, error) {
i -= 1
if i < 0 || i >= Number(len(_NumberIndex)-1) {
return fmt.Sprintf("Number(%d)", i+1)
return "", fmt.Errorf("value is out of enum range")
}
return _NumberName[_NumberIndex[i]:_NumberIndex[i+1]]
return _NumberName[_NumberIndex[i]:_NumberIndex[i+1]], nil
}

func (i Number) String() string {
val, _ := i.string()
return val
}

func (Number) Values() []string {
Expand Down
8 changes: 4 additions & 4 deletions testdata/prime.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ const (
)

func main() {
ck(0, "Prime(0)")
ck(1, "Prime(1)")
ck(0, "")
ck(1, "")
ck(p2, "p2")
ck(p3, "p3")
ck(4, "Prime(4)")
ck(4, "")
ck(p5, "p5")
ck(p7, "p7")
ck(p77, "p7")
Expand All @@ -46,7 +46,7 @@ func main() {
ck(p37, "p37")
ck(p41, "p41")
ck(p43, "p43")
ck(44, "Prime(44)")
ck(44, "")
}

func ck(prime Prime, str string) {
Expand Down
Loading