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
2a8d6f94
Unverified
Commit
2a8d6f94
authored
Nov 21, 2018
by
astaxie
Committed by
GitHub
Nov 21, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3408 from nlimpid/develop
add different column name parse strategy
parents
e22a5143
6b0155c4
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
63 additions
and
5 deletions
+63
-5
models_utils.go
orm/models_utils.go
+1
-1
orm.go
orm/orm.go
+2
-2
orm_raw.go
orm/orm_raw.go
+2
-2
utils.go
orm/utils.go
+41
-0
utils_test.go
orm/utils_test.go
+17
-0
No files found.
orm/models_utils.go
View file @
2a8d6f94
...
...
@@ -109,7 +109,7 @@ func getTableUnique(val reflect.Value) [][]string {
func
getColumnName
(
ft
int
,
addrField
reflect
.
Value
,
sf
reflect
.
StructField
,
col
string
)
string
{
column
:=
col
if
col
==
""
{
column
=
snakeString
(
sf
.
Name
)
column
=
nameStrategyMap
[
nameStrategy
]
(
sf
.
Name
)
}
switch
ft
{
case
RelForeignKey
,
RelOneToOne
:
...
...
orm/orm.go
View file @
2a8d6f94
...
...
@@ -425,7 +425,7 @@ func (o *orm) getRelQs(md interface{}, mi *modelInfo, fi *fieldInfo) *querySet {
func
(
o
*
orm
)
QueryTable
(
ptrStructOrTableName
interface
{})
(
qs
QuerySeter
)
{
var
name
string
if
table
,
ok
:=
ptrStructOrTableName
.
(
string
);
ok
{
name
=
snakeString
(
table
)
name
=
nameStrategyMap
[
defaultNameStrategy
]
(
table
)
if
mi
,
ok
:=
modelCache
.
get
(
name
);
ok
{
qs
=
newQuerySet
(
o
,
mi
)
}
...
...
@@ -549,7 +549,7 @@ func NewOrmWithDB(driverName, aliasName string, db *sql.DB) (Ormer, error) {
al
.
Name
=
aliasName
al
.
DriverName
=
driverName
al
.
DB
=
db
detectTZ
(
al
)
o
:=
new
(
orm
)
...
...
orm/orm_raw.go
View file @
2a8d6f94
...
...
@@ -358,7 +358,7 @@ func (o *rawSet) QueryRow(containers ...interface{}) error {
_
,
tags
:=
parseStructTag
(
fe
.
Tag
.
Get
(
defaultStructTagName
))
var
col
string
if
col
=
tags
[
"column"
];
col
==
""
{
col
=
snakeString
(
fe
.
Name
)
col
=
nameStrategyMap
[
nameStrategy
]
(
fe
.
Name
)
}
if
v
,
ok
:=
columnsMp
[
col
];
ok
{
value
:=
reflect
.
ValueOf
(
v
)
.
Elem
()
.
Interface
()
...
...
@@ -509,7 +509,7 @@ func (o *rawSet) QueryRows(containers ...interface{}) (int64, error) {
_
,
tags
:=
parseStructTag
(
fe
.
Tag
.
Get
(
defaultStructTagName
))
var
col
string
if
col
=
tags
[
"column"
];
col
==
""
{
col
=
snakeString
(
fe
.
Name
)
col
=
nameStrategyMap
[
nameStrategy
]
(
fe
.
Name
)
}
if
v
,
ok
:=
columnsMp
[
col
];
ok
{
value
:=
reflect
.
ValueOf
(
v
)
.
Elem
()
.
Interface
()
...
...
orm/utils.go
View file @
2a8d6f94
...
...
@@ -23,6 +23,18 @@ import (
"time"
)
type
fn
func
(
string
)
string
var
(
nameStrategyMap
=
map
[
string
]
fn
{
defaultNameStrategy
:
snakeString
,
SnakeAcronymNameStrategy
:
snakeStringWithAcronym
,
}
defaultNameStrategy
=
"snakeString"
SnakeAcronymNameStrategy
=
"snakeStringWithAcronym"
nameStrategy
=
defaultNameStrategy
)
// StrTo is the target string
type
StrTo
string
...
...
@@ -198,6 +210,27 @@ func ToInt64(value interface{}) (d int64) {
return
}
func
snakeStringWithAcronym
(
s
string
)
string
{
data
:=
make
([]
byte
,
0
,
len
(
s
)
*
2
)
num
:=
len
(
s
)
for
i
:=
0
;
i
<
num
;
i
++
{
d
:=
s
[
i
]
before
:=
false
after
:=
false
if
i
>
0
{
before
=
s
[
i
-
1
]
>=
'a'
&&
s
[
i
-
1
]
<=
'z'
}
if
i
+
1
<
num
{
after
=
s
[
i
+
1
]
>=
'a'
&&
s
[
i
+
1
]
<=
'z'
}
if
i
>
0
&&
d
>=
'A'
&&
d
<=
'Z'
&&
(
before
||
after
)
{
data
=
append
(
data
,
'_'
)
}
data
=
append
(
data
,
d
)
}
return
strings
.
ToLower
(
string
(
data
[
:
]))
}
// snake string, XxYy to xx_yy , XxYY to xx_y_y
func
snakeString
(
s
string
)
string
{
data
:=
make
([]
byte
,
0
,
len
(
s
)
*
2
)
...
...
@@ -216,6 +249,14 @@ func snakeString(s string) string {
return
strings
.
ToLower
(
string
(
data
[
:
]))
}
// SetNameStrategy set different name strategy
func
SetNameStrategy
(
s
string
)
{
if
SnakeAcronymNameStrategy
!=
s
{
nameStrategy
=
defaultNameStrategy
}
nameStrategy
=
s
}
// camel string, xx_yy to XxYy
func
camelString
(
s
string
)
string
{
data
:=
make
([]
byte
,
0
,
len
(
s
))
...
...
orm/utils_test.go
View file @
2a8d6f94
...
...
@@ -51,3 +51,20 @@ func TestSnakeString(t *testing.T) {
}
}
}
func
TestSnakeStringWithAcronym
(
t
*
testing
.
T
)
{
camel
:=
[]
string
{
"ID"
,
"PicURL"
,
"HelloWorld"
,
"HelloWorld"
,
"HelLOWord"
,
"PicUrl1"
,
"XyXX"
}
snake
:=
[]
string
{
"id"
,
"pic_url"
,
"hello_world"
,
"hello_world"
,
"hel_lo_word"
,
"pic_url1"
,
"xy_xx"
}
answer
:=
make
(
map
[
string
]
string
)
for
i
,
v
:=
range
camel
{
answer
[
v
]
=
snake
[
i
]
}
for
_
,
v
:=
range
camel
{
res
:=
snakeStringWithAcronym
(
v
)
if
res
!=
answer
[
v
]
{
t
.
Error
(
"Unit Test Fail:"
,
v
,
res
,
answer
[
v
])
}
}
}
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