github.com/prebid/prebid-server@v0.275.0/ortb/clone.go (about) 1 package ortb 2 3 import ( 4 "github.com/prebid/openrtb/v19/openrtb2" 5 "github.com/prebid/prebid-server/util/ptrutil" 6 "github.com/prebid/prebid-server/util/sliceutil" 7 ) 8 9 func CloneApp(s *openrtb2.App) *openrtb2.App { 10 if s == nil { 11 return nil 12 } 13 14 // Shallow Copy (Value Fields) 15 c := *s 16 17 // Deep Copy (Pointers) 18 c.Cat = sliceutil.Clone(s.Cat) 19 c.SectionCat = sliceutil.Clone(s.SectionCat) 20 c.PageCat = sliceutil.Clone(s.PageCat) 21 c.Publisher = ClonePublisher(s.Publisher) 22 c.Content = CloneContent(s.Content) 23 c.KwArray = sliceutil.Clone(s.KwArray) 24 c.Ext = sliceutil.Clone(s.Ext) 25 26 return &c 27 } 28 29 func ClonePublisher(s *openrtb2.Publisher) *openrtb2.Publisher { 30 if s == nil { 31 return nil 32 } 33 34 // Shallow Copy (Value Fields) 35 c := *s 36 37 // Deep Copy (Pointers) 38 c.Cat = sliceutil.Clone(s.Cat) 39 c.Ext = sliceutil.Clone(s.Ext) 40 41 return &c 42 } 43 44 func CloneContent(s *openrtb2.Content) *openrtb2.Content { 45 if s == nil { 46 return nil 47 } 48 49 // Shallow Copy (Value Fields) 50 c := *s 51 52 // Deep Copy (Pointers) 53 c.Producer = CloneProducer(s.Producer) 54 c.Cat = sliceutil.Clone(s.Cat) 55 c.ProdQ = ptrutil.Clone(s.ProdQ) 56 c.VideoQuality = ptrutil.Clone(s.VideoQuality) 57 c.KwArray = sliceutil.Clone(s.KwArray) 58 c.Data = CloneDataSlice(s.Data) 59 c.Network = CloneNetwork(s.Network) 60 c.Channel = CloneChannel(s.Channel) 61 c.Ext = sliceutil.Clone(s.Ext) 62 63 return &c 64 } 65 66 func CloneProducer(s *openrtb2.Producer) *openrtb2.Producer { 67 if s == nil { 68 return nil 69 } 70 71 // Shallow Copy (Value Fields) 72 c := *s 73 74 // Deep Copy (Pointers) 75 c.Cat = sliceutil.Clone(s.Cat) 76 c.Ext = sliceutil.Clone(s.Ext) 77 78 return &c 79 } 80 81 func CloneDataSlice(s []openrtb2.Data) []openrtb2.Data { 82 if s == nil { 83 return nil 84 } 85 86 c := make([]openrtb2.Data, len(s)) 87 for i, d := range s { 88 c[i] = CloneData(d) 89 } 90 91 return c 92 } 93 94 func CloneData(s openrtb2.Data) openrtb2.Data { 95 // Shallow Copy (Value Fields) Occurred By Passing Argument By Value 96 97 // Deep Copy (Pointers) 98 s.Segment = CloneSegmentSlice(s.Segment) 99 s.Ext = sliceutil.Clone(s.Ext) 100 101 return s 102 } 103 104 func CloneSegmentSlice(s []openrtb2.Segment) []openrtb2.Segment { 105 if s == nil { 106 return nil 107 } 108 109 c := make([]openrtb2.Segment, len(s)) 110 for i, d := range s { 111 c[i] = CloneSegment(d) 112 } 113 114 return c 115 } 116 117 func CloneSegment(s openrtb2.Segment) openrtb2.Segment { 118 // Shallow Copy (Value Fields) Occurred By Passing Argument By Value 119 120 // Deep Copy (Pointers) 121 s.Ext = sliceutil.Clone(s.Ext) 122 123 return s 124 } 125 126 func CloneNetwork(s *openrtb2.Network) *openrtb2.Network { 127 if s == nil { 128 return nil 129 } 130 131 // Shallow Copy (Value Fields) 132 c := *s 133 134 // Deep Copy (Pointers) 135 c.Ext = sliceutil.Clone(s.Ext) 136 137 return &c 138 } 139 140 func CloneChannel(s *openrtb2.Channel) *openrtb2.Channel { 141 if s == nil { 142 return nil 143 } 144 145 // Shallow Copy (Value Fields) 146 c := *s 147 148 // Deep Copy (Pointers) 149 c.Ext = sliceutil.Clone(s.Ext) 150 151 return &c 152 } 153 154 func CloneSite(s *openrtb2.Site) *openrtb2.Site { 155 if s == nil { 156 return nil 157 } 158 159 // Shallow Copy (Value Fields) 160 c := *s 161 162 // Deep Copy (Pointers) 163 c.Cat = sliceutil.Clone(s.Cat) 164 c.SectionCat = sliceutil.Clone(s.SectionCat) 165 c.PageCat = sliceutil.Clone(s.PageCat) 166 c.Publisher = ClonePublisher(s.Publisher) 167 c.Content = CloneContent(s.Content) 168 c.KwArray = sliceutil.Clone(s.KwArray) 169 c.Ext = sliceutil.Clone(s.Ext) 170 171 return &c 172 } 173 174 func CloneUser(s *openrtb2.User) *openrtb2.User { 175 if s == nil { 176 return nil 177 } 178 179 // Shallow Copy (Value Fields) 180 c := *s 181 182 // Deep Copy (Pointers) 183 c.KwArray = sliceutil.Clone(s.KwArray) 184 c.Geo = CloneGeo(s.Geo) 185 c.Data = CloneDataSlice(s.Data) 186 c.EIDs = CloneEIDSlice(s.EIDs) 187 c.Ext = sliceutil.Clone(s.Ext) 188 189 return &c 190 } 191 192 func CloneGeo(s *openrtb2.Geo) *openrtb2.Geo { 193 if s == nil { 194 return nil 195 } 196 197 // Shallow Copy (Value Fields) 198 c := *s 199 200 // Deep Copy (Pointers) 201 c.Ext = sliceutil.Clone(s.Ext) 202 203 return &c 204 } 205 206 func CloneEIDSlice(s []openrtb2.EID) []openrtb2.EID { 207 if s == nil { 208 return nil 209 } 210 211 c := make([]openrtb2.EID, len(s)) 212 for i, d := range s { 213 c[i] = CloneEID(d) 214 } 215 216 return c 217 } 218 219 func CloneEID(s openrtb2.EID) openrtb2.EID { 220 // Shallow Copy (Value Fields) Occurred By Passing Argument By Value 221 222 // Deep Copy (Pointers) 223 s.UIDs = CloneUIDSlice(s.UIDs) 224 s.Ext = sliceutil.Clone(s.Ext) 225 226 return s 227 } 228 229 func CloneUIDSlice(s []openrtb2.UID) []openrtb2.UID { 230 if s == nil { 231 return nil 232 } 233 234 c := make([]openrtb2.UID, len(s)) 235 for i, d := range s { 236 c[i] = CloneUID(d) 237 } 238 239 return c 240 } 241 242 func CloneUID(s openrtb2.UID) openrtb2.UID { 243 // Shallow Copy (Value Fields) Occurred By Passing Argument By Value 244 245 // Deep Copy (Pointers) 246 s.Ext = sliceutil.Clone(s.Ext) 247 248 return s 249 } 250 251 func CloneDOOH(s *openrtb2.DOOH) *openrtb2.DOOH { 252 if s == nil { 253 return nil 254 } 255 256 // Shallow Copy (Value Fields) 257 c := *s 258 259 // Deep Copy (Pointers) 260 c.VenueType = sliceutil.Clone(s.VenueType) 261 c.VenueTypeTax = ptrutil.Clone(s.VenueTypeTax) 262 c.Publisher = ClonePublisher(s.Publisher) 263 c.Content = CloneContent(s.Content) 264 c.Ext = sliceutil.Clone(s.Ext) 265 266 return &c 267 }