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

Added UnmarshalOpt to allow a best effort unmarshal #863

Merged
merged 17 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
18 changes: 14 additions & 4 deletions ytypes/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"reflect"

"github.com/golang/glog"
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/util"
"github.com/openconfig/ygot/ygot"
Expand Down Expand Up @@ -65,6 +66,7 @@ func UnmarshalNotifications(schema *Schema, ns []*gpb.Notification, opts ...Unma
func UnmarshalSetRequest(schema *Schema, req *gpb.SetRequest, opts ...UnmarshalOpt) error {
preferShadowPath := hasPreferShadowPath(opts)
ignoreExtraFields := hasIgnoreExtraFields(opts)
bestEffortUnmarshal := hasBestEffortUnmarshal(opts)
if req == nil {
return nil
}
Expand All @@ -87,10 +89,10 @@ func UnmarshalSetRequest(schema *Schema, req *gpb.SetRequest, opts ...UnmarshalO
if err := deletePaths(schema.SchemaTree[nodeName], node, prefix, req.Delete, preferShadowPath); err != nil {
lgomez9 marked this conversation as resolved.
Show resolved Hide resolved
return err
}
if err := replacePaths(schema.SchemaTree[nodeName], node, prefix, req.Replace, preferShadowPath, ignoreExtraFields); err != nil {
if err := replacePaths(schema.SchemaTree[nodeName], node, prefix, req.Replace, preferShadowPath, ignoreExtraFields, bestEffortUnmarshal); err != nil {
return err
}
if err := updatePaths(schema.SchemaTree[nodeName], node, prefix, req.Update, preferShadowPath, ignoreExtraFields); err != nil {
if err := updatePaths(schema.SchemaTree[nodeName], node, prefix, req.Update, preferShadowPath, ignoreExtraFields, bestEffortUnmarshal); err != nil {
return err
}

Expand Down Expand Up @@ -162,7 +164,7 @@ func joinPrefixToUpdate(prefix *gpb.Path, update *gpb.Update) (*gpb.Update, erro
// replacePaths unmarshals a slice of updates into the given GoStruct. It
// deletes the values at these paths before unmarshalling them. These updates
// can either by JSON-encoded or gNMI-encoded values (scalars).
func replacePaths(schema *yang.Entry, goStruct ygot.GoStruct, prefix *gpb.Path, updates []*gpb.Update, preferShadowPath, ignoreExtraFields bool) error {
func replacePaths(schema *yang.Entry, goStruct ygot.GoStruct, prefix *gpb.Path, updates []*gpb.Update, preferShadowPath, ignoreExtraFields, bestEffortUnmarshal bool) error {
var dopts []DelNodeOpt
if preferShadowPath {
dopts = append(dopts, &PreferShadowPath{})
Expand All @@ -177,6 +179,10 @@ func replacePaths(schema *yang.Entry, goStruct ygot.GoStruct, prefix *gpb.Path,
return err
}
if err := setNode(schema, goStruct, update, preferShadowPath, ignoreExtraFields); err != nil {
if bestEffortUnmarshal {
glog.Infof("error while unmarshalling: %v", err)
continue
}
return err
}
}
Expand All @@ -185,13 +191,17 @@ func replacePaths(schema *yang.Entry, goStruct ygot.GoStruct, prefix *gpb.Path,

// updatePaths unmarshals a slice of updates into the given GoStruct. These
// updates can either by JSON-encoded or gNMI-encoded values (scalars).
func updatePaths(schema *yang.Entry, goStruct ygot.GoStruct, prefix *gpb.Path, updates []*gpb.Update, preferShadowPath, ignoreExtraFields bool) error {
func updatePaths(schema *yang.Entry, goStruct ygot.GoStruct, prefix *gpb.Path, updates []*gpb.Update, preferShadowPath, ignoreExtraFields, bestEffortUnmarshal bool) error {
for _, update := range updates {
var err error
if update, err = joinPrefixToUpdate(prefix, update); err != nil {
return err
}
if err := setNode(schema, goStruct, update, preferShadowPath, ignoreExtraFields); err != nil {
if bestEffortUnmarshal {
glog.Infof("error while unmarshalling: %v", err)
continue
}
return err
}
}
Expand Down
20 changes: 20 additions & 0 deletions ytypes/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ type UnmarshalOpt interface {
IsUnmarshalOpt()
}

// BestEffortUnmarshal is an unmarshal option that 'ignores' errors while unmarshalling,
lgomez9 marked this conversation as resolved.
Show resolved Hide resolved
// and continues the unmarshaling process. A failing unmarshal will still fail, but it will
// fail as a series of error reports.
wenovus marked this conversation as resolved.
Show resolved Hide resolved
type BestEffortUnmarshal struct{}
wenovus marked this conversation as resolved.
Show resolved Hide resolved

// IsUnmarshalOpt marks BestEffortUnmarshal as a valid UnmarshalOpt.
func (*BestEffortUnmarshal) IsUnmarshalOpt() {}

// IgnoreExtraFields is an unmarshal option that controls the
// behaviour of the Unmarshal function when additional fields are
// found in the input JSON. By default, an error will be returned,
Expand Down Expand Up @@ -123,3 +131,15 @@ func hasPreferShadowPath(opts []UnmarshalOpt) bool {
}
return false
}

// hasBestEffortUnmarshal determines whether the supplied slice of UnmarshalOpts
// contains the BestEffortUnmarshal option.
func hasBestEffortUnmarshal(opts []UnmarshalOpt) bool {
for _, o := range opts {
if _, ok := o.(*BestEffortUnmarshal); ok {
return true
}
}

return false
}