good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
U
upper
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
open
upper
Commits
642dbdb3
Commit
642dbdb3
authored
May 13, 2014
by
José Carlos Nieto
Browse files
Options
Downloads
Patches
Plain Diff
Merging omitempty and inline tag options into the 'db' tag. Fixing #12.
parent
3ad6c1d5
Branches
Branches containing commit
Tags
v0.4.3
v0.4.4
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
util/main.go
+51
-23
51 additions, 23 deletions
util/main.go
util/sqlutil/main.go
+45
-31
45 additions, 31 deletions
util/sqlutil/main.go
with
96 additions
and
54 deletions
util/main.go
+
51
−
23
View file @
642dbdb3
...
@@ -43,6 +43,25 @@ type C struct {
...
@@ -43,6 +43,25 @@ type C struct {
SetName
string
SetName
string
}
}
type
tagOptions
map
[
string
]
bool
func
parseTagOptions
(
s
string
)
tagOptions
{
opts
:=
make
(
tagOptions
)
chunks
:=
strings
.
Split
(
s
,
","
)
for
_
,
chunk
:=
range
chunks
{
opts
[
strings
.
TrimSpace
(
chunk
)]
=
true
}
return
opts
}
// Based on http://golang.org/src/pkg/encoding/json/tags.go
func
ParseTag
(
tag
string
)
(
string
,
tagOptions
)
{
if
i
:=
strings
.
Index
(
tag
,
","
);
i
!=
-
1
{
return
tag
[
:
i
],
parseTagOptions
(
tag
[
i
+
1
:
])
}
return
tag
,
parseTagOptions
(
""
)
}
func
columnCompare
(
s
string
)
string
{
func
columnCompare
(
s
string
)
string
{
return
strings
.
ToLower
(
columnCompareExclude
.
ReplaceAllString
(
s
,
""
))
return
strings
.
ToLower
(
columnCompareExclude
.
ReplaceAllString
(
s
,
""
))
}
}
...
@@ -56,35 +75,46 @@ func GetStructFieldIndex(t reflect.Type, columnName string) []int {
...
@@ -56,35 +75,46 @@ func GetStructFieldIndex(t reflect.Type, columnName string) []int {
n
:=
t
.
NumField
()
n
:=
t
.
NumField
()
columnNameLower
:=
columnCompare
(
columnName
)
for
i
:=
0
;
i
<
n
;
i
++
{
for
i
:=
0
;
i
<
n
;
i
++
{
field
:=
t
.
Field
(
i
)
field
:=
t
.
Field
(
i
)
// Field is exported.
if
field
.
PkgPath
!=
""
{
if
field
.
PkgPath
==
""
{
// Field is unexported.
continue
}
tag
:=
field
.
Tag
// Attempt to use db:"column_name"
fieldName
,
fieldOptions
:=
ParseTag
(
field
.
Tag
.
Get
(
"db"
))
// Tag: field:"columnName"
// Deprecated "field" tag.
fieldName
:=
tag
.
Get
(
"field"
)
if
deprecatedField
:=
field
.
Tag
.
Get
(
"field"
);
deprecatedField
!=
""
{
fieldName
=
deprecatedField
}
if
fieldName
!=
""
{
// Deprecated "inline" tag.
if
fieldName
==
columnName
{
if
deprecatedInline
:=
field
.
Tag
.
Get
(
"inline"
);
deprecatedInline
!=
""
{
return
[]
int
{
i
}
fieldOptions
[
"inline"
]
=
true
}
}
// Matching fieldName
if
fieldName
==
"-"
{
continue
}
}
// Simply matching column to name.
// Attempt to match field name.
fieldNameLower
:=
columnCompare
(
field
.
Name
)
if
fieldName
==
columnName
{
return
[]
int
{
i
}
}
if
fieldNameLower
==
columnNameLower
{
if
fieldName
==
""
{
if
columnCompare
(
field
.
Name
)
==
columnCompare
(
columnName
)
{
return
[]
int
{
i
}
return
[]
int
{
i
}
}
}
}
//
Tag: inline:bool
//
Inline option.
if
tag
.
Get
(
"inline"
)
==
"
true
"
{
if
fieldOptions
[
"inline"
]
==
true
{
index
:=
GetStructFieldIndex
(
field
.
Type
,
columnName
)
index
:=
GetStructFieldIndex
(
field
.
Type
,
columnName
)
if
index
!=
nil
{
if
index
!=
nil
{
res
:=
append
([]
int
{
i
},
index
...
)
res
:=
append
([]
int
{
i
},
index
...
)
...
@@ -94,8 +124,6 @@ func GetStructFieldIndex(t reflect.Type, columnName string) []int {
...
@@ -94,8 +124,6 @@ func GetStructFieldIndex(t reflect.Type, columnName string) []int {
}
}
}
// No match.
// No match.
return
nil
return
nil
}
}
...
...
This diff is collapsed.
Click to expand it.
util/sqlutil/main.go
+
45
−
31
View file @
642dbdb3
...
@@ -270,6 +270,7 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa
...
@@ -270,6 +270,7 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa
switch
item_t
.
Kind
()
{
switch
item_t
.
Kind
()
{
case
reflect
.
Struct
:
case
reflect
.
Struct
:
nfields
:=
item_v
.
NumField
()
nfields
:=
item_v
.
NumField
()
values
=
make
([]
interface
{},
0
,
nfields
)
values
=
make
([]
interface
{},
0
,
nfields
)
...
@@ -279,26 +280,31 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa
...
@@ -279,26 +280,31 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa
field
:=
item_t
.
Field
(
i
)
field
:=
item_t
.
Field
(
i
)
if
field
.
PkgPath
==
""
{
if
field
.
PkgPath
!=
""
{
// Field is unexported.
value
:=
item_v
.
Field
(
i
)
.
Interface
()
continue
}
//
Struct tags
//
Field options.
tag
:=
field
.
Tag
fieldName
,
fieldOptions
:=
util
.
ParseTag
(
field
.
Tag
.
Get
(
"db"
))
// omitempty:bool
// Deprecated "field" tag.
if
tag
.
Get
(
"omitempty"
)
==
"true"
{
if
deprecatedField
:=
field
.
Tag
.
Get
(
"field"
);
deprecatedField
!=
""
{
zero
:=
reflect
.
Zero
(
reflect
.
TypeOf
(
value
))
.
Interface
()
fieldName
=
deprecatedField
if
value
==
zero
{
continue
}
}
// Deprecated "omitempty" tag.
if
deprecatedOmitEmpty
:=
field
.
Tag
.
Get
(
"omitempty"
);
deprecatedOmitEmpty
!=
""
{
fieldOptions
[
"omitempty"
]
=
true
}
}
// field:string
// Deprecated "inline" tag.
fieldName
:=
tag
.
Get
(
"field"
)
if
deprecatedInline
:=
field
.
Tag
.
Get
(
"inline"
);
deprecatedInline
!=
""
{
fieldOptions
[
"inline"
]
=
true
}
// Processing field name.
if
fieldName
==
"-"
{
if
fieldName
==
"-"
{
// Skip the field if its tag's value is -
continue
continue
}
}
...
@@ -306,8 +312,17 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa
...
@@ -306,8 +312,17 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa
fieldName
=
self
.
ColumnLike
(
field
.
Name
)
fieldName
=
self
.
ColumnLike
(
field
.
Name
)
}
}
// inline:bool
// Processing tag options.
if
tag
.
Get
(
"inline"
)
==
"true"
{
value
:=
item_v
.
Field
(
i
)
.
Interface
()
if
fieldOptions
[
"omitempty"
]
==
true
{
zero
:=
reflect
.
Zero
(
reflect
.
TypeOf
(
value
))
.
Interface
()
if
value
==
zero
{
continue
}
}
if
fieldOptions
[
"inline"
]
==
true
{
infields
,
invalues
,
inerr
:=
self
.
FieldValues
(
value
,
convertFn
)
infields
,
invalues
,
inerr
:=
self
.
FieldValues
(
value
,
convertFn
)
if
inerr
!=
nil
{
if
inerr
!=
nil
{
return
nil
,
nil
,
inerr
return
nil
,
nil
,
inerr
...
@@ -320,7 +335,6 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa
...
@@ -320,7 +335,6 @@ func (self *T) FieldValues(item interface{}, convertFn func(interface{}) interfa
}
}
}
}
}
case
reflect
.
Map
:
case
reflect
.
Map
:
nfields
:=
item_v
.
Len
()
nfields
:=
item_v
.
Len
()
values
=
make
([]
interface
{},
nfields
)
values
=
make
([]
interface
{},
nfields
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment