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

Allow for the deviation of Must Statements #278

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pkg/yang/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,10 @@ func (e *Entry) ApplyDeviate(deviateOpts ...DeviateOpt) []error {
deviatedNode.Type = devSpec.Type
}

if musts, exists := devSpec.Extra["must"]; exists {
deviatedNode.Extra["must"] = append(deviatedNode.Extra["must"], musts...)
}

case DeviationNotSupported:
dp := deviatedNode.Parent
if dp == nil {
Expand Down Expand Up @@ -1248,6 +1252,37 @@ func (e *Entry) ApplyDeviate(deviateOpts ...DeviateOpt) []error {
deviatedNode.ListAttr.MaxElements = math.MaxUint64
}

if musts, exists := devSpec.Extra["must"]; exists {
// range through the deviations to apply them
for _, mustDeleteInterf := range musts {

mustDelete := mustDeleteInterf.(*Must)

found := false
var idx int
var existingMustInterf interface{}
var existingMust *Must
for idx, existingMustInterf = range deviatedNode.Extra["must"] {
if existingMustInterf.(*Must).Name == mustDelete.Name {
existingMust = existingMustInterf.(*Must)
if (existingMust.ErrorMessage == nil && mustDelete.ErrorMessage == nil) ||
(existingMust.ErrorMessage != nil && mustDelete.ErrorMessage != nil &&
existingMust.ErrorMessage.Name == mustDelete.ErrorMessage.Name) {
found = true
break
}
}
}
if found {
devNodeMust := deviatedNode.Extra["must"]
devNodeMust[idx] = devNodeMust[len(devNodeMust)-1] // Swap with last element
deviatedNode.Extra["must"] = devNodeMust[:len(devNodeMust)-1] // Trim last element
} else {
appendErr(fmt.Errorf("must statement marked for deletion not found [%s]", existingMust.Name))
}
}
}

default:
appendErr(fmt.Errorf("invalid deviation type %s", dt))
}
Expand Down