Skip to content

Commit

Permalink
Fixed #5 and adjusted some indentations
Browse files Browse the repository at this point in the history
  • Loading branch information
philippmoehler0440 committed Jan 31, 2017
1 parent b67da1c commit a0d4948
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion document_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (self *DocumentBase) DefaultValidate() (bool, []error) {
validationName = strings.ToLower(fieldName)
}

if fieldValue.Kind() == reflect.Slice && relationTag != REL_1N {
if len(relationTag) > 0 && fieldValue.Kind() == reflect.Slice && relationTag != REL_1N {
self.AppendError(&validationErrors, L("validation.field_invalid_relation1n", validationName))
} else if fieldValue.Kind() != reflect.Slice && relationTag == REL_1N {
self.AppendError(&validationErrors, L("validation.field_invalid_relation11", validationName))
Expand Down
48 changes: 24 additions & 24 deletions mongodm.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ Now that you got some models it is important to create a connection to the datab
package mongodm

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"path"
"reflect"
"runtime"
"strings"
"time"
"io/ioutil"
"encoding/json"
"runtime"
"path"

"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
Expand All @@ -98,11 +98,11 @@ var locals map[string]string
type (
//Simple config object which has to be passed/set to create a new connection
Config struct {
DatabaseHosts []string
DatabaseName string
DatabaseUser string
DatabasePassword string
Locals map[string]string
DatabaseHosts []string
DatabaseName string
DatabaseUser string
DatabasePassword string
Locals map[string]string
}

//The "Database" object which stores all connections
Expand Down Expand Up @@ -173,17 +173,17 @@ func Connect(config *Config) (*Connection, error) {
filepath := path.Join(path.Dir(filename), "locals.json")
file, err := ioutil.ReadFile(filepath)

if err != nil {
return nil, err
}
if err != nil {
return nil, err
}

var localMap map[string]map[string]string
json.Unmarshal(file, &localMap)
var localMap map[string]map[string]string
json.Unmarshal(file, &localMap)

locals = localMap["en-US"]
} else {
panic("No caller information to read default localisation file")
}
locals = localMap["en-US"]
} else {
panic("No caller information to read default localisation file")
}
} else {
locals = config.Locals
}
Expand Down Expand Up @@ -298,12 +298,12 @@ func (self *Connection) Open() (err error) {
}()

info := &mgo.DialInfo{
Addrs: self.Config.DatabaseHosts,
Timeout: 3 * time.Second,
Database: self.Config.DatabaseName,
Username: self.Config.DatabaseUser,
Password: self.Config.DatabasePassword,
}
Addrs: self.Config.DatabaseHosts,
Timeout: 3 * time.Second,
Database: self.Config.DatabaseName,
Username: self.Config.DatabaseUser,
Password: self.Config.DatabasePassword,
}

session, err := mgo.DialWithInfo(info)

Expand Down
15 changes: 8 additions & 7 deletions mongodm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
const (
DBHost string = "127.0.0.1"
DBName string = "mongodm_test"
DBUser string = "admin"
DBPass string = "admin"
DBUser string = "admin"
DBPass string = "admin"
DBTestCollection string = "_testCollection"
DBTestRelCollection string = "_testRelationCollection"
)
Expand All @@ -26,6 +26,7 @@ type (
Name string `json:"name" bson:"name" required:"true" minLen:"2"`
Number int `json:"number" bson:"number"`
RequiredField string `json:"requiredField" bson:"requiredField" required:"true"`
SomeSlice []string `json:"to" bson:"to"`
Relation21 interface{} `json:"relationTwo" bson:"relationTwo" model:"TestRelationModel" relation:"11"`
Relation11 interface{} `json:"relationOne" bson:"relationOne" model:"TestRelationModel" relation:"11"`
Relation1N interface{} `json:"relationMany" bson:"relationMany" model:"TestRelationModel" relation:"1n"`
Expand Down Expand Up @@ -65,11 +66,11 @@ func TestConnection(t *testing.T) {
json.Unmarshal(localsFile, &localMap)

dbConfig := &Config{
DatabaseHosts: []string{DBHost},
DatabaseName: DBName,
DatabaseUser: DBUser,
DatabaseHosts: []string{DBHost},
DatabaseName: DBName,
DatabaseUser: DBUser,
DatabasePassword: DBPass,
Locals: localMap["en-US"],
Locals: localMap["en-US"],
}

db, err := Connect(dbConfig)
Expand Down Expand Up @@ -97,7 +98,7 @@ func TestConnectionWithoutExtendedConfig(t *testing.T) {

dbConfig := &Config{
DatabaseHosts: []string{DBHost},
DatabaseName: DBName,
DatabaseName: DBName,
}

_, err := Connect(dbConfig)
Expand Down

0 comments on commit a0d4948

Please sign in to comment.