refactor & format code (#255)

master
Kevin Wan 4 years ago committed by GitHub
parent b060867009
commit 65ee877236
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -33,7 +33,7 @@ func genDelete(table Table, withCache bool) (string, string, error) {
"upperStartCamelObject": camel,
"withCache": withCache,
"containsIndexCache": table.ContainsUniqueKey,
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).UnTitle(),
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
"dataType": table.PrimaryKey.DataType,
"keys": strings.Join(keySet.KeysStr(), "\n"),
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
@ -52,7 +52,7 @@ func genDelete(table Table, withCache bool) (string, string, error) {
deleteMethodOut, err := util.With("deleteMethod").
Parse(text).
Execute(map[string]interface{}{
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).UnTitle(),
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
"dataType": table.PrimaryKey.DataType,
})
if err != nil {

@ -18,9 +18,9 @@ func genFindOne(table Table, withCache bool) (string, string, error) {
Execute(map[string]interface{}{
"withCache": withCache,
"upperStartCamelObject": camel,
"lowerStartCamelObject": stringx.From(camel).UnTitle(),
"lowerStartCamelObject": stringx.From(camel).Untitle(),
"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,
"cacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].KeyExpression,
"cacheKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
@ -38,7 +38,7 @@ func genFindOne(table Table, withCache bool) (string, string, error) {
Parse(text).
Execute(map[string]interface{}{
"upperStartCamelObject": camel,
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).UnTitle(),
"lowerStartCamelPrimaryKey": stringx.From(table.PrimaryKey.Name.ToCamel()).Untitle(),
"dataType": table.PrimaryKey.DataType,
})
if err != nil {

@ -32,12 +32,12 @@ func genFindOneByField(table Table, withCache bool) (*findOneCode, error) {
output, err := t.Execute(map[string]interface{}{
"upperStartCamelObject": camelTableName,
"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,
"cacheKey": table.CacheKey[field.Name.Source()].KeyExpression,
"cacheKeyVariable": table.CacheKey[field.Name.Source()].Variable,
"lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
"lowerStartCamelField": stringx.From(camelFieldName).UnTitle(),
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
"lowerStartCamelField": stringx.From(camelFieldName).Untitle(),
"upperStartCamelPrimaryKey": table.PrimaryKey.Name.ToCamel(),
"originalField": field.Name.Source(),
})
@ -63,7 +63,7 @@ func genFindOneByField(table Table, withCache bool) (*findOneCode, error) {
output, err := t.Execute(map[string]interface{}{
"upperStartCamelObject": camelTableName,
"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 {
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{}{
"upperStartCamelObject": camelTableName,
"primaryKeyLeft": table.CacheKey[table.PrimaryKey.Name.Source()].Left,
"lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
"originalPrimaryField": table.PrimaryKey.Name.Source(),
})
if err != nil {

@ -45,7 +45,7 @@ func genInsert(table Table, withCache bool) (string, string, error) {
"withCache": withCache,
"containsIndexCache": table.ContainsUniqueKey,
"upperStartCamelObject": camel,
"lowerStartCamelObject": stringx.From(camel).UnTitle(),
"lowerStartCamelObject": stringx.From(camel).Untitle(),
"expression": strings.Join(expressions, ", "),
"expressionValues": strings.Join(expressionValues, ", "),
"keys": strings.Join(keySet.KeysStr(), "\n"),

@ -28,11 +28,11 @@ func genCacheKeys(table parser.Table) (map[string]Key, error) {
fields := table.Fields
m := make(map[string]Key)
camelTableName := table.Name.ToCamel()
lowerStartCamelTableName := stringx.From(camelTableName).UnTitle()
lowerStartCamelTableName := stringx.From(camelTableName).Untitle()
for _, field := range fields {
if field.IsUniqueKey || field.IsPrimaryKey {
camelFieldName := field.Name.ToCamel()
lowerStartCamelFieldName := stringx.From(camelFieldName).UnTitle()
lowerStartCamelFieldName := stringx.From(camelFieldName).Untitle()
left := fmt.Sprintf("cache%s%sPrefix", camelTableName, camelFieldName)
if strings.ToLower(camelFieldName) == strings.ToLower(camelTableName) {
left = fmt.Sprintf("cache%sPrefix", camelTableName)

@ -62,11 +62,11 @@ func TestGenCacheKeys(t *testing.T) {
for fieldName, key := range m {
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(`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, `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,
"primaryCacheKey": table.CacheKey[table.PrimaryKey.Name.Source()].DataKeyExpression,
"primaryKeyVariable": table.CacheKey[table.PrimaryKey.Name.Source()].Variable,
"lowerStartCamelObject": stringx.From(camelTableName).UnTitle(),
"lowerStartCamelObject": stringx.From(camelTableName).Untitle(),
"originalPrimaryKey": table.PrimaryKey.Name.Source(),
"expressionValues": strings.Join(expressionValues, ", "),
})

@ -23,7 +23,7 @@ func genVars(table Table, withCache bool) (string, error) {
Parse(text).
GoFmt(true).
Execute(map[string]interface{}{
"lowerStartCamelObject": stringx.From(camel).UnTitle(),
"lowerStartCamelObject": stringx.From(camel).Untitle(),
"upperStartCamelObject": camel,
"cacheKeys": strings.Join(keys, "\n"),
"autoIncrement": table.PrimaryKey.AutoIncrement,

@ -40,7 +40,7 @@ func ProtoTmpl(out string) error {
}
err = util.With("t").Parse(text).SaveTo(map[string]string{
"package": serviceName.UnTitle(),
"package": serviceName.Untitle(),
"serviceName": serviceName.Title(),
}, out, false)
return err

@ -6,11 +6,9 @@ import (
"unicode"
)
type (
String struct {
source string
}
)
type String struct {
source string
}
func From(data string) String {
return String{source: data}
@ -30,8 +28,12 @@ func (s String) Lower() string {
return strings.ToLower(s.source)
}
func (s String) Upper() string {
return strings.ToUpper(s.source)
func (s String) ReplaceAll(old, new string) string {
return strings.ReplaceAll(s.source, old, new)
}
func (s String) Source() string {
return s.source
}
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
func (s String) UnTitle() string {
func (s String) Untitle() string {
if s.IsEmptyOrSpace() {
return s.source
}
@ -75,6 +77,10 @@ func (s String) UnTitle() string {
return string(unicode.ToLower(r)) + s.source[1:]
}
func (s String) Upper() string {
return strings.ToUpper(s.source)
}
// it will not ignore spaces
func (s String) splitBy(fn func(r rune) bool, remove bool) []string {
if s.IsEmptyOrSpace() {
@ -100,11 +106,3 @@ func (s String) splitBy(fn func(r rune) bool, remove bool) []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
}

Loading…
Cancel
Save