-
Notifications
You must be signed in to change notification settings - Fork 114
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
schema: AnyOf FieldComparator implementation + #185 #216
Conversation
7156c2d
to
04e1027
Compare
3ebdaa7
to
1d4ba96
Compare
@rs / @Dragomir-Ivanov, this is now ready for review. |
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.
Except couple of code comments, LGTM.
1d4ba96
to
7cae1f7
Compare
Rebased just now. @rs - do you have some time to review this? |
7cae1f7
to
e8f1840
Compare
schema/error.go
Outdated
// Add adds an error to e and returns a new slice. If err is ErrorSlice, it is | ||
// extended so that all the elements in err are apppended to e. If err is nil, | ||
// then no error is appended. Note that calling Add on a nil slice is valid. | ||
func (e ErrorSlice) Add(err error) ErrorSlice { |
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.
As it works the same way as append
, why not calling this method Append
instead of Add
?
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.
Fixed
schema/error.go
Outdated
} | ||
|
||
// Extend copies all errors from err to e. | ||
func (e ErrorMap) Extend(err ErrorMap) { |
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.
Shouldn't this be called Merge
?
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.
Fixed
schema/error.go
Outdated
} | ||
|
||
// Tidy returns an un-typed nil if e is empty, and an ErrorMap otherwise. | ||
func (e ErrorMap) Tidy() error { |
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.
That's a strange API. Why do you need that? Couldn't we just expose a Len
method and let the caller do what's appropriate?
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.
I agree, it's strange. Will change it. Len could work.
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.
Fixed
schema/object.go
Outdated
var errMap ErrorMap | ||
errMap = errs | ||
return nil, errMap | ||
if err := ErrorMap(errs).Tidy(); err != nil { |
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.
Per my comment above, I would find it easier to read if it was:
if len(errs) > 0 {
return nil, ErrorMap(errs)
}
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.
Fixed
e8f1840
to
11ea732
Compare
@rs, apologies for my very slow response on this one. I have now updated the PR with your suggested changes. |
Nice work @smyrman . Everything looks good, however I find it strange when we get the first |
We have many use-case for Here are some examples of what we have (including some custom FieldValidators): Validator: &schema.AnyOf{
&schema.Null{},
&rsc.Duration{Min: 2 * time.Millisecond, Max: 365 * 24 * time.Hour},
} Validator: &schema.AnyOf{
&schema.Null{},
rsc.UTCTruncTime{Truncate: time.Hour * 24},
} But even if we removed the Validator: &schema.AnyOf{
&schema.Interger{Boundaries:&schema.Boundaries{Min:-5,Max:0}},
&schema.Interger{Boundaries:&schema.Boundaries{Min:5,Max:10}},
} Weird perhaps, but valid. I think it would be good to aim for feature completeness in what's supported for AnyOf, AllOf so that they are more useful and diverse. I am happy to discuss if adding the LessFunc has any unwanted side-effects that would be best to avoid. |
AnyOf and AllOf have received better error messages and doc-strings. They have also been changed so that they will accept any value if there are no members. Clean up mergeFieldErrors to be easer to read; Go allows appending to nil slice and expansion of slices in append, so there is no need for the if branch and nested for-loop that was there before. Simplify error handling in Object.Validate slightly. Moves the ErrorMap type from object.go to errors.go and add some helper methods.
Lets AnyOf and AllOf implement the FieldGetter interface.
This changes the interface of FieldComparator so that it's possible for FieldValidator types to determine, either run-time or compile-time, if the validator is comparable. AnyOf has been modified to Allow comparisons if one of it's sub-validators allows it. Changes the comparable types Float, Integer, AnyOf and Time to test only the public interface; testing the private interface gives little value, as it should be allowed to change without test updates.
Travis is running pre-scripts to go get everything without go modules enabled. Moving environment variable to `env` section. There was a typo in the scripts section when setting GO111MODULE.
11ea732
to
005ff22
Compare
rebased on master (no new changes) |
thx 🙇 |
This PR groups a few commits that does the following things in the schema package:
FieldComparator
implementations to test only the public interface.schema.AnyOf
.schema.AnyOf
errors.AnyOf
/AllOf
implement theFieldGetter
interface).Copy of commit messages (latest first):
commit 1d4ba96 (origin/smyrman/anyof-cmp)
Author: Sindre Røkenes Myren sindre@searis.no
Date: Tue Dec 11 22:08:15 2018 +0100
commit 7ed6b21
Author: Sindre Røkenes Myren sindre@searis.no
Date: Tue Dec 11 01:40:30 2018 +0100
commit 749c6a3
Author: Sindre Røkenes Myren sindre@searis.no
Date: Tue Dec 11 01:30:47 2018 +0100
commit 52c1b21
Author: Sindre Røkenes Myren sindre@searis.no
Date: Tue Dec 11 01:29:38 2018 +0100