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

store err type and path separately in strict errors #16

Merged

Conversation

kevindelgado
Copy link
Contributor

No description provided.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 12, 2022
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 12, 2022
@kevindelgado
Copy link
Contributor Author

/assign @liggitt


const (
unknownStrictErrType strictErrType = "unknown field"
duplicateStrictErrType = "duplicate field"
Copy link
Contributor

@liggitt liggitt Jul 12, 2022

Choose a reason for hiding this comment

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

duplicateStrictErrType strictErrType = "duplicate field"

Copy link
Contributor Author

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 + `"`
Copy link
Contributor

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:

Suggested change
return string(e.ErrType) + ` "` + e.Path + `"`
return string(e.ErrType) + ` "` + strconv.Quote(e.Path) + `"`

Copy link
Contributor Author

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

Copy link
Contributor

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

Comment on lines 164 to 182
// 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
}
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

Copy link
Contributor Author

@kevindelgado kevindelgado left a 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"
Copy link
Contributor Author

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 + `"`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 164 to 182
// 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
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

json.go Outdated
Comment on lines 141 to 142
// FieldError are errors that provide access to
// the path of the erroneous field
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// 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

Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

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:

Suggested change
Error() string
error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

json.go Outdated
Comment on lines 145 to 149
FieldPath() string
SetFieldPath(path string)
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor Author

@kevindelgado kevindelgado left a 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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

json.go Outdated
Comment on lines 141 to 142
// FieldError are errors that provide access to
// the path of the erroneous field
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

json.go Outdated
Comment on lines 145 to 149
FieldPath() string
SetFieldPath(path string)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@kevindelgado kevindelgado force-pushed the strict-error-path-separation branch 2 times, most recently from 6e2048b to 1e9e857 Compare July 12, 2022 22:29
@kevindelgado
Copy link
Contributor Author

/retest

@kevindelgado
Copy link
Contributor Author

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`.
Copy link
Contributor

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:

Suggested change
// 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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor Author

@kevindelgado kevindelgado left a 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`.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@liggitt
Copy link
Contributor

liggitt commented Jul 13, 2022

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 13, 2022
@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 13, 2022
@k8s-ci-robot k8s-ci-robot merged commit f223a00 into kubernetes-sigs:main Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants