-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add encode method to convert Sections into GPP String #9
Conversation
Can you put a note in the README that, in practice, only CMPs should be writing GPP strings as per the IAB documentation? We don't want to encourage people to be generating GPP strings in production when they really should not be. |
Just a friendly ping to see if you have read the comments, and intend to keep working on this PR. Thanks |
Coming back, still working on this.
Coming back, still working~ |
f8059d7
to
08c9814
Compare
It may be interesting to write a test where we decode some GPP strings, then re-encode them and compare to see if we got the original string back. It could uncover a subtle error, or potentially that the spec is under specified such that there are multiple ways to encode a set of data into a string. |
Sorry we have been a bit busy and haven't responded in a bit. This PR seems to be in a good state, and we should get the final review in soon. |
Hi @duduchristian, just checking in to see if you have any time to address my comments. Overall, it looks good. No rush and thanks! |
yep, I get some time next week. Just coming back from my holiday hahah. |
da27435
to
aef173f
Compare
Hi @duduchristian, just checking in to see if you have some time to address the outstanding comments. No rush 🙂 |
Hi @duduchristian. It looks like you made some of the updates but there are a few more comments outstanding. Are you planning to address those as well? Thanks! |
a392825
to
c304daf
Compare
Sorry I was so busy in the previous weeks. It seems I've addressed all the problems mentioned in comments, can you check the code and if there are any problems, make comments just as what you did before. Thank you. |
util/encoding.go
Outdated
} | ||
temp = make([]byte, final, size) | ||
} else { | ||
size := (final-expThreshold+step-1)/step*step + expThreshold |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to add a parenthesis to make the order of operations clearer for humans.
((final-expThreshold+step-1)/step)*step
It took me a bit to parse this correctly in my head.
util/encoding.go
Outdated
func (bs *BitStream) WriteFibonacciInt(num uint16) error { | ||
// The num should be [1,6765). Actually once the num is larger than or equal to 987, | ||
// the efficiency of Fibonacci Encoding would be no better than 'WriteUint16'. | ||
if num <= 0 || num >= fibonacci(fibLen) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are limiting to fibLen
, then you can just lookup in the fibLookup[i]
array rather than calling the fibonacci()
function. The function just adds code to handle the case where the number if greater than fibLen
.
so the indicator is 0, followed by Fibonacci encoded 1, while the next range [3, 4] refers to a range containing | ||
not only one integer, so the indicator is 1, followed by Fibonacci encoded 3 and 1 (4-3). | ||
*/ | ||
func (bs *BitStream) WriteIntRange(intRange *IntRange) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't Fibonacci enter the name someplace as the GPP spec also defines a non-Fibonacci int range. The non-Fibonacci range will be needed once TCFv2 support is added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you like to comment on the difference between the Fibonacci Int range and the legacy Int Range that is used in TCF. We do plan to add TCFv2 code to this library once we get some time to do it. It hasn't been too urgent as an independent TCFv2 library exists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current name will be an issue when TCFv2 is brought into the project and the original Int range is implemented. The documentation refers to them as Range (Fibonacci)
and Range (Int)
, so the current name could confuse people looking for the Range (Fibonacci)
implementation. This is why the corresponding read function is called ReadFibonacciRange()
.
Hi @duduchristian, thanks for responding to our comments. We recently left a few more for you to address when you get a chance. If you don't mind, could you please merge with master instead of force pushing? It will save us a lot of time reviewing. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me now. Will get a second pair of eyes to review before we merge.
Focusing on the big issue, I forgot there were smaller outstanding comments on Fibonacci.
return "", duplicatedSectionErr | ||
} | ||
if prevID+1 == id { | ||
intRange.Range[len(intRange.Range)-1].EndID = uint16(id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If id
equals zero in the first loop of this for
statement, we'll be referencing Range
at position -1
. The following corner case indeed makes Encode(sections []Section)
panic:
16 type gppEncodeTestData struct {
17 description string
18 sections []Section
19 expected string
20 }
21
+ type zeroSection struct{}
+
+ func (s zeroSection) Encode(b bool) []byte {
+ return nil
+ }
+
+ func (s zeroSection) GetID() constants.SectionID {
+ return constants.SectionID(0)
+ }
+
+ func (s zeroSection) GetValue() string {
+ return "string"
+ }
+
22 var testData = []gppEncodeTestData{
+ {
+ description: "Corner case of a section whose GetID returns 0",
+ sections: []Section{zeroSection{}},
+ },
23 {
24 description: "USPCA GPP string encoding",
25 expected: "DBABh4A~BlgWEYCY.QA~BSFgmiU",
26 sections: []Section{
27 uspca.USPCA{
28 CoreSegment: uspca.USPCACoreSegment{
29 Version: 1,
30 SaleOptOutNotice: 2,
31 SharingOptOutNotice: 1,
32 SensitiveDataLimitUseNotice: 1,
33 *--251 lines: SaleOptOut: 2,-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
284 },
285 }
286
287 func TestEncode(t *testing.T) {
288 for _, test := range testData {
289 result, err := Encode(test.sections)
encode_test.go
Can we add a check?
Hi @duduchristian. It looks like we're close here. If you could address the remaining comment when you get a chance, that would be great. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks pretty good to me. I just left two very minor comments
util/encoding.go
Outdated
) | ||
|
||
func getByteSlice() []byte { | ||
// Most string to be encoded are less than 8 bytes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: "Most strings"
encode_test.go
Outdated
MspaOptOutOptionMode: 1, | ||
MspaServiceProviderMode: 1, | ||
}, | ||
SectionID: 9, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: can we set to it's constants.SectionUSPVA
value instead?
88ec25c
to
b1c7c3f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
No description provided.