goctl model reactor (#15)
* reactor sql generation * reactor sql generation * add console & example * optimize unit test & add document * modify default config * remove test file * Revert "remove test file" This reverts commit 81041f9e * fix stringx.go & optimize example * remove unused code
This commit is contained in:
58
tools/goctl/util/console/console.go
Normal file
58
tools/goctl/util/console/console.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package console
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/logrusorgru/aurora"
|
||||
)
|
||||
|
||||
type (
|
||||
Console interface {
|
||||
Success(format string, a ...interface{})
|
||||
Warning(format string, a ...interface{})
|
||||
Error(format string, a ...interface{})
|
||||
}
|
||||
colorConsole struct {
|
||||
}
|
||||
// for idea log
|
||||
ideaConsole struct {
|
||||
}
|
||||
)
|
||||
|
||||
func NewColorConsole() *colorConsole {
|
||||
return &colorConsole{}
|
||||
}
|
||||
|
||||
func (c *colorConsole) Success(format string, a ...interface{}) {
|
||||
msg := fmt.Sprintf(format, a...)
|
||||
fmt.Println(aurora.Green(msg))
|
||||
}
|
||||
|
||||
func (c *colorConsole) Warning(format string, a ...interface{}) {
|
||||
msg := fmt.Sprintf(format, a...)
|
||||
fmt.Println(aurora.Yellow(msg))
|
||||
}
|
||||
|
||||
func (c *colorConsole) Error(format string, a ...interface{}) {
|
||||
msg := fmt.Sprintf(format, a...)
|
||||
fmt.Println(aurora.Red(msg))
|
||||
}
|
||||
|
||||
func NewIdeaConsole() *ideaConsole {
|
||||
return &ideaConsole{}
|
||||
}
|
||||
|
||||
func (i *ideaConsole) Success(format string, a ...interface{}) {
|
||||
msg := fmt.Sprintf(format, a...)
|
||||
fmt.Println("[SUCCESS]: ", msg)
|
||||
}
|
||||
|
||||
func (i *ideaConsole) Warning(format string, a ...interface{}) {
|
||||
msg := fmt.Sprintf(format, a...)
|
||||
fmt.Println("[WARNING]: ", msg)
|
||||
}
|
||||
|
||||
func (i *ideaConsole) Error(format string, a ...interface{}) {
|
||||
msg := fmt.Sprintf(format, a...)
|
||||
fmt.Println("[ERROR]: ", msg)
|
||||
}
|
||||
126
tools/goctl/util/stringx/string.go
Normal file
126
tools/goctl/util/stringx/string.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package stringx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const (
|
||||
emptyString = ""
|
||||
)
|
||||
|
||||
type (
|
||||
String struct {
|
||||
source string
|
||||
}
|
||||
)
|
||||
|
||||
func From(data string) String {
|
||||
return String{source: data}
|
||||
}
|
||||
|
||||
func (s String) IsEmptyOrSpace() bool {
|
||||
if len(s.source) == 0 {
|
||||
return true
|
||||
}
|
||||
if strings.TrimSpace(s.source) == "" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s String) Lower() string {
|
||||
if s.IsEmptyOrSpace() {
|
||||
return s.source
|
||||
}
|
||||
return strings.ToLower(s.source)
|
||||
}
|
||||
func (s String) Upper() string {
|
||||
if s.IsEmptyOrSpace() {
|
||||
return s.source
|
||||
}
|
||||
return strings.ToUpper(s.source)
|
||||
}
|
||||
func (s String) Title() string {
|
||||
if s.IsEmptyOrSpace() {
|
||||
return s.source
|
||||
}
|
||||
return strings.Title(s.source)
|
||||
}
|
||||
|
||||
// snake->camel(upper start)
|
||||
func (s String) Snake2Camel() string {
|
||||
if s.IsEmptyOrSpace() {
|
||||
return s.source
|
||||
}
|
||||
list := s.splitBy(func(r rune) bool {
|
||||
return r == '_'
|
||||
}, true)
|
||||
var target []string
|
||||
for _, item := range list {
|
||||
target = append(target, From(item).Title())
|
||||
}
|
||||
return strings.Join(target, "")
|
||||
}
|
||||
|
||||
// camel->snake
|
||||
func (s String) Camel2Snake() string {
|
||||
if s.IsEmptyOrSpace() {
|
||||
return s.source
|
||||
}
|
||||
list := s.splitBy(func(r rune) bool {
|
||||
return unicode.IsUpper(r)
|
||||
}, false)
|
||||
var target []string
|
||||
for _, item := range list {
|
||||
target = append(target, From(item).Lower())
|
||||
}
|
||||
return strings.Join(target, "_")
|
||||
}
|
||||
|
||||
// return original string if rune is not letter at index 0
|
||||
func (s String) LowerStart() string {
|
||||
if s.IsEmptyOrSpace() {
|
||||
return s.source
|
||||
}
|
||||
r := rune(s.source[0])
|
||||
if !unicode.IsUpper(r) && !unicode.IsLower(r) {
|
||||
return s.source
|
||||
}
|
||||
return string(unicode.ToLower(r)) + s.source[1:]
|
||||
}
|
||||
|
||||
// it will not ignore spaces
|
||||
func (s String) splitBy(fn func(r rune) bool, remove bool) []string {
|
||||
if s.IsEmptyOrSpace() {
|
||||
return nil
|
||||
}
|
||||
var list []string
|
||||
buffer := new(bytes.Buffer)
|
||||
for _, r := range s.source {
|
||||
if fn(r) {
|
||||
if buffer.Len() != 0 {
|
||||
list = append(list, buffer.String())
|
||||
buffer.Reset()
|
||||
}
|
||||
if !remove {
|
||||
buffer.WriteRune(r)
|
||||
}
|
||||
continue
|
||||
}
|
||||
buffer.WriteRune(r)
|
||||
}
|
||||
if buffer.Len() != 0 {
|
||||
list = append(list, buffer.String())
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func (s String) ReplaceAll(old, new string) string {
|
||||
return strings.ReplaceAll(s.source, old, new)
|
||||
}
|
||||
|
||||
func (s String) Source() string {
|
||||
return s.source
|
||||
}
|
||||
42
tools/goctl/util/stringx/string_test.go
Normal file
42
tools/goctl/util/stringx/string_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package stringx
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestString_IsEmptyOrSpace(t *testing.T) {
|
||||
ret := From(" ").IsEmptyOrSpace()
|
||||
assert.Equal(t, true, ret)
|
||||
ret2 := From("ll??").IsEmptyOrSpace()
|
||||
assert.Equal(t, false, ret2)
|
||||
ret3 := From(`
|
||||
`).IsEmptyOrSpace()
|
||||
assert.Equal(t, true, ret3)
|
||||
}
|
||||
|
||||
func TestString_Snake2Camel(t *testing.T) {
|
||||
ret := From("____this_is_snake").Snake2Camel()
|
||||
assert.Equal(t, "ThisIsSnake", ret)
|
||||
|
||||
ret2 := From("测试_test_Data").Snake2Camel()
|
||||
assert.Equal(t, "测试TestData", ret2)
|
||||
|
||||
ret3 := From("___").Snake2Camel()
|
||||
assert.Equal(t, "", ret3)
|
||||
|
||||
ret4 := From("testData_").Snake2Camel()
|
||||
assert.Equal(t, "TestData", ret4)
|
||||
|
||||
ret5 := From("testDataTestData").Snake2Camel()
|
||||
assert.Equal(t, "TestDataTestData", ret5)
|
||||
}
|
||||
|
||||
func TestString_Camel2Snake(t *testing.T) {
|
||||
ret := From("ThisIsCCCamel").Camel2Snake()
|
||||
assert.Equal(t, "this_is_c_c_camel", ret)
|
||||
|
||||
ret2 := From("测试Test_Data_test_data").Camel2Snake()
|
||||
assert.Equal(t, "测试_test__data_test_data", ret2)
|
||||
}
|
||||
67
tools/goctl/util/templatex/templatex.go
Normal file
67
tools/goctl/util/templatex/templatex.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package templatex
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
goformat "go/format"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type (
|
||||
defaultTemplate struct {
|
||||
name string
|
||||
text string
|
||||
goFmt bool
|
||||
savePath string
|
||||
}
|
||||
)
|
||||
|
||||
func With(name string) *defaultTemplate {
|
||||
return &defaultTemplate{
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
func (t *defaultTemplate) Parse(text string) *defaultTemplate {
|
||||
t.text = text
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *defaultTemplate) GoFmt(format bool) *defaultTemplate {
|
||||
t.goFmt = format
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *defaultTemplate) SaveTo(data interface{}, path string) error {
|
||||
output, err := t.execute(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(path, output.Bytes(), os.ModePerm)
|
||||
}
|
||||
|
||||
func (t *defaultTemplate) Execute(data interface{}) (*bytes.Buffer, error) {
|
||||
return t.execute(data)
|
||||
}
|
||||
|
||||
func (t *defaultTemplate) execute(data interface{}) (*bytes.Buffer, error) {
|
||||
tem, err := template.New(t.name).Parse(t.text)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
err = tem.Execute(buf, data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !t.goFmt {
|
||||
return buf, nil
|
||||
}
|
||||
formatOutput, err := goformat.Source(buf.Bytes())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf.Reset()
|
||||
buf.Write(formatOutput)
|
||||
return buf, nil
|
||||
}
|
||||
Reference in New Issue
Block a user