forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
156 lines (142 loc) · 3.37 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package format
import (
"bytes"
"errors"
"fmt"
"io"
"strings"
)
const (
flagGo = "GO"
flagZero = "ZERO"
unknown style = iota
title
lower
upper
)
// ErrNamingFormat defines an error for unknown format
var ErrNamingFormat = errors.New("unsupported format")
type (
styleFormat struct {
before string
through string
after string
goStyle style
zeroStyle style
}
style int
)
// FileNamingFormat is used to format the file name. You can define the format style
// through the go and zero formatting characters. For example, you can define the snake
// format as go_zero, and the camel case format as goZero. You can even specify the split
// character, such as go#Zero, theoretically any combination can be used, but the prerequisite
// must meet the naming conventions of each operating system file name.
// Note: Formatting is based on snake or camel string
func FileNamingFormat(format, content string) (string, error) {
upperFormat := strings.ToUpper(format)
indexGo := strings.Index(upperFormat, flagGo)
indexZero := strings.Index(upperFormat, flagZero)
if indexGo < 0 || indexZero < 0 || indexGo > indexZero {
return "", ErrNamingFormat
}
var (
before, through, after string
flagGo, flagZero string
goStyle, zeroStyle style
err error
)
before = format[:indexGo]
flagGo = format[indexGo : indexGo+2]
through = format[indexGo+2 : indexZero]
flagZero = format[indexZero : indexZero+4]
after = format[indexZero+4:]
goStyle, err = getStyle(flagGo)
if err != nil {
return "", err
}
zeroStyle, err = getStyle(flagZero)
if err != nil {
return "", err
}
var formatStyle styleFormat
formatStyle.goStyle = goStyle
formatStyle.zeroStyle = zeroStyle
formatStyle.before = before
formatStyle.through = through
formatStyle.after = after
return doFormat(formatStyle, content)
}
func doFormat(f styleFormat, content string) (string, error) {
splits, err := split(content)
if err != nil {
return "", err
}
var join []string
for index, split := range splits {
if index == 0 {
join = append(join, transferTo(split, f.goStyle))
continue
}
join = append(join, transferTo(split, f.zeroStyle))
}
joined := strings.Join(join, f.through)
return f.before + joined + f.after, nil
}
func transferTo(in string, style style) string {
switch style {
case upper:
return strings.ToUpper(in)
case lower:
return strings.ToLower(in)
case title:
return strings.Title(in)
default:
return in
}
}
func split(content string) ([]string, error) {
var (
list []string
reader = strings.NewReader(content)
buffer = bytes.NewBuffer(nil)
)
for {
r, _, err := reader.ReadRune()
if err != nil {
if err == io.EOF {
if buffer.Len() > 0 {
list = append(list, buffer.String())
}
return list, nil
}
return nil, err
}
if r == '_' {
if buffer.Len() > 0 {
list = append(list, buffer.String())
}
buffer.Reset()
continue
}
if r >= 'A' && r <= 'Z' {
if buffer.Len() > 0 {
list = append(list, buffer.String())
}
buffer.Reset()
}
buffer.WriteRune(r)
}
}
func getStyle(flag string) (style, error) {
compare := strings.ToLower(flag)
switch flag {
case strings.ToLower(compare):
return lower, nil
case strings.ToUpper(compare):
return upper, nil
case strings.Title(compare):
return title, nil
default:
return unknown, fmt.Errorf("unexpected format: %s", flag)
}
}