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 streaming Writer implementation #490

Merged
merged 1 commit into from
Jun 22, 2021

Conversation

dianale31
Copy link
Contributor

This implementation extends the existing Binary writer struct to include the new streaming methods. Some refactoring is done in the main writer.go file to reduce common code.

Tests have also been added to test for correctness of the new streaming methods, but we'll need to write extensive tests for code generation, where we will actually assert that container types have the proper elements.

@codecov
Copy link

codecov bot commented Jun 8, 2021

Codecov Report

Merging #490 (37ad6d1) into streamdev (fe65efe) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 37ad6d1 differs from pull request most recent head 228c133. Consider uploading reports for the commit 228c133 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           streamdev     #490   +/-   ##
==========================================
  Coverage      78.99%   78.99%           
==========================================
  Files            126      126           
  Lines          16067    16071    +4     
==========================================
+ Hits           12692    12696    +4     
  Misses          2065     2065           
  Partials        1310     1310           
Impacted Files Coverage Δ
idl/error.go 100.00% <100.00%> (ø)
idl/internal/parser.go 100.00% <100.00%> (ø)
idl/parser.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fe65efe...228c133. Read the comment docs.

Copy link
Contributor

@witriew witriew left a comment

Choose a reason for hiding this comment

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

We'll probably be able to improve upon test coverage once both the stream.Writer and the stream.Reader land, then we can repeat tests similar to those done with "encode <-> decode".

protocol/binary/stream_writer.go Outdated Show resolved Hide resolved
protocol/binary/stream_writer.go Outdated Show resolved Hide resolved
protocol/binary/writer.go Outdated Show resolved Hide resolved
protocol/binary/stream_writer.go Outdated Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved
@dianale31 dianale31 marked this pull request as draft June 9, 2021 16:24
@dianale31 dianale31 force-pushed the dianale/writer branch 2 times, most recently from f7b8357 to aa086f6 Compare June 9, 2021 19:10
return testValue{val: val}
}

func (v testValue) streamEncodeStruct(sw *binary.StreamWriter) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I couldn't add these stream methods directly onto the wire.Value object because it would have caused an import cycle. Wrapped it here and made it explicit that this is only for testing since objects using the wire.Value intermediate representation should continue to use the binary.Encode and binary.Decode methods.

@dianale31 dianale31 requested a review from witriew June 9, 2021 19:19
@dianale31 dianale31 marked this pull request as ready for review June 9, 2021 19:20
@dianale31 dianale31 requested a review from abhinav June 9, 2021 19:20
protocol/binary/stream_writer.go Outdated Show resolved Hide resolved
protocol/binary/stream_writer.go Outdated Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved
protocol/value_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@abhinav abhinav left a comment

Choose a reason for hiding this comment

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

This is a good first stab. I'd like to reduce the duplication in the implementation and the tests.
We should be able to reuse most of the existing test cases to exercise both code paths, especially if binary.Writer is implemented in terms of binary.StreamWriter.

protocol/binary/stream_writer.go Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved
protocol/value_test.go Outdated Show resolved Hide resolved
@dianale31 dianale31 force-pushed the dianale/writer branch 3 times, most recently from 77caf89 to 2319785 Compare June 15, 2021 16:51
@dianale31 dianale31 requested review from abhinav and witriew June 15, 2021 16:54
protocol/binary/stream_writer.go Outdated Show resolved Hide resolved
protocol/binary/writer.go Outdated Show resolved Hide resolved
protocol/binary/stream_writer.go Outdated Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@abhinav abhinav left a comment

Choose a reason for hiding this comment

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

Thanks for updating! This is generally looking great, except:

  • Looks like a couple commits from dev unintentionally ended up on this PR
  • There are a couple other spots in binary.Writer that it should use the exported StreamWriter methods rather than unexported
  • I left some optional minor stylistic comments for the tests

