-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Unique
constrain.
#63
Support Unique
constrain.
#63
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super nice! Only a few comments.
schema/field.go
Outdated
@@ -53,6 +53,10 @@ type Constraints struct { | |||
// represent null values. | |||
Required bool `json:"required,omitempty"` | |||
|
|||
// Unique indicates whether this field is allowed to have duplicates. | |||
// This constrain is only relevant for Schema.DecodeTable | |||
Unique bool `json:"unique,omitempty"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Format?
schema/schema.go
Outdated
elemp := reflect.New(elemt) | ||
if err := s.Decode(iter.Row(), elemp.Interface()); err != nil { | ||
return err | ||
} | ||
for _, k := range uniqueFieldIndexes { | ||
if _, ok := uniqueCache[uniqueKey{k, elemp.Elem().Field(k).Interface()}]; ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Would you mind making a variable: field := elemp.Elem().Field(k) and making here and bellow a little simple?
schema/schema.go
Outdated
@@ -332,6 +332,11 @@ func processPlaceholder(ph interface{}, v *[]string) error { | |||
return fmt.Errorf("") | |||
} | |||
|
|||
type uniqueKey struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is super nice, but not obvious for other contributors. Would you mind adding a comment explaining the reason for this data structure?
schema/schema_test.go
Outdated
@@ -532,6 +532,43 @@ func TestDecodeTable(t *testing.T) { | |||
t.Fatalf("err want:err got:nil") | |||
} | |||
}) | |||
t.Run("UniqueConstrain", func(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: t at the end.
Could you please name this Error_UniqueConstraint?
schema/schema_test.go
Outdated
t.Fatalf("len(got) want:0 got:%v", len(got)) | ||
} | ||
}) | ||
t.Run("PrimaryKeyAndUniqueConstrain", func(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error_PrimaryKeyAndUniqueConstraint
schema/schema_test.go
Outdated
@@ -532,6 +532,43 @@ func TestDecodeTable(t *testing.T) { | |||
t.Fatalf("err want:err got:nil") | |||
} | |||
}) | |||
t.Run("UniqueConstrain", func(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you either add or change other tests where the unique constraint does not error? (can be the Example_DecodeTable or something)
@danielfireman thank you for your effort on making this review. I have addressed all of your comments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
This is for #47. In that issue python implementation was mentioned where uniqueness is checked for the field where this constraint is set and also for all PKs - in my implementation I did the same.