Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
d528fafd
Commit
d528fafd
authored
Jun 03, 2016
by
ysqi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ignore case of tag and fixed bug for columName
parent
1fe2226c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
10 deletions
+72
-10
models_utils.go
orm/models_utils.go
+3
-3
orm_test.go
orm/orm_test.go
+44
-0
utils.go
orm/utils.go
+25
-7
No files found.
orm/models_utils.go
View file @
d528fafd
...
...
@@ -192,10 +192,10 @@ func parseStructTag(data string, attrs *map[string]bool, tags *map[string]string
tag
:=
make
(
map
[
string
]
string
)
for
_
,
v
:=
range
strings
.
Split
(
data
,
defaultStructTagDelim
)
{
v
=
strings
.
TrimSpace
(
v
)
if
supportTag
[
v
]
==
1
{
attr
[
v
]
=
true
if
t
:=
strings
.
ToLower
(
v
);
supportTag
[
t
]
==
1
{
attr
[
t
]
=
true
}
else
if
i
:=
strings
.
Index
(
v
,
"("
);
i
>
0
&&
strings
.
Index
(
v
,
")"
)
==
len
(
v
)
-
1
{
name
:=
v
[
:
i
]
name
:=
t
[
:
i
]
if
supportTag
[
name
]
==
2
{
v
=
v
[
i
+
1
:
len
(
v
)
-
1
]
tag
[
name
]
=
v
...
...
orm/orm_test.go
View file @
d528fafd
...
...
@@ -2117,3 +2117,47 @@ func TestUintPk(t *testing.T) {
dORM
.
Delete
(
u
)
}
func
TestSnake
(
t
*
testing
.
T
)
{
cases
:=
map
[
string
]
string
{
"i"
:
"i"
,
"I"
:
"i"
,
"iD"
:
"i_d"
,
//01
"ID"
:
"id"
,
//11 pre+curent=2
"NO"
:
"no"
,
//11
"NOO"
:
"noo"
,
//111 10
"NOOooOOoo"
:
"noo_oo_oo_oo"
,
//111_00_11_00
"OrderNO"
:
"order_no"
,
//10000_11
"tagName"
:
"tag_name"
,
//000_1000
"tag_Name"
:
"tag_name"
,
"tag_name"
:
"tag_name"
,
"_tag_name"
:
"_tag_name"
,
"tag_666name"
:
"tag_666name"
,
"tag_666Name"
:
"tag_666_name"
,
}
for
name
,
want
:=
range
cases
{
got
:=
snakeString
(
name
)
throwFail
(
t
,
AssertIs
(
got
,
want
))
}
}
func
TestIgnoreCaseTag
(
t
*
testing
.
T
)
{
type
testTagModel
struct
{
ID
int
`orm:"pk"`
NOO
string
`orm:"column(n)"`
Name01
string
`orm:"NULL"`
Name02
string
`orm:"COLUMN(Name)"`
Name03
string
`orm:"Column(name)"`
}
RegisterModel
(
&
testTagModel
{})
info
,
ok
:=
modelCache
.
get
(
"test_tag_model"
)
throwFail
(
t
,
AssertIs
(
ok
,
true
))
throwFail
(
t
,
AssertNot
(
info
,
nil
))
if
t
==
nil
{
return
}
throwFail
(
t
,
AssertIs
(
info
.
fields
.
GetByName
(
"NOO"
)
.
column
,
"n"
))
throwFail
(
t
,
AssertIs
(
info
.
fields
.
GetByName
(
"Name01"
)
.
null
,
true
))
throwFail
(
t
,
AssertIs
(
info
.
fields
.
GetByName
(
"Name02"
)
.
column
,
"Name"
))
throwFail
(
t
,
AssertIs
(
info
.
fields
.
GetByName
(
"Name03"
)
.
column
,
"name"
))
}
orm/utils.go
View file @
d528fafd
...
...
@@ -181,18 +181,36 @@ func ToInt64(value interface{}) (d int64) {
return
}
// snake string, XxYy to xx_yy
// 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
>=
'A'
&&
d
<=
'Z'
&&
j
{
data
=
append
(
data
,
'_'
)
}
if
d
!=
'_'
{
j
=
true
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
,
'_'
)
}
}
data
=
append
(
data
,
d
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment