-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
148 lines (122 loc) · 4.44 KB
/
converter.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
package gocc
import (
"github.com/dtomasi/gocc/internal/converter"
cs "github.com/dtomasi/gocc/pkg/casestyle"
"github.com/pkg/errors"
"strings"
)
// C returns a new Converter instance.
func C(s string) *Converter {
return &Converter{
s: strings.TrimSpace(s),
}
}
// Converter is a simple struct that holds a string.
type Converter struct {
s string
}
// Style returns a CaseStyle (int) see: style.go.
func (c *Converter) Style() (cs.CaseStyle, error) {
return cs.DetectCaseStyle(c.s) // nolint:wrapcheck
}
// Convert converts from -> to given types. This function is more performant than the case/format detection
// on other convert functions.
func (c *Converter) Convert(from cs.CaseStyle, to cs.CaseStyle) (string, error) {
switch to {
case cs.StylePascalCase:
return converter.ToPascalCaseFromCaseStyle(from, c.s) // nolint:wrapcheck
case cs.StyleCamelCase:
return converter.ToCamelCaseFromCaseStyle(from, c.s) // nolint:wrapcheck
case cs.StyleSnakeCase:
return converter.ToSnakeCaseFromCaseStyle(from, c.s) // nolint:wrapcheck
case cs.StyleKebabCase:
return converter.ToKebabCaseFromCaseStyle(from, c.s) // nolint:wrapcheck
case cs.StyleDotNotation:
return converter.ToDotNotationFromCaseStyle(from, c.s) // nolint:wrapcheck
}
return c.s, errors.Errorf(
"cannot convert %s to %s. Parameter to is a unknown case style. Got %d",
c.s,
from.String(),
to,
)
}
func (c *Converter) convertToWithDetect(to cs.CaseStyle) (string, error) {
caseStyle, err := c.Style()
if err != nil {
return c.s, err
}
return c.Convert(caseStyle, to)
}
// ConvertCustomDelimiter converts given case style to custom delimiter separated format.
func (c *Converter) ConvertCustomDelimiter(from cs.CaseStyle, delimiter string) (string, error) {
return converter.ToCustomDelimiterFromCaseStyle(from, c.s, delimiter) // nolint:wrapcheck
}
// ToSnakeCase converts the string value of Converter to a snake case string.
func (c *Converter) ToSnakeCase() (string, error) {
return c.convertToWithDetect(cs.StyleSnakeCase)
}
// ToPascalCase converts the string value of Converter to a pascal case string.
func (c *Converter) ToPascalCase() (string, error) {
return c.convertToWithDetect(cs.StylePascalCase)
}
// ToCamelCase converts the string value of Converter to a camel case string.
func (c *Converter) ToCamelCase() (string, error) {
return c.convertToWithDetect(cs.StyleCamelCase)
}
// ToKebabCase converts the string value of Converter to a kebab case string.
func (c *Converter) ToKebabCase() (string, error) {
return c.convertToWithDetect(cs.StyleKebabCase)
}
// nolint:godox,nolintlint // ToDotNotation converts the string value of Converter to a kebab case string.
func (c *Converter) ToDotNotation() (string, error) {
return c.convertToWithDetect(cs.StyleDotNotation)
}
// ToCustomDelimiter converts the string to a string separated by given delimiter.
func (c *Converter) ToCustomDelimiter(delimiter string) (string, error) {
style, err := c.Style()
if err != nil {
return c.s, err
}
return c.ConvertCustomDelimiter(style, delimiter)
}
// ToUpperCustomDelimiter converts the string to a string separated by given delimiter in upper case.
func (c *Converter) ToUpperCustomDelimiter(delimiter string) (string, error) {
style, err := c.Style()
if err != nil {
return c.s, err
}
result, err := c.ConvertCustomDelimiter(style, delimiter)
if err != nil {
return c.s, err
}
return strings.ToUpper(result), nil
}
// IsSnakeCase detects if a string is snake case style.
func (c *Converter) IsSnakeCase() bool {
return cs.IsSnakeCase(c.s)
}
// IsPascalCase detects if a string is pascal case style.
func (c *Converter) IsPascalCase() bool {
return cs.IsPascalCase(c.s)
}
// IsCamelCase detects if a string is camel case style.
func (c *Converter) IsCamelCase() bool {
return cs.IsCamelCase(c.s)
}
// IsKebabCase detects if a string is kebab case style.
func (c *Converter) IsKebabCase() bool {
return cs.IsKebabCase(c.s)
}
// IsDotNotation detects if a string is dot notation style.
func (c *Converter) IsDotNotation() bool {
return cs.IsDotNotation(c.s)
}
// IsCustomDelimiter detects if a string is separated by a given custom delimiter.
func (c *Converter) IsCustomDelimiter(delimiter string) bool {
return cs.IsCustomDelimiter(c.s, delimiter)
}
// IsUpperCustomDelimiter detects if a string is separated by a given custom delimiter as upper case string.
func (c *Converter) IsUpperCustomDelimiter(delimiter string) bool {
return cs.IsUpperCustomDelimiter(c.s, delimiter)
}