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

Add GetNodeByOID which keeps index #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,12 @@ func GetNodeByOID(oid types.Oid) (node SmiNode, err error) {
}
return CreateNode(smiNode), nil
}

func GetNodeByOIDWithIndex(oid types.Oid) (node SmiNode, index []types.SmiSubId, err error) {
smiNode, index := smi.GetNodeByOIDWithIndex(oid)
if smiNode == nil {
err = fmt.Errorf("Could not find node for OID %s", oid)
return
}
return CreateNode(smiNode), index, nil
}
19 changes: 19 additions & 0 deletions smi/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ func GetNodeByOID(oid types.Oid) *types.SmiNode {
return nodePtr.FirstObject.GetSmiNode()
}

func GetNodeByOIDWithIndex(oid types.Oid) (*types.SmiNode, []types.SmiSubId) {
if len(oid) == 0 || internal.Root() == nil {
return nil, nil
}
var parentPtr, nodePtr *internal.Node = nil, internal.Root()
i := 0
for ; i < len(oid) && nodePtr != nil; i++ {
parentPtr, nodePtr = nodePtr, nodePtr.Children.Get(oid[i])
}
if nodePtr == nil {
nodePtr = parentPtr
i--
}
if nodePtr == nil || nodePtr.FirstObject == nil {
return nil, nil
}
return nodePtr.FirstObject.GetSmiNode(), oid[i:]
}

// SmiNode *smiGetFirstNode(SmiModule *smiModulePtr, SmiNodekind nodekind)
func GetFirstNode(smiModulePtr *types.SmiModule, nodekind types.NodeKind) *types.SmiNode {
if smiModulePtr == nil {
Expand Down