CHANGELOG.md Outdated Show resolved Hide resolved
protocol/binary/writer.go Outdated Show resolved Hide resolved
protocol/binary/writer.go Outdated Show resolved Hide resolved
protocol/binary/writer.go Outdated Show resolved Hide resolved
protocol/binary/writer.go Outdated Show resolved Hide resolved
protocol/binary/writer.go Outdated Show resolved Hide resolved
protocol/binary/writer.go Outdated Show resolved Hide resolved
protocol/binary/writer.go Outdated Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved
@dianale31 dianale31 force-pushed the dianale/writer branch 2 times, most recently from 81de41f to 4a4fd56 Compare June 21, 2021 16:00
@dianale31 dianale31 requested review from witriew and abhinav June 21, 2021 16:02
Copy link
Contributor

@abhinav abhinav left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for making the changes.

protocol/binary_test.go Outdated Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved

var streamWriterPool = sync.Pool{
New: func() interface{} {
writer := &StreamWriter{}
Copy link
Contributor

Choose a reason for hiding this comment

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

&StreamWriter{} can be returned.

protocol/binary/stream_writer.go Outdated Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved
protocol/binary_test.go Outdated Show resolved Hide resolved
func TestStructBeginAndEndEncode(t *testing.T) {
var (
streamBuff bytes.Buffer
err error
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a need for the the err variable? I believe we can just require.NoError(t, err)? This comment applies to similar pattern throughout.

@dianale31 dianale31 merged commit 5075744 into thriftrw:streamdev Jun 22, 2021
dianale31 added a commit that referenced this pull request Jun 22, 2021
@abhinav abhinav mentioned this pull request Aug 30, 2021
abhinav added a commit that referenced this pull request Aug 30, 2021
# Commits

The following comments are included in this release. Some of these
cherry-picked and released in v1.28.0, but they appear again in the
list above.

- protocol: Add streaming interfaces (#485)
- Move Stream-based interfaces into their own package
- Make Streaming interfaces private to allow for safe experimentation (#488)
- idl: Return structured ParseError from idl.Parse() (#492)
- Add CHANGELOG entry for #492 (#494)
- Support "<" in the templating language (#499)
- idl: add a Position struct to wrap reported lines (#497)
- Add streamwriter implementation (#490)
- Add a "StreamReader" which implements "stream.Reader"
- Use the "stream.Reader" in the "binary.Reader"
- Add code generation for all wire types for stream encoding (#500)
- Generate "Decode" for "enums" that will directly decode (#495)
- Provide "decode" code generation for the streaming variants for all other types (#496)
- idl: record document positions on constant nodes (#503)
- ast: move idl.Position to the ast package (#504)
- idl: replace internal.Position with ast.Position (#505)
- Expose stream protocol method to close Writer (#506)
- idl: add column numbers to parse error positions (#507)
- idl: record full positions for constants (#508)
- Mark assertParseCases() as a test helper (#509)
- protocol/stream: Define enveloping interfaces (#511)
- protocol/stream: Declare interface for encoding envelopes (#513)
- binary/StreamWriter: Borrow => New; unexport Return (#515)
- stream: add Close method, pool binary reader (#514)
- binary/reader: Return to pool after ReadValue (#517)
- binary/reader: Skip fixed width collections faster (#518)
- binary/stream/reader: Fast-path offsetReader skips (#519)
- binary: Move Responders and Protocol into package (#516)
- benchmark: Refactor into a suite (#520)
- Upgrade to Ragel version 6.10 (from 6.9) (#523)
- Responder: Deduplicate interface (#524)
- gen/quick_test: Add missing types (#525)
- enum/json: Support rejecting unknown values (#502)
- Back to development
- Upgrade to golang.org/x/tools version 0.1.5 (#529)
- ast: add column values to the AST nodes (#522)
- stream: Implement Request and Response handling with Enveloping (#526)
- offsetReader: Implement io.Seeker
- binary/ReadRequest: Use io.Seeker if available
- StreamReader: Use Seeker instead of offsetReader
- protocol/stream: Unembed stream.Protocol from stream.RequestReader (#532)
- thrifttest: Add mocks for streaming interfaces (#527)
- streaming: Unembed iface.Private in streaming-based interfaces (#533)
- Regenerate files for tests after merging `streamdev`
- ast: formally declare CppInclude as a Node (#536)
- ast: add Annotations(Node) []*Annotations (#537)
- Preparing release v1.29.0

# API changes

I ran apidiff on all packages in v1.28.0 and compared it with this
release. Removing changes to gen/internal/tests, the result is:

```
--- go.uber.org/thriftrw/ast ---
Compatible changes:
- Annotation.Column: added
- Annotations: added
- BaseType.Column: added
- Constant.Column: added
- ConstantList.Column: added
- ConstantMap.Column: added
- ConstantMapItem.Column: added
- ConstantReference.Column: added
- CppInclude.Column: added
- DefinitionInfo.Column: added
- Enum.Column: added
- EnumItem.Column: added
- Field.Column: added
- Function.Column: added
- Include.Column: added
- ListType.Column: added
- MapType.Column: added
- Namespace.Column: added
- Position.Column: added
- Position.String: added
- Service.Column: added
- ServiceReference.Column: added
- SetType.Column: added
- Struct.Column: added
- TypeReference.Column: added
- Typedef.Column: added

--- go.uber.org/thriftrw/envelope/stream ---
NEW PACKAGE

--- go.uber.org/thriftrw/gen ---
Compatible changes:
- StreamGenerator: added

--- go.uber.org/thriftrw/internal/envelope/exception ---
Compatible changes:
- (*ExceptionType).Decode: added
- (*TApplicationException).Decode: added
- (*TApplicationException).Encode: added
- ExceptionType.Encode: added

--- go.uber.org/thriftrw/plugin/api ---
Compatible changes:
- (*Argument).Decode: added
- (*Argument).Encode: added
- (*Feature).Decode: added
- (*Function).Decode: added
- (*Function).Encode: added
- (*GenerateServiceRequest).Decode: added
- (*GenerateServiceRequest).Encode: added
- (*GenerateServiceResponse).Decode: added
- (*GenerateServiceResponse).Encode: added
- (*HandshakeRequest).Decode: added
- (*HandshakeRequest).Encode: added
- (*HandshakeResponse).Decode: added
- (*HandshakeResponse).Encode: added
- (*Module).Decode: added
- (*Module).Encode: added
- (*ModuleID).Decode: added
- (*Plugin_Goodbye_Args).Decode: added
- (*Plugin_Goodbye_Args).Encode: added
- (*Plugin_Goodbye_Result).Decode: added
- (*Plugin_Goodbye_Result).Encode: added
- (*Plugin_Handshake_Args).Decode: added
- (*Plugin_Handshake_Args).Encode: added
- (*Plugin_Handshake_Result).Decode: added
- (*Plugin_Handshake_Result).Encode: added
- (*Service).Decode: added
- (*Service).Encode: added
- (*ServiceGenerator_Generate_Args).Decode: added
- (*ServiceGenerator_Generate_Args).Encode: added
- (*ServiceGenerator_Generate_Result).Decode: added
- (*ServiceGenerator_Generate_Result).Encode: added
- (*ServiceID).Decode: added
- (*SimpleType).Decode: added
- (*Type).Decode: added
- (*Type).Encode: added
- (*TypePair).Decode: added
- (*TypePair).Encode: added
- (*TypeReference).Decode: added
- (*TypeReference).Encode: added
- Feature.Encode: added
- ModuleID.Encode: added
- ServiceID.Encode: added
- SimpleType.Encode: added

--- go.uber.org/thriftrw/protocol ---
Compatible changes:
- BinaryStreamer: added

--- go.uber.org/thriftrw/protocol/binary ---
Compatible changes:
- Default: added
- EnvelopeV0Responder: added
- EnvelopeV1Responder: added
- NewStreamReader: added
- NewStreamWriter: added
- NoEnvelopeResponder: added
- Protocol: added
- Responder: added
- StreamReader: added
- StreamWriter: added

--- go.uber.org/thriftrw/protocol/envelope ---
NEW PACKAGE

--- go.uber.org/thriftrw/protocol/stream ---
NEW PACKAGE

--- go.uber.org/thriftrw/thrifttest/streamtest ---
NEW PACKAGE

--- go.uber.org/thriftrw/version ---
Incompatible changes:
- Version: value changed from "1.28.0" to "1.29.0"
```
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.

4 participants