Skip to content

Commit 4c2fa67

Browse files
committed
support constraint
1 parent e6a1b99 commit 4c2fa67

File tree

7 files changed

+68
-9
lines changed

7 files changed

+68
-9
lines changed

add_error_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package changeset
22

33
import (
4+
"errors"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -24,3 +25,12 @@ func TestAddError(t *testing.T) {
2425
assert.Equal(t, 2, len(ch.Errors()))
2526
assert.Equal(t, "field2 is not valid", ch.Errors()[1].Error())
2627
}
28+
29+
func TestError_Unwrap(t *testing.T) {
30+
var (
31+
wrappedError = errors.New("error")
32+
err = Error{Err: wrappedError}
33+
)
34+
35+
assert.Equal(t, wrappedError, err.Unwrap())
36+
}

changeset.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ func (c *Changeset) Apply(doc *rel.Document, mut *rel.Mutation) {
103103
if doc.Flag(rel.HasUpdatedAt) {
104104
c.set(doc, mut, "updated_at", now)
105105
}
106+
107+
// add error func
108+
if len(c.constraints) > 0 {
109+
mut.ErrorFunc = c.constraints.GetError
110+
}
106111
}
107112

108113
func (c *Changeset) set(doc *rel.Document, mut *rel.Mutation, field string, value interface{}) {

changeset_test.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,6 @@ func TestChangesetApply(t *testing.T) {
139139
)
140140
)
141141

142-
userMutation.SetAssoc("transactions", transaction1Mutation, transaction2Mutation)
143-
userMutation.SetAssoc("address", addressMutation)
144-
145142
ch := Cast(user, input, []string{"name", "age"})
146143
CastAssoc(ch, "transactions", func(data interface{}, input params.Params) *Changeset {
147144
ch := Cast(data, input, []string{"item", "status"})
@@ -152,6 +149,9 @@ func TestChangesetApply(t *testing.T) {
152149
return ch
153150
})
154151

152+
userMutation.SetAssoc("transactions", transaction1Mutation, transaction2Mutation)
153+
userMutation.SetAssoc("address", addressMutation)
154+
155155
assert.Nil(t, ch.Error())
156156
assert.Equal(t, userMutation, rel.Apply(doc, ch))
157157
assert.Equal(t, User{
@@ -170,3 +170,35 @@ func TestChangesetApply(t *testing.T) {
170170
},
171171
}, user)
172172
}
173+
174+
func TestChangesetApply_constraint(t *testing.T) {
175+
var (
176+
user User
177+
now = time.Now().Truncate(time.Second)
178+
doc = rel.NewDocument(&user)
179+
input = params.Map{
180+
"name": "Luffy",
181+
"age": 20,
182+
}
183+
userMutation = rel.Apply(rel.NewDocument(&User{}),
184+
rel.Set("name", "Luffy"),
185+
rel.Set("age", 20),
186+
rel.Set("created_at", now),
187+
rel.Set("updated_at", now),
188+
)
189+
)
190+
191+
ch := Cast(user, input, []string{"name", "age"})
192+
UniqueConstraint(ch, "name")
193+
mut := rel.Apply(doc, ch)
194+
195+
assert.Nil(t, ch.Error())
196+
assert.Equal(t, userMutation.Mutates, mut.Mutates)
197+
assert.NotNil(t, mut.ErrorFunc)
198+
assert.Equal(t, User{
199+
Name: "Luffy",
200+
Age: 20,
201+
CreatedAt: now,
202+
UpdatedAt: now,
203+
}, user)
204+
}

constraint.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ type Constraints []Constraint
2424
// If the original error is constraint error, and it's defined in the constraint list, then it'll be updated with constraint's message.
2525
// If the original error is constraint error but not defined in the constraint list, it'll be converted to unexpected error.
2626
// else it'll not modify the error.
27-
func (constraints Constraints) GetError(err rel.ConstraintError) error {
27+
func (constraints Constraints) GetError(err error) error {
28+
cerr, ok := err.(rel.ConstraintError)
29+
if !ok {
30+
return err
31+
}
32+
2833
for _, c := range constraints {
29-
if c.Type == err.Type {
30-
if c.Exact && c.Name != err.Key {
34+
if c.Type == cerr.Type {
35+
if c.Exact && c.Name != cerr.Key {
3136
continue
3237
}
3338

34-
if !c.Exact && !strings.Contains(err.Key, c.Name) {
39+
if !c.Exact && !strings.Contains(cerr.Key, c.Name) {
3540
continue
3641
}
3742

constraint_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestConstraint_GetError(t *testing.T) {
1515

1616
tests := []struct {
1717
name string
18-
err rel.ConstraintError
18+
err error
1919
expected error
2020
}{
2121
{
@@ -43,6 +43,11 @@ func TestConstraint_GetError(t *testing.T) {
4343
err: rel.ConstraintError{Key: "other_id_ibfk1", Type: rel.ForeignKeyConstraint},
4444
expected: rel.ConstraintError{Key: "other_id_ibfk1", Type: rel.ForeignKeyConstraint},
4545
},
46+
{
47+
name: "other error",
48+
err: rel.NotFoundError{},
49+
expected: rel.NotFoundError{},
50+
},
4651
}
4752

4853
for _, tt := range tests {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.13
44

55
require (
66
github.com/Fs02/grimoire v1.5.0
7-
github.com/Fs02/rel v0.4.0
7+
github.com/Fs02/rel v0.4.1-0.20200613111837-557349925ce2
88
github.com/azer/snakecase v0.0.0-20161028114325-c818dddafb5c
99
github.com/stretchr/testify v1.5.1
1010
github.com/tidwall/gjson v1.6.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/Fs02/grimoire v1.5.0 h1:BnFaZ0FHzQpnzLlIGe8iOqgGeRLps33TeVGSsCFUAio=
44
github.com/Fs02/grimoire v1.5.0/go.mod h1:Zym+jMh+rHBZxP342yxGJxQImPV0Nc6meEt5+qQY3N8=
55
github.com/Fs02/rel v0.4.0 h1:iop6aPDb2gfTIxYIbFq1sTEMJzSjlryb+mfFkSqgeig=
66
github.com/Fs02/rel v0.4.0/go.mod h1:MeJHkZO46QVfjagPSnYX/noguNDOqNK2LLGse77asxw=
7+
github.com/Fs02/rel v0.4.1-0.20200613111837-557349925ce2 h1:zTJnXFb2L571q6RpZcQco/kGnwFaYcrPKhYA2WwAkto=
8+
github.com/Fs02/rel v0.4.1-0.20200613111837-557349925ce2/go.mod h1:MeJHkZO46QVfjagPSnYX/noguNDOqNK2LLGse77asxw=
79
github.com/azer/snakecase v0.0.0-20161028114325-c818dddafb5c h1:7zL0ljVI6ads5EFvx+Oq+uompnFBMJqtbuHvyobbJ1Q=
810
github.com/azer/snakecase v0.0.0-20161028114325-c818dddafb5c/go.mod h1:iApMeoHF0YlMPzCwqH/d59E3w2s8SeO4rGK+iGClS8Y=
911
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=

0 commit comments

Comments
 (0)