Skip to content

Commit 465610b

Browse files
authored
Make rangeConsent and rangeException struct types (#30)
1 parent 44e39b4 commit 465610b

File tree

3 files changed

+45
-72
lines changed

3 files changed

+45
-72
lines changed

vendorconsent/tcf1/rangesection.go

+20-32
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ func parseRangeSection(data consentMetadata) (*rangeSection, error) {
1717
// Parse out the "exceptions" here.
1818
currentOffset := uint(186)
1919
exceptions := make([]rangeException, numEntries)
20-
for i := uint16(0); i < numEntries; i++ {
21-
thisException, bitsConsumed, err := parseException(data, currentOffset)
20+
for i := range exceptions {
21+
bitsConsumed, err := parseException(&exceptions[i], data, currentOffset)
2222
if err != nil {
2323
return nil, err
2424
}
25-
exceptions[i] = thisException
2625
currentOffset = currentOffset + bitsConsumed
2726
}
2827

@@ -45,46 +44,47 @@ func parseNumEntries(data []byte) uint16 {
4544
// RangeSection Exception implemnetations
4645

4746
// parseException parses a RangeSection exception starting from the initial bit.
48-
// It returns the exception, as well as the number of bits consumed by the parsing.
49-
func parseException(data consentMetadata, initialBit uint) (rangeException, uint, error) {
47+
// It returns the number of bits consumed by the parsing.
48+
func parseException(dst *rangeException, data consentMetadata, initialBit uint) (uint, error) {
5049
// Fixes #10
5150
if uint(len(data)) <= initialBit/8 {
52-
return nil, 0, fmt.Errorf("bit %d was supposed to start a new RangeEntry, but the consent string was only %d bytes long", initialBit, len(data))
51+
return 0, fmt.Errorf("bit %d was supposed to start a new RangeEntry, but the consent string was only %d bytes long", initialBit, len(data))
5352
}
5453
// If the first bit is set, it's a Range of IDs
5554
if isSet(data, initialBit) {
5655
start, err := parseUInt16(data, initialBit+1)
5756
if err != nil {
58-
return nil, 0, err
57+
return 0, err
5958
}
6059
end, err := parseUInt16(data, initialBit+17)
6160
if err != nil {
62-
return nil, 0, err
61+
return 0, err
6362
}
6463
if start == 0 {
65-
return nil, 0, fmt.Errorf("bit %d range entry exclusion starts at 0, but the min vendor ID is 1", initialBit)
64+
return 0, fmt.Errorf("bit %d range entry exclusion starts at 0, but the min vendor ID is 1", initialBit)
6665
}
6766
if end > data.MaxVendorID() {
68-
return nil, 0, fmt.Errorf("bit %d range entry exclusion ends at %d, but the max vendor ID is %d", initialBit, end, data.MaxVendorID())
67+
return 0, fmt.Errorf("bit %d range entry exclusion ends at %d, but the max vendor ID is %d", initialBit, end, data.MaxVendorID())
6968
}
7069
if end <= start {
71-
return nil, 0, fmt.Errorf("bit %d range entry excludes vendors [%d, %d]. The start should be less than the end", initialBit, start, end)
70+
return 0, fmt.Errorf("bit %d range entry excludes vendors [%d, %d]. The start should be less than the end", initialBit, start, end)
7271
}
73-
return rangeVendorException{
74-
startID: start,
75-
endID: end,
76-
}, uint(33), nil
72+
dst.startID = start
73+
dst.endID = end
74+
return 33, nil
7775
}
7876

7977
vendorID, err := parseUInt16(data, initialBit+1)
8078
if err != nil {
81-
return nil, 0, err
79+
return 0, err
8280
}
8381
if vendorID == 0 || vendorID > data.MaxVendorID() {
84-
return nil, 0, fmt.Errorf("bit %d range entry excludes vendor %d, but only vendors [1, %d] are valid", initialBit, vendorID, data.MaxVendorID())
82+
return 0, fmt.Errorf("bit %d range entry excludes vendor %d, but only vendors [1, %d] are valid", initialBit, vendorID, data.MaxVendorID())
8583
}
8684

87-
return singleVendorException(vendorID), 17, nil
85+
dst.startID = vendorID
86+
dst.endID = vendorID
87+
return 17, nil
8888
}
8989

9090
// parseUInt16 parses a 16-bit integer from the data array, starting at the given index
@@ -135,25 +135,13 @@ func (p rangeSection) VendorConsent(id uint16) bool { // TODO check if possible
135135
return p.defaultValue
136136
}
137137

138-
// A RangeSection has a default consent value and a list of "exceptions". This represents an "exception" blob
139-
type rangeException interface {
140-
Contains(id uint16) bool
141-
}
142-
143-
// This is a RangeSection exception for a single vendor.
144-
type singleVendorException uint16
145-
146-
func (e singleVendorException) Contains(id uint16) bool {
147-
return uint16(e) == id
148-
}
149-
150138
// This is a RangeSection exception for a range of IDs.
151139
// The start and end bounds here are inclusive.
152-
type rangeVendorException struct {
140+
type rangeException struct {
153141
startID uint16
154142
endID uint16
155143
}
156144

157-
func (e rangeVendorException) Contains(id uint16) bool { // TODO check if possible convert to pointer receiver
145+
func (e rangeException) Contains(id uint16) bool {
158146
return e.startID <= id && e.endID >= id
159147
}

vendorconsent/tcf2/pubrestrict.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ func parsePubRestriction(metadata ConsentMetadata, startbit uint) (*pubRestricti
3131
}
3232
currentOffset = currentOffset + 12
3333
vendors := make([]rangeConsent, numEntries)
34-
for i := uint16(0); i < numEntries; i++ {
35-
thisConsent, bitsConsumed, err := parseRangeConsent(data, currentOffset, assumedMaxVendorID)
34+
for i := range vendors {
35+
bitsConsumed, err := parseRangeConsent(&vendors[i], data, currentOffset, assumedMaxVendorID)
3636
if err != nil {
3737
return nil, 0, err
3838
}
39-
vendors[i] = thisConsent
4039
currentOffset = currentOffset + bitsConsumed
4140
}
4241
restrictions[restrictData] = pubRestriction{

vendorconsent/tcf2/rangesection.go

+23-37
Original file line numberDiff line numberDiff line change
@@ -22,70 +22,68 @@ func parseRangeSection(metadata ConsentMetadata, maxVendorID uint16, startbit ui
2222
// Parse out the "exceptions" here.
2323
currentOffset := startbit + 12
2424
consents := make([]rangeConsent, numEntries)
25-
for i := uint16(0); i < numEntries; i++ {
26-
thisConsent, bitsConsumed, err := parseRangeConsent(data, currentOffset, maxVendorID)
25+
for i := range consents {
26+
bitsConsumed, err := parseRangeConsent(&consents[i], data, currentOffset, maxVendorID)
2727
if err != nil {
2828
return nil, 0, err
2929
}
30-
consents[i] = thisConsent
3130
currentOffset = currentOffset + bitsConsumed
3231
}
3332

3433
return &rangeSection{
35-
consentData: data,
3634
consents: consents,
3735
maxVendorID: maxVendorID,
3836
}, currentOffset, nil
3937
}
4038

41-
// RangeSection Exception implemnetations
39+
// RangeSection Exception implementations
4240

4341
// parseRangeConsents parses a RangeSection starting from the initial bit.
44-
// It returns the exception, as well as the number of bits consumed by the parsing.
45-
func parseRangeConsent(data []byte, initialBit uint, maxVendorID uint16) (rangeConsent, uint, error) {
42+
// It returns the number of bits consumed by the parsing.
43+
func parseRangeConsent(dst *rangeConsent, data []byte, initialBit uint, maxVendorID uint16) (uint, error) {
4644
// Fixes #10
4745
if uint(len(data)) <= initialBit/8 {
48-
return nil, 0, fmt.Errorf("bit %d was supposed to start a new RangeEntry, but the consent string was only %d bytes long", initialBit, len(data))
46+
return 0, fmt.Errorf("bit %d was supposed to start a new RangeEntry, but the consent string was only %d bytes long", initialBit, len(data))
4947
}
5048
// If the first bit is set, it's a Range of IDs
5149
if isSet(data, initialBit) {
5250
start, err := bitutils.ParseUInt16(data, initialBit+1)
5351
if err != nil {
54-
return nil, 0, err
52+
return 0, err
5553
}
5654
end, err := bitutils.ParseUInt16(data, initialBit+17)
5755
if err != nil {
58-
return nil, 0, err
56+
return 0, err
5957
}
6058
if start == 0 {
61-
return nil, 0, fmt.Errorf("bit %d range entry exclusion starts at 0, but the min vendor ID is 1", initialBit)
59+
return 0, fmt.Errorf("bit %d range entry exclusion starts at 0, but the min vendor ID is 1", initialBit)
6260
}
6361
if end > maxVendorID {
64-
return nil, 0, fmt.Errorf("bit %d range entry exclusion ends at %d, but the max vendor ID is %d", initialBit, end, maxVendorID)
62+
return 0, fmt.Errorf("bit %d range entry exclusion ends at %d, but the max vendor ID is %d", initialBit, end, maxVendorID)
6563
}
6664
if end <= start {
67-
return nil, 0, fmt.Errorf("bit %d range entry excludes vendors [%d, %d]. The start should be less than the end", initialBit, start, end)
65+
return 0, fmt.Errorf("bit %d range entry excludes vendors [%d, %d]. The start should be less than the end", initialBit, start, end)
6866
}
69-
return rangeVendorConsent{
70-
startID: start,
71-
endID: end,
72-
}, uint(33), nil
67+
dst.startID = start
68+
dst.endID = end
69+
return 33, nil
7370
}
7471

7572
vendorID, err := bitutils.ParseUInt16(data, initialBit+1)
7673
if err != nil {
77-
return nil, 0, err
74+
return 0, err
7875
}
7976
if vendorID == 0 || vendorID > maxVendorID {
80-
return nil, 0, fmt.Errorf("bit %d range entry excludes vendor %d, but only vendors [1, %d] are valid", initialBit, vendorID, maxVendorID)
77+
return 0, fmt.Errorf("bit %d range entry excludes vendor %d, but only vendors [1, %d] are valid", initialBit, vendorID, maxVendorID)
8178
}
8279

83-
return singleVendorConsent(vendorID), 17, nil
80+
dst.startID = vendorID
81+
dst.endID = vendorID
82+
return 17, nil
8483
}
8584

8685
// A RangeConsents encodes consents that have been registered.
8786
type rangeSection struct {
88-
consentData []byte
8987
consents []rangeConsent
9088
maxVendorID uint16
9189
}
@@ -99,38 +97,26 @@ func (p *rangeSection) MaxVendorID() uint16 {
9997
}
10098

10199
// VendorConsents implementation
102-
func (p rangeSection) VendorConsent(id uint16) bool { // TODO consider convert to pointer receiver
100+
func (p *rangeSection) VendorConsent(id uint16) bool {
103101
if id < 1 || id > p.maxVendorID {
104102
return false
105103
}
106104

107-
for i := 0; i < len(p.consents); i++ {
105+
for i := range p.consents {
108106
if p.consents[i].Contains(id) {
109107
return true
110108
}
111109
}
112110
return false
113111
}
114112

115-
// A RangeSection has a default consent value and a list of "exceptions". This represents an "exception" blob
116-
type rangeConsent interface {
117-
Contains(id uint16) bool
118-
}
119-
120-
// This is a RangeSection exception for a single vendor.
121-
type singleVendorConsent uint16
122-
123-
func (e singleVendorConsent) Contains(id uint16) bool {
124-
return uint16(e) == id
125-
}
126-
127113
// This is a RangeSection exception for a range of IDs.
128114
// The start and end bounds here are inclusive.
129-
type rangeVendorConsent struct {
115+
type rangeConsent struct {
130116
startID uint16
131117
endID uint16
132118
}
133119

134-
func (e rangeVendorConsent) Contains(id uint16) bool { // TODO consider convert to pointer receiver
120+
func (e rangeConsent) Contains(id uint16) bool {
135121
return e.startID <= id && e.endID >= id
136122
}

0 commit comments

Comments
 (0)