mirror of
https://github.com/therootcompany/golib.git
synced 2026-11-04 15:05:00 +00:00
27 lines
505 B
Go
27 lines
505 B
Go
package csvx
|
|
|
|
import (
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
// Listish is a list encoded as comma- or whitespace-delimited text.
|
|
type Listish []string
|
|
|
|
// UnmarshalCSV parses comma- or whitespace-delimited values.
|
|
func (s *Listish) UnmarshalCSV(data []byte) error {
|
|
if len(data) == 0 {
|
|
*s = Listish{}
|
|
return nil
|
|
}
|
|
parts := strings.FieldsFunc(string(data), func(r rune) bool {
|
|
return r == ',' || unicode.IsSpace(r)
|
|
})
|
|
if len(parts) == 0 {
|
|
*s = Listish{}
|
|
return nil
|
|
}
|
|
*s = Listish(parts)
|
|
return nil
|
|
}
|