-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
resolver: Implement replace operation
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 <[email protected]>
- Loading branch information
Showing
6 changed files
with
263 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2021 NMPolicy Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package resolver | ||
|
||
import ( | ||
"github.com/nmstate/nmpolicy/nmpolicy/internal/ast" | ||
) | ||
|
||
func replace(inputState map[string]interface{}, path ast.VariadicOperator, expectedNode ast.Node) (map[string]interface{}, error) { | ||
replaced, err := applyFuncOnMap(path, inputState, expectedNode, replaceMapFieldValue, false, false) | ||
|
||
if err != nil { | ||
return nil, replaceError("failed applying operation on the path: %v", err) | ||
} | ||
|
||
replacedMap, ok := replaced.(map[string]interface{}) | ||
if !ok { | ||
return nil, replaceError("failed converting result to a map") | ||
} | ||
return replacedMap, nil | ||
} | ||
|
||
func replaceMapFieldValue(inputMap map[string]interface{}, mapEntryKeyToReplace string, expectedNode ast.Node) (interface{}, error) { | ||
if expectedNode.String == nil { | ||
return false, replaceError("the desired value %+v is not supported. Curretly only string values are supported", expectedNode) | ||
} | ||
modifiedMap := map[string]interface{}{} | ||
for k, v := range inputMap { | ||
modifiedMap[k] = v | ||
} | ||
|
||
modifiedMap[mapEntryKeyToReplace] = *expectedNode.String | ||
return modifiedMap, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.