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

Refactor DocumentEncode & Document Bytes Conversion #12

Merged
merged 4 commits into from
Dec 13, 2022

Conversation

sarvalabs-manish
Copy link
Member

Document Encoding Refactor

  • Fixes Undecodable Document Wire for Document.Bytes() #11
  • Refactored DocumentEncode() function to return a Document instead of an encoded []byte.
  • Extended the Polorize function to check if the object to be encoded is a Document and if it is, it will cheaply pack it and create a document-encoded wire tagged with WireDoc. Now the correct way to encode a struct into a document-encoded wire is follows:
document, err := DocumentEncode(object)
if err != nil {
    log.Fatal(err)
}

// This is the best way to collapse a Document into its wire form
converted := document.Bytes()

// This is also a valid way, and is the same functionality invoked by the Bytes()
// method but the error check is not necessary as the conversion is safe.
encoded, err := Polorize(document)
if err != nil {
    log.Fatal(err)
}

// Both options generate the exact same wire and take the same 
// amount of time. It is only a matter of syntactic sugar.
fmt.Println(bytes.Equal(converted, encoded))

// Output:
// true
  • Updated Document test cases and examples for new syntax of DocumentEncode
  • Updated BenchmarkDocument for new syntax of DocumentEncode

- Refactored DocumentEncode function to generate and return a Document object instead of the serialized bytes. This Document object can be collapsed with Bytes().
- Refactored the Polorize flow to check if a type is Document when it encounters maps. polorizeMap will now check for this and serialize the Document object into a document-encoded wire efficiently and tag it as WireDoc. This coupled with the above change for DocumentEncode solve #11.
- Update go.mod dependencies
Refactored Document Encoding test cases for the proper flow of document to wire conversion. Also accommodates changes to DocumentEncode function returning Document instead of Bytes #11
Update BenchmarkDocument for the DocumentEncode returns.
Copy link
Member

@sarvalabs-rahul sarvalabs-rahul left a comment

Choose a reason for hiding this comment

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

LGTM 👏

@sarvalabs-rahul sarvalabs-rahul merged commit dc7da60 into main Dec 13, 2022
@sarvalabs-manish sarvalabs-manish deleted the fix/doc-encoding-collapse branch December 13, 2022 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Fix for bug enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Undecodable Document Wire for Document.Bytes()
3 participants