refactor & format code (#255)
This commit is contained in:
@@ -33,7 +33,7 @@ func genDelete(table Table, withCache bool) (string, string, error) {
|
|||||||
"upperStartCamelObject": camel,
|
"upperStartCamelObject": camel,
|
||||||
"withCache": withCache,
|
"withCache": withCache,
|
||||||
"containsIndexCache": table.ContainsUniqueKey,
|
"containsIndexCache": table.ContainsUniqueKey,
|
||||||
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).UnTitle(),
|
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
|
||||||
"dataType": table.PrimaryKey.DataType,
|
"dataType": table.PrimaryKey.DataType,
|
||||||
"keys": strings.Join(keySet.KeysStr(), "\n"),
|
"keys": strings.Join(keySet.KeysStr(), "\n"),
|
||||||
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
||||||
@@ -52,7 +52,7 @@ func genDelete(table Table, withCache bool) (string, string, error) {
|
|||||||
deleteMethodOut, err := util.With("deleteMethod").
|
deleteMethodOut, err := util.With("deleteMethod").
|
||||||
Parse(text).
|
Parse(text).
|
||||||
Execute(map[string]interface{}{
|
Execute(map[string]interface{}{
|
||||||
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).UnTitle(),
|
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
|
||||||
"dataType": table.PrimaryKey.DataType,
|
"dataType": table.PrimaryKey.DataType,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ func genFindOne(table Table, withCache bool) (string, string, error) {
|
|||||||
Execute(map[string]interface{}{
|
Execute(map[string]interface{}{
|
||||||
"withCache": withCache,
|
"withCache": withCache,
|
||||||
"upperStartCamelObject": camel,
|
"upperStartCamelObject": camel,
|
||||||
"lowerStartCamelObject": stringx.From(camel).UnTitle(),
|
"lowerStartCamelObject": stringx.From(camel).Untitle(),
|
||||||
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
||||||
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).UnTitle(),
|
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
|
||||||
"dataType": table.PrimaryKey.DataType,
|
"dataType": table.PrimaryKey.DataType,
|
||||||
"cacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].KeyExpression,
|
"cacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].KeyExpression,
|
||||||
"cacheKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
|
"cacheKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
|
||||||
@@ -38,7 +38,7 @@ func genFindOne(table Table, withCache bool) (string, string, error) {
|
|||||||
Parse(text).
|
Parse(text).
|
||||||
Execute(map[string]interface{}{
|
Execute(map[string]interface{}{
|
||||||
"upperStartCamelObject": camel,
|
"upperStartCamelObject": camel,
|
||||||
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).UnTitle(),
|
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
|
||||||
"dataType": table.PrimaryKey.DataType,
|
"dataType": table.PrimaryKey.DataType,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -32,12 +32,12 @@ func genFindOneByField(table Table, withCache bool) (*findOneCode, error) {
|
|||||||
output, err := t.Execute(map[string]interface{}{
|
output, err := t.Execute(map[string]interface{}{
|
||||||
"upperStartCamelObject": camelTableName,
|
"upperStartCamelObject": camelTableName,
|
||||||
"upperField": camelFieldName,
|
"upperField": camelFieldName,
|
||||||
"in": fmt.Sprintf("%s %s", stringx.From(camelFieldName).UnTitle(), field.DataType),
|
"in": fmt.Sprintf("%s %s", stringx.From(camelFieldName).Untitle(), field.DataType),
|
||||||
"withCache": withCache,
|
"withCache": withCache,
|
||||||
"cacheKey": table.CacheKey[field.Name.Source()].KeyExpression,
|
"cacheKey": table.CacheKey[field.Name.Source()].KeyExpression,
|
||||||
"cacheKeyVariable": table.CacheKey[field.Name.Source()].Variable,
|
"cacheKeyVariable": table.CacheKey[field.Name.Source()].Variable,
|
||||||
"lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
|
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
|
||||||
"lowerStartCamelField": stringx.From(camelFieldName).UnTitle(),
|
"lowerStartCamelField": stringx.From(camelFieldName).Untitle(),
|
||||||
"upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(),
|
"upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(),
|
||||||
"originalField": field.Name.Source(),
|
"originalField": field.Name.Source(),
|
||||||
})
|
})
|
||||||
@@ -63,7 +63,7 @@ func genFindOneByField(table Table, withCache bool) (*findOneCode, error) {
|
|||||||
output, err := t.Execute(map[string]interface{}{
|
output, err := t.Execute(map[string]interface{}{
|
||||||
"upperStartCamelObject": camelTableName,
|
"upperStartCamelObject": camelTableName,
|
||||||
"upperField": camelFieldName,
|
"upperField": camelFieldName,
|
||||||
"in": fmt.Sprintf("%s %s", stringx.From(camelFieldName).UnTitle(), field.DataType),
|
"in": fmt.Sprintf("%s %s", stringx.From(camelFieldName).Untitle(), field.DataType),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -81,7 +81,7 @@ func genFindOneByField(table Table, withCache bool) (*findOneCode, error) {
|
|||||||
out, err := util.With("findOneByFieldExtraMethod").Parse(text).Execute(map[string]interface{}{
|
out, err := util.With("findOneByFieldExtraMethod").Parse(text).Execute(map[string]interface{}{
|
||||||
"upperStartCamelObject": camelTableName,
|
"upperStartCamelObject": camelTableName,
|
||||||
"primaryKeyLeft": table.CacheKey[table.PrimaryKey.Name.Source()].Left,
|
"primaryKeyLeft": table.CacheKey[table.PrimaryKey.Name.Source()].Left,
|
||||||
"lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
|
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
|
||||||
"originalPrimaryField": table.PrimaryKey.Name.Source(),
|
"originalPrimaryField": table.PrimaryKey.Name.Source(),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func genInsert(table Table, withCache bool) (string, string, error) {
|
|||||||
"withCache": withCache,
|
"withCache": withCache,
|
||||||
"containsIndexCache": table.ContainsUniqueKey,
|
"containsIndexCache": table.ContainsUniqueKey,
|
||||||
"upperStartCamelObject": camel,
|
"upperStartCamelObject": camel,
|
||||||
"lowerStartCamelObject": stringx.From(camel).UnTitle(),
|
"lowerStartCamelObject": stringx.From(camel).Untitle(),
|
||||||
"expression": strings.Join(expressions, ", "),
|
"expression": strings.Join(expressions, ", "),
|
||||||
"expressionValues": strings.Join(expressionValues, ", "),
|
"expressionValues": strings.Join(expressionValues, ", "),
|
||||||
"keys": strings.Join(keySet.KeysStr(), "\n"),
|
"keys": strings.Join(keySet.KeysStr(), "\n"),
|
||||||
|
|||||||
@@ -28,11 +28,11 @@ func genCacheKeys(table parser.Table) (map[string]Key, error) {
|
|||||||
fields := table.Fields
|
fields := table.Fields
|
||||||
m := make(map[string]Key)
|
m := make(map[string]Key)
|
||||||
camelTableName := table.Name.ToCamel()
|
camelTableName := table.Name.ToCamel()
|
||||||
lowerStartCamelTableName := stringx.From(camelTableName).UnTitle()
|
lowerStartCamelTableName := stringx.From(camelTableName).Untitle()
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
if field.IsUniqueKey || field.IsPrimaryKey {
|
if field.IsUniqueKey || field.IsPrimaryKey {
|
||||||
camelFieldName := field.Name.ToCamel()
|
camelFieldName := field.Name.ToCamel()
|
||||||
lowerStartCamelFieldName := stringx.From(camelFieldName).UnTitle()
|
lowerStartCamelFieldName := stringx.From(camelFieldName).Untitle()
|
||||||
left := fmt.Sprintf("cache%s%sPrefix", camelTableName, camelFieldName)
|
left := fmt.Sprintf("cache%s%sPrefix", camelTableName, camelFieldName)
|
||||||
if strings.ToLower(camelFieldName) == strings.ToLower(camelTableName) {
|
if strings.ToLower(camelFieldName) == strings.ToLower(camelTableName) {
|
||||||
left = fmt.Sprintf("cache%sPrefix", camelTableName)
|
left = fmt.Sprintf("cache%sPrefix", camelTableName)
|
||||||
|
|||||||
@@ -62,11 +62,11 @@ func TestGenCacheKeys(t *testing.T) {
|
|||||||
|
|
||||||
for fieldName, key := range m {
|
for fieldName, key := range m {
|
||||||
name := stringx.From(fieldName)
|
name := stringx.From(fieldName)
|
||||||
assert.Equal(t, fmt.Sprintf(`cacheUser%sPrefix = "cache#User#%s#"`, name.ToCamel(), name.UnTitle()), key.VarExpression)
|
assert.Equal(t, fmt.Sprintf(`cacheUser%sPrefix = "cache#User#%s#"`, name.ToCamel(), name.Untitle()), key.VarExpression)
|
||||||
assert.Equal(t, fmt.Sprintf(`cacheUser%sPrefix`, name.ToCamel()), key.Left)
|
assert.Equal(t, fmt.Sprintf(`cacheUser%sPrefix`, name.ToCamel()), key.Left)
|
||||||
assert.Equal(t, fmt.Sprintf(`cache#User#%s#`, name.UnTitle()), key.Right)
|
assert.Equal(t, fmt.Sprintf(`cache#User#%s#`, name.Untitle()), key.Right)
|
||||||
assert.Equal(t, fmt.Sprintf(`user%sKey`, name.ToCamel()), key.Variable)
|
assert.Equal(t, fmt.Sprintf(`user%sKey`, name.ToCamel()), key.Variable)
|
||||||
assert.Equal(t, `user`+name.ToCamel()+`Key := fmt.Sprintf("%s%v", cacheUser`+name.ToCamel()+`Prefix,`+name.UnTitle()+`)`, key.KeyExpression)
|
assert.Equal(t, `user`+name.ToCamel()+`Key := fmt.Sprintf("%s%v", cacheUser`+name.ToCamel()+`Prefix,`+name.Untitle()+`)`, key.KeyExpression)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func genUpdate(table Table, withCache bool) (string, string, error) {
|
|||||||
"upperStartCamelObject": camelTableName,
|
"upperStartCamelObject": camelTableName,
|
||||||
"primaryCacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].DataKeyExpression,
|
"primaryCacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].DataKeyExpression,
|
||||||
"primaryKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
|
"primaryKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
|
||||||
"lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
|
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
|
||||||
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
|
||||||
"expressionValues": strings.Join(expressionValues, ", "),
|
"expressionValues": strings.Join(expressionValues, ", "),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func genVars(table Table, withCache bool) (string, error) {
|
|||||||
Parse(text).
|
Parse(text).
|
||||||
GoFmt(true).
|
GoFmt(true).
|
||||||
Execute(map[string]interface{}{
|
Execute(map[string]interface{}{
|
||||||
"lowerStartCamelObject": stringx.From(camel).UnTitle(),
|
"lowerStartCamelObject": stringx.From(camel).Untitle(),
|
||||||
"upperStartCamelObject": camel,
|
"upperStartCamelObject": camel,
|
||||||
"cacheKeys": strings.Join(keys, "\n"),
|
"cacheKeys": strings.Join(keys, "\n"),
|
||||||
"autoIncrement": table.PrimaryKey.AutoIncrement,
|
"autoIncrement": table.PrimaryKey.AutoIncrement,
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func ProtoTmpl(out string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = util.With("t").Parse(text).SaveTo(map[string]string{
|
err = util.With("t").Parse(text).SaveTo(map[string]string{
|
||||||
"package": serviceName.UnTitle(),
|
"package": serviceName.Untitle(),
|
||||||
"serviceName": serviceName.Title(),
|
"serviceName": serviceName.Title(),
|
||||||
}, out, false)
|
}, out, false)
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -6,11 +6,9 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type String struct {
|
||||||
String struct {
|
source string
|
||||||
source string
|
}
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func From(data string) String {
|
func From(data string) String {
|
||||||
return String{source: data}
|
return String{source: data}
|
||||||
@@ -30,8 +28,12 @@ func (s String) Lower() string {
|
|||||||
return strings.ToLower(s.source)
|
return strings.ToLower(s.source)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s String) Upper() string {
|
func (s String) ReplaceAll(old, new string) string {
|
||||||
return strings.ToUpper(s.source)
|
return strings.ReplaceAll(s.source, old, new)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s String) Source() string {
|
||||||
|
return s.source
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s String) Title() string {
|
func (s String) Title() string {
|
||||||
@@ -64,7 +66,7 @@ func (s String) ToSnake() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// return original string if rune is not letter at index 0
|
// return original string if rune is not letter at index 0
|
||||||
func (s String) UnTitle() string {
|
func (s String) Untitle() string {
|
||||||
if s.IsEmptyOrSpace() {
|
if s.IsEmptyOrSpace() {
|
||||||
return s.source
|
return s.source
|
||||||
}
|
}
|
||||||
@@ -75,6 +77,10 @@ func (s String) UnTitle() string {
|
|||||||
return string(unicode.ToLower(r)) + s.source[1:]
|
return string(unicode.ToLower(r)) + s.source[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s String) Upper() string {
|
||||||
|
return strings.ToUpper(s.source)
|
||||||
|
}
|
||||||
|
|
||||||
// it will not ignore spaces
|
// it will not ignore spaces
|
||||||
func (s String) splitBy(fn func(r rune) bool, remove bool) []string {
|
func (s String) splitBy(fn func(r rune) bool, remove bool) []string {
|
||||||
if s.IsEmptyOrSpace() {
|
if s.IsEmptyOrSpace() {
|
||||||
@@ -100,11 +106,3 @@ func (s String) splitBy(fn func(r rune) bool, remove bool) []string {
|
|||||||
}
|
}
|
||||||
return list
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user