Skip to content

Commit

Permalink
Handle node id string representation
Browse files Browse the repository at this point in the history
  • Loading branch information
kanapuli committed Feb 4, 2025
1 parent 4db3728 commit 460339a
Showing 1 changed file with 17 additions and 2 deletions.
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=i=")
}
if strings.HasPrefix(id, "s=ns=") {
return strings.TrimPrefix(id, "s=ns=")
}
return id
}

0 comments on commit 460339a

Please sign in to comment.