Skip to content

Commit 04a7a9d

Browse files
committed
feat: add hooks arg to mutations in resource-store
1 parent 23cf3a3 commit 04a7a9d

File tree

5 files changed

+104
-47
lines changed

5 files changed

+104
-47
lines changed

.golangci.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
run:
22
go: '1.17'
33
timeout: 10m
4+
skip-files:
5+
- expt/main.go
6+
47
output:
58
format: colored-line-number
9+
610
linters:
711
enable-all: true
812
disable:
@@ -49,7 +53,7 @@ linters-settings:
4953
threshold: 100
5054
errcheck: # Report about not checking of errors in type assertions: `a := b.(MyStruct)`.
5155
check-type-assertions: true
52-
goconst:
56+
goconst:
5357
min-len: 2 # Minimal length of string constant.
5458
min-occurrences: 2 # Minimum occurrences of constant string count to trigger issue.
5559
ignore-tests: true
@@ -120,13 +124,14 @@ linters-settings:
120124
- singleCaseSwitch
121125
enabled-tags:
122126
- diagnostic
123-
# - style
124-
# - opinionated
125-
# - performance
127+
# - style
128+
# - opinionated
129+
# - performance
126130
unparam:
127131
# Inspect exported functions.
128132
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
129133
check-exported: true
134+
130135
issues:
131136
exclude-rules:
132137
# Exclude some linters from running on tests files.

core/mocks/resource_repository.go renamed to core/mocks/resource_repo.go

Lines changed: 72 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/resource/resource.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package resource
22

3+
//go:generate mockery --name=Repository -r --case underscore --with-expecter --structname ResourceRepository --filename=resource_repo.go --output=../mocks
4+
35
import (
46
"context"
57
"encoding/json"
@@ -14,6 +16,23 @@ const urnSeparator = ":"
1416

1517
var namingPattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_-]+$`)
1618

19+
type Repository interface {
20+
GetByURN(ctx context.Context, urn string) (*Resource, error)
21+
List(ctx context.Context, filter map[string]string) ([]*Resource, error)
22+
23+
Create(ctx context.Context, r Resource, hooks ...MutationHook) error
24+
Update(ctx context.Context, r Resource, hooks ...MutationHook) error
25+
Delete(ctx context.Context, urn string, hooks ...MutationHook) error
26+
27+
DoPending(ctx context.Context, fn PendingHandler) error
28+
}
29+
30+
// MutationHook values are passed to mutation operations of resource storage
31+
// to handle any transactional requirements.
32+
type MutationHook func(ctx context.Context) error
33+
34+
type PendingHandler func(ctx context.Context, res Resource) (*Resource, bool, error)
35+
1736
type Resource struct {
1837
URN string `json:"urn"`
1938
Kind string `json:"kind"`
@@ -31,18 +50,6 @@ type Spec struct {
3150
Dependencies map[string]string `json:"dependencies"`
3251
}
3352

34-
type Repository interface {
35-
GetByURN(ctx context.Context, urn string) (*Resource, error)
36-
List(ctx context.Context, filter map[string]string) ([]*Resource, error)
37-
Create(ctx context.Context, r Resource) error
38-
Update(ctx context.Context, r Resource) error
39-
Delete(ctx context.Context, urn string) error
40-
41-
DoPending(ctx context.Context, fn PendingHandler) error
42-
}
43-
44-
type PendingHandler func(ctx context.Context, res Resource) (*Resource, bool, error)
45-
4653
func (res *Resource) Validate() error {
4754
res.Kind = strings.TrimSpace(res.Kind)
4855
res.Name = strings.TrimSpace(res.Name)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ require (
3030
require (
3131
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
3232
github.com/BurntSushi/toml v1.0.0 // indirect
33-
github.com/MakeNowJust/heredoc v1.0.0 // indirect
33+
github.com/MakeNowJust/heredoc v1.0.0
3434
github.com/Masterminds/goutils v1.1.1 // indirect
3535
github.com/Masterminds/semver/v3 v3.1.1 // indirect
3636
github.com/Masterminds/sprig/v3 v3.2.2 // indirect

internal/store/mongodb/resource_repository.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (rc *ResourceRepository) List(ctx context.Context, filter map[string]string
5454
return res, nil
5555
}
5656

57-
func (rc *ResourceRepository) Create(ctx context.Context, res resource.Resource) error {
57+
func (rc *ResourceRepository) Create(ctx context.Context, res resource.Resource, _ ...resource.MutationHook) error {
5858
res.CreatedAt = time.Now()
5959
res.UpdatedAt = time.Now()
6060

@@ -69,7 +69,7 @@ func (rc *ResourceRepository) Create(ctx context.Context, res resource.Resource)
6969
return nil
7070
}
7171

72-
func (rc *ResourceRepository) Update(ctx context.Context, res resource.Resource) error {
72+
func (rc *ResourceRepository) Update(ctx context.Context, res resource.Resource, _ ...resource.MutationHook) error {
7373
res.UpdatedAt = time.Now()
7474

7575
filter := map[string]interface{}{"urn": res.URN}
@@ -86,7 +86,7 @@ func (rc *ResourceRepository) Update(ctx context.Context, res resource.Resource)
8686
return nil
8787
}
8888

89-
func (rc *ResourceRepository) Delete(ctx context.Context, urn string) error {
89+
func (rc *ResourceRepository) Delete(ctx context.Context, urn string, _ ...resource.MutationHook) error {
9090
_, err := rc.coll.DeleteOne(ctx, map[string]interface{}{"urn": urn})
9191
if err != nil {
9292
if errors.Is(err, mongo.ErrNoDocuments) {

0 commit comments

Comments
 (0)