forked from mirror/enumer
Merge branch 'master' of git://github.com/alvaroloes/enumer
This commit is contained in:
commit
276f4081b1
|
@ -85,7 +85,7 @@ name := MyTypeValue.String() // name => "MyTypeValue"
|
|||
```
|
||||
|
||||
Sometimes you need to use some other string representation format than CamelCase (i.e. in JSON).
|
||||
|
||||
|
||||
To transform it from CamelCase to snake_case or kebab-case, you can use the `transform` flag.
|
||||
|
||||
For example, the command `enumer -type=MyType -json -transform=snake` would generate the following string representation:
|
||||
|
@ -93,7 +93,7 @@ For example, the command `enumer -type=MyType -json -transform=snake` would gene
|
|||
```go
|
||||
name := MyTypeValue.String() // name => "my_type_value"
|
||||
```
|
||||
**Note**: The transformation only works form CamelCase to sanake_case or kebab-case, not the other way around.
|
||||
**Note**: The transformation only works form CamelCase to snake_case or kebab-case, not the other way around.
|
||||
|
||||
## How to use
|
||||
The usage of Enumer is the same as Stringer, so you can refer to the [Stringer docs](https://godoc.org/golang.org/x/tools/cmd/stringer)
|
||||
|
|
|
@ -33,7 +33,7 @@ func TestEndToEnd(t *testing.T) {
|
|||
defer os.RemoveAll(dir)
|
||||
// Create stringer in temporary directory.
|
||||
stringer := filepath.Join(dir, "stringer.exe")
|
||||
err = run("go", "build", "-o", stringer, "enumer.go", "sql.go", "stringer.go", "transformer.go", "trim.go")
|
||||
err = run("go", "build", "-o", stringer)
|
||||
if err != nil {
|
||||
t.Fatalf("building stringer: %s", err)
|
||||
}
|
||||
|
|
10
enumer.go
10
enumer.go
|
@ -4,8 +4,10 @@ import "fmt"
|
|||
|
||||
// Arguments to format are:
|
||||
// [1]: type name
|
||||
const stringValueToNameMap = `func %[1]sString(s string) (%[1]s, error) {
|
||||
if val, ok := _%[1]sNameToValue_map[s]; ok {
|
||||
const stringValueToNameMap = `// %[1]sString retrieves an enum value from the enum constants string name.
|
||||
// Throws an error if the param is not part of the enum.
|
||||
func %[1]sString(s string) (%[1]s, error) {
|
||||
if val, ok := _%[1]sNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%%s does not belong to %[1]s values", s)
|
||||
|
@ -14,7 +16,7 @@ const stringValueToNameMap = `func %[1]sString(s string) (%[1]s, error) {
|
|||
|
||||
func (g *Generator) buildValueToNameMap(runs [][]Value, typeName string, runsThreshold int) {
|
||||
// At this moment, either "g.declareIndexAndNameVars()" or "g.declareNameVars()" has been called
|
||||
g.Printf("\nvar _%sNameToValue_map = map[string]%s{\n", typeName, typeName)
|
||||
g.Printf("\nvar _%sNameToValueMap = map[string]%s{\n", typeName, typeName)
|
||||
thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold
|
||||
var n int
|
||||
var runID string
|
||||
|
@ -27,7 +29,7 @@ func (g *Generator) buildValueToNameMap(runs [][]Value, typeName string, runsThr
|
|||
}
|
||||
|
||||
for _, value := range values {
|
||||
g.Printf("\t_%s_name%s[%d:%d]: %s,\n", typeName, runID, n, n+len(value.name), &value)
|
||||
g.Printf("\t_%sName%s[%d:%d]: %s,\n", typeName, runID, n, n+len(value.name), &value)
|
||||
n += len(value.name)
|
||||
}
|
||||
}
|
||||
|
|
440
golden_test.go
440
golden_test.go
|
@ -62,29 +62,29 @@ const (
|
|||
`
|
||||
|
||||
const day_out = `
|
||||
const _Day_name = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday"
|
||||
const _DayName = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday"
|
||||
|
||||
var _Day_index = [...]uint8{0, 6, 13, 22, 30, 36, 44, 50}
|
||||
var _DayIndex = [...]uint8{0, 6, 13, 22, 30, 36, 44, 50}
|
||||
|
||||
func (i Day) String() string {
|
||||
if i < 0 || i >= Day(len(_Day_index)-1) {
|
||||
if i < 0 || i >= Day(len(_DayIndex)-1) {
|
||||
return fmt.Sprintf("Day(%d)", i)
|
||||
}
|
||||
return _Day_name[_Day_index[i]:_Day_index[i+1]]
|
||||
return _DayName[_DayIndex[i]:_DayIndex[i+1]]
|
||||
}
|
||||
|
||||
var _DayNameToValue_map = map[string]Day{
|
||||
_Day_name[0:6]: 0,
|
||||
_Day_name[6:13]: 1,
|
||||
_Day_name[13:22]: 2,
|
||||
_Day_name[22:30]: 3,
|
||||
_Day_name[30:36]: 4,
|
||||
_Day_name[36:44]: 5,
|
||||
_Day_name[44:50]: 6,
|
||||
var _DayNameToValueMap = map[string]Day{
|
||||
_DayName[0:6]: 0,
|
||||
_DayName[6:13]: 1,
|
||||
_DayName[13:22]: 2,
|
||||
_DayName[22:30]: 3,
|
||||
_DayName[30:36]: 4,
|
||||
_DayName[36:44]: 5,
|
||||
_DayName[44:50]: 6,
|
||||
}
|
||||
|
||||
func DayString(s string) (Day, error) {
|
||||
if val, ok := _DayNameToValue_map[s]; ok {
|
||||
if val, ok := _DayNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Day values", s)
|
||||
|
@ -104,26 +104,26 @@ const (
|
|||
`
|
||||
|
||||
const offset_out = `
|
||||
const _Number_name = "OneTwoThree"
|
||||
const _NumberName = "OneTwoThree"
|
||||
|
||||
var _Number_index = [...]uint8{0, 3, 6, 11}
|
||||
var _NumberIndex = [...]uint8{0, 3, 6, 11}
|
||||
|
||||
func (i Number) String() string {
|
||||
i -= 1
|
||||
if i < 0 || i >= Number(len(_Number_index)-1) {
|
||||
if i < 0 || i >= Number(len(_NumberIndex)-1) {
|
||||
return fmt.Sprintf("Number(%d)", i+1)
|
||||
}
|
||||
return _Number_name[_Number_index[i]:_Number_index[i+1]]
|
||||
return _NumberName[_NumberIndex[i]:_NumberIndex[i+1]]
|
||||
}
|
||||
|
||||
var _NumberNameToValue_map = map[string]Number{
|
||||
_Number_name[0:3]: 1,
|
||||
_Number_name[3:6]: 2,
|
||||
_Number_name[6:11]: 3,
|
||||
var _NumberNameToValueMap = map[string]Number{
|
||||
_NumberName[0:3]: 1,
|
||||
_NumberName[3:6]: 2,
|
||||
_NumberName[6:11]: 3,
|
||||
}
|
||||
|
||||
func NumberString(s string) (Number, error) {
|
||||
if val, ok := _NumberNameToValue_map[s]; ok {
|
||||
if val, ok := _NumberNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Number values", s)
|
||||
|
@ -146,45 +146,45 @@ const (
|
|||
|
||||
const gap_out = `
|
||||
const (
|
||||
_Gap_name_0 = "TwoThree"
|
||||
_Gap_name_1 = "FiveSixSevenEightNine"
|
||||
_Gap_name_2 = "Eleven"
|
||||
_GapName_0 = "TwoThree"
|
||||
_GapName_1 = "FiveSixSevenEightNine"
|
||||
_GapName_2 = "Eleven"
|
||||
)
|
||||
|
||||
var (
|
||||
_Gap_index_0 = [...]uint8{0, 3, 8}
|
||||
_Gap_index_1 = [...]uint8{0, 4, 7, 12, 17, 21}
|
||||
_Gap_index_2 = [...]uint8{0, 6}
|
||||
_GapIndex_0 = [...]uint8{0, 3, 8}
|
||||
_GapIndex_1 = [...]uint8{0, 4, 7, 12, 17, 21}
|
||||
_GapIndex_2 = [...]uint8{0, 6}
|
||||
)
|
||||
|
||||
func (i Gap) String() string {
|
||||
switch {
|
||||
case 2 <= i && i <= 3:
|
||||
i -= 2
|
||||
return _Gap_name_0[_Gap_index_0[i]:_Gap_index_0[i+1]]
|
||||
return _GapName_0[_GapIndex_0[i]:_GapIndex_0[i+1]]
|
||||
case 5 <= i && i <= 9:
|
||||
i -= 5
|
||||
return _Gap_name_1[_Gap_index_1[i]:_Gap_index_1[i+1]]
|
||||
return _GapName_1[_GapIndex_1[i]:_GapIndex_1[i+1]]
|
||||
case i == 11:
|
||||
return _Gap_name_2
|
||||
return _GapName_2
|
||||
default:
|
||||
return fmt.Sprintf("Gap(%d)", i)
|
||||
}
|
||||
}
|
||||
|
||||
var _GapNameToValue_map = map[string]Gap{
|
||||
_Gap_name_0[0:3]: 2,
|
||||
_Gap_name_0[3:8]: 3,
|
||||
_Gap_name_1[0:4]: 5,
|
||||
_Gap_name_1[4:7]: 6,
|
||||
_Gap_name_1[7:12]: 7,
|
||||
_Gap_name_1[12:17]: 8,
|
||||
_Gap_name_1[17:21]: 9,
|
||||
_Gap_name_2[0:6]: 11,
|
||||
var _GapNameToValueMap = map[string]Gap{
|
||||
_GapName_0[0:3]: 2,
|
||||
_GapName_0[3:8]: 3,
|
||||
_GapName_1[0:4]: 5,
|
||||
_GapName_1[4:7]: 6,
|
||||
_GapName_1[7:12]: 7,
|
||||
_GapName_1[12:17]: 8,
|
||||
_GapName_1[17:21]: 9,
|
||||
_GapName_2[0:6]: 11,
|
||||
}
|
||||
|
||||
func GapString(s string) (Gap, error) {
|
||||
if val, ok := _GapNameToValue_map[s]; ok {
|
||||
if val, ok := _GapNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Gap values", s)
|
||||
|
@ -203,28 +203,28 @@ const (
|
|||
`
|
||||
|
||||
const num_out = `
|
||||
const _Num_name = "m_2m_1m0m1m2"
|
||||
const _NumName = "m_2m_1m0m1m2"
|
||||
|
||||
var _Num_index = [...]uint8{0, 3, 6, 8, 10, 12}
|
||||
var _NumIndex = [...]uint8{0, 3, 6, 8, 10, 12}
|
||||
|
||||
func (i Num) String() string {
|
||||
i -= -2
|
||||
if i < 0 || i >= Num(len(_Num_index)-1) {
|
||||
if i < 0 || i >= Num(len(_NumIndex)-1) {
|
||||
return fmt.Sprintf("Num(%d)", i+-2)
|
||||
}
|
||||
return _Num_name[_Num_index[i]:_Num_index[i+1]]
|
||||
return _NumName[_NumIndex[i]:_NumIndex[i+1]]
|
||||
}
|
||||
|
||||
var _NumNameToValue_map = map[string]Num{
|
||||
_Num_name[0:3]: -2,
|
||||
_Num_name[3:6]: -1,
|
||||
_Num_name[6:8]: 0,
|
||||
_Num_name[8:10]: 1,
|
||||
_Num_name[10:12]: 2,
|
||||
var _NumNameToValueMap = map[string]Num{
|
||||
_NumName[0:3]: -2,
|
||||
_NumName[3:6]: -1,
|
||||
_NumName[6:8]: 0,
|
||||
_NumName[8:10]: 1,
|
||||
_NumName[10:12]: 2,
|
||||
}
|
||||
|
||||
func NumString(s string) (Num, error) {
|
||||
if val, ok := _NumNameToValue_map[s]; ok {
|
||||
if val, ok := _NumNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Num values", s)
|
||||
|
@ -247,37 +247,37 @@ const (
|
|||
|
||||
const unum_out = `
|
||||
const (
|
||||
_Unum_name_0 = "m0m1m2"
|
||||
_Unum_name_1 = "m_2m_1"
|
||||
_UnumName_0 = "m0m1m2"
|
||||
_UnumName_1 = "m_2m_1"
|
||||
)
|
||||
|
||||
var (
|
||||
_Unum_index_0 = [...]uint8{0, 2, 4, 6}
|
||||
_Unum_index_1 = [...]uint8{0, 3, 6}
|
||||
_UnumIndex_0 = [...]uint8{0, 2, 4, 6}
|
||||
_UnumIndex_1 = [...]uint8{0, 3, 6}
|
||||
)
|
||||
|
||||
func (i Unum) String() string {
|
||||
switch {
|
||||
case 0 <= i && i <= 2:
|
||||
return _Unum_name_0[_Unum_index_0[i]:_Unum_index_0[i+1]]
|
||||
return _UnumName_0[_UnumIndex_0[i]:_UnumIndex_0[i+1]]
|
||||
case 253 <= i && i <= 254:
|
||||
i -= 253
|
||||
return _Unum_name_1[_Unum_index_1[i]:_Unum_index_1[i+1]]
|
||||
return _UnumName_1[_UnumIndex_1[i]:_UnumIndex_1[i+1]]
|
||||
default:
|
||||
return fmt.Sprintf("Unum(%d)", i)
|
||||
}
|
||||
}
|
||||
|
||||
var _UnumNameToValue_map = map[string]Unum{
|
||||
_Unum_name_0[0:2]: 0,
|
||||
_Unum_name_0[2:4]: 1,
|
||||
_Unum_name_0[4:6]: 2,
|
||||
_Unum_name_1[0:3]: 253,
|
||||
_Unum_name_1[3:6]: 254,
|
||||
var _UnumNameToValueMap = map[string]Unum{
|
||||
_UnumName_0[0:2]: 0,
|
||||
_UnumName_0[2:4]: 1,
|
||||
_UnumName_0[4:6]: 2,
|
||||
_UnumName_1[0:3]: 253,
|
||||
_UnumName_1[3:6]: 254,
|
||||
}
|
||||
|
||||
func UnumString(s string) (Unum, error) {
|
||||
if val, ok := _UnumNameToValue_map[s]; ok {
|
||||
if val, ok := _UnumNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Unum values", s)
|
||||
|
@ -306,49 +306,49 @@ const (
|
|||
`
|
||||
|
||||
const prime_out = `
|
||||
const _Prime_name = "p2p3p5p7p11p13p17p19p23p29p37p41p43"
|
||||
const _PrimeName = "p2p3p5p7p11p13p17p19p23p29p37p41p43"
|
||||
|
||||
var _Prime_map = map[Prime]string{
|
||||
2: _Prime_name[0:2],
|
||||
3: _Prime_name[2:4],
|
||||
5: _Prime_name[4:6],
|
||||
7: _Prime_name[6:8],
|
||||
11: _Prime_name[8:11],
|
||||
13: _Prime_name[11:14],
|
||||
17: _Prime_name[14:17],
|
||||
19: _Prime_name[17:20],
|
||||
23: _Prime_name[20:23],
|
||||
29: _Prime_name[23:26],
|
||||
31: _Prime_name[26:29],
|
||||
41: _Prime_name[29:32],
|
||||
43: _Prime_name[32:35],
|
||||
var _PrimeMap = map[Prime]string{
|
||||
2: _PrimeName[0:2],
|
||||
3: _PrimeName[2:4],
|
||||
5: _PrimeName[4:6],
|
||||
7: _PrimeName[6:8],
|
||||
11: _PrimeName[8:11],
|
||||
13: _PrimeName[11:14],
|
||||
17: _PrimeName[14:17],
|
||||
19: _PrimeName[17:20],
|
||||
23: _PrimeName[20:23],
|
||||
29: _PrimeName[23:26],
|
||||
31: _PrimeName[26:29],
|
||||
41: _PrimeName[29:32],
|
||||
43: _PrimeName[32:35],
|
||||
}
|
||||
|
||||
func (i Prime) String() string {
|
||||
if str, ok := _Prime_map[i]; ok {
|
||||
if str, ok := _PrimeMap[i]; ok {
|
||||
return str
|
||||
}
|
||||
return fmt.Sprintf("Prime(%d)", i)
|
||||
}
|
||||
|
||||
var _PrimeNameToValue_map = map[string]Prime{
|
||||
_Prime_name[0:2]: 2,
|
||||
_Prime_name[2:4]: 3,
|
||||
_Prime_name[4:6]: 5,
|
||||
_Prime_name[6:8]: 7,
|
||||
_Prime_name[8:11]: 11,
|
||||
_Prime_name[11:14]: 13,
|
||||
_Prime_name[14:17]: 17,
|
||||
_Prime_name[17:20]: 19,
|
||||
_Prime_name[20:23]: 23,
|
||||
_Prime_name[23:26]: 29,
|
||||
_Prime_name[26:29]: 31,
|
||||
_Prime_name[29:32]: 41,
|
||||
_Prime_name[32:35]: 43,
|
||||
var _PrimeNameToValueMap = map[string]Prime{
|
||||
_PrimeName[0:2]: 2,
|
||||
_PrimeName[2:4]: 3,
|
||||
_PrimeName[4:6]: 5,
|
||||
_PrimeName[6:8]: 7,
|
||||
_PrimeName[8:11]: 11,
|
||||
_PrimeName[11:14]: 13,
|
||||
_PrimeName[14:17]: 17,
|
||||
_PrimeName[17:20]: 19,
|
||||
_PrimeName[20:23]: 23,
|
||||
_PrimeName[23:26]: 29,
|
||||
_PrimeName[26:29]: 31,
|
||||
_PrimeName[29:32]: 41,
|
||||
_PrimeName[32:35]: 43,
|
||||
}
|
||||
|
||||
func PrimeString(s string) (Prime, error) {
|
||||
if val, ok := _PrimeNameToValue_map[s]; ok {
|
||||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||
|
@ -374,49 +374,49 @@ const (
|
|||
`
|
||||
|
||||
const prime_json_out = `
|
||||
const _Prime_name = "p2p3p5p7p11p13p17p19p23p29p37p41p43"
|
||||
const _PrimeName = "p2p3p5p7p11p13p17p19p23p29p37p41p43"
|
||||
|
||||
var _Prime_map = map[Prime]string{
|
||||
2: _Prime_name[0:2],
|
||||
3: _Prime_name[2:4],
|
||||
5: _Prime_name[4:6],
|
||||
7: _Prime_name[6:8],
|
||||
11: _Prime_name[8:11],
|
||||
13: _Prime_name[11:14],
|
||||
17: _Prime_name[14:17],
|
||||
19: _Prime_name[17:20],
|
||||
23: _Prime_name[20:23],
|
||||
29: _Prime_name[23:26],
|
||||
31: _Prime_name[26:29],
|
||||
41: _Prime_name[29:32],
|
||||
43: _Prime_name[32:35],
|
||||
var _PrimeMap = map[Prime]string{
|
||||
2: _PrimeName[0:2],
|
||||
3: _PrimeName[2:4],
|
||||
5: _PrimeName[4:6],
|
||||
7: _PrimeName[6:8],
|
||||
11: _PrimeName[8:11],
|
||||
13: _PrimeName[11:14],
|
||||
17: _PrimeName[14:17],
|
||||
19: _PrimeName[17:20],
|
||||
23: _PrimeName[20:23],
|
||||
29: _PrimeName[23:26],
|
||||
31: _PrimeName[26:29],
|
||||
41: _PrimeName[29:32],
|
||||
43: _PrimeName[32:35],
|
||||
}
|
||||
|
||||
func (i Prime) String() string {
|
||||
if str, ok := _Prime_map[i]; ok {
|
||||
if str, ok := _PrimeMap[i]; ok {
|
||||
return str
|
||||
}
|
||||
return fmt.Sprintf("Prime(%d)", i)
|
||||
}
|
||||
|
||||
var _PrimeNameToValue_map = map[string]Prime{
|
||||
_Prime_name[0:2]: 2,
|
||||
_Prime_name[2:4]: 3,
|
||||
_Prime_name[4:6]: 5,
|
||||
_Prime_name[6:8]: 7,
|
||||
_Prime_name[8:11]: 11,
|
||||
_Prime_name[11:14]: 13,
|
||||
_Prime_name[14:17]: 17,
|
||||
_Prime_name[17:20]: 19,
|
||||
_Prime_name[20:23]: 23,
|
||||
_Prime_name[23:26]: 29,
|
||||
_Prime_name[26:29]: 31,
|
||||
_Prime_name[29:32]: 41,
|
||||
_Prime_name[32:35]: 43,
|
||||
var _PrimeNameToValueMap = map[string]Prime{
|
||||
_PrimeName[0:2]: 2,
|
||||
_PrimeName[2:4]: 3,
|
||||
_PrimeName[4:6]: 5,
|
||||
_PrimeName[6:8]: 7,
|
||||
_PrimeName[8:11]: 11,
|
||||
_PrimeName[11:14]: 13,
|
||||
_PrimeName[14:17]: 17,
|
||||
_PrimeName[17:20]: 19,
|
||||
_PrimeName[20:23]: 23,
|
||||
_PrimeName[23:26]: 29,
|
||||
_PrimeName[26:29]: 31,
|
||||
_PrimeName[29:32]: 41,
|
||||
_PrimeName[32:35]: 43,
|
||||
}
|
||||
|
||||
func PrimeString(s string) (Prime, error) {
|
||||
if val, ok := _PrimeNameToValue_map[s]; ok {
|
||||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||
|
@ -458,49 +458,49 @@ const (
|
|||
`
|
||||
|
||||
const prime_yaml_out = `
|
||||
const _Prime_name = "p2p3p5p7p11p13p17p19p23p29p37p41p43"
|
||||
const _PrimeName = "p2p3p5p7p11p13p17p19p23p29p37p41p43"
|
||||
|
||||
var _Prime_map = map[Prime]string{
|
||||
2: _Prime_name[0:2],
|
||||
3: _Prime_name[2:4],
|
||||
5: _Prime_name[4:6],
|
||||
7: _Prime_name[6:8],
|
||||
11: _Prime_name[8:11],
|
||||
13: _Prime_name[11:14],
|
||||
17: _Prime_name[14:17],
|
||||
19: _Prime_name[17:20],
|
||||
23: _Prime_name[20:23],
|
||||
29: _Prime_name[23:26],
|
||||
31: _Prime_name[26:29],
|
||||
41: _Prime_name[29:32],
|
||||
43: _Prime_name[32:35],
|
||||
var _PrimeMap = map[Prime]string{
|
||||
2: _PrimeName[0:2],
|
||||
3: _PrimeName[2:4],
|
||||
5: _PrimeName[4:6],
|
||||
7: _PrimeName[6:8],
|
||||
11: _PrimeName[8:11],
|
||||
13: _PrimeName[11:14],
|
||||
17: _PrimeName[14:17],
|
||||
19: _PrimeName[17:20],
|
||||
23: _PrimeName[20:23],
|
||||
29: _PrimeName[23:26],
|
||||
31: _PrimeName[26:29],
|
||||
41: _PrimeName[29:32],
|
||||
43: _PrimeName[32:35],
|
||||
}
|
||||
|
||||
func (i Prime) String() string {
|
||||
if str, ok := _Prime_map[i]; ok {
|
||||
if str, ok := _PrimeMap[i]; ok {
|
||||
return str
|
||||
}
|
||||
return fmt.Sprintf("Prime(%d)", i)
|
||||
}
|
||||
|
||||
var _PrimeNameToValue_map = map[string]Prime{
|
||||
_Prime_name[0:2]: 2,
|
||||
_Prime_name[2:4]: 3,
|
||||
_Prime_name[4:6]: 5,
|
||||
_Prime_name[6:8]: 7,
|
||||
_Prime_name[8:11]: 11,
|
||||
_Prime_name[11:14]: 13,
|
||||
_Prime_name[14:17]: 17,
|
||||
_Prime_name[17:20]: 19,
|
||||
_Prime_name[20:23]: 23,
|
||||
_Prime_name[23:26]: 29,
|
||||
_Prime_name[26:29]: 31,
|
||||
_Prime_name[29:32]: 41,
|
||||
_Prime_name[32:35]: 43,
|
||||
var _PrimeNameToValueMap = map[string]Prime{
|
||||
_PrimeName[0:2]: 2,
|
||||
_PrimeName[2:4]: 3,
|
||||
_PrimeName[4:6]: 5,
|
||||
_PrimeName[6:8]: 7,
|
||||
_PrimeName[8:11]: 11,
|
||||
_PrimeName[11:14]: 13,
|
||||
_PrimeName[14:17]: 17,
|
||||
_PrimeName[17:20]: 19,
|
||||
_PrimeName[20:23]: 23,
|
||||
_PrimeName[23:26]: 29,
|
||||
_PrimeName[26:29]: 31,
|
||||
_PrimeName[29:32]: 41,
|
||||
_PrimeName[32:35]: 43,
|
||||
}
|
||||
|
||||
func PrimeString(s string) (Prime, error) {
|
||||
if val, ok := _PrimeNameToValue_map[s]; ok {
|
||||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||
|
@ -542,49 +542,49 @@ const (
|
|||
`
|
||||
|
||||
const prime_sql_out = `
|
||||
const _Prime_name = "p2p3p5p7p11p13p17p19p23p29p37p41p43"
|
||||
const _PrimeName = "p2p3p5p7p11p13p17p19p23p29p37p41p43"
|
||||
|
||||
var _Prime_map = map[Prime]string{
|
||||
2: _Prime_name[0:2],
|
||||
3: _Prime_name[2:4],
|
||||
5: _Prime_name[4:6],
|
||||
7: _Prime_name[6:8],
|
||||
11: _Prime_name[8:11],
|
||||
13: _Prime_name[11:14],
|
||||
17: _Prime_name[14:17],
|
||||
19: _Prime_name[17:20],
|
||||
23: _Prime_name[20:23],
|
||||
29: _Prime_name[23:26],
|
||||
31: _Prime_name[26:29],
|
||||
41: _Prime_name[29:32],
|
||||
43: _Prime_name[32:35],
|
||||
var _PrimeMap = map[Prime]string{
|
||||
2: _PrimeName[0:2],
|
||||
3: _PrimeName[2:4],
|
||||
5: _PrimeName[4:6],
|
||||
7: _PrimeName[6:8],
|
||||
11: _PrimeName[8:11],
|
||||
13: _PrimeName[11:14],
|
||||
17: _PrimeName[14:17],
|
||||
19: _PrimeName[17:20],
|
||||
23: _PrimeName[20:23],
|
||||
29: _PrimeName[23:26],
|
||||
31: _PrimeName[26:29],
|
||||
41: _PrimeName[29:32],
|
||||
43: _PrimeName[32:35],
|
||||
}
|
||||
|
||||
func (i Prime) String() string {
|
||||
if str, ok := _Prime_map[i]; ok {
|
||||
if str, ok := _PrimeMap[i]; ok {
|
||||
return str
|
||||
}
|
||||
return fmt.Sprintf("Prime(%d)", i)
|
||||
}
|
||||
|
||||
var _PrimeNameToValue_map = map[string]Prime{
|
||||
_Prime_name[0:2]: 2,
|
||||
_Prime_name[2:4]: 3,
|
||||
_Prime_name[4:6]: 5,
|
||||
_Prime_name[6:8]: 7,
|
||||
_Prime_name[8:11]: 11,
|
||||
_Prime_name[11:14]: 13,
|
||||
_Prime_name[14:17]: 17,
|
||||
_Prime_name[17:20]: 19,
|
||||
_Prime_name[20:23]: 23,
|
||||
_Prime_name[23:26]: 29,
|
||||
_Prime_name[26:29]: 31,
|
||||
_Prime_name[29:32]: 41,
|
||||
_Prime_name[32:35]: 43,
|
||||
var _PrimeNameToValueMap = map[string]Prime{
|
||||
_PrimeName[0:2]: 2,
|
||||
_PrimeName[2:4]: 3,
|
||||
_PrimeName[4:6]: 5,
|
||||
_PrimeName[6:8]: 7,
|
||||
_PrimeName[8:11]: 11,
|
||||
_PrimeName[11:14]: 13,
|
||||
_PrimeName[14:17]: 17,
|
||||
_PrimeName[17:20]: 19,
|
||||
_PrimeName[20:23]: 23,
|
||||
_PrimeName[23:26]: 29,
|
||||
_PrimeName[26:29]: 31,
|
||||
_PrimeName[29:32]: 41,
|
||||
_PrimeName[32:35]: 43,
|
||||
}
|
||||
|
||||
func PrimeString(s string) (Prime, error) {
|
||||
if val, ok := _PrimeNameToValue_map[s]; ok {
|
||||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||
|
@ -639,49 +639,49 @@ const (
|
|||
`
|
||||
|
||||
const prime_json_and_sql_out = `
|
||||
const _Prime_name = "p2p3p5p7p11p13p17p19p23p29p37p41p43"
|
||||
const _PrimeName = "p2p3p5p7p11p13p17p19p23p29p37p41p43"
|
||||
|
||||
var _Prime_map = map[Prime]string{
|
||||
2: _Prime_name[0:2],
|
||||
3: _Prime_name[2:4],
|
||||
5: _Prime_name[4:6],
|
||||
7: _Prime_name[6:8],
|
||||
11: _Prime_name[8:11],
|
||||
13: _Prime_name[11:14],
|
||||
17: _Prime_name[14:17],
|
||||
19: _Prime_name[17:20],
|
||||
23: _Prime_name[20:23],
|
||||
29: _Prime_name[23:26],
|
||||
31: _Prime_name[26:29],
|
||||
41: _Prime_name[29:32],
|
||||
43: _Prime_name[32:35],
|
||||
var _PrimeMap = map[Prime]string{
|
||||
2: _PrimeName[0:2],
|
||||
3: _PrimeName[2:4],
|
||||
5: _PrimeName[4:6],
|
||||
7: _PrimeName[6:8],
|
||||
11: _PrimeName[8:11],
|
||||
13: _PrimeName[11:14],
|
||||
17: _PrimeName[14:17],
|
||||
19: _PrimeName[17:20],
|
||||
23: _PrimeName[20:23],
|
||||
29: _PrimeName[23:26],
|
||||
31: _PrimeName[26:29],
|
||||
41: _PrimeName[29:32],
|
||||
43: _PrimeName[32:35],
|
||||
}
|
||||
|
||||
func (i Prime) String() string {
|
||||
if str, ok := _Prime_map[i]; ok {
|
||||
if str, ok := _PrimeMap[i]; ok {
|
||||
return str
|
||||
}
|
||||
return fmt.Sprintf("Prime(%d)", i)
|
||||
}
|
||||
|
||||
var _PrimeNameToValue_map = map[string]Prime{
|
||||
_Prime_name[0:2]: 2,
|
||||
_Prime_name[2:4]: 3,
|
||||
_Prime_name[4:6]: 5,
|
||||
_Prime_name[6:8]: 7,
|
||||
_Prime_name[8:11]: 11,
|
||||
_Prime_name[11:14]: 13,
|
||||
_Prime_name[14:17]: 17,
|
||||
_Prime_name[17:20]: 19,
|
||||
_Prime_name[20:23]: 23,
|
||||
_Prime_name[23:26]: 29,
|
||||
_Prime_name[26:29]: 31,
|
||||
_Prime_name[29:32]: 41,
|
||||
_Prime_name[32:35]: 43,
|
||||
var _PrimeNameToValueMap = map[string]Prime{
|
||||
_PrimeName[0:2]: 2,
|
||||
_PrimeName[2:4]: 3,
|
||||
_PrimeName[4:6]: 5,
|
||||
_PrimeName[6:8]: 7,
|
||||
_PrimeName[8:11]: 11,
|
||||
_PrimeName[11:14]: 13,
|
||||
_PrimeName[14:17]: 17,
|
||||
_PrimeName[17:20]: 19,
|
||||
_PrimeName[20:23]: 23,
|
||||
_PrimeName[23:26]: 29,
|
||||
_PrimeName[26:29]: 31,
|
||||
_PrimeName[29:32]: 41,
|
||||
_PrimeName[32:35]: 43,
|
||||
}
|
||||
|
||||
func PrimeString(s string) (Prime, error) {
|
||||
if val, ok := _PrimeNameToValue_map[s]; ok {
|
||||
if val, ok := _PrimeNameToValueMap[s]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return 0, fmt.Errorf("%s does not belong to Prime values", s)
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
// Copyright 2017 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build !go1.9
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"go/importer"
|
||||
"go/types"
|
||||
)
|
||||
|
||||
func defaultImporter() types.Importer {
|
||||
return importer.Default()
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
// Copyright 2017 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build go1.9
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"go/importer"
|
||||
"go/types"
|
||||
)
|
||||
|
||||
func defaultImporter() types.Importer {
|
||||
return importer.For("source", nil)
|
||||
}
|
42
stringer.go
42
stringer.go
|
@ -58,6 +58,11 @@
|
|||
// where t is the lower-cased name of the first type listed. It can be overridden
|
||||
// with the -output flag.
|
||||
//
|
||||
// ------
|
||||
// This is a patched versin of the original stringer. The original generates source code
|
||||
// using '_' (underscore) as part of generated golang identifiers. This creates a number of
|
||||
// golint warnings. The patched version simply leaves the '_' out
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -68,7 +73,6 @@ import (
|
|||
"go/build"
|
||||
exact "go/constant"
|
||||
"go/format"
|
||||
"go/importer"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"go/types"
|
||||
|
@ -78,6 +82,8 @@ import (
|
|||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/pascaldekloe/name"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -275,7 +281,7 @@ func (g *Generator) parsePackage(directory string, names []string, text interfac
|
|||
// check type-checks the package. The package must be OK to proceed.
|
||||
func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) {
|
||||
pkg.defs = make(map[*ast.Ident]types.Object)
|
||||
config := types.Config{Importer: importer.Default(), FakeImportC: true}
|
||||
config := types.Config{Importer: defaultImporter(), FakeImportC: true}
|
||||
info := &types.Info{
|
||||
Defs: pkg.defs,
|
||||
}
|
||||
|
@ -287,18 +293,18 @@ func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) {
|
|||
}
|
||||
|
||||
func (g *Generator) transformValueNames(values []Value, transformMethod string) {
|
||||
var transform func(string) string
|
||||
var sep rune
|
||||
switch transformMethod {
|
||||
case "snake":
|
||||
transform = toSnakeCase
|
||||
sep = '_'
|
||||
case "kebab":
|
||||
transform = toKebabCase
|
||||
sep = '-'
|
||||
default:
|
||||
return
|
||||
}
|
||||
|
||||
for i := range values {
|
||||
values[i].name = transform(values[i].name)
|
||||
values[i].name = strings.ToLower(name.Delimit(values[i].name, sep))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -567,10 +573,10 @@ func (g *Generator) createIndexAndNameDecl(run []Value, typeName string, suffix
|
|||
b.WriteString(run[i].name)
|
||||
indexes[i] = b.Len()
|
||||
}
|
||||
nameConst := fmt.Sprintf("_%s_name%s = %q", typeName, suffix, b.String())
|
||||
nameConst := fmt.Sprintf("_%sName%s = %q", typeName, suffix, b.String())
|
||||
nameLen := b.Len()
|
||||
b.Reset()
|
||||
fmt.Fprintf(b, "_%s_index%s = [...]uint%d{0, ", typeName, suffix, usize(nameLen))
|
||||
fmt.Fprintf(b, "_%sIndex%s = [...]uint%d{0, ", typeName, suffix, usize(nameLen))
|
||||
for i, v := range indexes {
|
||||
if i > 0 {
|
||||
fmt.Fprintf(b, ", ")
|
||||
|
@ -583,7 +589,7 @@ func (g *Generator) createIndexAndNameDecl(run []Value, typeName string, suffix
|
|||
|
||||
// declareNameVars declares the concatenated names string representing all the values in the runs.
|
||||
func (g *Generator) declareNameVars(runs [][]Value, typeName string, suffix string) {
|
||||
g.Printf("const _%s_name%s = \"", typeName, suffix)
|
||||
g.Printf("const _%sName%s = \"", typeName, suffix)
|
||||
for _, run := range runs {
|
||||
for i := range run {
|
||||
g.Printf("%s", run[i].name)
|
||||
|
@ -614,10 +620,10 @@ func (g *Generator) buildOneRun(runs [][]Value, typeName string) {
|
|||
// [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 {
|
||||
if %[3]si >= %[1]s(len(_%[1]s_index)-1) {
|
||||
if %[3]si >= %[1]s(len(_%[1]sIndex)-1) {
|
||||
return fmt.Sprintf("%[1]s(%%d)", i)
|
||||
}
|
||||
return _%[1]s_name[_%[1]s_index[i]:_%[1]s_index[i+1]]
|
||||
return _%[1]sName[_%[1]sIndex[i]:_%[1]sIndex[i+1]]
|
||||
}
|
||||
`
|
||||
|
||||
|
@ -630,10 +636,10 @@ const stringOneRun = `func (i %[1]s) String() string {
|
|||
*/
|
||||
const stringOneRunWithOffset = `func (i %[1]s) String() string {
|
||||
i -= %[2]s
|
||||
if %[4]si >= %[1]s(len(_%[1]s_index)-1) {
|
||||
if %[4]si >= %[1]s(len(_%[1]sIndex)-1) {
|
||||
return fmt.Sprintf("%[1]s(%%d)", i + %[2]s)
|
||||
}
|
||||
return _%[1]s_name[_%[1]s_index[i] : _%[1]s_index[i+1]]
|
||||
return _%[1]sName[_%[1]sIndex[i] : _%[1]sIndex[i+1]]
|
||||
}
|
||||
`
|
||||
|
||||
|
@ -647,14 +653,14 @@ func (g *Generator) buildMultipleRuns(runs [][]Value, typeName string) {
|
|||
for i, values := range runs {
|
||||
if len(values) == 1 {
|
||||
g.Printf("\tcase i == %s:\n", &values[0])
|
||||
g.Printf("\t\treturn _%s_name_%d\n", typeName, i)
|
||||
g.Printf("\t\treturn _%sName_%d\n", typeName, i)
|
||||
continue
|
||||
}
|
||||
g.Printf("\tcase %s <= i && i <= %s:\n", &values[0], &values[len(values)-1])
|
||||
if values[0].value != 0 {
|
||||
g.Printf("\t\ti -= %s\n", &values[0])
|
||||
}
|
||||
g.Printf("\t\treturn _%s_name_%d[_%s_index_%d[i]:_%s_index_%d[i+1]]\n",
|
||||
g.Printf("\t\treturn _%sName_%d[_%sIndex_%d[i]:_%sIndex_%d[i+1]]\n",
|
||||
typeName, i, typeName, i, typeName, i)
|
||||
}
|
||||
g.Printf("\tdefault:\n")
|
||||
|
@ -668,11 +674,11 @@ func (g *Generator) buildMultipleRuns(runs [][]Value, typeName string) {
|
|||
func (g *Generator) buildMap(runs [][]Value, typeName string) {
|
||||
g.Printf("\n")
|
||||
g.declareNameVars(runs, typeName, "")
|
||||
g.Printf("\nvar _%s_map = map[%s]string{\n", typeName, typeName)
|
||||
g.Printf("\nvar _%sMap = map[%s]string{\n", typeName, typeName)
|
||||
n := 0
|
||||
for _, values := range runs {
|
||||
for _, value := range values {
|
||||
g.Printf("\t%s: _%s_name[%d:%d],\n", &value, typeName, n, n+len(value.name))
|
||||
g.Printf("\t%s: _%sName[%d:%d],\n", &value, typeName, n, n+len(value.name))
|
||||
n += len(value.name)
|
||||
}
|
||||
}
|
||||
|
@ -682,7 +688,7 @@ func (g *Generator) buildMap(runs [][]Value, typeName string) {
|
|||
|
||||
// Argument to format is the type name.
|
||||
const stringMap = `func (i %[1]s) String() string {
|
||||
if str, ok := _%[1]s_map[i]; ok {
|
||||
if str, ok := _%[1]sMap[i]; ok {
|
||||
return str
|
||||
}
|
||||
return fmt.Sprintf("%[1]s(%%d)", i)
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/camelcase"
|
||||
)
|
||||
|
||||
func transform(src, delim string) string {
|
||||
entries := camelcase.Split(src)
|
||||
if len(entries) <= 1 {
|
||||
return strings.ToLower(src)
|
||||
}
|
||||
|
||||
result := strings.ToLower(entries[0])
|
||||
for i := 1; i < len(entries); i++ {
|
||||
result += delim + strings.ToLower(entries[i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func toSnakeCase(src string) string {
|
||||
return transform(src, "_")
|
||||
}
|
||||
|
||||
func toKebabCase(src string) string {
|
||||
return transform(src, "-")
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTransform(t *testing.T) {
|
||||
result := transform("CamelCaseStringValue", ".")
|
||||
|
||||
const expected = "camel.case.string.value"
|
||||
if result != expected {
|
||||
t.Errorf("\ngot: %s\n====\nexpected: %s", result, expected)
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
language: go
|
||||
go: 1.4
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Fatih Arslan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -1,58 +0,0 @@
|
|||
# CamelCase [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/fatih/camelcase) [![Build Status](http://img.shields.io/travis/fatih/camelcase.svg?style=flat-square)](https://travis-ci.org/fatih/camelcase)
|
||||
|
||||
CamelCase is a Golang (Go) package to split the words of a camelcase type
|
||||
string into a slice of words. It can be used to convert a camelcase word (lower
|
||||
or upper case) into any type of word.
|
||||
|
||||
## Splitting rules:
|
||||
|
||||
1. If string is not valid UTF-8, return it without splitting as
|
||||
single item array.
|
||||
2. Assign all unicode characters into one of 4 sets: lower case
|
||||
letters, upper case letters, numbers, and all other characters.
|
||||
3. Iterate through characters of string, introducing splits
|
||||
between adjacent characters that belong to different sets.
|
||||
4. Iterate through array of split strings, and if a given string
|
||||
is upper case:
|
||||
* if subsequent string is lower case:
|
||||
* move last character of upper case string to beginning of
|
||||
lower case string
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
go get github.com/fatih/camelcase
|
||||
```
|
||||
|
||||
## Usage and examples
|
||||
|
||||
```go
|
||||
splitted := camelcase.Split("GolangPackage")
|
||||
|
||||
fmt.Println(splitted[0], splitted[1]) // prints: "Golang", "Package"
|
||||
```
|
||||
|
||||
Both lower camel case and upper camel case are supported. For more info please
|
||||
check: [http://en.wikipedia.org/wiki/CamelCase](http://en.wikipedia.org/wiki/CamelCase)
|
||||
|
||||
Below are some example cases:
|
||||
|
||||
```
|
||||
"" => []
|
||||
"lowercase" => ["lowercase"]
|
||||
"Class" => ["Class"]
|
||||
"MyClass" => ["My", "Class"]
|
||||
"MyC" => ["My", "C"]
|
||||
"HTML" => ["HTML"]
|
||||
"PDFLoader" => ["PDF", "Loader"]
|
||||
"AString" => ["A", "String"]
|
||||
"SimpleXMLParser" => ["Simple", "XML", "Parser"]
|
||||
"vimRPCPlugin" => ["vim", "RPC", "Plugin"]
|
||||
"GL11Version" => ["GL", "11", "Version"]
|
||||
"99Bottles" => ["99", "Bottles"]
|
||||
"May5" => ["May", "5"]
|
||||
"BFG9000" => ["BFG", "9000"]
|
||||
"BöseÜberraschung" => ["Böse", "Überraschung"]
|
||||
"Two spaces" => ["Two", " ", "spaces"]
|
||||
"BadUTF8\xe2\xe2\xa1" => ["BadUTF8\xe2\xe2\xa1"]
|
||||
```
|
|
@ -1,90 +0,0 @@
|
|||
// Package camelcase is a micro package to split the words of a camelcase type
|
||||
// string into a slice of words.
|
||||
package camelcase
|
||||
|
||||
import (
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Split splits the camelcase word and returns a list of words. It also
|
||||
// supports digits. Both lower camel case and upper camel case are supported.
|
||||
// For more info please check: http://en.wikipedia.org/wiki/CamelCase
|
||||
//
|
||||
// Examples
|
||||
//
|
||||
// "" => [""]
|
||||
// "lowercase" => ["lowercase"]
|
||||
// "Class" => ["Class"]
|
||||
// "MyClass" => ["My", "Class"]
|
||||
// "MyC" => ["My", "C"]
|
||||
// "HTML" => ["HTML"]
|
||||
// "PDFLoader" => ["PDF", "Loader"]
|
||||
// "AString" => ["A", "String"]
|
||||
// "SimpleXMLParser" => ["Simple", "XML", "Parser"]
|
||||
// "vimRPCPlugin" => ["vim", "RPC", "Plugin"]
|
||||
// "GL11Version" => ["GL", "11", "Version"]
|
||||
// "99Bottles" => ["99", "Bottles"]
|
||||
// "May5" => ["May", "5"]
|
||||
// "BFG9000" => ["BFG", "9000"]
|
||||
// "BöseÜberraschung" => ["Böse", "Überraschung"]
|
||||
// "Two spaces" => ["Two", " ", "spaces"]
|
||||
// "BadUTF8\xe2\xe2\xa1" => ["BadUTF8\xe2\xe2\xa1"]
|
||||
//
|
||||
// Splitting rules
|
||||
//
|
||||
// 1) If string is not valid UTF-8, return it without splitting as
|
||||
// single item array.
|
||||
// 2) Assign all unicode characters into one of 4 sets: lower case
|
||||
// letters, upper case letters, numbers, and all other characters.
|
||||
// 3) Iterate through characters of string, introducing splits
|
||||
// between adjacent characters that belong to different sets.
|
||||
// 4) Iterate through array of split strings, and if a given string
|
||||
// is upper case:
|
||||
// if subsequent string is lower case:
|
||||
// move last character of upper case string to beginning of
|
||||
// lower case string
|
||||
func Split(src string) (entries []string) {
|
||||
// don't split invalid utf8
|
||||
if !utf8.ValidString(src) {
|
||||
return []string{src}
|
||||
}
|
||||
entries = []string{}
|
||||
var runes [][]rune
|
||||
lastClass := 0
|
||||
class := 0
|
||||
// split into fields based on class of unicode character
|
||||
for _, r := range src {
|
||||
switch true {
|
||||
case unicode.IsLower(r):
|
||||
class = 1
|
||||
case unicode.IsUpper(r):
|
||||
class = 2
|
||||
case unicode.IsDigit(r):
|
||||
class = 3
|
||||
default:
|
||||
class = 4
|
||||
}
|
||||
if class == lastClass {
|
||||
runes[len(runes)-1] = append(runes[len(runes)-1], r)
|
||||
} else {
|
||||
runes = append(runes, []rune{r})
|
||||
}
|
||||
lastClass = class
|
||||
}
|
||||
// handle upper case -> lower case sequences, e.g.
|
||||
// "PDFL", "oader" -> "PDF", "Loader"
|
||||
for i := 0; i < len(runes)-1; i++ {
|
||||
if unicode.IsUpper(runes[i][0]) && unicode.IsLower(runes[i+1][0]) {
|
||||
runes[i+1] = append([]rune{runes[i][len(runes[i])-1]}, runes[i+1]...)
|
||||
runes[i] = runes[i][:len(runes[i])-1]
|
||||
}
|
||||
}
|
||||
// construct []string from results
|
||||
for _, s := range runes {
|
||||
if len(s) > 0 {
|
||||
entries = append(entries, string(s))
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
package camelcase
|
||||
|
||||
import "fmt"
|
||||
|
||||
func ExampleSplit() {
|
||||
|
||||
for _, c := range []string{
|
||||
"",
|
||||
"lowercase",
|
||||
"Class",
|
||||
"MyClass",
|
||||
"MyC",
|
||||
"HTML",
|
||||
"PDFLoader",
|
||||
"AString",
|
||||
"SimpleXMLParser",
|
||||
"vimRPCPlugin",
|
||||
"GL11Version",
|
||||
"99Bottles",
|
||||
"May5",
|
||||
"BFG9000",
|
||||
"BöseÜberraschung",
|
||||
"Two spaces",
|
||||
"BadUTF8\xe2\xe2\xa1",
|
||||
} {
|
||||
fmt.Printf("%#v => %#v\n", c, Split(c))
|
||||
}
|
||||
|
||||
// Output:
|
||||
// "" => []string{}
|
||||
// "lowercase" => []string{"lowercase"}
|
||||
// "Class" => []string{"Class"}
|
||||
// "MyClass" => []string{"My", "Class"}
|
||||
// "MyC" => []string{"My", "C"}
|
||||
// "HTML" => []string{"HTML"}
|
||||
// "PDFLoader" => []string{"PDF", "Loader"}
|
||||
// "AString" => []string{"A", "String"}
|
||||
// "SimpleXMLParser" => []string{"Simple", "XML", "Parser"}
|
||||
// "vimRPCPlugin" => []string{"vim", "RPC", "Plugin"}
|
||||
// "GL11Version" => []string{"GL", "11", "Version"}
|
||||
// "99Bottles" => []string{"99", "Bottles"}
|
||||
// "May5" => []string{"May", "5"}
|
||||
// "BFG9000" => []string{"BFG", "9000"}
|
||||
// "BöseÜberraschung" => []string{"Böse", "Überraschung"}
|
||||
// "Two spaces" => []string{"Two", " ", "spaces"}
|
||||
// "BadUTF8\xe2\xe2\xa1" => []string{"BadUTF8\xe2\xe2\xa1"}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
To the extent possible under law, Pascal S. de Kloe has waived all
|
||||
copyright and related or neighboring rights to Colfer. This work is
|
||||
published from The Netherlands.
|
|
@ -0,0 +1,6 @@
|
|||
[![GoDoc](https://godoc.org/github.com/pascaldekloe/name?status.svg)](https://godoc.org/github.com/pascaldekloe/name)
|
||||
|
||||
Naming convention library for the Go programming language (golang).
|
||||
|
||||
This is free and unencumbered software released into the
|
||||
[public domain](http://creativecommons.org/publicdomain/zero/1.0).
|
|
@ -0,0 +1,112 @@
|
|||
// Package name implements naming conventions.
|
||||
package name
|
||||
|
||||
import "unicode"
|
||||
|
||||
// CamelCase returns the medial capitals form of word sequence s.
|
||||
// The input can be any case or even just a bunch of words.
|
||||
// Upper case abbreviations are preserved.
|
||||
// Argument upper sets the casing for the first rune.
|
||||
func CamelCase(s string, upper bool) string {
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
out := make([]rune, 1, len(s)+5)
|
||||
for i, r := range s {
|
||||
if i == 0 {
|
||||
if upper {
|
||||
r = unicode.ToUpper(r)
|
||||
}
|
||||
out[0] = r
|
||||
continue
|
||||
}
|
||||
|
||||
if i == 1 {
|
||||
if !upper && unicode.Is(unicode.Lower, r) {
|
||||
out[0] = unicode.ToLower(out[0])
|
||||
}
|
||||
|
||||
upper = false
|
||||
}
|
||||
|
||||
switch {
|
||||
case unicode.IsLetter(r):
|
||||
if upper {
|
||||
r = unicode.ToUpper(r)
|
||||
}
|
||||
|
||||
fallthrough
|
||||
case unicode.IsNumber(r):
|
||||
upper = false
|
||||
out = append(out, r)
|
||||
|
||||
default:
|
||||
upper = true
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// SnakeCase is an alias for Delimit(s, '_').
|
||||
func SnakeCase(s string) string {
|
||||
return Delimit(s, '_')
|
||||
}
|
||||
|
||||
// DotSeparated is an alias for Delimit(s, '.').
|
||||
func DotSeparated(s string) string {
|
||||
return Delimit(s, '.')
|
||||
}
|
||||
|
||||
// Delimit returns word sequence s delimited with sep.
|
||||
// The input can be any case or even just a bunch of words.
|
||||
// Upper case abbreviations are preserved. Use strings.ToLower
|
||||
// and strings.ToUpper to enforce a letter case.
|
||||
func Delimit(s string, sep rune) string {
|
||||
out := make([]rune, 0, len(s)+5)
|
||||
|
||||
for _, r := range s {
|
||||
switch {
|
||||
case unicode.IsUpper(r):
|
||||
if last := len(out) - 1; last >= 0 && unicode.IsLower(out[last]) {
|
||||
out = append(out, sep)
|
||||
}
|
||||
|
||||
case unicode.IsLetter(r):
|
||||
if i := len(out) - 1; i >= 0 {
|
||||
if last := out[i]; unicode.IsUpper(last) {
|
||||
out = out[:i]
|
||||
if i > 0 && out[i-1] != sep {
|
||||
out = append(out, sep)
|
||||
}
|
||||
out = append(out, unicode.ToLower(last))
|
||||
}
|
||||
}
|
||||
|
||||
case !unicode.IsNumber(r):
|
||||
if i := len(out); i != 0 && out[i-1] != sep {
|
||||
out = append(out, sep)
|
||||
}
|
||||
continue
|
||||
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
|
||||
if len(out) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// trim tailing separator
|
||||
if i := len(out) - 1; out[i] == sep {
|
||||
out = out[:i]
|
||||
}
|
||||
|
||||
if len(out) == 1 {
|
||||
out[0] = unicode.ToLower(out[0])
|
||||
}
|
||||
|
||||
return string(out)
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
package name
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type goldenCase struct {
|
||||
snake, lowerCamel, upperCamel string
|
||||
}
|
||||
|
||||
var goldenCases = []goldenCase{
|
||||
{"", "", ""},
|
||||
{"i", "i", "I"},
|
||||
{"name", "name", "Name"},
|
||||
{"ID", "ID", "ID"},
|
||||
{"wi_fi", "wiFi", "WiFi"},
|
||||
|
||||
// single outer abbreviation
|
||||
{"vitamin_C", "vitaminC", "VitaminC"},
|
||||
{"T_cell", "TCell", "TCell"},
|
||||
|
||||
// double outer abbreviation
|
||||
{"master_DB", "masterDB", "MasterDB"},
|
||||
{"IO_bounds", "IOBounds", "IOBounds"},
|
||||
|
||||
// tripple outer abbreviation
|
||||
{"main_API", "mainAPI", "MainAPI"},
|
||||
{"TCP_conn", "TCPConn", "TCPConn"},
|
||||
|
||||
// inner abbreviation
|
||||
{"raw_URL_query", "rawURLQuery", "RawURLQuery"},
|
||||
|
||||
// numbers
|
||||
{"4x4", "4x4", "4x4"},
|
||||
{"no5", "no5", "No5"},
|
||||
{"DB2", "DB2", "DB2"},
|
||||
{"3M", "3M", "3M"},
|
||||
{"7_up", "7Up", "7Up"},
|
||||
{"20th", "20th", "20th"},
|
||||
}
|
||||
|
||||
func TestSnakeToSnake(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
s := golden.snake
|
||||
if got := SnakeCase(s); got != s {
|
||||
t.Errorf("%q: got %q", s, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLowerCamelToLowerCamel(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
s := golden.lowerCamel
|
||||
if got := CamelCase(s, false); got != s {
|
||||
t.Errorf("%q: got %q", s, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpperCamelToUpperCamel(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
s := golden.upperCamel
|
||||
if got := CamelCase(s, true); got != s {
|
||||
t.Errorf("%q: got %q", s, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnakeToLowerCamel(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
snake, want := golden.snake, golden.lowerCamel
|
||||
if got := CamelCase(snake, false); got != want {
|
||||
t.Errorf("%q: got %q, want %q", snake, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnakeToUpperCamel(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
snake, want := golden.snake, golden.upperCamel
|
||||
if got := CamelCase(snake, true); got != want {
|
||||
t.Errorf("%q: got %q, want %q", snake, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLowerCamelToSnake(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
camel, want := golden.lowerCamel, golden.snake
|
||||
if got := SnakeCase(camel); got != want {
|
||||
t.Errorf("%q: got %q, want %q", camel, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpperCamelToSnake(t *testing.T) {
|
||||
for _, golden := range goldenCases {
|
||||
camel, want := golden.upperCamel, golden.snake
|
||||
if got := SnakeCase(camel); got != want {
|
||||
t.Errorf("%q: got %q, want %q", camel, got, want)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package name_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pascaldekloe/name"
|
||||
)
|
||||
|
||||
func ExampleCamelCase() {
|
||||
fmt.Println(name.CamelCase("pascal case", true))
|
||||
fmt.Println(name.CamelCase("snake_to_camel AND CamelToCamel?", false))
|
||||
|
||||
// Output:
|
||||
// PascalCase
|
||||
// snakeToCamelANDCamelToCamel
|
||||
}
|
||||
|
||||
func ExampleDelimit() {
|
||||
// Garbage to Lisp-case:
|
||||
fmt.Println(name.Delimit("* All Hype is aGoodThing (TM)", '-'))
|
||||
|
||||
// Builds a Java property key:
|
||||
fmt.Println(name.DotSeparated("WebCrawler#socketTimeout"))
|
||||
|
||||
// Output:
|
||||
// all-hype-is-a-good-thing-TM
|
||||
// web.crawler.socket.timeout
|
||||
}
|
Loading…
Reference in New Issue