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

Commit 9713cea

Browse files
authored
Merge pull request #45 from neuronlabs/develop
Fixes issues #43 and #44
2 parents 45d6941 + da755ca commit 9713cea

File tree

10 files changed

+141
-96
lines changed

10 files changed

+141
-96
lines changed

.github/ISSUE_TEMPLATE/tests.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
name: Tests
3+
about: Suggest the tests for the project
4+
title: "[TESTS]"
5+
labels: tests
6+
---
7+
**Describe what parts of code requires tests**
8+
List the package names/ functions / methods that requires to be tested.
9+
10+
**Define the test name skeletons**
11+
Point defined test name skeletons.
12+
13+
**Additional Context**
14+
Add any other context or screenshots about the feature request here.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
name: Simple PR with Checklist
3+
about: Create a pull request with the default Checklist
4+
---
5+
### Description
6+
Please explain the changes you made here.
7+
8+
### Checklist
9+
- [ ] Code compiles correctly
10+
- [ ] Created tests which fail without the change (if possible)
11+
- [ ] All tests passing
12+
- [ ] Run `golangci-lint run ./... --build-tags integrate` or `golangci-lint run ./...` without failure
13+
- [ ] Updated Go modules, if necessary
14+
- [ ] Extended the README / documentation, if necessary
15+
- [ ] Added myself / the copyright holder to the AUTHORS file

config/processor.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package config
22

33
import (
44
"errors"
5-
"github.com/spf13/viper"
65
"strings"
76
"time"
87

8+
"github.com/spf13/viper"
9+
910
"github.com/neuronlabs/neuron-core/internal"
1011
)
1112

