Skip to content

Commit

Permalink
ORTB 2.6: Adds a Regs clone (#4012)
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhjort authored Oct 25, 2024
1 parent 470cc49 commit c482a75
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions exchange/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func (rs *requestSplitter) cleanOpenRTBRequests(ctx context.Context,
// down convert
info, ok := rs.bidderInfo[bidder]
if !ok || info.OpenRTB == nil || info.OpenRTB.Version != "2.6" {
reqWrapperCopy.Regs = ortb.CloneRegs(reqWrapperCopy.Regs)
if err := openrtb_ext.ConvertDownTo25(reqWrapperCopy); err != nil {
errs = append(errs, err)
continue
Expand Down
16 changes: 16 additions & 0 deletions ortb/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,19 @@ func CloneBidRequestPartial(s *openrtb2.BidRequest) *openrtb2.BidRequest {

return &c
}

func CloneRegs(s *openrtb2.Regs) *openrtb2.Regs {
if s == nil {
return nil
}

// Shallow Copy (Value Fields)
c := *s

// Deep Copy (Pointers)
c.GDPR = ptrutil.Clone(s.GDPR)
c.GPPSID = slices.Clone(s.GPPSID)
c.Ext = slices.Clone(s.Ext)

return &c
}
41 changes: 41 additions & 0 deletions ortb/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,3 +759,44 @@ func discoverPointerFields(t reflect.Type) []string {
}
return fields
}

func TestCloneRegs(t *testing.T) {
t.Run("nil", func(t *testing.T) {
result := CloneRegs(nil)
assert.Nil(t, result)
})

t.Run("empty", func(t *testing.T) {
given := &openrtb2.Regs{}
result := CloneRegs(given)
assert.Empty(t, result)
assert.NotSame(t, given, result)
})

t.Run("populated", func(t *testing.T) {
given := &openrtb2.Regs{
COPPA: 1,
GDPR: ptrutil.ToPtr(int8(0)),
USPrivacy: "1YNN",
GPP: "SomeGPPStrig",
GPPSID: []int8{1, 2, 3},
Ext: json.RawMessage(`{"anyField":1}`),
}
result := CloneRegs(given)
assert.Equal(t, given, result, "equality")
assert.NotSame(t, given, result, "pointer")
assert.NotSame(t, given.GDPR, result.GDPR, "gdpr")
assert.NotSame(t, given.GPPSID, result.GPPSID, "gppsid[]")
assert.NotSame(t, given.GPPSID[0], result.GPPSID[0], "gppsid[0]")
assert.NotSame(t, given.Ext, result.Ext, "ext")
})

t.Run("assumptions", func(t *testing.T) {
assert.ElementsMatch(t, discoverPointerFields(reflect.TypeOf(openrtb2.Regs{})),
[]string{
"GDPR",
"GPPSID",
"Ext",
})
})
}

0 comments on commit c482a75

Please sign in to comment.