package reader import ( "fmt" "io" "strconv" "strings" "git.wecise.com/wecise/util/merrs" "github.com/spf13/cast" ) type CSVBlockReader struct { *LineReader csvkeys []string } func NewCSVBlockReader(filename string, reader io.Reader) *CSVBlockReader { return &CSVBlockReader{ LineReader: NewLineReader(filename, reader), } } func (br *CSVBlockReader) ReadBlock() (block map[string]any, line string, linecount int, err error) { eof := false for { line, linecount, eof, err = br.ReadLine() if err != nil { return } if linecount == 1 { br.csvkeys = strings.Split(line, "^") line, linecount, eof, err = br.ReadLine() if err != nil { return } } if line == "" { if eof { return } continue } values := strings.Split(line, "^") if len(values) != len(br.csvkeys) { err = merrs.NewError(fmt.Sprint(br.filename, " format error, values count not match keys count, line ", br.linecount)) return } block = map[string]any{} for i, k := range br.csvkeys { v := values[i] if v != "" { n := cast.ToInt(v) if n != 0 || v == "0" { block[k] = n continue } f := cast.ToFloat64(v) if f != 0 || v == "0" || v == "0.0" || v == ".0" || v == "0." { block[k] = f continue } b := cast.ToBool(v) if v == "true" || v == "false" { block[k] = b continue } s, e := strconv.Unquote(v) if e == nil { block[k] = s continue } } block[k] = v } return } }