@@ -75,6 +76,7 @@ func defaultThreadsafeProcessorConfig() map[string]interface{} {
7576
internal.ProcessTxBegin,
7677
internal.ProcessHookBeforeCreate,
7778
internal.ProcessSetBelongsToRelations,
79+
internal.ProcessSetCreatedAt,
7880
internal.ProcessCreate,
7981
internal.ProcessStoreScopePrimaries,
8082
internal.ProcessPatchForeignRelationsSafe,
@@ -114,6 +116,7 @@ func defaultThreadsafeProcessorConfig() map[string]interface{} {
114116
internal.ProcessDeletedAtFilter,
115117
internal.ProcessReducePrimaryFilters,
116118
internal.ProcessPatchBelongsToRelations,
119+
internal.ProcessSetUpdatedAt,
117120
internal.ProcessPatch,
118121
internal.ProcessPatchForeignRelationsSafe,
119122
internal.ProcessHookAfterPatch,
@@ -124,6 +127,7 @@ func defaultThreadsafeProcessorConfig() map[string]interface{} {
124127
internal.ProcessReducePrimaryFilters,
125128
internal.ProcessHookBeforeDelete,
126129
internal.ProcessReducePrimaryFilters,
130+
internal.ProcessSetDeletedAt,
127131
internal.ProcessDelete,
128132
internal.ProcessDeleteForeignRelationsSafe,
129133
internal.ProcessHookAfterDelete,
@@ -179,6 +183,7 @@ func defaultConcurrentProcessorConfig() map[string]interface{} {
179183
internal.ProcessTxBegin,
180184
internal.ProcessHookBeforeCreate,
181185
internal.ProcessSetBelongsToRelations,
186+
internal.ProcessSetCreatedAt,
182187
internal.ProcessCreate,
183188
internal.ProcessStoreScopePrimaries,
184189
internal.ProcessPatchForeignRelations,
@@ -216,6 +221,7 @@ func defaultConcurrentProcessorConfig() map[string]interface{} {
216221
internal.ProcessDeletedAtFilter,
217222
internal.ProcessReducePrimaryFilters,
218223
internal.ProcessPatchBelongsToRelations,
224+
internal.ProcessSetUpdatedAt,
219225
internal.ProcessPatch,
220226
internal.ProcessPatchForeignRelations,
221227
internal.ProcessHookAfterPatch,
@@ -226,6 +232,7 @@ func defaultConcurrentProcessorConfig() map[string]interface{} {
226232
internal.ProcessReducePrimaryFilters,
227233
internal.ProcessHookBeforeDelete,
228234
internal.ProcessReducePrimaryFilters,
235+
internal.ProcessSetDeletedAt,
229236
internal.ProcessDelete,
230237
internal.ProcessDeleteForeignRelations,
231238
internal.ProcessHookAfterDelete,

internal/flags.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

internal/processes.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package internal
44
var Processes = map[string]struct{}{
55
ProcessHookBeforeCreate: struct{}{},
66
ProcessSetBelongsToRelations: struct{}{},
7+
ProcessSetCreatedAt: struct{}{},
78
ProcessCreate: struct{}{},
89
ProcessStoreScopePrimaries: struct{}{},
910
ProcessPatchForeignRelations: struct{}{},
@@ -24,12 +25,14 @@ var Processes = map[string]struct{}{
2425
ProcessHookAfterList: struct{}{},
2526
ProcessGetIncluded: struct{}{},
2627
ProcessGetIncludedSafe: struct{}{},
28+
ProcessSetUpdatedAt: struct{}{},
2729
ProcessHookBeforePatch: struct{}{},
2830
ProcessPatch: struct{}{},
2931
ProcessHookAfterPatch: struct{}{},
3032
ProcessPatchBelongsToRelations: struct{}{},
3133
ProcessReducePrimaryFilters: struct{}{},
3234
ProcessHookBeforeDelete: struct{}{},
35+
ProcessSetDeletedAt: struct{}{},
3336
ProcessDelete: struct{}{},
3437
ProcessHookAfterDelete: struct{}{},
3538
ProcessDeleteForeignRelations: struct{}{},
@@ -46,6 +49,7 @@ const (
4649
// Create processes
4750
ProcessHookBeforeCreate = "hook_before_create"
4851
ProcessSetBelongsToRelations = "set_belongs_to_relations"
52+
ProcessSetCreatedAt = "set_created_at"
4953
ProcessCreate = "create"
5054
ProcessStoreScopePrimaries = "store_scope_primaries"
5155
ProcessPatchForeignRelations = "patch_foreign_relations"
@@ -73,13 +77,15 @@ const (
7377

7478
// Patch processes
7579
ProcessHookBeforePatch = "hook_before_patch"
80+
ProcessSetUpdatedAt = "set_updated_at"
7681
ProcessPatch = "patch"
7782
ProcessHookAfterPatch = "hook_after_patch"
7883
ProcessPatchBelongsToRelations = "patch_belongs_to_relations"
7984

8085
// Delete processes
8186
ProcessReducePrimaryFilters = "reduce_primary_filters"
8287
ProcessHookBeforeDelete = "hook_before_delete"
88+
ProcessSetDeletedAt = "set_deleted_at"
8389
ProcessDelete = "delete"
8490
ProcessHookAfterDelete = "hook_after_delete"
8591
ProcessDeleteForeignRelations = "delete_foreign_relations"

query/const.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141
const (
4242
ProcessHookBeforeCreate = internal.ProcessHookBeforeCreate
4343
ProcessSetBelongsToRelations = internal.ProcessSetBelongsToRelations
44+
ProcessSetCreatedAt = internal.ProcessSetCreatedAt
4445
ProcessCreate = internal.ProcessCreate
4546
ProcessStoreScopePrimaries = internal.ProcessStoreScopePrimaries
4647
ProcessPatchForeignRelations = internal.ProcessPatchForeignRelations
@@ -65,12 +66,14 @@ const (
6566
ProcessGetIncludedSafe = internal.ProcessGetIncludedSafe
6667

6768
ProcessHookBeforePatch = internal.ProcessHookBeforePatch
69+
ProcessSetUpdatedAt = internal.ProcessSetUpdatedAt
6870
ProcessPatch = internal.ProcessPatch
6971
ProcessHookAfterPatch = internal.ProcessHookAfterPatch
7072
ProcessPatchBelongsToRelations = internal.ProcessPatchBelongsToRelations
7173

7274
ProcessReducePrimaryFilters = internal.ProcessReducePrimaryFilters
7375
ProcessHookBeforeDelete = internal.ProcessHookBeforeDelete
76+
ProcessSetDeletedAt = internal.ProcessSetDeletedAt
7477
ProcessDelete = internal.ProcessDelete
7578
ProcessHookAfterDelete = internal.ProcessHookAfterDelete
7679
ProcessDeleteForeignRelations = internal.ProcessDeleteForeignRelations

query/process-create.go

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,6 @@ func createFunc(ctx context.Context, s *Scope) error {
2929
return errors.NewDet(class.RepositoryNotImplementsCreator, "repository doesn't implement Creator interface")
3030
}
3131

32-
// Set created at field if possible
33-
createdAtField, ok := s.Struct().CreatedAt()
34-
if ok {
35-
// by default scope has auto selected fields setCreatedAt should be true
36-
setCreatedAt := s.autosetFields
37-
if !setCreatedAt {
38-
_, found := s.Fieldset[createdAtField.NeuronName()]
39-
// if the fields were not auto selected check if the field is selected by user
40-
setCreatedAt = !found
41-
}
42-
43-
if setCreatedAt {
44-
// Check if the value of the created at field is not already set by the user.
45-
v := reflect.ValueOf(s.Value).Elem().FieldByIndex(createdAtField.ReflectField().Index)
46-
47-
if s.autosetFields {
48-
setCreatedAt = reflect.DeepEqual(v.Interface(), reflect.Zero(createdAtField.ReflectField().Type).Interface())
49-
}
50-
51-
if setCreatedAt {
52-
switch {
53-
case createdAtField.IsTimePointer():
54-
tv := time.Now()
55-
v.Set(reflect.ValueOf(&tv))
56-
case createdAtField.IsTime():
57-
v.Set(reflect.ValueOf(time.Now()))
58-
}
59-
60-
s.Fieldset[createdAtField.NeuronName()] = createdAtField
61-
62-
}
63-
}
64-
}
65-
6632
if err := creator.Create(ctx, s); err != nil {
6733
return err
6834
}
@@ -123,3 +89,45 @@ func storeScopePrimaries(ctx context.Context, s *Scope) error {
12389

12490
return nil
12591
}
92+
93+
func setCreatedAtField(ctx context.Context, s *Scope) error {
94+
if s.Error != nil {
95+
return nil
96+
}
97+
98+
// Set created at field if possible
99+
createdAtField, ok := s.Struct().CreatedAt()
100+
if !ok {
101+
return nil
102+
}
103+
// by default scope has auto selected fields setCreatedAt should be true
104+
setCreatedAt := s.autosetFields
105+
if !setCreatedAt {
106+
_, found := s.Fieldset[createdAtField.NeuronName()]
107+
// if the fields were not auto selected check if the field is selected by user
108+
setCreatedAt = !found
109+
}
110+
111+
if !setCreatedAt {
112+
return nil
113+
}
114+
// Check if the value of the created at field is not already set by the user.
115+
v := reflect.ValueOf(s.Value).Elem().FieldByIndex(createdAtField.ReflectField().Index)
116+
117+
if s.autosetFields {
118+
setCreatedAt = reflect.DeepEqual(v.Interface(), reflect.Zero(createdAtField.ReflectField().Type).Interface())
119+
}
120+
121+
if setCreatedAt {
122+
switch {
123+
case createdAtField.IsTimePointer():
124+
tv := time.Now()
125+
v.Set(reflect.ValueOf(&tv))
126+
case createdAtField.IsTime():
127+
v.Set(reflect.ValueOf(time.Now()))
128+
}
129+
130+
s.Fieldset[createdAtField.NeuronName()] = createdAtField
131+
}
132+
return nil
133+
}

query/process-delete.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,8 @@ func deleteFunc(ctx context.Context, s *Scope) error {
2525
return err
2626
}
2727

28-
deletedAt, hasDeletedAt := s.Struct().DeletedAt()
28+
_, hasDeletedAt := s.Struct().DeletedAt()
2929
if hasDeletedAt {
30-
s.Fieldset[deletedAt.NeuronName()] = deletedAt
31-
32-
v := reflect.ValueOf(s.Value).Elem().FieldByIndex(deletedAt.ReflectField().Index)
33-
t := time.Now()
34-
v.Set(reflect.ValueOf(&t))
35-
3630
patcher, ok := repo.(Patcher)
3731
if !ok {
3832
log.Warningf("Repository for model: '%s' doesn't implement Patcher interface", s.Struct().Type())
@@ -506,3 +500,20 @@ func reducePrimaryFilters(ctx context.Context, s *Scope) error {
506500

507501
return nil
508502
}
503+
504+
func setDeletedAtField(ctx context.Context, s *Scope) error {
505+
if s.Error != nil {
506+
return nil
507+
}
508+
deletedAt, hasDeletedAt := s.Struct().DeletedAt()
509+
if !hasDeletedAt {
510+
return nil
511+
}
512+
513+
v := reflect.ValueOf(s.Value).Elem().FieldByIndex(deletedAt.ReflectField().Index)
514+
t := time.Now()
515+
v.Set(reflect.ValueOf(&t))
516+
517+
s.Fieldset[deletedAt.NeuronName()] = deletedAt
518+
return nil
519+
}

query/process-patch.go

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,8 @@ func patchFunc(ctx context.Context, s *Scope) error {
3131
return errors.NewDetf(class.RepositoryNotImplementsPatcher, "repository: '%T' doesn't implement Patcher interface", repo)
3232
}
3333

34-
updatedAt, hasUpdatedAt := s.Struct().UpdatedAt()
35-
// if there are any selected fields that are not a foreign relationships
36-
// (attributes, foreign keys etc, relationship-belongs-to...) do the patch process
37-
if !hasUpdatedAt && len(s.Fieldset) == 0 {
38-
return errors.NewDet(class.QuerySelectedFieldsNotSelected, "no fields selected for patch process")
39-
}
40-
34+
_, hasUpdatedAt := s.Struct().UpdatedAt()
4135
onlyForeignRelationships := true
42-
var updatedAtSelected bool
4336
for _, selected := range s.Fieldset {
4437
if selected.IsPrimary() {
4538
if len(s.Fieldset) == 1 {
@@ -56,35 +49,9 @@ func patchFunc(ctx context.Context, s *Scope) error {
5649
} else if selected.Relationship().Kind() == mapping.RelBelongsTo {
5750
onlyForeignRelationships = false
5851
}
59-
60-
if hasUpdatedAt && selected == updatedAt {
61-
updatedAtSelected = true
62-
}
6352
}
6453

6554
if !onlyForeignRelationships || hasUpdatedAt {
66-
if hasUpdatedAt {
67-
v := reflect.ValueOf(s.Value).Elem().FieldByIndex(updatedAt.ReflectField().Index)
68-
69-
var setUpdatedAt bool
70-
if s.autosetFields {
71-
setUpdatedAt = reflect.DeepEqual(v.Interface(), reflect.Zero(updatedAt.ReflectField().Type).Interface())
72-
} else {
73-
setUpdatedAt = !updatedAtSelected
74-
}
75-
if setUpdatedAt {
76-
t := time.Now()
77-
switch {
78-
case updatedAt.IsTimePointer():
79-
v.Set(reflect.ValueOf(&t))
80-
case updatedAt.IsTime():
81-
v.Set(reflect.ValueOf(t))
82-
}
83-
if !updatedAtSelected {
84-
s.Fieldset[updatedAt.NeuronName()] = updatedAt
85-
}
86-
}
87-
}
8855
if log.Level().IsAllowed(log.LDEBUG3) {
8956
log.Debug3f("SCOPE[%s][%s] patching: %s", s.ID().String(), s.Struct().Collection(), s.String())
9057
}
@@ -925,3 +892,34 @@ func patchClearRelationshipWithForeignKey(ctx context.Context, s *Scope, relFiel
925892
}
926893
return nil
927894
}
895+
896+
func setUpdatedAtField(ctx context.Context, s *Scope) error {
897+
if s.Error != nil {
898+
return nil
899+
}
900+
901+
updatedAt, hasUpdatedAt := s.Struct().UpdatedAt()
902+
// if there are any selected fields that are not a foreign relationships
903+
// (attributes, foreign keys etc, relationship-belongs-to...) do the patch process
904+
if !hasUpdatedAt && len(s.Fieldset) == 0 {
905+
return errors.NewDet(class.QuerySelectedFieldsNotSelected, "no fields selected for patch process")
906+
}
907+
if !hasUpdatedAt {
908+
return nil
909+
}
910+
v := reflect.ValueOf(s.Value).Elem().FieldByIndex(updatedAt.ReflectField().Index)
911+
912+
if !reflect.DeepEqual(v.Interface(), reflect.Zero(updatedAt.ReflectField().Type).Interface()) {
913+
return nil
914+
}
915+
916+
t := time.Now()
917+
switch {
918+
case updatedAt.IsTimePointer():
919+
v.Set(reflect.ValueOf(&t))
920+
case updatedAt.IsTime():
921+
v.Set(reflect.ValueOf(t))
922+
}
923+
s.Fieldset[updatedAt.NeuronName()] = updatedAt
924+
return nil
925+
}

0 commit comments

Comments
 (0)