11//
2- // Copyright (C) 2020-2024 IOTech Ltd
2+ // Copyright (C) 2020-2025 IOTech Ltd
33//
44// SPDX-License-Identifier: Apache-2.0
55
66package models
77
8+ import "maps"
9+
810type Device struct {
911 DBTimestamp
1012 Id string
@@ -26,6 +28,12 @@ type Device struct {
2628// ProtocolProperties contains the device connection information in key/value pair
2729type ProtocolProperties map [string ]any
2830
31+ func (p ProtocolProperties ) Clone () ProtocolProperties {
32+ cloned := make (map [string ]any )
33+ maps .Copy (cloned , p )
34+ return cloned
35+ }
36+
2937// AdminState controls the range of values which constitute valid administrative states for a device
3038type AdminState string
3139
@@ -39,3 +47,41 @@ func AssignAdminState(dtoAdminState string) AdminState {
3947
4048// OperatingState is an indication of the operations of the device.
4149type OperatingState string
50+
51+ func (device Device ) Clone () Device {
52+ cloned := Device {
53+ DBTimestamp : device .DBTimestamp ,
54+ Id : device .Id ,
55+ Name : device .Name ,
56+ Parent : device .Parent ,
57+ Description : device .Description ,
58+ AdminState : device .AdminState ,
59+ OperatingState : device .OperatingState ,
60+ Location : device .Location ,
61+ ServiceName : device .ServiceName ,
62+ ProfileName : device .ProfileName ,
63+ }
64+ if len (device .Protocols ) > 0 {
65+ cloned .Protocols = make (map [string ]ProtocolProperties )
66+ for k , v := range device .Protocols {
67+ cloned .Protocols [k ] = v .Clone ()
68+ }
69+ }
70+ if len (device .Labels ) > 0 {
71+ cloned .Labels = make ([]string , len (device .Labels ))
72+ copy (cloned .Labels , device .Labels )
73+ }
74+ if len (device .AutoEvents ) > 0 {
75+ cloned .AutoEvents = make ([]AutoEvent , len (device .AutoEvents ))
76+ copy (cloned .AutoEvents , device .AutoEvents )
77+ }
78+ if len (device .Tags ) > 0 {
79+ cloned .Tags = make (map [string ]any )
80+ maps .Copy (cloned .Tags , device .Tags )
81+ }
82+ if len (device .Properties ) > 0 {
83+ cloned .Properties = make (map [string ]any )
84+ maps .Copy (cloned .Properties , device .Properties )
85+ }
86+ return cloned
87+ }
0 commit comments