|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// Bytes is a pipeline wrapper around Option[[]byte]. |
| 11 | +type Bytes struct{ Option[[]byte] } |
| 12 | + |
| 13 | +// Str is a pipeline wrapper around Option[string]. |
| 14 | +type Str struct{ Option[string] } |
| 15 | + |
| 16 | +// Lines is a pipeline wrapper around Option[[]string]. |
| 17 | +type Lines struct{ Option[[]string] } |
| 18 | + |
| 19 | +// Reader is a pipeline wrapper around Option[io.Reader]. |
| 20 | +type Reader struct{ Option[io.Reader] } |
| 21 | + |
| 22 | +// ReadCloser is a pipeline wrapper around Option[io.ReadCloser]. |
| 23 | +type ReadCloser struct{ Option[io.ReadCloser] } |
| 24 | + |
| 25 | +// WriteCloser is a pipeline wrapper around Option[io.WriteCloser]. |
| 26 | +type WriteCloser struct{ Option[io.WriteCloser] } |
| 27 | + |
| 28 | +// BytesOf wraps a raw []byte into a Bytes pipeline. |
| 29 | +func BytesOf(v []byte) Bytes { return Bytes{Ok(v)} } |
| 30 | + |
| 31 | +// StrOf wraps a raw string into a Str pipeline. |
| 32 | +func StrOf(v string) Str { return Str{Ok(v)} } |
| 33 | + |
| 34 | +// LinesOf wraps a raw []string into a Lines pipeline. |
| 35 | +func LinesOf(v []string) Lines { return Lines{Ok(v)} } |
| 36 | + |
| 37 | +// String converts bytes to string. |
| 38 | +func (b Bytes) String() Str { |
| 39 | + if b.err != nil { |
| 40 | + return Str{Err[string](b.err)} |
| 41 | + } |
| 42 | + return Str{Ok(string(b.v))} |
| 43 | +} |
| 44 | + |
| 45 | +// Bytes converts string to bytes. |
| 46 | +func (s Str) Bytes() Bytes { |
| 47 | + if s.err != nil { |
| 48 | + return Bytes{Err[[]byte](s.err)} |
| 49 | + } |
| 50 | + return Bytes{Ok([]byte(s.v))} |
| 51 | +} |
| 52 | + |
| 53 | +// Reader converts bytes to an io.Reader. |
| 54 | +func (b Bytes) Reader() Reader { |
| 55 | + if b.err != nil { |
| 56 | + return Reader{Err[io.Reader](b.err)} |
| 57 | + } |
| 58 | + var r io.Reader = bytes.NewReader(b.v) |
| 59 | + return Reader{Ok(r)} |
| 60 | +} |
| 61 | + |
| 62 | +// Reader converts string to an io.Reader. |
| 63 | +func (s Str) Reader() Reader { |
| 64 | + if s.err != nil { |
| 65 | + return Reader{Err[io.Reader](s.err)} |
| 66 | + } |
| 67 | + var r io.Reader = strings.NewReader(s.v) |
| 68 | + return Reader{Ok(r)} |
| 69 | +} |
| 70 | + |
| 71 | +// ReadAll reads all content from the contained io.Reader. |
| 72 | +func (r Reader) ReadAll() Bytes { |
| 73 | + if r.err != nil { |
| 74 | + return Bytes{Err[[]byte](r.err)} |
| 75 | + } |
| 76 | + return ReadAll(r.v) |
| 77 | +} |
| 78 | + |
| 79 | +// Slurp reads all content from the contained io.ReadCloser and closes it. |
| 80 | +func (r ReadCloser) Slurp() Bytes { |
| 81 | + if r.err != nil { |
| 82 | + return Bytes{Err[[]byte](r.err)} |
| 83 | + } |
| 84 | + return Slurp(r.v) |
| 85 | +} |
| 86 | + |
| 87 | +// WriteTo implements io.WriterTo for Bytes. |
| 88 | +func (b Bytes) WriteTo(w io.Writer) (int64, error) { |
| 89 | + if b.err != nil { |
| 90 | + return 0, b.err |
| 91 | + } |
| 92 | + if w == nil { |
| 93 | + return 0, errors.New("lambda/v2: nil writer") |
| 94 | + } |
| 95 | + n, err := w.Write(b.v) |
| 96 | + return int64(n), err |
| 97 | +} |
| 98 | + |
| 99 | +// WriteToWriter preserves chain-friendly behavior. |
| 100 | +func (b Bytes) WriteToWriter(w io.Writer) Bytes { |
| 101 | + _, err := b.WriteTo(w) |
| 102 | + return Bytes{Wrap(b.v, err)} |
| 103 | +} |
0 commit comments