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

fix: ReferenceNodes usage with mask set #683

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,13 @@ func (c *Client) Node(id *ua.NodeID) *Node {
return &Node{ID: id, c: c}
}

// NodeFromExpandedNodeID returns a node object which accesses its attributes
// through this client connection. This is usually needed when working with node ids returned
// from browse responses by the server.
func (c *Client) NodeFromExpandedNodeID(id *ua.ExpandedNodeID) *Node {
return &Node{ID: ua.NewNodeIDFromExpandedNodeID(id), c: c}
}

// FindServers finds the servers available at an endpoint
func (c *Client) FindServers(ctx context.Context) (*ua.FindServersResponse, error) {
stats.Client().Add("FindServers", 1)
Expand Down
2 changes: 1 addition & 1 deletion node.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (n *Node) ReferencedNodes(ctx context.Context, refs uint32, dir ua.BrowseDi
return nil, err
}
for _, r := range res {
nodes = append(nodes, n.c.Node(r.NodeID.NodeID))
nodes = append(nodes, n.c.NodeFromExpandedNodeID(r.NodeID))
}
return nodes, nil
}
Expand Down
29 changes: 29 additions & 0 deletions ua/node_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,35 @@ func NewByteStringNodeID(ns uint16, id []byte) *NodeID {
}
}

// NewNodeIDFromExpandedNodeID returns a new NodeID derived from ExpandedNodeID
func NewNodeIDFromExpandedNodeID(id *ExpandedNodeID) *NodeID {
bid := make([]byte, len(id.NodeID.bid))
copy(bid, id.NodeID.bid)
if len(bid) == 0 {
bid = nil
}
var gid *GUID
if egid := id.NodeID.gid; egid != nil {
var ngid GUID
ngid.Data1 = egid.Data1
ngid.Data2 = egid.Data2
ngid.Data3 = egid.Data3
ngid.Data4 = make([]byte, len(egid.Data4))
copy(ngid.Data4, egid.Data4)
if len(ngid.Data4) == 0 {
ngid.Data4 = nil
}
gid = &ngid
}
return &NodeID{
mask: id.NodeID.mask & 0b00111111, // expanded flag NamespaceURI and ServerIndex need to be unset
ns: id.NodeID.ns,
nid: id.NodeID.nid,
bid: bid,
gid: gid,
}
}

// MustParseNodeID returns a node id from a string definition
// if it is parseable by ParseNodeID. Otherwise, the function
// panics.
Expand Down
68 changes: 68 additions & 0 deletions ua/node_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,71 @@ func TestNodeIDToString(t *testing.T) {
})
}
}

func TestNewNodeIDFromExpandedNodeID(t *testing.T) {
type args struct {
id *ExpandedNodeID
}
tests := []struct {
name string
args args
want *NodeID
}{
{
name: "NewExpandedNodeID",
args: args{
id: NewExpandedNodeID(NewTwoByteNodeID(42), "someUri", 42),
},
want: NewTwoByteNodeID(42),
},
{
name: "NewTwoByteExpandedNodeID",
args: args{
id: NewTwoByteExpandedNodeID(42),
},
want: NewTwoByteNodeID(42),
},
{
name: "NewFourByteExpandedNodeID",
args: args{
NewFourByteExpandedNodeID(42, 24),
},
want: NewFourByteNodeID(42, 24),
},
{
name: "NewNumericExpandedNodeID",
args: args{
NewNumericExpandedNodeID(42, 24),
},
want: NewNumericNodeID(42, 24),
},
{
name: "NewStringExpandedNodeID",
args: args{
NewStringExpandedNodeID(42, "42"),
},
want: NewStringNodeID(42, "42"),
},
{
name: "NewGUIDExpandedNodeID",
args: args{
NewGUIDExpandedNodeID(42, "42"),
},
want: NewGUIDNodeID(42, "42"),
},
{
name: "NewByteStringExpandedNodeID",
args: args{
NewByteStringExpandedNodeID(42, []byte{0xAF, 0xFE}),
},
want: NewByteStringNodeID(42, []byte{0xAF, 0xFE}),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewNodeIDFromExpandedNodeID(tt.args.id); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewNodeIDFromExpandedNodeID() = %#v, want %#v", got, tt.want)
}
})
}
}