good morning!!!!

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

Formatting with go fmt.

parent 9797142f
No related branches found
No related tags found
No related merge requests found
......@@ -24,14 +24,14 @@
package db
import (
"strings"
"reflect"
"time"
"net/url"
"regexp"
. "github.com/xiam/gosexy"
"launchpad.net/mgo"
"launchpad.net/mgo/bson"
. "github.com/xiam/gosexy"
"net/url"
"reflect"
"regexp"
"strings"
"time"
)
// MongoDB session.
......@@ -51,7 +51,7 @@ type MongoDBCollection struct {
func (c *MongoDBCollection) marshal(where Where) map[string]interface{} {
conds := make(map[string]interface{})
for key, val := range(where) {
for key, val := range where {
key = strings.Trim(key, " ")
chunks := strings.Split(key, " ")
......@@ -107,7 +107,8 @@ func (c *MongoDBCollection) Append(items ...interface {}) bool {
// Compiles terms into conditions that mgo can understand.
func (c *MongoDBCollection) compileConditions(term interface{}) interface{} {
switch term.(type) {
case []interface{}: {
case []interface{}:
{
values := []interface{}{}
itop := len(term.([]interface{}))
for i := 0; i < itop; i++ {
......@@ -120,7 +121,8 @@ func (c *MongoDBCollection) compileConditions(term interface{}) interface{} {
return values
}
}
case Or: {
case Or:
{
values := []interface{}{}
itop := len(term.(Or))
for i := 0; i < itop; i++ {
......@@ -129,7 +131,8 @@ func (c *MongoDBCollection) compileConditions(term interface{}) interface{} {
condition := map[string]interface{}{"$or": values}
return condition
}
case And: {
case And:
{
values := []interface{}{}
itop := len(term.(And))
for i := 0; i < itop; i++ {
......@@ -138,7 +141,8 @@ func (c *MongoDBCollection) compileConditions(term interface{}) interface{} {
condition := map[string]interface{}{"$and": values}
return condition
}
case Where: {
case Where:
{
return c.marshal(term.(Where))
}
}
......@@ -195,7 +199,8 @@ func (c *MongoDBCollection) Remove(terms ...interface{}) bool {
term := terms[i]
switch term.(type) {
case multiFlag: {
case multiFlag:
{
multi = term.(multiFlag)
}
}
......@@ -264,22 +269,25 @@ func (c *MongoDBCollection) Update(terms ...interface{}) bool {
term := terms[i]
switch term.(type) {
case Set: {
case Set:
{
set = term.(Set)
}
case Upsert: {
case Upsert:
{
upsert = term.(Upsert)
}
case Modify: {
case Modify:
{
modify = term.(Modify)
}
case multiFlag: {
case multiFlag:
{
multi = term.(multiFlag)
}
}
}
if multi != nil {
if set != nil {
......@@ -400,13 +408,16 @@ func (c *MongoDBCollection) BuildQuery(terms ...interface{}) *mgo.Query {
term := terms[i]
switch term.(type) {
case Limit: {
case Limit:
{
limit = int(term.(Limit))
}
case Offset: {
case Offset:
{
offset = int(term.(Offset))
}
case Sort: {
case Sort:
{
sort = term.(Sort)
}
}
......@@ -459,10 +470,12 @@ func (c *MongoDBCollection) FindAll(terms ...interface{}) []Item {
term := terms[i]
switch term.(type) {
case Relate: {
case Relate:
{
relate = term.(Relate)
}
case RelateAll: {
case RelateAll:
{
relateAll = term.(RelateAll)
}
}
......@@ -486,13 +499,14 @@ func (c *MongoDBCollection) FindAll(terms ...interface{}) []Item {
for t := ttop - 1; t >= 0; t-- {
rterm := rterms[t]
switch rterm.(type) {
case Collection: {
case Collection:
{
rcollection = rterm.(Collection)
}
}
}
relations = append(relations, Tuple { "all": false, "name": rname, "collection": rcollection, "terms": rterms, })
relations = append(relations, Tuple{"all": false, "name": rname, "collection": rcollection, "terms": rterms})
}
}
......@@ -504,13 +518,14 @@ func (c *MongoDBCollection) FindAll(terms ...interface{}) []Item {
for t := ttop - 1; t >= 0; t-- {
rterm := rterms[t]
switch rterm.(type) {
case Collection: {
case Collection:
{
rcollection = rterm.(Collection)
}
}
}
relations = append(relations, Tuple { "all": true, "name": rname, "collection": rcollection, "terms": rterms, })
relations = append(relations, Tuple{"all": true, "name": rname, "collection": rcollection, "terms": rterms})
}
}
......@@ -546,7 +561,8 @@ func (c *MongoDBCollection) FindAll(terms ...interface{}) []Item {
switch term.(type) {
// Just waiting for Where statements.
case Where: {
case Where:
{
for wkey, wval := range term.(Where) {
//if reflect.TypeOf(wval).Kind() == reflect.String { // does not always work.
if reflect.TypeOf(wval).Name() == "string" {
......
......@@ -2,8 +2,8 @@ package db
import (
"fmt"
"math/rand"
"github.com/kr/pretty"
"math/rand"
"testing"
)
......@@ -76,7 +76,6 @@ func TestMgAppend(t *testing.T) {
t.Error("Could not append all items.")
}
}
func TestMgFind(t *testing.T) {
......@@ -240,4 +239,3 @@ func TestMgRelation(t *testing.T) {
fmt.Printf("%# v\n", pretty.Formatter(result))
}
......@@ -24,14 +24,14 @@
package db
import (
"fmt"
_ "code.google.com/p/go-mysql-driver/mysql"
"database/sql"
"strings"
"strconv"
"fmt"
. "github.com/xiam/gosexy"
"reflect"
"regexp"
_ "code.google.com/p/go-mysql-driver/mysql"
. "github.com/xiam/gosexy"
"strconv"
"strings"
)
type Values []string
......@@ -49,15 +49,18 @@ func myCompile(terms []interface{}) *myQuery {
for _, term := range terms {
switch term.(type) {
case string: {
case string:
{
q.Query = append(q.Query, term.(string))
}
case Args: {
case Args:
{
for _, arg := range term.(Args) {
q.Args = append(q.Args, arg)
}
}
case Values: {
case Values:
{
args := make([]string, len(term.(Values)))
for i, arg := range term.(Values) {
args[i] = "?"
......@@ -128,19 +131,23 @@ func (t *MysqlTable) myFetchAll(rows sql.Rows) []Item {
strval := fmt.Sprintf("%s", *res[name])
switch t.types[name] {
case reflect.Uint64: {
case reflect.Uint64:
{
intval, _ := strconv.Atoi(strval)
item[name] = uint64(intval)
}
case reflect.Int64: {
case reflect.Int64:
{
intval, _ := strconv.Atoi(strval)
item[name] = intval
}
case reflect.Float64: {
case reflect.Float64:
{
floatval, _ := strconv.ParseFloat(strval, 10)
item[name] = floatval
}
default: {
default:
{
item[name] = strval
}
}
......@@ -269,7 +276,8 @@ func (t *MysqlTable) compileConditions(term interface{}) (string, Args) {
args := Args{}
switch term.(type) {
case []interface{}: {
case []interface{}:
{
itop := len(term.([]interface{}))
......@@ -287,7 +295,8 @@ func (t *MysqlTable) compileConditions(term interface{}) (string, Args) {
return "(" + strings.Join(sql, " AND ") + ")", args
}
}
case Or: {
case Or:
{
itop := len(term.(Or))
......@@ -305,7 +314,8 @@ func (t *MysqlTable) compileConditions(term interface{}) (string, Args) {
return "(" + strings.Join(sql, " OR ") + ")", args
}
}
case And: {
case And:
{
itop := len(term.(Or))
......@@ -323,7 +333,8 @@ func (t *MysqlTable) compileConditions(term interface{}) (string, Args) {
return "(" + strings.Join(sql, " AND ") + ")", args
}
}
case Where: {
case Where:
{
return t.marshal(term.(Where))
}
......@@ -334,7 +345,7 @@ func (t *MysqlTable) compileConditions(term interface{}) (string, Args) {
func (t *MysqlTable) marshal(where Where) (string, []string) {
for key, val := range(where) {
for key, val := range where {
key = strings.Trim(key, " ")
chunks := strings.Split(key, " ")
......@@ -361,7 +372,6 @@ func (t *MysqlTable) Truncate() bool {
return false
}
func (t *MysqlTable) Remove(terms ...interface{}) bool {
terms = append(terms, Limit(1))
......@@ -394,10 +404,12 @@ func (t *MysqlTable) RemoveAll(terms ...interface{}) bool {
for _, term := range terms {
switch term.(type) {
case Limit: {
case Limit:
{
limit = fmt.Sprintf("LIMIT %v", term.(Limit))
}
case Offset: {
case Offset:
{
offset = fmt.Sprintf("OFFSET %v", term.(Offset))
}
}
......@@ -428,13 +440,16 @@ func (t *MysqlTable) UpdateAll(terms ...interface{}) bool {
for _, term := range terms {
switch term.(type) {
case Set: {
case Set:
{
fields, fargs = t.compileSet(term.(Set))
}
case Limit: {
case Limit:
{
limit = fmt.Sprintf("LIMIT %v", term.(Limit))
}
case Offset: {
case Offset:
{
offset = fmt.Sprintf("OFFSET %v", term.(Offset))
}
}
......@@ -465,7 +480,6 @@ func (t *MysqlTable) FindAll(terms ...interface{}) []Item {
limit := ""
offset := ""
// Analyzing
itop = len(terms)
......@@ -473,19 +487,24 @@ func (t *MysqlTable) FindAll(terms ...interface{}) []Item {
term := terms[i]
switch term.(type) {
case Limit: {
case Limit:
{
limit = fmt.Sprintf("LIMIT %v", term.(Limit))
}
case Offset: {
case Offset:
{
offset = fmt.Sprintf("OFFSET %v", term.(Offset))
}
case Fields: {
case Fields:
{
fields = strings.Join(term.(Fields), ", ")
}
case Relate: {
case Relate:
{
relate = term.(Relate)
}
case RelateAll: {
case RelateAll:
{
relateAll = term.(RelateAll)
}
}
......@@ -519,7 +538,8 @@ func (t *MysqlTable) FindAll(terms ...interface{}) []Item {
for t := ttop - 1; t >= 0; t-- {
rterm := rterms[t]
switch rterm.(type) {
case Collection: {
case Collection:
{
rcollection = rterm.(Collection)
}
}
......@@ -529,7 +549,7 @@ func (t *MysqlTable) FindAll(terms ...interface{}) []Item {
rcollection = t.parent.Collection(rname)
}
relations = append(relations, Tuple { "all": false, "name": rname, "collection": rcollection, "terms": rterms, })
relations = append(relations, Tuple{"all": false, "name": rname, "collection": rcollection, "terms": rterms})
}
}
......@@ -541,7 +561,8 @@ func (t *MysqlTable) FindAll(terms ...interface{}) []Item {
for t := ttop - 1; t >= 0; t-- {
rterm := rterms[t]
switch rterm.(type) {
case Collection: {
case Collection:
{
rcollection = rterm.(Collection)
}
}
......@@ -551,7 +572,7 @@ func (t *MysqlTable) FindAll(terms ...interface{}) []Item {
rcollection = t.parent.Collection(rname)
}
relations = append(relations, Tuple { "all": true, "name": rname, "collection": rcollection, "terms": rterms, })
relations = append(relations, Tuple{"all": true, "name": rname, "collection": rcollection, "terms": rterms})
}
}
......@@ -587,7 +608,8 @@ func (t *MysqlTable) FindAll(terms ...interface{}) []Item {
switch term.(type) {
// Just waiting for Where statements.
case Where: {
case Where:
{
for wkey, wval := range term.(Where) {
//if reflect.TypeOf(wval).Kind() == reflect.String { // does not always work.
if reflect.TypeOf(wval).Name() == "string" {
......@@ -730,14 +752,16 @@ func (my *MysqlDB) Collection(name string) Collection {
// Guessing datatypes.
switch dtype {
case "tinyint", "smallint", "mediumint", "int", "bigint": {
case "tinyint", "smallint", "mediumint", "int", "bigint":
{
if dextra == "unsigned" {
vtype = reflect.Uint64
} else {
vtype = reflect.Int64
}
}
case "decimal", "float", "double": {
case "decimal", "float", "double":
{
vtype = reflect.Float64
}
}
......@@ -753,5 +777,3 @@ func (my *MysqlDB) Collection(name string) Collection {
return t
}
package db
import (
"testing"
"fmt"
"github.com/kr/pretty"
"math/rand"
"fmt"
"testing"
)
func TestMyTruncate(t *testing.T) {
......@@ -27,7 +27,6 @@ func TestMyTruncate(t *testing.T) {
}
}
}
func TestMyAppend(t *testing.T) {
......@@ -175,7 +174,6 @@ func TestMyPopulate(t *testing.T) {
}
func TestMyRelation(t *testing.T) {
db := NewMysqlDB(&DataSource{Host: "localhost", Database: "gotest", User: "gotest", Password: "gopass"})
......@@ -214,4 +212,3 @@ func TestMyRelation(t *testing.T) {
fmt.Printf("%# v\n", pretty.Formatter(result))
}
......@@ -62,10 +62,12 @@ func (c *Storage) Get(path string, def interface { }) interface { } {
if ok == true {
switch value.(type) {
case gosexy.Tuple: {
case gosexy.Tuple:
{
p = value.(gosexy.Tuple)
}
default: {
default:
{
return def
}
}
......@@ -101,10 +103,12 @@ func (c *Storage) Set(path string, value interface { }) {
// Searching.
if ok == true {
switch current.(type) {
case gosexy.Tuple: {
case gosexy.Tuple:
{
// Just skip.
}
default: {
default:
{
delete(p, chunks[i])
p[chunks[i]] = gosexy.Tuple{}
}
......@@ -127,22 +131,28 @@ func (c *Storage) Map(data interface { }, parent *gosexy.Tuple) {
name = strings.ToLower(key.(string))
switch value.(type) {
case []interface {}: {
case []interface{}:
{
(*parent)[name] = value.([]interface{})
}
case string: {
case string:
{
(*parent)[name] = value.(string)
}
case int: {
case int:
{
(*parent)[name] = value.(int)
}
case bool: {
case bool:
{
(*parent)[name] = value.(bool)
}
case float64: {
case float64:
{
(*parent)[name] = value.(float64)
}
case interface {}: {
case interface{}:
{
values := &gosexy.Tuple{}
c.Map(value, values)
(*parent)[name] = *values
......@@ -206,5 +216,3 @@ func (c *Storage) Read(filename string) {
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment