Resolver: Place snippet marker more accuratly#82
Resolver: Place snippet marker more accuratly#82qinqon merged 3 commits intonmstate-archive:mainfrom
Conversation
|
✔️ 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 |
b035a7b to
f7fe7a1
Compare
f7fe7a1 to
6476cfe
Compare
qinqon
left a comment
There was a problem hiding this comment.
We can prevent changing all the receivers to pointer receivers also updateError is not needed.
nmpolicy/internal/resolver/errors.go
Outdated
| ) | ||
|
|
||
| type PathError struct { | ||
| err error |
There was a problem hiding this comment.
Can we name this inner like https://github.com/nmstate/nmpolicy/blob/main/nmpolicy/internal/parser/errors.go#L29-L31
Also we have to implement Unwrap function to support error unwrapping
https://github.com/nmstate/nmpolicy/blob/main/nmpolicy/internal/parser/errors.go#L29-L31
There was a problem hiding this comment.
Done. Although I think the Unwrap is redundant in this stage, it is not used.
nmpolicy/internal/resolver/errors.go
Outdated
| 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} |
There was a problem hiding this comment.
The invalid path has to be a prefix used at both pathError and wrapWithPathError
There was a problem hiding this comment.
It is, pathError calls wrapWithPathError.
nmpolicy/internal/resolver/path.go
Outdated
|
|
||
| type pathVisitor struct { | ||
| path []ast.Node | ||
| currentStep ast.Node |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
changed currentStep to be a pointer.
| r.currentNode = &(*operator)[2] | ||
| value, err := r.resolveStringOrCaptureEntryPath() | ||
| if err != nil { | ||
| r.updatePathError(err) |
There was a problem hiding this comment.
The error "downcastig" to PathError to discover the node has to be done at https://github.com/nmstate/nmpolicy/blob/6ba34e92110dbbcb092a3aff5e706afa09306f36/nmpolicy/internal/resolver/resolver.go#L230-L238
…ration Signed-off-by: Alona Kaplan <alkaplan@redhat.com>
6476cfe to
3af5440
Compare
qinqon
left a comment
There was a problem hiding this comment.
Let's not change resolver receiver if it's not needed
|
|
||
| var pathError PathError | ||
| if errors.As(err, &pathError) { | ||
| r.currentNode = &pathError.errorNode |
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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.
nmpolicy/internal/resolver/errors.go
Outdated
|
|
||
| type PathError struct { | ||
| inner error | ||
| errorNode ast.Node |
There was a problem hiding this comment.
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.
3af5440 to
fb18beb
Compare
| errorNode = &pathError.errorNode | ||
| } | ||
|
|
||
| if r.currentNode == nil || r.currentExpression == nil { |
There was a problem hiding this comment.
| if r.currentNode == nil || r.currentExpression == nil { | |
| if errorNode == nil || r.currentExpression == nil { |
fb18beb to
883473b
Compare
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>
883473b to
d8e17a3
Compare
The PR contains 3 commits-