Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Resolver: Place snippet marker more accuratly#82

Merged
qinqon merged 3 commits intonmstate-archive:mainfrom
AlonaKaplan:pathError
Jan 20, 2022
Merged

Resolver: Place snippet marker more accuratly#82
qinqon merged 3 commits intonmstate-archive:mainfrom
AlonaKaplan:pathError

Conversation

@AlonaKaplan
Copy link
Collaborator

The PR contains 3 commits-

  1. Place snippet marker on correct part of a Ternary operation
  2. Place snippet marker on correct key of a path
  3. Support optional keys on any part of the path (continues PR resolver, eqfilter: Support optional fields #75, which allowed optional keys on the last key of the path).

@netlify
Copy link

netlify bot commented Jan 17, 2022

✔️ Deploy Preview for nmpolicy ready!

🔨 Explore the source changes: d8e17a3

🔍 Inspect the deploy log: https://app.netlify.com/sites/nmpolicy/deploys/61e95512ee92bd0007a9f0cd

😎 Browse the preview: https://deploy-preview-82--nmpolicy.netlify.app

Copy link
Contributor

@qinqon qinqon left a comment

Choose a reason for hiding this comment

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

We can prevent changing all the receivers to pointer receivers also updateError is not needed.

)

type PathError struct {
err error
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done. Although I think the Unwrap is redundant in this stage, it is not used.

Comment on lines +34 to +39
func pathError(currentStepNode ast.Node, format string, a ...interface{}) PathError {
return wrapWithPathError(currentStepNode, fmt.Errorf(format, a...))
}

func wrapWithPathError(currentStepNode ast.Node, err error) PathError {
return PathError{err: fmt.Errorf("invalid path: %v", err), errorNode: currentStepNode}
Copy link
Contributor

Choose a reason for hiding this comment

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

The invalid path has to be a prefix used at both pathError and wrapWithPathError

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is, pathError calls wrapWithPathError.


type pathVisitor struct {
path []ast.Node
currentStep ast.Node
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be an int with the path index so we don't have to change v pathVisitor to v* pathVisitor only because the struct has too much stuff, and alternative is to make currentStep a pointer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed currentStep to be a pointer.

r.currentNode = &(*operator)[2]
value, err := r.resolveStringOrCaptureEntryPath()
if err != nil {
r.updatePathError(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

…ration

Signed-off-by: Alona Kaplan <alkaplan@redhat.com>
Copy link
Contributor

@qinqon qinqon left a comment

Choose a reason for hiding this comment

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

Let's not change resolver receiver if it's not needed


var pathError PathError
if errors.As(err, &pathError) {
r.currentNode = &pathError.errorNode
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to change resolver state also, the resolver is "by value" at the reciver let's just do the following it will be less confusing:

    errorNode := r.currentNode
    if errors.As(err, &pathError) {
		errorNode = &pathError.errorNode
    }
    if errorNode == nil || r.currentExpression == nil {
		return err
    }
    return expression.WrapError(err, *r.currentExpression, errorNode.Meta.Position)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Since it is "by value` it can be changed and it will effect only the current method.
But I"m ok with both versions, so done.


type PathError struct {
inner error
errorNode ast.Node
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a pointer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

why?

Copy link
Contributor

@qinqon qinqon Jan 20, 2022

Choose a reason for hiding this comment

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

To detect if we are missing setting it (by a bug or the like) is better if we have a pointer if we don't it's more difficult.

We are using like this right now

if errors.As(err, &pathError) {
	errorNode = &pathError.errorNode
}

So even if it's not set it will set it to default errorNode with have position 0 wich is wrong, so better be explicit about errorNode not set using a pointer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

errorNode = &pathError.errorNode
}

if r.currentNode == nil || r.currentExpression == nil {
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
if r.currentNode == nil || r.currentExpression == nil {
if errorNode == nil || r.currentExpression == nil {

Signed-off-by: Alona Kaplan <alkaplan@redhat.com>
This commit continues PR nmstate-archive#75.

In case the path contains fields that don't exist on the input state,
the resolver shouldn't fail, just return an empty result.

Signed-off-by: Alona Kaplan <alkaplan@redhat.com>
@qinqon qinqon merged commit 51221c1 into nmstate-archive:main Jan 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants