Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Rename content to framing type
Browse files Browse the repository at this point in the history
  • Loading branch information
luxas committed Jul 9, 2021
1 parent bf3af4a commit aa04350
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 53 deletions.
12 changes: 6 additions & 6 deletions pkg/serializer/frame/content_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 }
20 changes: 10 additions & 10 deletions pkg/serializer/frame/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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": "" ... }'.
Expand All @@ -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.
Expand All @@ -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.
Expand Down
22 changes: 11 additions & 11 deletions pkg/serializer/frame/reader_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -35,18 +35,18 @@ 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:
return newJSONReader(rc, o)
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))
}
}

Expand All @@ -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"
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/serializer/frame/reader_streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions pkg/serializer/frame/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
},
Expand All @@ -315,23 +315,23 @@ 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}},
},
readOpts: []ReaderOption{&ReaderWriterOptions{MaxFrameSize: otherFrameLen, MaxFrames: 1}},
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},
},
readOpts: []ReaderOption{&ReaderWriterOptions{MaxFrameSize: otherFrameLen - 1, MaxFrames: 1}},
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}},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/serializer/frame/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pkg/serializer/frame/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/serializer/frame/writer_delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions pkg/serializer/frame/writer_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
}
}

Expand All @@ -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"
Expand Down

0 comments on commit aa04350

Please sign in to comment.