...

Source file src/github.com/go-openapi/runtime/csv_options.go

Documentation: github.com/go-openapi/runtime

     1  package runtime
     2  
     3  import (
     4  	"encoding/csv"
     5  	"io"
     6  )
     7  
     8  // CSVOpts alter the behavior of the CSV consumer or producer.
     9  type CSVOpt func(*csvOpts)
    10  
    11  type csvOpts struct {
    12  	csvReader    csv.Reader
    13  	csvWriter    csv.Writer
    14  	skippedLines int
    15  	closeStream  bool
    16  }
    17  
    18  // WithCSVReaderOpts specifies the options to csv.Reader
    19  // when reading CSV.
    20  func WithCSVReaderOpts(reader csv.Reader) CSVOpt {
    21  	return func(o *csvOpts) {
    22  		o.csvReader = reader
    23  	}
    24  }
    25  
    26  // WithCSVWriterOpts specifies the options to csv.Writer
    27  // when writing CSV.
    28  func WithCSVWriterOpts(writer csv.Writer) CSVOpt {
    29  	return func(o *csvOpts) {
    30  		o.csvWriter = writer
    31  	}
    32  }
    33  
    34  // WithCSVSkipLines will skip header lines.
    35  func WithCSVSkipLines(skipped int) CSVOpt {
    36  	return func(o *csvOpts) {
    37  		o.skippedLines = skipped
    38  	}
    39  }
    40  
    41  func WithCSVClosesStream() CSVOpt {
    42  	return func(o *csvOpts) {
    43  		o.closeStream = true
    44  	}
    45  }
    46  
    47  func (o csvOpts) applyToReader(in *csv.Reader) {
    48  	if o.csvReader.Comma != 0 {
    49  		in.Comma = o.csvReader.Comma
    50  	}
    51  	if o.csvReader.Comment != 0 {
    52  		in.Comment = o.csvReader.Comment
    53  	}
    54  	if o.csvReader.FieldsPerRecord != 0 {
    55  		in.FieldsPerRecord = o.csvReader.FieldsPerRecord
    56  	}
    57  
    58  	in.LazyQuotes = o.csvReader.LazyQuotes
    59  	in.TrimLeadingSpace = o.csvReader.TrimLeadingSpace
    60  	in.ReuseRecord = o.csvReader.ReuseRecord
    61  }
    62  
    63  func (o csvOpts) applyToWriter(in *csv.Writer) {
    64  	if o.csvWriter.Comma != 0 {
    65  		in.Comma = o.csvWriter.Comma
    66  	}
    67  	in.UseCRLF = o.csvWriter.UseCRLF
    68  }
    69  
    70  func csvOptsWithDefaults(opts []CSVOpt) csvOpts {
    71  	var o csvOpts
    72  	for _, apply := range opts {
    73  		apply(&o)
    74  	}
    75  
    76  	return o
    77  }
    78  
    79  type CSVWriter interface {
    80  	Write([]string) error
    81  	Flush()
    82  	Error() error
    83  }
    84  
    85  type CSVReader interface {
    86  	Read() ([]string, error)
    87  }
    88  
    89  var (
    90  	_ CSVWriter = &csvRecordsWriter{}
    91  	_ CSVReader = &csvRecordsWriter{}
    92  )
    93  
    94  // csvRecordsWriter is an internal container to move CSV records back and forth
    95  type csvRecordsWriter struct {
    96  	i       int
    97  	records [][]string
    98  }
    99  
   100  func (w *csvRecordsWriter) Write(record []string) error {
   101  	w.records = append(w.records, record)
   102  
   103  	return nil
   104  }
   105  
   106  func (w *csvRecordsWriter) Read() ([]string, error) {
   107  	if w.i >= len(w.records) {
   108  		return nil, io.EOF
   109  	}
   110  	defer func() {
   111  		w.i++
   112  	}()
   113  
   114  	return w.records[w.i], nil
   115  }
   116  
   117  func (w *csvRecordsWriter) Flush() {}
   118  
   119  func (w *csvRecordsWriter) Error() error {
   120  	return nil
   121  }
   122  

View as plain text