Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit 9a7027d

Browse files
authored
Merge pull request #46 from neuronlabs/develop
Minor fix while registering multiple models.
2 parents 9713cea + cd11aaa commit 9a7027d

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ require (
1515
github.com/pelletier/go-toml v1.4.0 // indirect
1616
github.com/spf13/afero v1.2.2 // indirect
1717
github.com/spf13/jwalterweatherman v1.1.0 // indirect
18+
github.com/spf13/pflag v1.0.5 // indirect
1819
github.com/spf13/viper v1.4.0
1920
github.com/stretchr/objx v0.2.0 // indirect
2021
github.com/stretchr/testify v1.4.0
21-
golang.org/x/sys v0.0.0-20190909082730-f460065e899a // indirect
22+
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24 // indirect
2223
golang.org/x/text v0.3.2
23-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2424
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
25-
gopkg.in/go-playground/validator.v9 v9.29.1
25+
gopkg.in/go-playground/validator.v9 v9.30.0
26+
gopkg.in/yaml.v2 v2.2.3 // indirect
2627
)

go.sum

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmq
114114
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
115115
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
116116
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
117+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
118+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
117119
github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU=
118120
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
119121
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -154,8 +156,8 @@ golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5h
154156
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
155157
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
156158
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
157-
golang.org/x/sys v0.0.0-20190909082730-f460065e899a h1:mIzbOulag9/gXacgxKlFVwpCOWSfBT3/pDyyCwGA9as=
158-
golang.org/x/sys v0.0.0-20190909082730-f460065e899a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
159+
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24 h1:R8bzl0244nw47n1xKs1MUMAaTNgjavKcN/aX2Ss3+Fo=
160+
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
159161
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
160162
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
161163
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
@@ -172,15 +174,15 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks
172174
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
173175
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
174176
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
175-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
176-
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
177177
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
178178
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
179-
gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc=
180-
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
179+
gopkg.in/go-playground/validator.v9 v9.30.0 h1:Wk0Z37oBmKj9/n+tPyBHZmeL19LaCoK3Qq48VwYENss=
180+
gopkg.in/go-playground/validator.v9 v9.30.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
181181
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
182182
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
183183
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
184184
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
185185
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
186+
gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
187+
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
186188
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

mapping/map.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,24 @@ func (m *ModelMap) ModelByName(name string) *ModelStruct {
9595
// RegisterModels registers the model within the model map container.
9696
func (m *ModelMap) RegisterModels(models ...interface{}) error {
9797
// iterate over models and register one by one
98-
var err error
98+
var thisModels []*ModelStruct
9999
for _, model := range models {
100+
log.Debug3f("Registering Model: '%T'", model)
100101
// build the model's structure and set into model map.
101102
mStruct, err := buildModelStruct(model, m.NamerFunc)
102103
if err != nil {
103104
return err
104105
}
105106

106-
if err = m.Set(mStruct); err != nil {
107-
continue
107+
// check if the model wasn't already registered within the ModelMap 'm'.
108+
if m.models[mStruct.Type()] != nil {
109+
return errors.Newf(class.ModelInSchemaAlreadyRegistered, "model: '%s' was already registered.", mStruct.Collection())
108110
}
109111

110-
var modelConfig *config.ModelConfig
112+
if err = m.Set(mStruct); err != nil {
113+
return err
114+
}
115+
thisModels = append(thisModels, mStruct)
111116

112117
modelConfig, ok := m.Configs[mStruct.collectionType]
113118
if !ok {
@@ -131,12 +136,14 @@ func (m *ModelMap) RegisterModels(models ...interface{}) error {
131136
mStruct.namerFunc = m.NamerFunc
132137
}
133138

134-
for _, modelStruct := range m.models {
139+
var err error
140+
for _, modelStruct := range thisModels {
135141
if err = m.setUntaggedFields(modelStruct); err != nil {
136142
return err
137143
}
138144

139145
if modelStruct.assignedFields() == 0 {
146+
log.Debug3f("Assigned fields: %v - number: %v", modelStruct.assignedFields(), modelStruct.store[assignedFieldsKey])
140147
err = errors.NewDetf(class.ModelMappingNoFields, "model: '%s' have no fields defined", modelStruct.Type().Name())
141148
return err
142149
}
@@ -171,11 +178,10 @@ func (m *ModelMap) RegisterModels(models ...interface{}) error {
171178
return err
172179
}
173180

174-
for _, model := range m.models {
181+
for _, model := range thisModels {
175182
model.structFieldCount = len(model.StructFields())
176183
model.clearInitializeStoreKeys()
177184
}
178-
179185
return nil
180186
}
181187

mapping/model-structs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ func (m *ModelStruct) clearInitializeStoreKeys() {
380380
// clear untagged fields
381381
delete(m.store, untaggedFieldKey)
382382
delete(m.store, assignedFieldsKey)
383+
log.Debug3f("Cleared model: '%s' internal store key", m.Collection())
383384
}
384385

385386
// computeNestedIncludedCount computes the included count for given limit
@@ -459,6 +460,7 @@ func (m *ModelStruct) increaseAssignedFields() {
459460
assignedFields = v.(int)
460461
}
461462
assignedFields++
463+
log.Debug3f("Model: '%s' assignedFields: %d", m.Collection(), assignedFields)
462464
m.store[assignedFieldsKey] = assignedFields
463465
}
464466

0 commit comments

Comments
 (0)