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

parser, resolver: Implement Replace operator :=#49

Merged
qinqon merged 2 commits intonmstate-archive:mainfrom
qinqon:replace
Dec 2, 2021
Merged

parser, resolver: Implement Replace operator :=#49
qinqon merged 2 commits intonmstate-archive:mainfrom
qinqon:replace

Conversation

@qinqon
Copy link
Contributor

@qinqon qinqon commented Nov 23, 2021

For the main scenario NMPolicy need to resolve capture.base-iface-routes | routes.running.next-hop-interface:="br1" part of this expression is the replace operator := this PR implement that operator at parser and resolver and also allow filter/replace to use a capture ref path as input so next PR implementing the pipe will involve only the parser.

Thech debt:

  • For apply functions at nmpolicy/internal/path.go:
    • Use a struct so func and flags does not need to be passed around
    • The expectedNode argument is not needed to be passed if we function to apply is a closure containing it.

Copy link
Collaborator

@AlonaKaplan AlonaKaplan left a comment

Choose a reason for hiding this comment

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

Reviewed the first two commits.

@qinqon
Copy link
Contributor Author

qinqon commented Dec 1, 2021

/kind enhancement

@github-actions github-actions bot added the kind/enhancement New feature or request label Dec 1, 2021
@qinqon qinqon removed the enhancement label Dec 1, 2021
To implement the Replace operation the token REPLACE has to be
understood by the parser and construct the proper ast.Node.

Signed-off-by: Quique Llorente <ellorent@redhat.com>
@qinqon
Copy link
Contributor Author

qinqon commented Dec 2, 2021

/hold

Let's not filter on replace if users need it they will have to pipe it first.

@github-actions github-actions bot added the hold label Dec 2, 2021

func filter(inputState map[string]interface{}, path ast.VariadicOperator, expectedNode ast.Node) (map[string]interface{}, error) {
filtered, err := applyFuncOnPath(inputState, path, expectedNode, mapContainsValue, true)
filtered, err := applyFuncOnMap(path, inputState, expectedNode, mapContainsValue, true, true)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see applyFuncOnMap as an internal method that should be used only by applyFuncOnPath.
Anyway, how is this change related to the commit/PR?

Copy link
Contributor Author

@qinqon qinqon Dec 2, 2021

Choose a reason for hiding this comment

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

this is internal code, and we know that inputState is a map, does not make sense to call applyFuncOnPath, this way we remove one call, that is doing some reflection that is not needed here.

As first step to implement the Replace operation at the resolver this
change implement path value resplacement but just for current state
since it does not involve changing path.go file to traverse capture
entry path ref and apply a function.

The replace operation has to be able to use a capture ref path as input,
also some tuning is needed at the apply functions since the modified
value has to be returned and that's different from filter function.

Signed-off-by: Quique Llorente <ellorent@redhat.com>
@qinqon
Copy link
Contributor Author

qinqon commented Dec 2, 2021

/hold cancel

@github-actions github-actions bot removed the hold label Dec 2, 2021
sliceEmptyAfterApply = false
adjustedSlice = append(adjustedSlice, valueAfterApply)
} else if !shouldFilterSlice {
adjustedSlice = append(adjustedSlice, valueToCheck)
Copy link
Collaborator

Choose a reason for hiding this comment

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

missing sliceEmptyAfterApply=false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here valueAfterApply is nil we don't have to set the variable

}

if len(filteredSlice) == 0 {
if sliceEmptyAfterApply {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would keep it len(adjustedSlice) == 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is no tthe same, adjustedSlice can be > 0 and sliceEmptyAfterApply false.

@qinqon qinqon requested a review from AlonaKaplan December 2, 2021 13:45
@qinqon qinqon merged commit 0649f70 into nmstate-archive:main Dec 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

kind/enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants