github.com/prebid/prebid-server/v2@v2.18.0/ortb/clone.go (about) 1 package ortb 2 3 import ( 4 "github.com/prebid/openrtb/v20/openrtb2" 5 "github.com/prebid/prebid-server/v2/util/ptrutil" 6 "github.com/prebid/prebid-server/v2/util/sliceutil" 7 ) 8 9 func CloneDataSlice(s []openrtb2.Data) []openrtb2.Data { 10 if s == nil { 11 return nil 12 } 13 14 c := make([]openrtb2.Data, len(s)) 15 for i, d := range s { 16 c[i] = CloneData(d) 17 } 18 19 return c 20 } 21 22 func CloneData(s openrtb2.Data) openrtb2.Data { 23 // Shallow Copy (Value Fields) Occurred By Passing Argument By Value 24 // - Implicitly created by the cloned array. 25 26 // Deep Copy (Pointers) 27 s.Segment = CloneSegmentSlice(s.Segment) 28 s.Ext = sliceutil.Clone(s.Ext) 29 30 return s 31 } 32 33 func CloneSegmentSlice(s []openrtb2.Segment) []openrtb2.Segment { 34 if s == nil { 35 return nil 36 } 37 38 c := make([]openrtb2.Segment, len(s)) 39 for i, d := range s { 40 c[i] = CloneSegment(d) 41 } 42 43 return c 44 } 45 46 func CloneSegment(s openrtb2.Segment) openrtb2.Segment { 47 // Shallow Copy (Value Fields) Occurred By Passing Argument By Value 48 // - Implicitly created by the cloned array. 49 50 // Deep Copy (Pointers) 51 s.Ext = sliceutil.Clone(s.Ext) 52 53 return s 54 } 55 56 func CloneUser(s *openrtb2.User) *openrtb2.User { 57 if s == nil { 58 return nil 59 } 60 61 // Shallow Copy (Value Fields) 62 c := *s 63 64 // Deep Copy (Pointers) 65 c.KwArray = sliceutil.Clone(s.KwArray) 66 c.Geo = CloneGeo(s.Geo) 67 c.Data = CloneDataSlice(s.Data) 68 c.EIDs = CloneEIDSlice(s.EIDs) 69 c.Ext = sliceutil.Clone(s.Ext) 70 71 return &c 72 } 73 74 func CloneDevice(s *openrtb2.Device) *openrtb2.Device { 75 if s == nil { 76 return nil 77 } 78 79 // Shallow Copy (Value Fields) 80 c := *s 81 82 // Deep Copy (Pointers) 83 c.Geo = CloneGeo(s.Geo) 84 c.DNT = ptrutil.Clone(s.DNT) 85 c.Lmt = ptrutil.Clone(s.Lmt) 86 c.SUA = CloneUserAgent(s.SUA) 87 c.JS = ptrutil.Clone(s.JS) 88 c.GeoFetch = ptrutil.Clone(s.GeoFetch) 89 c.ConnectionType = ptrutil.Clone(s.ConnectionType) 90 c.Ext = sliceutil.Clone(s.Ext) 91 92 return &c 93 } 94 95 func CloneUserAgent(s *openrtb2.UserAgent) *openrtb2.UserAgent { 96 if s == nil { 97 return nil 98 } 99 100 // Shallow Copy (Value Fields) 101 c := *s 102 103 // Deep Copy (Pointers) 104 c.Browsers = CloneBrandVersionSlice(s.Browsers) 105 c.Platform = CloneBrandVersion(s.Platform) 106 107 if s.Mobile != nil { 108 mobileCopy := *s.Mobile 109 c.Mobile = &mobileCopy 110 } 111 s.Ext = sliceutil.Clone(s.Ext) 112 113 return &c 114 } 115 116 func CloneBrandVersionSlice(s []openrtb2.BrandVersion) []openrtb2.BrandVersion { 117 if s == nil { 118 return nil 119 } 120 121 c := make([]openrtb2.BrandVersion, len(s)) 122 for i, d := range s { 123 bv := CloneBrandVersion(&d) 124 c[i] = *bv 125 } 126 127 return c 128 } 129 130 func CloneBrandVersion(s *openrtb2.BrandVersion) *openrtb2.BrandVersion { 131 if s == nil { 132 return nil 133 } 134 135 // Shallow Copy (Value Fields) Occurred By Passing Argument By Value 136 c := *s 137 138 // Deep Copy (Pointers) 139 c.Version = sliceutil.Clone(s.Version) 140 c.Ext = sliceutil.Clone(s.Ext) 141 142 return &c 143 } 144 145 func CloneSource(s *openrtb2.Source) *openrtb2.Source { 146 if s == nil { 147 return nil 148 } 149 150 // Shallow Copy (Value Fields) 151 c := *s 152 153 // Deep Copy (Pointers) 154 c.FD = ptrutil.Clone(s.FD) 155 c.SChain = CloneSChain(s.SChain) 156 c.Ext = sliceutil.Clone(s.Ext) 157 158 return &c 159 } 160 161 func CloneSChain(s *openrtb2.SupplyChain) *openrtb2.SupplyChain { 162 if s == nil { 163 return nil 164 } 165 166 // Shallow Copy (Value Fields) 167 c := *s 168 169 // Deep Copy (Pointers) 170 c.Nodes = CloneSupplyChainNodes(s.Nodes) 171 c.Ext = sliceutil.Clone(s.Ext) 172 173 return &c 174 } 175 176 func CloneSupplyChainNodes(s []openrtb2.SupplyChainNode) []openrtb2.SupplyChainNode { 177 if s == nil { 178 return nil 179 } 180 181 c := make([]openrtb2.SupplyChainNode, len(s)) 182 for i, d := range s { 183 c[i] = CloneSupplyChainNode(d) 184 } 185 186 return c 187 } 188 189 func CloneSupplyChainNode(s openrtb2.SupplyChainNode) openrtb2.SupplyChainNode { 190 // Shallow Copy (Value Fields) Occurred By Passing Argument By Value 191 // - Implicitly created by the cloned array. 192 193 // Deep Copy (Pointers) 194 s.HP = ptrutil.Clone(s.HP) 195 s.Ext = sliceutil.Clone(s.Ext) 196 197 return s 198 } 199 200 func CloneGeo(s *openrtb2.Geo) *openrtb2.Geo { 201 if s == nil { 202 return nil 203 } 204 205 // Shallow Copy (Value Fields) 206 c := *s 207 208 // Deep Copy (Pointers) 209 c.Lat = ptrutil.Clone(s.Lat) 210 c.Lon = ptrutil.Clone(s.Lon) 211 c.Ext = sliceutil.Clone(s.Ext) 212 213 return &c 214 } 215 216 func CloneEIDSlice(s []openrtb2.EID) []openrtb2.EID { 217 if s == nil { 218 return nil 219 } 220 221 c := make([]openrtb2.EID, len(s)) 222 for i, d := range s { 223 c[i] = CloneEID(d) 224 } 225 226 return c 227 } 228 229 func CloneEID(s openrtb2.EID) openrtb2.EID { 230 // Shallow Copy (Value Fields) Occurred By Passing Argument By Value 231 // - Implicitly created by the cloned array. 232 233 // Deep Copy (Pointers) 234 s.UIDs = CloneUIDSlice(s.UIDs) 235 s.Ext = sliceutil.Clone(s.Ext) 236 237 return s 238 } 239 240 func CloneUIDSlice(s []openrtb2.UID) []openrtb2.UID { 241 if s == nil { 242 return nil 243 } 244 245 c := make([]openrtb2.UID, len(s)) 246 for i, d := range s { 247 c[i] = CloneUID(d) 248 } 249 250 return c 251 } 252 253 func CloneUID(s openrtb2.UID) openrtb2.UID { 254 // Shallow Copy (Value Fields) Occurred By Passing Argument By Value 255 // - Implicitly created by the cloned array. 256 257 // Deep Copy (Pointers) 258 s.Ext = sliceutil.Clone(s.Ext) 259 260 return s 261 } 262 263 // CloneBidRequestPartial performs a deep clone of just the bid request device, user, and source fields. 264 func CloneBidRequestPartial(s *openrtb2.BidRequest) *openrtb2.BidRequest { 265 if s == nil { 266 return nil 267 } 268 269 // Shallow Copy (Value Fields) 270 c := *s 271 272 // Deep Copy (Pointers) - PARTIAL CLONE 273 c.Device = CloneDevice(s.Device) 274 c.User = CloneUser(s.User) 275 c.Source = CloneSource(s.Source) 276 277 return &c 278 }