/
exec.go
50 lines (44 loc) · 1.21 KB
/
exec.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
package name
import (
"github.com/iancoleman/strcase"
"github.com/pkg/errors"
"github.com/unionj-cloud/go-doudou/cmd/internal/astutils"
"github.com/unionj-cloud/go-doudou/toolkit/stringutils"
)
// Name wraps config properties for name command
type Name struct {
File string
Strategy string
Omitempty bool
Form bool
}
const (
lowerCamelStrategy = "lowerCamel"
snakeStrategy = "snake"
)
// Exec rewrites the json tag of each field of all structs in the file as snake case or lower camel case.
// Unexported or ignored fields will be skipped.
func (receiver Name) Exec() {
if stringutils.IsEmpty(receiver.File) {
panic(errors.New("file flag should not be empty"))
}
var convert func(string) string
switch receiver.Strategy {
case lowerCamelStrategy:
convert = strcase.ToLowerCamel
case snakeStrategy:
convert = strcase.ToSnake
default:
panic(errors.New(`unknown strategy. currently only support "lowerCamel" and "snake"`))
}
newcode, err := astutils.RewriteTag(astutils.RewriteTagConfig{
File: receiver.File,
Omitempty: receiver.Omitempty,
ConvertFunc: convert,
Form: receiver.Form,
})
if err != nil {
panic(err)
}
astutils.FixImport([]byte(newcode), receiver.File)
}