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

[ENG-2252] Handle node id string representation #124

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
19 changes: 17 additions & 2 deletions opcua_plugin/browse_frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func collectNodes(ctx context.Context, nodeBrowserChan chan NodeDef, nodeIDMap m
case <-ctx.Done():
return
default:
nodeID := node.NodeID.String()
nodeID := normalizeNodeID(node.NodeID)
nodeIDMap[nodeID] = &node
*nodes = append(*nodes, node)
}
Expand All @@ -121,7 +121,8 @@ func constructNodeHierarchy(rootNode *Node, node NodeDef, nodeIDMap map[string]*
for i, part := range paths {
if _, exists := current.ChildIDMap[part]; !exists {
parentNode := findNthParentNode(length-i-1, &node, nodeIDMap)
id, err := ua.ParseNodeID(parentNode.NodeID.String())
normalizedParentID := normalizeNodeID(parentNode.NodeID)
id, err := ua.ParseNodeID(normalizedParentID)
if err != nil {
// This should never happen
// All node ids should be valid
Expand Down Expand Up @@ -158,3 +159,17 @@ func findNthParentNode(n int, node *NodeDef, nodeIDMap map[string]*NodeDef) *Nod

return node
}

// normalizeNodeID normalizes a node id string representation by removing unwanted 's=' prefixes
// that can occur when the nodeID type is wrongly interpreted as a string type instead of a numeric type.
// The prefix 's=' happens rarely but this function acts as a defensive mechanism to handle such scenarios
func normalizeNodeID(nodeID *ua.NodeID) string {
id := nodeID.String()
if strings.HasPrefix(id, "s=i=") {
return strings.TrimPrefix(id, "s=")
}
if strings.HasPrefix(id, "s=ns=") {
return strings.TrimPrefix(id, "s=")
}
return id
}
Loading