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
0ba555f2
Commit
0ba555f2
authored
May 12, 2015
by
Peter Kieltyka
Browse files
Options
Downloads
Patches
Plain Diff
Use reflectx refactored code for struct traversing, its cached so will be faster
parent
c486e70c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
postgresql/database.go
+1
-0
1 addition, 0 deletions
postgresql/database.go
postgresql/database_test.go
+99
-32
99 additions, 32 deletions
postgresql/database_test.go
util/main.go
+0
-55
0 additions, 55 deletions
util/main.go
util/sqlutil/main.go
+23
-76
23 additions, 76 deletions
util/sqlutil/main.go
with
123 additions
and
163 deletions
postgresql/database.go
+
1
−
0
View file @
0ba555f2
...
...
@@ -166,6 +166,7 @@ func (s *source) Collection(names ...string) (db.Collection, error) {
source
:
s
,
names
:
names
,
}
col
.
T
.
Mapper
=
s
.
session
.
Mapper
for
_
,
name
:=
range
names
{
chunks
:=
strings
.
SplitN
(
name
,
` `
,
2
)
...
...
This diff is collapsed.
Click to expand it.
postgresql/database_test.go
+
99
−
32
View file @
0ba555f2
...
...
@@ -175,7 +175,7 @@ func TestOpenFailed(t *testing.T) {
}
// Attempts to open an empty datasource.
func
TestOpenWithWrongData
(
t
*
testing
.
T
)
{
func
Skip
TestOpenWithWrongData
(
t
*
testing
.
T
)
{
var
err
error
var
rightSettings
,
wrongSettings
db
.
Settings
...
...
@@ -496,34 +496,37 @@ func TestResultFetch(t *testing.T) {
res
.
Close
()
// NOTE: tags are required.. unless a different type mapper
// is specified..
// Dumping into an struct with no tags.
rowStruct
:=
struct
{
ID
uint64
Name
string
}{}
//
rowStruct := struct {
//
ID uint64
`db:"id,omitempty"`
//
Name string
`db:"name"`
//
}{}
res
=
artist
.
Find
()
//
res = artist.Find()
for
{
err
=
res
.
Next
(
&
rowStruct
)
//
for {
//
err = res.Next(&rowStruct)
if
err
==
db
.
ErrNoMoreRows
{
break
}
//
if err == db.ErrNoMoreRows {
//
break
//
}
if
err
==
nil
{
if
rowStruct
.
ID
==
0
{
t
.
Fatalf
(
"Expecting a not null ID."
)
}
if
rowStruct
.
Name
==
""
{
t
.
Fatalf
(
"Expecting a name."
)
}
}
else
{
t
.
Fatal
(
err
)
}
}
//
if err == nil {
//
if rowStruct.ID == 0 {
//
t.Fatalf("Expecting a not null ID.")
//
}
//
if rowStruct.Name == "" {
//
t.Fatalf("Expecting a name.")
//
}
//
} else {
//
t.Fatal(err)
//
}
//
}
res
.
Close
()
//
res.Close()
// Dumping into a tagged struct.
rowStruct2
:=
struct
{
...
...
@@ -574,8 +577,8 @@ func TestResultFetch(t *testing.T) {
// Dumping into a slice of structs.
allRowsStruct
:=
[]
struct
{
ID
uint64
Name
string
ID
uint64
`db:"id,omitempty"`
Name
string
`db:"name"`
}{}
res
=
artist
.
Find
()
...
...
@@ -710,6 +713,70 @@ func TestResultFetchAll(t *testing.T) {
}
}
func
TestInlineStructs
(
t
*
testing
.
T
)
{
var
sess
db
.
Database
var
err
error
var
review
db
.
Collection
type
reviewTypeDetails
struct
{
Name
string
`db:"name"`
Comments
string
`db:"comments"`
Created
time
.
Time
`db:"created"`
}
type
reviewType
struct
{
ID
int64
`db:"id,omitempty"`
PublicationID
int64
`db:"publication_id"`
Details
reviewTypeDetails
`db:",inline"`
}
if
sess
,
err
=
db
.
Open
(
Adapter
,
settings
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
sess
.
Close
()
if
review
,
err
=
sess
.
Collection
(
"review"
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
err
=
review
.
Truncate
();
err
!=
nil
{
t
.
Fatal
(
err
)
}
rec
:=
reviewType
{
PublicationID
:
123
,
Details
:
reviewTypeDetails
{
Name
:
"..name.."
,
Comments
:
"..comments.."
,
},
}
id
,
err
:=
review
.
Append
(
rec
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
id
.
(
int64
)
<=
0
{
t
.
Fatal
(
"bad id"
)
}
rec
.
ID
=
id
.
(
int64
)
var
recChk
reviewType
err
=
review
.
Find
()
.
One
(
&
recChk
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
recChk
.
ID
!=
rec
.
ID
{
t
.
Fatal
(
"ID of review does not match, expecting:"
,
rec
.
ID
,
"got:"
,
recChk
.
ID
)
}
if
recChk
.
Details
.
Name
!=
rec
.
Details
.
Name
{
t
.
Fatal
(
"Name of inline field does not match, expecting:"
,
rec
.
Details
.
Name
,
"got:"
,
recChk
.
Details
.
Name
)
}
}
// Attempts to modify previously added rows.
func
TestUpdate
(
t
*
testing
.
T
)
{
var
err
error
...
...
@@ -728,8 +795,8 @@ func TestUpdate(t *testing.T) {
// Defining destination struct
value
:=
struct
{
ID
uint64
Name
string
ID
uint64
`db:"id,omitempty"`
Name
string
`db:"name"`
}{}
// Getting the first artist.
...
...
@@ -760,7 +827,7 @@ func TestUpdate(t *testing.T) {
// Updating set with a struct
rowStruct
:=
struct
{
Name
string
Name
string
`db:"name"`
}{
strings
.
ToLower
(
value
.
Name
)}
if
err
=
res
.
Update
(
rowStruct
);
err
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
util/main.go
deleted
100644 → 0
+
0
−
55
View file @
c486e70c
// Copyright (c) 2012-2015 José Carlos Nieto, https://menteslibres.net/xiam
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package
util
import
(
"regexp"
"strings"
)
var
reColumnCompareExclude
=
regexp
.
MustCompile
(
`[^a-zA-Z0-9]`
)
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
}
// ParseTag splits a struct tag into comma separated chunks. The first chunk is
// returned as a string value, remaining chunks are considered enabled options.
func
ParseTag
(
tag
string
)
(
string
,
tagOptions
)
{
// Based on http://golang.org/src/pkg/encoding/json/tags.go
if
i
:=
strings
.
Index
(
tag
,
`,`
);
i
!=
-
1
{
return
tag
[
:
i
],
parseTagOptions
(
tag
[
i
+
1
:
])
}
return
tag
,
parseTagOptions
(
``
)
}
// NormalizeColumn prepares a column for comparison against another column.
func
NormalizeColumn
(
s
string
)
string
{
return
strings
.
ToLower
(
reColumnCompareExclude
.
ReplaceAllString
(
s
,
""
))
}
This diff is collapsed.
Click to expand it.
util/sqlutil/main.go
+
23
−
76
View file @
0ba555f2
...
...
@@ -32,11 +32,11 @@ import (
"menteslibres.net/gosexy/to"
"upper.io/db"
"upper.io/db/util"
)
var
(
reInvisibleChars
=
regexp
.
MustCompile
(
`[\s\r\n\t]+`
)
reColumnCompareExclude
=
regexp
.
MustCompile
(
`[^a-zA-Z0-9]`
)
)
var
(
...
...
@@ -46,15 +46,21 @@ var (
nullStringType
=
reflect
.
TypeOf
(
sql
.
NullString
{})
)
// NormalizeColumn prepares a column for comparison against another column.
func
NormalizeColumn
(
s
string
)
string
{
return
strings
.
ToLower
(
reColumnCompareExclude
.
ReplaceAllString
(
s
,
""
))
}
// T type is commonly used by adapters to map database/sql values to Go values
// using FieldValues()
type
T
struct
{
Columns
[]
string
Mapper
*
reflectx
.
Mapper
}
func
(
t
*
T
)
columnLike
(
s
string
)
string
{
for
_
,
name
:=
range
t
.
Columns
{
if
util
.
NormalizeColumn
(
s
)
==
util
.
NormalizeColumn
(
name
)
{
if
NormalizeColumn
(
s
)
==
NormalizeColumn
(
name
)
{
return
name
}
}
...
...
@@ -62,14 +68,12 @@ func (t *T) columnLike(s string) string {
}
func
marshal
(
v
interface
{})
(
interface
{},
error
)
{
if
m
,
isMarshaler
:=
v
.
(
db
.
Marshaler
);
isMarshaler
{
var
err
error
if
v
,
err
=
m
.
MarshalDB
();
err
!=
nil
{
return
nil
,
err
}
}
return
v
,
nil
}
...
...
@@ -90,79 +94,31 @@ func (t *T) FieldValues(item interface{}) ([]string, []interface{}, error) {
switch
itemT
.
Kind
()
{
case
reflect
.
Struct
:
nfields
:=
itemV
.
NumField
()
fieldMap
:=
t
.
Mapper
.
TypeMap
(
itemT
)
.
FieldMap
()
nfields
:=
len
(
fieldMap
)
values
=
make
([]
interface
{},
0
,
nfields
)
fields
=
make
([]
string
,
0
,
nfields
)
for
i
:=
0
;
i
<
nfields
;
i
++
{
field
:=
itemT
.
Field
(
i
)
if
field
.
PkgPath
!=
``
{
// Field is unexported.
continue
}
// TODO: can we get the placeholder used above somewhere...?
// from the sqlx part..?
for
_
,
fi
:=
range
fieldMap
{
value
:=
reflectx
.
FieldByIndexesReadOnly
(
itemV
,
fi
.
Index
)
.
Interface
()
if
field
.
Anonymous
{
// It's an anonymous field. Let's skip it unless it has an explicit
// `db` tag.
if
field
.
Tag
.
Get
(
`db`
)
==
``
{
if
_
,
ok
:=
fi
.
Options
[
"omitempty"
];
ok
{
if
value
==
fi
.
Zero
.
Interface
()
{
continue
}
}
// Field options.
fieldName
,
fieldOptions
:=
util
.
ParseTag
(
field
.
Tag
.
Get
(
`db`
))
// Skipping field
if
fieldName
==
`-`
{
continue
}
// Trying to match field name.
// Still don't have a match? try to match againt JSON.
if
fieldName
==
``
{
fieldName
,
_
=
util
.
ParseTag
(
field
.
Tag
.
Get
(
`json`
))
}
// TODO: columnLike stuff...?
// Nothing works, trying to match by name.
if
fieldName
==
``
{
fieldName
=
t
.
columnLike
(
field
.
Name
)
}
// Processing tag options.
value
:=
itemV
.
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
:=
t
.
FieldValues
(
value
)
if
inerr
!=
nil
{
return
nil
,
nil
,
inerr
}
fields
=
append
(
fields
,
infields
...
)
values
=
append
(
values
,
invalues
...
)
}
else
{
fields
=
append
(
fields
,
fieldName
)
fields
=
append
(
fields
,
fi
.
Name
)
v
,
err
:=
marshal
(
value
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
values
=
append
(
values
,
v
)
}
}
case
reflect
.
Map
:
nfields
:=
itemV
.
Len
()
...
...
@@ -199,14 +155,5 @@ func reset(data interface{}) error {
// NewMapper creates a reflectx.Mapper
func
NewMapper
()
*
reflectx
.
Mapper
{
mapFunc
:=
strings
.
ToLower
tagFunc
:=
func
(
value
string
)
string
{
if
strings
.
Contains
(
value
,
","
)
{
return
strings
.
Split
(
value
,
","
)[
0
]
}
return
value
}
return
reflectx
.
NewMapperTagFunc
(
"db"
,
mapFunc
,
tagFunc
)
return
reflectx
.
NewMapper
(
"db"
)
}
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