diff --git a/orm/orm_test.go b/orm/orm_test.go index 75a60cb0..bb1831f9 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -2135,13 +2135,13 @@ func TestSnake(t *testing.T) { "i": "i", "I": "i", "iD": "i_d", - "ID": "id", - "NO": "no", - "NOO": "noo", - "NOOooOOoo": "noo_oo_oo_oo", - "OrderNO": "order_no", + "ID": "i_d", + "NO": "n_o", + "NOO": "n_o_o", + "NOOooOOoo": "n_o_ooo_o_ooo", + "OrderNO": "order_n_o", "tagName": "tag_name", - "tag_Name": "tag_name", + "tag_Name": "tag__name", "tag_name": "tag_name", "_tag_name": "_tag_name", "tag_666name": "tag_666name", diff --git a/orm/utils.go b/orm/utils.go index e3cd8ad6..6e23447e 100644 --- a/orm/utils.go +++ b/orm/utils.go @@ -184,33 +184,15 @@ func ToInt64(value interface{}) (d int64) { // snake string, XxYy to xx_yy , XxYY to xx_yy func snakeString(s string) string { data := make([]byte, 0, len(s)*2) + j := false num := len(s) for i := 0; i < num; i++ { d := s[i] - if i > 0 && d != '_' && s[i-1] != '_' { - need := false - // upper as 1, lower as 0 - // XX -> 11 -> 11 - // Xx -> 10 -> 10 - // XxYyZZ -> 101011 -> 10_10_11 - isUpper := d >= 'A' && d <= 'Z' - preIsUpper := s[i-1] >= 'A' && s[i-1] <= 'Z' - if isUpper { - // like : xxYy - if !preIsUpper { - need = true - } - } else { - if preIsUpper { - // ignore "Xy" in "xxXyy" - if i-2 >= 0 && s[i-2] >= 'A' && s[i-2] <= 'Z' { - need = true - } - } - } - if need { - data = append(data, '_') - } + if i > 0 && d >= 'A' && d <= 'Z' && j { + data = append(data, '_') + } + if d != '_' { + j = true } data = append(data, d) }