github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/httptransport/transformer/util_io.go (about)

     1  package transformer
     2  
     3  import (
     4  	"io"
     5  	"strings"
     6  
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  type CanInterface interface {
    11  	Interface() interface{}
    12  }
    13  
    14  type CanString interface {
    15  	String() string
    16  }
    17  
    18  func NewStringReaders(values []string) *StringReaders {
    19  	bs := make([]io.Reader, len(values))
    20  	for i := range values {
    21  		bs[i] = &StringReader{v: values[i]}
    22  	}
    23  
    24  	return &StringReaders{
    25  		readers: bs,
    26  		values:  values,
    27  	}
    28  }
    29  
    30  type StringReaders struct {
    31  	idx     int
    32  	readers []io.Reader
    33  	values  []string
    34  }
    35  
    36  func (v *StringReaders) Interface() interface{} { return v.values }
    37  
    38  func (v *StringReaders) Len() int { return len(v.readers) }
    39  
    40  func (v *StringReaders) Read(p []byte) (n int, err error) {
    41  	if v.idx < len(v.readers) {
    42  		return v.readers[v.idx].Read(p)
    43  	}
    44  	return -1, errors.Errorf("bounds out of range, %d", v.idx)
    45  }
    46  
    47  func (v *StringReaders) NextReader() io.Reader {
    48  	r := v.readers[v.idx]
    49  	v.idx++
    50  	return r
    51  }
    52  
    53  func NewStringReader(v string) *StringReader { return &StringReader{v: v} }
    54  
    55  type StringReader struct {
    56  	v string
    57  	r io.Reader
    58  }
    59  
    60  func (r *StringReader) Read(p []byte) (n int, err error) {
    61  	if r.r == nil {
    62  		r.r = strings.NewReader(r.v)
    63  	}
    64  	return r.r.Read(p)
    65  }
    66  
    67  func (r *StringReader) Interface() interface{} { return r.v }
    68  
    69  func (r *StringReader) String() string { return r.v }
    70  
    71  func NewStringBuilders() *StringBuilders { return &StringBuilders{} }
    72  
    73  type StringBuilders struct {
    74  	idx     int
    75  	buffers []*strings.Builder
    76  }
    77  
    78  func (v *StringBuilders) SetN(n int) {
    79  	v.buffers = make([]*strings.Builder, n)
    80  	v.idx = 0
    81  	for i := range v.buffers {
    82  		v.buffers[i] = &strings.Builder{}
    83  	}
    84  }
    85  func (v *StringBuilders) NextWriter() io.Writer {
    86  	if v.idx == 0 && len(v.buffers) == 0 {
    87  		v.SetN(1)
    88  	}
    89  	r := v.buffers[v.idx]
    90  	v.idx++
    91  	return r
    92  }
    93  
    94  func (v *StringBuilders) Write(p []byte) (n int, err error) {
    95  	if v.idx == 0 && len(v.buffers) == 0 {
    96  		v.SetN(1)
    97  	}
    98  	return v.buffers[v.idx].Write(p)
    99  }
   100  
   101  func (v *StringBuilders) StringSlice() []string {
   102  	values := make([]string, len(v.buffers))
   103  	for i, b := range v.buffers {
   104  		values[i] = b.String()
   105  	}
   106  	return values
   107  }