Skip to content
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

Merged
merged 2 commits into from
Oct 10, 2017

Conversation

ajurasz
Copy link
Contributor

@ajurasz ajurasz commented Oct 10, 2017

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.

@coveralls
Copy link

coveralls commented Oct 10, 2017

Coverage Status

Coverage increased (+0.1%) to 94.169% when pulling ef1839f on ajurasz:unique_constrain into 9556734 on frictionlessdata:master.

Copy link
Collaborator

@danielfireman danielfireman left a 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"`
Copy link
Collaborator

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 {
Copy link
Collaborator

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 {
Copy link
Collaborator

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?

@@ -532,6 +532,43 @@ func TestDecodeTable(t *testing.T) {
t.Fatalf("err want:err got:nil")
}
})
t.Run("UniqueConstrain", func(t *testing.T) {
Copy link
Collaborator

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?

t.Fatalf("len(got) want:0 got:%v", len(got))
}
})
t.Run("PrimaryKeyAndUniqueConstrain", func(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error_PrimaryKeyAndUniqueConstraint

@@ -532,6 +532,43 @@ func TestDecodeTable(t *testing.T) {
t.Fatalf("err want:err got:nil")
}
})
t.Run("UniqueConstrain", func(t *testing.T) {
Copy link
Collaborator

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)

@coveralls
Copy link

coveralls commented Oct 10, 2017

Coverage Status

Coverage increased (+0.2%) to 94.175% when pulling 46e07d0 on ajurasz:unique_constrain into 9556734 on frictionlessdata:master.

@ajurasz
Copy link
Contributor Author

ajurasz commented Oct 10, 2017

@danielfireman thank you for your effort on making this review. I have addressed all of your comments.

Copy link
Collaborator

@danielfireman danielfireman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@danielfireman danielfireman merged commit 9c1457a into frictionlessdata:master Oct 10, 2017
@ajurasz ajurasz deleted the unique_constrain branch October 10, 2017 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants