-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
014f774
ci: Add .deepsource.toml
deepsource-io[bot] 69a7f5c
Implements base encoding methods
DuduPan 11983b3
Encoding method for uspct
DuduPan ae35648
Encoding method for uspco and uspva
DuduPan b6aa138
Encoding method for uspca
DuduPan eaa27d9
Encoding method for uspnat
DuduPan 111f86c
Encoding method for usput
DuduPan e5b7e82
Complete the encode method
DuduPan d029401
Add some comments.
DuduPan 4ca9574
Optimize the encoding pipeline.
DuduPan c28c904
Update README.md
DuduPan 102e8ae
Test script for running out all the testing cases and showing the cov…
DuduPan 23b6619
Bug fix and add more test cases
DuduPan e8fbc80
Add test cases for WriteIntRange
DuduPan 467a30a
More testing cases applied.
DuduPan 7c06c2f
Refinement for tests and coding style
DuduPan de00444
make test.sh executable and modify .gitignore
DuduPan c304daf
Add more comments
DuduPan 84fa3f2
Refine the code
DuduPan 83c5eb0
Fix bug
DuduPan 97a2ee2
optimize WriteFibonacciInt
DuduPan 4f52cf7
fix the bug of zero section id panic
DuduPan b1c7c3f
fix picked nits
DuduPan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,8 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
release/ | ||
.idea/ | ||
.vscode/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
# go-gpp | ||
Golang support for the IAB's GPP framework | ||
Golang support for the IAB's GPP framework. | ||
|
||
Provided the basic *Parse* and *Encode* methods for conversions between IAB-supported | ||
sections and GPP strings, people other than CMPs are not encouraged to generate GPP strings | ||
in production according to the IAB documentation. | ||
|
||
For more information, please refer to https://github.com/InteractiveAdvertisingBureau/Global-Privacy-Platform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package gpp | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/prebid/go-gpp/constants" | ||
"github.com/prebid/go-gpp/util" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
const ( | ||
// the first 6 bits of the header must always evaluate to the integer '3'. | ||
gppType byte = 0x3 | ||
gppVersion byte = 0x1 | ||
// the range of SectionID must start with 1 and end with the maximum value represented by uint16. | ||
minSectionId constants.SectionID = 1 | ||
maxSectionId constants.SectionID = 0xffff | ||
) | ||
|
||
var ( | ||
sectionIdOutOfRangeErr = errors.New("section ID out of range") | ||
duplicatedSectionErr = errors.New("duplicated sections") | ||
) | ||
|
||
func Encode(sections []Section) (string, error) { | ||
bs := util.NewBitStreamForWrite() | ||
builder := strings.Builder{} | ||
|
||
bs.WriteByte6(gppType) | ||
bs.WriteByte6(gppVersion) | ||
|
||
sort.Slice(sections, func(i, j int) bool { | ||
return sections[i].GetID() < sections[j].GetID() | ||
}) | ||
|
||
if len(sections) > 0 && (sections[0].GetID() < minSectionId || | ||
sections[len(sections)-1].GetID() > maxSectionId) { | ||
return "", sectionIdOutOfRangeErr | ||
} | ||
// Generate int range object. | ||
intRange := new(util.IntRange) | ||
// Since the minimum sectionID is 1, the previous one should start with -1, which makes it not continuous. | ||
var prevID constants.SectionID = -1 | ||
for _, sec := range sections { | ||
id := sec.GetID() | ||
if id == prevID { | ||
return "", duplicatedSectionErr | ||
} | ||
if prevID+1 == id { | ||
intRange.Range[len(intRange.Range)-1].EndID = uint16(id) | ||
} else { | ||
intRange.Range = append(intRange.Range, util.IRange{StartID: uint16(id), EndID: uint16(id)}) | ||
} | ||
prevID = id | ||
} | ||
intRange.Size = uint16(len(intRange.Range)) | ||
|
||
err := bs.WriteIntRange(intRange) | ||
if err != nil { | ||
return "", fmt.Errorf("write int range error: %v", err) | ||
} | ||
|
||
builder.Write(bs.Base64Encode()) | ||
|
||
for _, sec := range sections { | ||
builder.WriteByte('~') | ||
// By default, GPP is included. | ||
builder.Write(sec.Encode(true)) | ||
} | ||
|
||
return builder.String(), nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 thisfor
statement, we'll be referencingRange
at position-1
. The following corner case indeed makesEncode(sections []Section)
panic:Can we add a check?