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
store err type and path separately in strict errors #16
store err type and path separately in strict errors #16
Conversation
/assign @liggitt |
|
||
const ( | ||
unknownStrictErrType strictErrType = "unknown field" | ||
duplicateStrictErrType = "duplicate field" |
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.
duplicateStrictErrType strictErrType = "duplicate field"
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.
done
} | ||
|
||
func (e *StrictError) Error() string { | ||
return string(e.ErrType) + ` "` + e.Path + `"` |
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.
quote to keep current behavior:
return string(e.ErrType) + ` "` + e.Path + `"` | |
return string(e.ErrType) + ` "` + strconv.Quote(e.Path) + `"` |
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.
done
json.go
Outdated
@@ -75,6 +75,11 @@ const ( | |||
|
|||
// DisallowUnknownFields returns strict errors if data contains unknown fields when decoding into typed structs | |||
DisallowUnknownFields StrictOption = 2 | |||
|
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.
rather than making a new decode option and truncating the non-path information in the returned error, it might be simpler to make the returned strict errors conform to an interface so the field path can be retrieved/set:
FieldPath() string
SetFieldPath(string)
then consumers of the strict errors that need to get field path info could extract it, and consumers that need to prepend additional field paths could do so as well
// FullErrors returns all the errors with each containing the prefix | ||
// indicating what type of strict error it is. | ||
func (e *UnmarshalStrictError) FullErrors() []error { | ||
errs := make([]error, len(e.Errors)) | ||
for i, err := range e.Errors { | ||
errs[i] = errors.New(err.Error()) | ||
} | ||
return errs | ||
} | ||
|
||
// PathOnlyErrors returns the errors as only the path to the | ||
// erroneous field, leaving off the error type prefix. | ||
func (e *UnmarshalStrictError) PathOnlyErrors() []error { | ||
errs := make([]error, len(e.Errors)) | ||
for i, err := range e.Errors { | ||
errs[i] = errors.New(err.Path) | ||
} | ||
return 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.
if we go the interface route where strict errors expose getter/setter for the field path, I don't think we need these methods
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.
removed
fc711c9
to
00a6311
Compare
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.
feedback addressed, PTAL @liggitt
|
||
const ( | ||
unknownStrictErrType strictErrType = "unknown field" | ||
duplicateStrictErrType = "duplicate field" |
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.
done
} | ||
|
||
func (e *StrictError) Error() string { | ||
return string(e.ErrType) + ` "` + e.Path + `"` |
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.
done
// FullErrors returns all the errors with each containing the prefix | ||
// indicating what type of strict error it is. | ||
func (e *UnmarshalStrictError) FullErrors() []error { | ||
errs := make([]error, len(e.Errors)) | ||
for i, err := range e.Errors { | ||
errs[i] = errors.New(err.Error()) | ||
} | ||
return errs | ||
} | ||
|
||
// PathOnlyErrors returns the errors as only the path to the | ||
// erroneous field, leaving off the error type prefix. | ||
func (e *UnmarshalStrictError) PathOnlyErrors() []error { | ||
errs := make([]error, len(e.Errors)) | ||
for i, err := range e.Errors { | ||
errs[i] = errors.New(err.Path) | ||
} | ||
return 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.
removed
json.go
Outdated
// FieldError are errors that provide access to | ||
// the path of the erroneous field |
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.
// FieldError are errors that provide access to | |
// the path of the erroneous field | |
// FieldError is an error that provides access to the path of the erroneous field |
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.
update the godoc for UnmarshalStrict that returned strict errors about specific fields will implement the FieldError interface, and add type assertions to the unit tests in this package that that is the case
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.
done
json.go
Outdated
// FieldError are errors that provide access to | ||
// the path of the erroneous field | ||
type FieldError interface { | ||
Error() string |
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.
slight preference to embed the default error interface:
Error() string | |
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.
done
json.go
Outdated
FieldPath() string | ||
SetFieldPath(path string) |
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.
add godoc for these methods
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.
done
00a6311
to
1e9e857
Compare
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.
All feedback addressed, ptal @liggitt
json.go
Outdated
// FieldError are errors that provide access to | ||
// the path of the erroneous field | ||
type FieldError interface { | ||
Error() string |
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.
done
json.go
Outdated
// FieldError are errors that provide access to | ||
// the path of the erroneous field |
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.
done
json.go
Outdated
FieldPath() string | ||
SetFieldPath(path string) |
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.
done
6e2048b
to
1e9e857
Compare
/retest |
1e9e857
to
6486ab1
Compare
CI green, ptal @liggitt |
json.go
Outdated
error | ||
// FieldPath provides the full path of the erroneous field within the json object. | ||
FieldPath() string | ||
// SetFieldPath updates the path of the erroneous field to the one provided as `path`. |
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.
tweak to this to make it clear this only impacts the error message:
// SetFieldPath updates the path of the erroneous field to the one provided as `path`. | |
// SetFieldPath updates the path of the erroneous field output in the error message |
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.
done
6486ab1
to
b536e95
Compare
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.
feedback addressed, ptal @liggitt
json.go
Outdated
error | ||
// FieldPath provides the full path of the erroneous field within the json object. | ||
FieldPath() string | ||
// SetFieldPath updates the path of the erroneous field to the one provided as `path`. |
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.
done
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: kevindelgado, liggitt The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
No description provided.