good morning!!!!

Skip to content
Snippets Groups Projects
Commit 23c5c597 authored by José Carlos Nieto's avatar José Carlos Nieto
Browse files

Removing gosexy/to, flags and making golint happy + some code cosmetics.

parent 62f79257
Branches
Tags
No related merge requests found
...@@ -28,7 +28,6 @@ import ( ...@@ -28,7 +28,6 @@ import (
"strings" "strings"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"menteslibres.net/gosexy/to"
"upper.io/db" "upper.io/db"
"upper.io/db/util/sqlgen" "upper.io/db/util/sqlgen"
"upper.io/db/util/sqlutil" "upper.io/db/util/sqlutil"
...@@ -162,7 +161,7 @@ func conditionValues(cond db.Cond) (columnValues sqlgen.ColumnValues, args []int ...@@ -162,7 +161,7 @@ func conditionValues(cond db.Cond) (columnValues sqlgen.ColumnValues, args []int
l := len(v) l := len(v)
if v == nil || l == 0 { if v == nil || l == 0 {
// Nil value given. // Nil value given.
columnValue.Value = sqlgen.Value{sqlgen.Raw{`NULL`}} columnValue.Value = sqlgen.Value{sqlgen.Raw{psqlNull}}
} else { } else {
if l > 1 { if l > 1 {
// Array value given. // Array value given.
...@@ -238,6 +237,7 @@ func (t *table) Append(item interface{}) (interface{}, error) { ...@@ -238,6 +237,7 @@ func (t *table) Append(item interface{}) (interface{}, error) {
arguments = make([]interface{}, 0, len(vals)) arguments = make([]interface{}, 0, len(vals))
values = make(sqlgen.Values, 0, len(vals)) values = make(sqlgen.Values, 0, len(vals))
for i := range vals { for i := range vals {
switch v := vals[i].(type) { switch v := vals[i].(type) {
case sqlgen.Value: case sqlgen.Value:
...@@ -304,15 +304,13 @@ func (t *table) Append(item interface{}) (interface{}, error) { ...@@ -304,15 +304,13 @@ func (t *table) Append(item interface{}) (interface{}, error) {
return nil, nil return nil, nil
} }
// The IDSetter interface does not match, we'll be looking for another // The IDSetter interface does not match, look for another interface match.
// interface match.
if len(keyMap) == 1 { if len(keyMap) == 1 {
id := keyMap[pKey[0]] id := keyMap[pKey[0]]
// Matches db.Int64IDSetter // Matches db.Int64IDSetter
if setter, ok := item.(db.Int64IDSetter); ok { if setter, ok := item.(db.Int64IDSetter); ok {
if err = setter.SetID(to.Int64(id)); err != nil { if err = setter.SetID(id.(int64)); err != nil {
return nil, err return nil, err
} }
return nil, nil return nil, nil
...@@ -320,14 +318,14 @@ func (t *table) Append(item interface{}) (interface{}, error) { ...@@ -320,14 +318,14 @@ func (t *table) Append(item interface{}) (interface{}, error) {
// Matches db.Uint64IDSetter // Matches db.Uint64IDSetter
if setter, ok := item.(db.Uint64IDSetter); ok { if setter, ok := item.(db.Uint64IDSetter); ok {
if err = setter.SetID(to.Uint64(id)); err != nil { if err = setter.SetID(uint64(id.(int64))); err != nil {
return nil, err return nil, err
} }
return nil, nil return nil, nil
} }
// No interface matched, falling back to old behaviour. // No interface matched, falling back to old behaviour.
return to.Int64(id), nil return id.(int64), nil
} }
// More than one key, no interface matched, let's return a map. // More than one key, no interface matched, let's return a map.
......
...@@ -24,7 +24,7 @@ package postgresql ...@@ -24,7 +24,7 @@ package postgresql
import ( import (
"database/sql" "database/sql"
"errors" "errors"
"flag" "fmt"
"os" "os"
"reflect" "reflect"
"strconv" "strconv"
...@@ -52,7 +52,7 @@ var settings = ConnectionURL{ ...@@ -52,7 +52,7 @@ var settings = ConnectionURL{
Password: password, Password: password,
} }
var host = flag.String("host", "localhost", "Testing server address.") var host string
// Structure for testing conversions and datatypes. // Structure for testing conversions and datatypes.
type testValuesStruct struct { type testValuesStruct struct {
...@@ -122,8 +122,13 @@ func (item *itemWithKey) SetID(keys map[string]interface{}) error { ...@@ -122,8 +122,13 @@ func (item *itemWithKey) SetID(keys map[string]interface{}) error {
var testValues testValuesStruct var testValues testValuesStruct
func init() { func init() {
loc, _ := time.LoadLocation("Canada/Eastern") loc, err := time.LoadLocation("Canada/Eastern")
t := time.Date(2012, 7, 28, 1, 2, 3, 0, loc) // timestamp with time zone
if err != nil {
panic(err.Error())
}
t := time.Date(2011, 7, 28, 1, 2, 3, 0, loc) // timestamp with time zone
tnz := time.Date(2012, 7, 28, 1, 2, 3, 0, time.FixedZone("", 0)) // timestamp without time zone tnz := time.Date(2012, 7, 28, 1, 2, 3, 0, time.FixedZone("", 0)) // timestamp without time zone
testValues = testValuesStruct{ testValues = testValuesStruct{
...@@ -139,8 +144,11 @@ func init() { ...@@ -139,8 +144,11 @@ func init() {
int64(time.Second * time.Duration(7331)), int64(time.Second * time.Duration(7331)),
} }
flag.Parse() if host = os.Getenv("TEST_HOST"); host == "" {
settings.Address = db.ParseAddress(*host) host = "localhost"
}
settings.Address = db.ParseAddress(host)
} }
// Logging some information to stdout (like the SQL query and its // Logging some information to stdout (like the SQL query and its
...@@ -168,7 +176,7 @@ func TestOpenWithWrongData(t *testing.T) { ...@@ -168,7 +176,7 @@ func TestOpenWithWrongData(t *testing.T) {
// Attempt to open with safe settings. // Attempt to open with safe settings.
rightSettings = db.Settings{ rightSettings = db.Settings{
Database: database, Database: database,
Host: *host, Host: host,
User: username, User: username,
Password: password, Password: password,
} }
...@@ -182,7 +190,7 @@ func TestOpenWithWrongData(t *testing.T) { ...@@ -182,7 +190,7 @@ func TestOpenWithWrongData(t *testing.T) {
// Attempt to open with wrong password. // Attempt to open with wrong password.
wrongSettings = db.Settings{ wrongSettings = db.Settings{
Database: database, Database: database,
Host: *host, Host: host,
User: username, User: username,
Password: "fail", Password: "fail",
} }
...@@ -194,7 +202,7 @@ func TestOpenWithWrongData(t *testing.T) { ...@@ -194,7 +202,7 @@ func TestOpenWithWrongData(t *testing.T) {
// Attempt to open with wrong database. // Attempt to open with wrong database.
wrongSettings = db.Settings{ wrongSettings = db.Settings{
Database: "fail", Database: "fail",
Host: *host, Host: host,
User: username, User: username,
Password: password, Password: password,
} }
...@@ -206,7 +214,7 @@ func TestOpenWithWrongData(t *testing.T) { ...@@ -206,7 +214,7 @@ func TestOpenWithWrongData(t *testing.T) {
// Attempt to open with wrong username. // Attempt to open with wrong username.
wrongSettings = db.Settings{ wrongSettings = db.Settings{
Database: database, Database: database,
Host: *host, Host: host,
User: "fail", User: "fail",
Password: password, Password: password,
} }
...@@ -225,7 +233,7 @@ func TestOldSettings(t *testing.T) { ...@@ -225,7 +233,7 @@ func TestOldSettings(t *testing.T) {
Database: database, Database: database,
User: username, User: username,
Password: password, Password: password,
Host: *host, Host: host,
} }
// Opening database. // Opening database.
...@@ -1551,19 +1559,21 @@ func TestDataTypes(t *testing.T) { ...@@ -1551,19 +1559,21 @@ func TestDataTypes(t *testing.T) {
// Trying to dump the subject into an empty structure of the same type. // Trying to dump the subject into an empty structure of the same type.
var item testValuesStruct var item testValuesStruct
err = res.One(&item) if err = res.One(&item); err != nil {
if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if item.DateD == nil { if item.DateD == nil {
t.Fatal("Expecting default date to have been set on append") t.Fatal("Expecting default date to have been set on append")
} }
// Copy the default date
// Copy the default date (this value is set by the database)
testValues.DateD = item.DateD testValues.DateD = item.DateD
// The original value and the test subject must match. // The original value and the test subject must match.
if reflect.DeepEqual(item, testValues) == false { if reflect.DeepEqual(item, testValues) == false {
fmt.Printf("item1: %v\n", item)
fmt.Printf("test2: %v\n", testValues)
t.Fatalf("Struct is different.") t.Fatalf("Struct is different.")
} }
} }
......
...@@ -92,6 +92,7 @@ func (r *result) Group(fields ...interface{}) db.Result { ...@@ -92,6 +92,7 @@ func (r *result) Group(fields ...interface{}) db.Result {
groupByColumns := make(sqlgen.GroupBy, 0, len(fields)) groupByColumns := make(sqlgen.GroupBy, 0, len(fields))
l := len(fields) l := len(fields)
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
switch value := fields[i].(type) { switch value := fields[i].(type) {
// Maybe other types? // Maybe other types?
...@@ -217,14 +218,14 @@ func (r *result) One(dst interface{}) error { ...@@ -217,14 +218,14 @@ func (r *result) One(dst interface{}) error {
} }
// Fetches the next result from the resultset. // Fetches the next result from the resultset.
func (r *result) Next(dst interface{}) error { func (r *result) Next(dst interface{}) (err error) {
err := r.setCursor()
if err != nil { if err = r.setCursor(); err != nil {
r.Close() r.Close()
return err return err
} }
if err := sqlutil.FetchRow(r.cursor, dst); err != nil { if err = sqlutil.FetchRow(r.cursor, dst); err != nil {
r.Close() r.Close()
return err return err
} }
...@@ -235,11 +236,13 @@ func (r *result) Next(dst interface{}) error { ...@@ -235,11 +236,13 @@ func (r *result) Next(dst interface{}) error {
// Removes the matching items from the collection. // Removes the matching items from the collection.
func (r *result) Remove() error { func (r *result) Remove() error {
var err error var err error
_, err = r.table.source.doExec(sqlgen.Statement{ _, err = r.table.source.doExec(sqlgen.Statement{
Type: sqlgen.SqlDelete, Type: sqlgen.SqlDelete,
Table: sqlgen.Table{r.table.Name()}, Table: sqlgen.Table{r.table.Name()},
Where: r.where, Where: r.where,
}, r.arguments...) }, r.arguments...)
return err return err
} }
...@@ -274,8 +277,7 @@ func (r *result) Update(values interface{}) error { ...@@ -274,8 +277,7 @@ func (r *result) Update(values interface{}) error {
} }
// Closes the result set. // Closes the result set.
func (r *result) Close() error { func (r *result) Close() (err error) {
var err error
if r.cursor != nil { if r.cursor != nil {
err = r.cursor.Close() err = r.cursor.Close()
r.cursor = nil r.cursor = nil
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment