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
eda1364e
Commit
eda1364e
authored
Apr 27, 2013
by
José Carlos Nieto
Browse files
Options
Downloads
Patches
Plain Diff
Fixing a conversion bug with int64 and time.Duration.
parent
316a203b
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
mysql/mysql_test.go
+7
-7
7 additions, 7 deletions
mysql/mysql_test.go
util/main.go
+33
-13
33 additions, 13 deletions
util/main.go
util/sqlutil/main.go
+22
-14
22 additions, 14 deletions
util/sqlutil/main.go
with
62 additions
and
34 deletions
mysql/mysql_test.go
+
7
−
7
View file @
eda1364e
...
...
@@ -73,7 +73,7 @@ func TestOpenFailed(t *testing.T) {
_
,
err
:=
db
.
Open
(
wrapperName
,
db
.
DataSource
{})
if
err
==
nil
{
t
.
Error
f
(
"Could not open database."
)
t
.
Fatal
f
(
"Could not open database."
)
}
}
...
...
@@ -103,7 +103,7 @@ func TestTruncate(t *testing.T) {
}
if
total
!=
0
{
t
.
Error
f
(
"Could not truncate."
)
t
.
Fatal
f
(
"Could not truncate."
)
}
}
...
...
@@ -422,7 +422,7 @@ func TestPopulate(t *testing.T) {
sess
,
err
:=
db
.
Open
(
wrapperName
,
settings
)
if
err
!=
nil
{
t
.
Error
f
(
err
.
Error
())
t
.
Fatal
f
(
err
.
Error
())
}
defer
sess
.
Close
()
...
...
@@ -482,7 +482,7 @@ func TestRelation(t *testing.T) {
sess
,
err
:=
db
.
Open
(
wrapperName
,
settings
)
if
err
!=
nil
{
t
.
Error
f
(
err
.
Error
())
t
.
Fatal
f
(
err
.
Error
())
}
defer
sess
.
Close
()
...
...
@@ -539,7 +539,7 @@ func TestRelationStruct(t *testing.T) {
sess
,
err
:=
db
.
Open
(
wrapperName
,
settings
)
if
err
!=
nil
{
t
.
Error
f
(
err
.
Error
())
t
.
Fatal
f
(
err
.
Error
())
}
defer
sess
.
Close
()
...
...
@@ -632,7 +632,7 @@ func TestDataTypes(t *testing.T) {
}
if
found
==
0
{
t
.
Error
f
(
"Expecting an item."
)
t
.
Fatal
f
(
"Expecting an item."
)
}
// Getting and reinserting (a db.Item).
...
...
@@ -695,7 +695,7 @@ func TestDataTypes(t *testing.T) {
// Testing struct equality
for
_
,
item
:=
range
sresults
{
if
reflect
.
DeepEqual
(
item
,
testValues
)
==
false
{
t
.
Error
f
(
"Struct is different."
)
t
.
Fatal
f
(
"Struct is different."
)
}
}
...
...
This diff is collapsed.
Click to expand it.
util/main.go
+
33
−
13
View file @
eda1364e
...
...
@@ -34,6 +34,12 @@ import (
"time"
)
var
(
ErrExpectingPointer
=
errors
.
New
(
`Expecting a pointer destination (dst interface{}).`
)
ErrExpectingSlicePointer
=
errors
.
New
(
`Expecting a pointer to an slice (dst interface{}).`
)
ErrExpectingSliceMapStruct
=
errors
.
New
(
`Expecting a pointer to an slice of maps or structs (dst interface{}).`
)
)
var
extRelationPattern
=
regexp
.
MustCompile
(
`\{(.+)\}`
)
var
columnCompareExclude
=
regexp
.
MustCompile
(
`[^a-zA-Z0-9]`
)
...
...
@@ -305,7 +311,7 @@ func (self *C) FetchRelation(dst interface{}, relations []db.Relation, convertFn
dstv
:=
reflect
.
ValueOf
(
dst
)
if
dstv
.
Kind
()
!=
reflect
.
Ptr
||
dstv
.
IsNil
()
{
return
e
rr
ors
.
New
(
"
Expecting
a p
ointer
."
)
return
E
rrExpecting
P
ointer
}
err
=
fetchItemRelations
(
dstv
.
Elem
(),
relations
,
convertFn
)
...
...
@@ -350,21 +356,39 @@ func ValidateSliceDestination(dst interface{}) error {
// Checking input
dstv
=
reflect
.
ValueOf
(
dst
)
if
dstv
.
Kind
()
!=
reflect
.
Ptr
||
dstv
.
IsNil
()
||
dstv
.
Elem
()
.
Kind
()
!=
reflect
.
Slice
{
return
errors
.
New
(
"Expecting a pointer to slice."
)
if
dstv
.
IsNil
()
||
dstv
.
Kind
()
!=
reflect
.
Ptr
{
return
ErrExpectingPointer
}
if
dstv
.
Elem
()
.
Kind
()
!=
reflect
.
Slice
{
return
ErrExpectingSlicePointer
}
itemv
=
dstv
.
Elem
()
itemk
=
itemv
.
Type
()
.
Elem
()
.
Kind
()
if
itemk
!=
reflect
.
Struct
&&
itemk
!=
reflect
.
Map
{
return
e
rr
ors
.
New
(
"Expecting a pointer to slice of maps or s
truct
s."
)
return
E
rr
ExpectingSliceMapS
truct
}
return
nil
}
func
ConvertValue
(
src
string
,
dstk
reflect
.
Kind
)
(
reflect
.
Value
,
error
)
{
func
StringToType
(
src
string
,
dstt
reflect
.
Type
)
(
reflect
.
Value
,
error
)
{
var
srcv
reflect
.
Value
switch
dstt
{
case
durationType
:
srcv
=
reflect
.
ValueOf
(
to
.
Duration
(
src
))
case
timeType
:
// Destination is time.Time
srcv
=
reflect
.
ValueOf
(
to
.
Time
(
src
))
default
:
return
StringToKind
(
src
,
dstt
.
Kind
())
}
return
srcv
,
nil
}
func
StringToKind
(
src
string
,
dstk
reflect
.
Kind
)
(
reflect
.
Value
,
error
)
{
var
srcv
reflect
.
Value
// Destination type.
...
...
@@ -372,15 +396,11 @@ func ConvertValue(src string, dstk reflect.Kind) (reflect.Value, error) {
case
reflect
.
Interface
:
// Destination is interface, nuff said.
srcv
=
reflect
.
ValueOf
(
src
)
case
durationType
.
Kind
()
:
// Destination is time.Duration
srcv
=
reflect
.
ValueOf
(
to
.
Duration
(
src
))
case
timeType
.
Kind
()
:
// Destination is time.Time
srcv
=
reflect
.
ValueOf
(
to
.
Time
(
src
))
default
:
// Destination is of an unknown type.
cv
,
_
:=
to
.
Convert
(
src
,
dstk
)
cv
,
err
:=
to
.
Convert
(
src
,
dstk
)
if
err
!=
nil
{
return
srcv
,
nil
}
srcv
=
reflect
.
ValueOf
(
cv
)
}
...
...
This diff is collapsed.
Click to expand it.
util/sqlutil/main.go
+
22
−
14
View file @
eda1364e
...
...
@@ -34,6 +34,10 @@ import (
"strings"
)
var
(
ErrNoMoreRows
=
errors
.
New
(
`There are no more rows in this result set.`
)
)
type
T
struct
{
PrimaryKey
string
ColumnTypes
map
[
string
]
reflect
.
Kind
...
...
@@ -103,21 +107,18 @@ func (self *T) fetchResult(itemt reflect.Type, rows *sql.Rows, columns []string)
var
cv
reflect
.
Value
if
_
,
ok
:=
self
.
ColumnTypes
[
column
];
ok
==
true
{
v
,
_
:=
to
.
Convert
(
s
tring
(
*
value
)
,
self
.
ColumnTypes
[
column
])
v
,
_
:=
to
.
Convert
(
svalue
,
self
.
ColumnTypes
[
column
])
cv
=
reflect
.
ValueOf
(
v
)
}
else
{
v
,
_
:=
to
.
Convert
(
s
tring
(
*
value
)
,
reflect
.
String
)
v
,
_
:=
to
.
Convert
(
svalue
,
reflect
.
String
)
cv
=
reflect
.
ValueOf
(
v
)
}
switch
itemt
.
Kind
()
{
// Destination is a map.
case
reflect
.
Map
:
if
cv
.
Type
()
.
Kind
()
!=
itemt
.
Elem
()
.
Kind
()
{
if
itemt
.
Elem
()
.
Kind
()
!=
reflect
.
Interface
{
// Converting value.
cv
,
_
=
util
.
ConvertValue
(
svalue
,
itemt
.
Elem
()
.
Kind
())
}
if
cv
.
Type
()
!=
itemt
.
Elem
()
{
cv
,
_
=
util
.
StringToType
(
svalue
,
itemt
.
Elem
())
}
if
cv
.
IsValid
()
{
item
.
SetMapIndex
(
reflect
.
ValueOf
(
column
),
cv
)
...
...
@@ -135,10 +136,9 @@ func (self *T) fetchResult(itemt reflect.Type, rows *sql.Rows, columns []string)
destf
:=
item
.
Elem
()
.
FieldByIndex
(
index
)
if
destf
.
IsValid
()
{
if
cv
.
Type
()
.
Kind
()
!=
destf
.
Type
()
.
Kind
()
{
if
cv
.
Type
()
!=
destf
.
Type
()
{
if
destf
.
Type
()
.
Kind
()
!=
reflect
.
Interface
{
// Converting value.
cv
,
_
=
util
.
ConvertValue
(
svalue
,
destf
.
Type
()
.
Kind
())
cv
,
_
=
util
.
StringToType
(
svalue
,
destf
.
Type
())
}
}
// Copying value.
...
...
@@ -178,8 +178,8 @@ func (self *T) FetchRow(dst interface{}, rows *sql.Rows) error {
dstv
:=
reflect
.
ValueOf
(
dst
)
if
dstv
.
Kind
()
!=
reflect
.
Ptr
||
dstv
.
IsNil
()
{
return
errors
.
New
(
"fetchRows expects a pointer to slice."
)
if
dstv
.
IsNil
()
||
dstv
.
Kind
()
!=
reflect
.
Ptr
{
return
util
.
ErrExpectingPointer
}
itemv
:=
dstv
.
Elem
()
...
...
@@ -193,7 +193,7 @@ func (self *T) FetchRow(dst interface{}, rows *sql.Rows) error {
next
:=
rows
.
Next
()
if
next
==
false
{
return
fmt
.
Errorf
(
"No m
ore
r
ows
."
)
return
ErrNoM
ore
R
ows
}
item
,
err
:=
self
.
fetchResult
(
itemv
.
Type
(),
rows
,
columns
)
...
...
@@ -215,8 +215,16 @@ func (self *T) FetchRows(dst interface{}, rows *sql.Rows) error {
// Destination.
dstv
:=
reflect
.
ValueOf
(
dst
)
if
dstv
.
IsNil
()
||
dstv
.
Kind
()
!=
reflect
.
Ptr
{
return
util
.
ErrExpectingPointer
}
if
dstv
.
Elem
()
.
Kind
()
!=
reflect
.
Slice
{
return
util
.
ErrExpectingSlicePointer
}
if
dstv
.
Kind
()
!=
reflect
.
Ptr
||
dstv
.
Elem
()
.
Kind
()
!=
reflect
.
Slice
||
dstv
.
IsNil
()
{
return
errors
.
New
(
"Expecting a pointer to slice of maps or s
truct
s."
)
return
util
.
ErrExpectingSliceMapS
truct
}
columns
,
err
:=
getRowColumns
(
rows
)
...
...
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