From aa04350635a97b7b3925be11deacfbc7097ac38b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20K=C3=A4ldstr=C3=B6m?= Date: Fri, 9 Jul 2021 19:45:40 +0300 Subject: [PATCH] Rename content to framing type --- pkg/serializer/frame/content_type.go | 12 ++++++------ pkg/serializer/frame/interfaces.go | 20 ++++++++++---------- pkg/serializer/frame/reader_factory.go | 22 +++++++++++----------- pkg/serializer/frame/reader_streaming.go | 4 ++-- pkg/serializer/frame/reader_test.go | 10 +++++----- pkg/serializer/frame/sanitize.go | 2 +- pkg/serializer/frame/single.go | 8 ++++---- pkg/serializer/frame/writer_delegate.go | 8 ++++---- pkg/serializer/frame/writer_factory.go | 20 ++++++++++---------- 9 files changed, 53 insertions(+), 53 deletions(-) diff --git a/pkg/serializer/frame/content_type.go b/pkg/serializer/frame/content_type.go index c03dd611..b89ddc95 100644 --- a/pkg/serializer/frame/content_type.go +++ b/pkg/serializer/frame/content_type.go @@ -8,8 +8,8 @@ import ( ) var ( - // ErrUnsupportedFramingType is returned if the specified content type isn't supported - ErrUnsupportedFramingType = errors.New("unsupported content type") + // ErrUnsupportedFramingType is returned if the specified framing type isn't supported + ErrUnsupportedFramingType = errors.New("unsupported framing type") ) // MakeUnsupportedFramingTypeError returns a wrapped ErrUnsupportedFramingType along with @@ -32,7 +32,7 @@ const ( ) func (ct FramingType) FramingType() FramingType { return ct } -func (ct FramingType) ToFramingTyped() FramingTyped { return &contentTyped{ct} } +func (ct FramingType) ToFramingTyped() FramingTyped { return &framingTyped{ct} } // FramingTyped is an interface for objects that are specific to a FramingType. type FramingTyped interface { @@ -43,7 +43,7 @@ type FramingTyped interface { // FramingType implements the FramingTyped interface. var _ FramingTyped = FramingType("") -// contentTyped is a helper struct that implements the FramingTyped interface. -type contentTyped struct{ contentType FramingType } +// framingTyped is a helper struct that implements the FramingTyped interface. +type framingTyped struct{ framingType FramingType } -func (ct *contentTyped) FramingType() FramingType { return ct.contentType } +func (ct *framingTyped) FramingType() FramingType { return ct.framingType } diff --git a/pkg/serializer/frame/interfaces.go b/pkg/serializer/frame/interfaces.go index 98753f38..26e9fc3c 100644 --- a/pkg/serializer/frame/interfaces.go +++ b/pkg/serializer/frame/interfaces.go @@ -16,11 +16,11 @@ type Closer interface { Close(ctx context.Context) error } -// Reader is a content-type specific reader of an underlying io.Reader or io.ReadCloser. +// Reader is a framing type specific reader of an underlying io.Reader or io.ReadCloser. // If an io.Reader is used, Close(ctx) is a no-op. If an io.ReadCloser is used, Close(ctx) // will close the underlying io.ReadCloser. // -// The Reader returns frames, as defined by the relevant content type. +// The Reader returns frames, as defined by the relevant framing type. // For example, for YAML a frame represents a YAML document, while JSON is a self-framing // format, i.e. encoded objects can be written to a stream just as // '{ "a": "" ... }{ "b": "" ... }' and separated from there. @@ -49,7 +49,7 @@ type Closer interface { // The Reader MAY respect cancellation signals on the context, depending on ReaderOptions. // The Reader MAY support reporting trace spans for how long certain operations take. type Reader interface { - // The Reader is specific to this content type + // The Reader is specific to this framing type FramingTyped // ReadFrame reads one frame from the underlying io.Read(Clos)er. At maximum, the frame is as // large as ReadWriterOptions.MaxFrameSize. See the documentation on the Reader interface for more @@ -68,16 +68,16 @@ type ReaderFactory interface { // The options are parsed in order, and the latter options override the former. // The given io.Reader can also be a io.ReadCloser, and if so, Reader.Close(ctx) // will close that io.ReadCloser. - // The ReaderFactory might allow any contentType as long as ReaderOptions.MaxFrames + // The ReaderFactory might allow any framingType as long as ReaderOptions.MaxFrames // is 1, because then there might not be a need to perform framing. - NewReader(contentType FramingType, r io.Reader, opts ...ReaderOption) Reader + NewReader(framingType FramingType, r io.Reader, opts ...ReaderOption) Reader } -// Writer is a content-type specific writer to an underlying io.Writer or io.WriteCloser. +// Writer is a framing type specific writer to an underlying io.Writer or io.WriteCloser. // If an io.Writer is used, Close(ctx) is a no-op. If an io.WriteCloser is used, Close(ctx) // will close the underlying io.WriteCloser. // -// The Writer writes frames to the underlying stream, as defined by the content type. +// The Writer writes frames to the underlying stream, as defined by the framing type. // For example, for YAML a frame represents a YAML document, while JSON is a self-framing // format, i.e. encoded objects can be written to a stream just as // '{ "a": "" ... }{ "b": "" ... }'. @@ -102,7 +102,7 @@ type ReaderFactory interface { // The Writer MAY respect cancellation signals on the context, depending on WriterOptions. // The Writer MAY support reporting trace spans for how long certain operations take. type Writer interface { - // The Reader is specific to this content type + // The Reader is specific to this framing type FramingTyped // WriteFrame writes one frame to the underlying io.Write(Close)r. // See the documentation on the Writer interface for more details. @@ -119,9 +119,9 @@ type WriterFactory interface { // The options are parsed in order, and the latter options override the former. // The given io.Writer can also be a io.WriteCloser, and if so, Writer.Close(ctx) // will close that io.WriteCloser. - // The WriterFactory might allow any contentType as long as WriterOptions.MaxFrames + // The WriterFactory might allow any framingType as long as WriterOptions.MaxFrames // is 1, because then there might not be a need to perform framing. - NewWriter(contentType FramingType, w io.Writer, opts ...WriterOption) Writer + NewWriter(framingType FramingType, w io.Writer, opts ...WriterOption) Writer } // Factory combines ReaderFactory and WriterFactory. diff --git a/pkg/serializer/frame/reader_factory.go b/pkg/serializer/frame/reader_factory.go index 910fbe36..4df5aaed 100644 --- a/pkg/serializer/frame/reader_factory.go +++ b/pkg/serializer/frame/reader_factory.go @@ -14,13 +14,13 @@ import ( // will write the given frame to the underlying io.Writer as-is. type DefaultFactory struct{} -func (f DefaultFactory) NewReader(contentType FramingType, r io.Reader, opts ...ReaderOption) Reader { +func (f DefaultFactory) NewReader(framingType FramingType, r io.Reader, opts ...ReaderOption) Reader { // Build the options from the defaults o := defaultReaderOpts().ApplyOptions(opts) // Wrap r in a io.NopCloser if it isn't closable. Mark os.Std{in,out,err} as not closable. rc, hasCloser := toReadCloser(r) // Wrap the low-level Reader from lowlevelFromReadCloser in a composite highlevelReader applying common policy - return newHighlevelReader(f.lowlevelFromReadCloser(contentType, rc, o), hasCloser, o) + return newHighlevelReader(f.lowlevelFromReadCloser(framingType, rc, o), hasCloser, o) } func toReadCloser(r io.Reader) (rc io.ReadCloser, hasCloser bool) { @@ -35,8 +35,8 @@ func toReadCloser(r io.Reader) (rc io.ReadCloser, hasCloser bool) { return rc, hasCloser } -func (DefaultFactory) lowlevelFromReadCloser(contentType FramingType, rc io.ReadCloser, o *ReaderOptions) Reader { - switch contentType { +func (DefaultFactory) lowlevelFromReadCloser(framingType FramingType, rc io.ReadCloser, o *ReaderOptions) Reader { + switch framingType { case FramingTypeYAML: return newYAMLReader(rc, o) case FramingTypeJSON: @@ -44,9 +44,9 @@ func (DefaultFactory) lowlevelFromReadCloser(contentType FramingType, rc io.Read default: // If only one frame is allowed, there is no need to frame. if o.MaxFrames == 1 { - return newSingleReader(contentType, rc, o) + return newSingleReader(framingType, rc, o) } - return newErrReader(contentType, MakeUnsupportedFramingTypeError(contentType)) + return newErrReader(framingType, MakeUnsupportedFramingTypeError(framingType)) } } @@ -55,9 +55,9 @@ var defaultReaderFactory ReaderFactory = DefaultFactory{} // NewReader returns a Reader for the given FramingType and underlying io.Read(Clos)er. // -// This is a shorthand for DefaultFactory{}.NewReader(contentType, r, opts...) -func NewReader(contentType FramingType, r io.Reader, opts ...ReaderOption) Reader { - return defaultReaderFactory.NewReader(contentType, r, opts...) +// This is a shorthand for DefaultFactory{}.NewReader(framingType, r, opts...) +func NewReader(framingType FramingType, r io.Reader, opts ...ReaderOption) Reader { + return defaultReaderFactory.NewReader(framingType, r, opts...) } // NewYAMLReader returns a Reader that supports both YAML and JSON. Frames are separated by "---\n" @@ -75,8 +75,8 @@ func NewJSONReader(r io.Reader, opts ...ReaderOption) Reader { return NewReader(FramingTypeJSON, r, opts...) } -func newErrReader(contentType FramingType, err error) Reader { - return &errReader{contentType.ToFramingTyped(), &nopCloser{}, err} +func newErrReader(framingType FramingType, err error) Reader { + return &errReader{framingType.ToFramingTyped(), &nopCloser{}, err} } // errReader always returns an error diff --git a/pkg/serializer/frame/reader_streaming.go b/pkg/serializer/frame/reader_streaming.go index 54622ef5..cd87ac18 100644 --- a/pkg/serializer/frame/reader_streaming.go +++ b/pkg/serializer/frame/reader_streaming.go @@ -24,11 +24,11 @@ func newJSONReader(rc io.ReadCloser, o *ReaderOptions) Reader { // // Note: This Reader is a so-called "low-level" one. It doesn't do tracing, mutex locking, or // proper closing logic. It must be wrapped by a composite, high-level Reader like highlevelReader. -func newStreamingReader(contentType FramingType, rc io.ReadCloser, o *ReaderOptions) Reader { +func newStreamingReader(framingType FramingType, rc io.ReadCloser, o *ReaderOptions) Reader { // Limit the amount of bytes that can be read in one frame lr := NewIoLimitedReader(rc, o.MaxFrameSize) return &streamingReader{ - FramingTyped: contentType.ToFramingTyped(), + FramingTyped: framingType.ToFramingTyped(), lr: lr, streamReader: newK8sStreamingReader(ioReadCloser{lr, rc}, o.MaxFrameSize), maxFrameSize: o.MaxFrameSize, diff --git a/pkg/serializer/frame/reader_test.go b/pkg/serializer/frame/reader_test.go index a9aff770..6876a08c 100644 --- a/pkg/serializer/frame/reader_test.go +++ b/pkg/serializer/frame/reader_test.go @@ -303,9 +303,9 @@ var defaultTestCases = []testcase{ readResults: []error{nil, nil, io.EOF, io.EOF}, readOpts: []ReaderOption{&ReaderWriterOptions{MaxFrames: 2}}, }, - // Other Content Types + // Other Framing Types { - name: "Roundtrip: Allow reading other content types when MaxFrames == 1, check overflows too", + name: "Roundtrip: Allow reading other framing types when MaxFrames == 1, check overflows too", testdata: []testdata{ {ct: otherCT, rawData: otherFrame, frames: []string{otherFrame}}, }, @@ -315,7 +315,7 @@ var defaultTestCases = []testcase{ readOpts: []ReaderOption{&ReaderWriterOptions{MaxFrames: 1}}, }, { - name: "Read: other content type frame size is exactly within bounds", + name: "Read: other framing type frame size is exactly within bounds", testdata: []testdata{ {ct: otherCT, rawData: otherFrame, frames: []string{otherFrame}}, }, @@ -323,7 +323,7 @@ var defaultTestCases = []testcase{ readResults: []error{nil, io.EOF}, }, { - name: "Read: other content type frame size overflow", + name: "Read: other framing type frame size overflow", testdata: []testdata{ {ct: otherCT, rawData: otherFrame}, }, @@ -331,7 +331,7 @@ var defaultTestCases = []testcase{ readResults: []error{ErrFrameSizeOverflow, io.EOF, io.EOF}, }, { - name: "Write: other content type frame size overflow", + name: "Write: other framing type frame size overflow", testdata: []testdata{ {ct: otherCT, frames: []string{otherFrame, otherFrame}}, }, diff --git a/pkg/serializer/frame/sanitize.go b/pkg/serializer/frame/sanitize.go index 3480a537..1400d807 100644 --- a/pkg/serializer/frame/sanitize.go +++ b/pkg/serializer/frame/sanitize.go @@ -9,7 +9,7 @@ type Sanitizer interface { // FramingType. If the FramingType isn't known, the Sanitizer can choose between // returning an ErrUnsupportedFramingType error or just returning frame, nil unmodified. // If ErrUnsupportedFramingType is returned, the consumer won't probably be able to handle - // other content types than the default ones, which might not be desired. + // other framing types than the default ones, which might not be desired. // // The returned frame should have len == 0 if it's considered empty. Sanitize(ct FramingType, frame []byte) ([]byte, error) diff --git a/pkg/serializer/frame/single.go b/pkg/serializer/frame/single.go index 50a38923..b8012099 100644 --- a/pkg/serializer/frame/single.go +++ b/pkg/serializer/frame/single.go @@ -5,17 +5,17 @@ import ( "io" ) -func newSingleReader(contentType FramingType, rc io.ReadCloser, o *ReaderOptions) Reader { +func newSingleReader(framingType FramingType, rc io.ReadCloser, o *ReaderOptions) Reader { return &singleReader{ - FramingTyped: contentType.ToFramingTyped(), + FramingTyped: framingType.ToFramingTyped(), r: NewIoLimitedReader(rc, o.MaxFrameSize), c: rc, } } -func newSingleWriter(contentType FramingType, wc io.WriteCloser, _ *WriterOptions) Writer { +func newSingleWriter(framingType FramingType, wc io.WriteCloser, _ *WriterOptions) Writer { return &singleWriter{ - FramingTyped: contentType.ToFramingTyped(), + FramingTyped: framingType.ToFramingTyped(), wc: wc, } } diff --git a/pkg/serializer/frame/writer_delegate.go b/pkg/serializer/frame/writer_delegate.go index 8769d00f..65405e2c 100644 --- a/pkg/serializer/frame/writer_delegate.go +++ b/pkg/serializer/frame/writer_delegate.go @@ -5,9 +5,9 @@ import ( "io" ) -func newDelegatingWriter(contentType FramingType, w io.Writer, c io.Closer, opts *WriterOptions) Writer { +func newDelegatingWriter(framingType FramingType, w io.Writer, c io.Closer, opts *WriterOptions) Writer { return &delegatingWriter{ - FramingTyped: contentType.ToFramingTyped(), + FramingTyped: framingType.ToFramingTyped(), w: w, c: c, opts: opts, @@ -31,8 +31,8 @@ func (w *delegatingWriter) WriteFrame(_ context.Context, frame []byte) error { func (w *delegatingWriter) Close(context.Context) error { return w.c.Close() } -func newErrWriter(contentType FramingType, err error) Writer { - return &errWriter{contentType.ToFramingTyped(), &nopCloser{}, err} +func newErrWriter(framingType FramingType, err error) Writer { + return &errWriter{framingType.ToFramingTyped(), &nopCloser{}, err} } type errWriter struct { diff --git a/pkg/serializer/frame/writer_factory.go b/pkg/serializer/frame/writer_factory.go index 8c2e79d4..b0ad6dad 100644 --- a/pkg/serializer/frame/writer_factory.go +++ b/pkg/serializer/frame/writer_factory.go @@ -7,12 +7,12 @@ import ( ) // Documentation below attached to NewWriter. -func (f DefaultFactory) NewWriter(contentType FramingType, w io.Writer, opts ...WriterOption) Writer { +func (f DefaultFactory) NewWriter(framingType FramingType, w io.Writer, opts ...WriterOption) Writer { // Build the concrete options struct from defaults and modifiers o := defaultWriterOpts().ApplyOptions(opts) wc, hasCloser := toWriteCloser(w) // Wrap the writer in a layer that provides tracing and mutex capabilities - return newHighlevelWriter(f.newFromWriteCloser(contentType, wc, o), hasCloser, o) + return newHighlevelWriter(f.newFromWriteCloser(framingType, wc, o), hasCloser, o) } func toWriteCloser(w io.Writer) (wc io.WriteCloser, hasCloser bool) { @@ -26,18 +26,18 @@ func toWriteCloser(w io.Writer) (wc io.WriteCloser, hasCloser bool) { return wc, hasCloser } -func (DefaultFactory) newFromWriteCloser(contentType FramingType, wc io.WriteCloser, o *WriterOptions) Writer { - switch contentType { +func (DefaultFactory) newFromWriteCloser(framingType FramingType, wc io.WriteCloser, o *WriterOptions) Writer { + switch framingType { case FramingTypeYAML: - return newDelegatingWriter(contentType, json.YAMLFramer.NewFrameWriter(wc), wc, o) + return newDelegatingWriter(framingType, json.YAMLFramer.NewFrameWriter(wc), wc, o) case FramingTypeJSON: - return newDelegatingWriter(contentType, json.Framer.NewFrameWriter(wc), wc, o) + return newDelegatingWriter(framingType, json.Framer.NewFrameWriter(wc), wc, o) default: // If only one frame is allowed, there is no need to frame. if o.MaxFrames == 1 { - return newSingleWriter(contentType, wc, o) + return newSingleWriter(framingType, wc, o) } - return newErrWriter(contentType, MakeUnsupportedFramingTypeError(contentType)) + return newErrWriter(framingType, MakeUnsupportedFramingTypeError(framingType)) } } @@ -46,8 +46,8 @@ var defaultWriterFactory WriterFactory = DefaultFactory{} // NewWriter returns a new Writer for the given Writer and FramingType. // The returned Writer is thread-safe. -func NewWriter(contentType FramingType, w io.Writer, opts ...WriterOption) Writer { - return defaultWriterFactory.NewWriter(contentType, w, opts...) +func NewWriter(framingType FramingType, w io.Writer, opts ...WriterOption) Writer { + return defaultWriterFactory.NewWriter(framingType, w, opts...) } // NewYAMLWriter returns a Writer that writes YAML frames separated by "---\n"