Skip to content
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

Merged
merged 23 commits into from
Nov 14, 2023

Conversation

duduchristian
Copy link

No description provided.

util/encoding.go Outdated Show resolved Hide resolved
encode.go Outdated Show resolved Hide resolved
@hhhjort
Copy link
Collaborator

hhhjort commented Feb 27, 2023

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.

@bsardo bsardo self-requested a review March 13, 2023 17:19
@bsardo bsardo self-assigned this Mar 13, 2023
@hhhjort
Copy link
Collaborator

hhhjort commented Mar 15, 2023

Just a friendly ping to see if you have read the comments, and intend to keep working on this PR.

Thanks

@duduchristian
Copy link
Author

Coming back, still working on this.

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~

@duduchristian duduchristian force-pushed the feature/encode branch 2 times, most recently from f8059d7 to 08c9814 Compare March 27, 2023 08:15
encode.go Outdated Show resolved Hide resolved
@hhhjort
Copy link
Collaborator

hhhjort commented Mar 29, 2023

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.

@hhhjort
Copy link
Collaborator

hhhjort commented Apr 10, 2023

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.

encode.go Outdated Show resolved Hide resolved
util/encoding.go Outdated Show resolved Hide resolved
encode.go Outdated Show resolved Hide resolved
encode_test.go Outdated Show resolved Hide resolved
util/encoding_test.go Outdated Show resolved Hide resolved
util/encoding_test.go Show resolved Hide resolved
util/encoding_test.go Outdated Show resolved Hide resolved
util/encoding_test.go Outdated Show resolved Hide resolved
util/encoding.go Outdated Show resolved Hide resolved
util/encoding_test.go Show resolved Hide resolved
@bsardo
Copy link
Contributor

bsardo commented Apr 27, 2023

Hi @duduchristian, just checking in to see if you have any time to address my comments. Overall, it looks good. No rush and thanks!

@duduchristian
Copy link
Author

yep, I get some time next week. Just coming back from my holiday hahah.

@bsardo
Copy link
Contributor

bsardo commented May 15, 2023

Hi @duduchristian, just checking in to see if you have some time to address the outstanding comments. No rush 🙂

@bsardo
Copy link
Contributor

bsardo commented Jun 5, 2023

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!

@duduchristian
Copy link
Author

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!

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.

.deepsource.toml Outdated Show resolved Hide resolved
util/encoding.go Outdated Show resolved Hide resolved
util/encoding.go Outdated
}
temp = make([]byte, final, size)
} else {
size := (final-expThreshold+step-1)/step*step + expThreshold
Copy link
Collaborator

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) {
Copy link
Collaborator

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 {
Copy link
Collaborator

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.

Copy link
Collaborator

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.

Copy link
Collaborator

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().

@bsardo
Copy link
Contributor

bsardo commented Jul 5, 2023

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!

util/encoding.go Outdated Show resolved Hide resolved
hhhjort
hhhjort previously approved these changes Jul 31, 2023
Copy link
Collaborator

@hhhjort hhhjort left a 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.

util/encoding.go Outdated Show resolved Hide resolved
@hhhjort hhhjort dismissed their stale review August 2, 2023 15:35

Focusing on the big issue, I forgot there were smaller outstanding comments on Fibonacci.

@hhhjort hhhjort assigned guscarreon and hhhjort and unassigned bsardo Aug 3, 2023
return "", duplicatedSectionErr
}
if prevID+1 == id {
intRange.Range[len(intRange.Range)-1].EndID = uint16(id)

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?

@bsardo
Copy link
Contributor

bsardo commented Oct 2, 2023

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!

hhhjort
hhhjort previously approved these changes Oct 30, 2023
Copy link
Collaborator

@hhhjort hhhjort left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link

@guscarreon guscarreon left a 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.

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,

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?

Copy link

@guscarreon guscarreon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bsardo bsardo merged commit 8cca024 into prebid:main Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants