Ver Fonte

修改bug

yjp há 3 meses atrás
pai
commit
f215ff0d80
2 ficheiros alterados com 32 adições e 9 exclusões
  1. 18 9
      template/template_func.go
  2. 14 0
      template/template_test.go

+ 18 - 9
template/template_func.go

@@ -64,15 +64,18 @@ func UpperShort(s string) string {
 
 func Id(s string) string {
 	if strings.HasSuffix(s, "ID") {
-		return strings.ReplaceAll(s, "ID", "Id")
+		index := strings.LastIndex(s, "ID")
+		return s[0:index] + "Id"
 	}
 
 	if strings.HasSuffix(s, "id") {
-		return strings.ReplaceAll(s, "id", "Id")
+		index := strings.LastIndex(s, "id")
+		return s[0:index] + "Id"
 	}
 
 	if strings.HasSuffix(s, "iD") {
-		return strings.ReplaceAll(s, "iD", "Id")
+		index := strings.LastIndex(s, "iD")
+		return s[0:index] + "Id"
 	}
 
 	return s
@@ -80,15 +83,18 @@ func Id(s string) string {
 
 func ID(s string) string {
 	if strings.HasSuffix(s, "Id") {
-		return strings.ReplaceAll(s, "Id", "ID")
+		index := strings.LastIndex(s, "Id")
+		return s[0:index] + "ID"
 	}
 
 	if strings.HasSuffix(s, "id") {
-		return strings.ReplaceAll(s, "id", "ID")
+		index := strings.LastIndex(s, "id")
+		return s[0:index] + "ID"
 	}
 
 	if strings.HasSuffix(s, "iD") {
-		return strings.ReplaceAll(s, "iD", "ID")
+		index := strings.LastIndex(s, "iD")
+		return s[0:index] + "ID"
 	}
 
 	return s
@@ -96,15 +102,18 @@ func ID(s string) string {
 
 func id(s string) string {
 	if strings.HasSuffix(s, "Id") {
-		return strings.ReplaceAll(s, "Id", "id")
+		index := strings.LastIndex(s, "Id")
+		return s[0:index] + "id"
 	}
 
 	if strings.HasSuffix(s, "ID") {
-		return strings.ReplaceAll(s, "ID", "id")
+		index := strings.LastIndex(s, "ID")
+		return s[0:index] + "id"
 	}
 
 	if strings.HasSuffix(s, "iD") {
-		return strings.ReplaceAll(s, "iD", "id")
+		index := strings.LastIndex(s, "iD")
+		return s[0:index] + "id"
 	}
 
 	return s

+ 14 - 0
template/template_test.go

@@ -124,3 +124,17 @@ func TestParseTemplateStringToBytes(t *testing.T) {
 		t.Fatal("渲染后的模板值不一致")
 	}
 }
+
+func TestIDTemplateFunc(t *testing.T) {
+	if Id("MiddleID") != "MiddleId" {
+		t.Fatal("Id转换错误")
+	}
+
+	if id("MiddleID") != "Middleid" {
+		t.Fatal("Id转换错误")
+	}
+
+	if ID("Middleid") != "MiddleID" {
+		t.Fatal("Id转换错误")
+	}
+}