wip: unmarshall add uint handling
This commit is contained in:
parent
f02f5f8410
commit
32c10a9c7a
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/.idea/
|
||||||
10
decode.go
Normal file
10
decode.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package gocsvparser
|
||||||
|
|
||||||
|
type Decoder struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDecoder() *Decoder {
|
||||||
|
decoder := new(Decoder)
|
||||||
|
|
||||||
|
return decoder
|
||||||
|
}
|
||||||
@ -52,7 +52,7 @@ func (d *defaultRecordHandler) HandleRecord(v any, record []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
d.columnNameMapped = true
|
d.columnNameMapped = true
|
||||||
return HeaderRead
|
return ErrHeaderRead
|
||||||
}
|
}
|
||||||
|
|
||||||
val = reflect.ValueOf(v)
|
val = reflect.ValueOf(v)
|
||||||
@ -85,7 +85,7 @@ func (d *defaultRecordHandler) setValue(val reflect.Value, strValue string, stru
|
|||||||
fieldVal reflect.Value
|
fieldVal reflect.Value
|
||||||
f64 float64
|
f64 float64
|
||||||
i64 int64
|
i64 int64
|
||||||
i int
|
u64 uint64
|
||||||
err error
|
err error
|
||||||
b bool
|
b bool
|
||||||
)
|
)
|
||||||
@ -100,48 +100,36 @@ func (d *defaultRecordHandler) setValue(val reflect.Value, strValue string, stru
|
|||||||
return reflect.Value{}, fmt.Errorf("ParseBool: %+v", err)
|
return reflect.Value{}, fmt.Errorf("ParseBool: %+v", err)
|
||||||
}
|
}
|
||||||
fieldVal.SetBool(b)
|
fieldVal.SetBool(b)
|
||||||
case reflect.Int64:
|
case reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8, reflect.Int:
|
||||||
i64, err = strconv.ParseInt(strValue, 0, 64)
|
i64, err = strconv.ParseInt(strValue, 0, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return reflect.Value{}, fmt.Errorf("ParseInt 64: %+v", err)
|
return reflect.Value{}, fmt.Errorf("ParseInt %s: %+v", strValue, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if fieldVal.OverflowInt(i64) {
|
||||||
|
return reflect.Value{}, fmt.Errorf("int value overflow: %s", strValue)
|
||||||
|
}
|
||||||
|
|
||||||
fieldVal.SetInt(i64)
|
fieldVal.SetInt(i64)
|
||||||
case reflect.Int32:
|
|
||||||
i64, err = strconv.ParseInt(strValue, 0, 32)
|
|
||||||
if err != nil {
|
|
||||||
return reflect.Value{}, fmt.Errorf("ParseInt 32: %+v", err)
|
|
||||||
}
|
|
||||||
fieldVal.SetInt(i64)
|
|
||||||
case reflect.Int16:
|
|
||||||
i64, err = strconv.ParseInt(strValue, 0, 16)
|
|
||||||
if err != nil {
|
|
||||||
return reflect.Value{}, fmt.Errorf("ParseInt 16: %+v", err)
|
|
||||||
}
|
|
||||||
fieldVal.SetInt(i64)
|
|
||||||
case reflect.Int8:
|
|
||||||
i64, err = strconv.ParseInt(strValue, 0, 8)
|
|
||||||
if err != nil {
|
|
||||||
return reflect.Value{}, fmt.Errorf("ParseInt 8: %+v", err)
|
|
||||||
}
|
|
||||||
fieldVal.SetInt(i64)
|
|
||||||
case reflect.Int:
|
|
||||||
i, err = strconv.Atoi(strValue)
|
|
||||||
if err != nil {
|
|
||||||
return reflect.Value{}, fmt.Errorf("strconv.Atoi: %+v", err)
|
|
||||||
}
|
|
||||||
fieldVal.SetInt(int64(i))
|
|
||||||
case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint:
|
case reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8, reflect.Uint:
|
||||||
return reflect.Value{}, errors.New("unimplemented") //TODO: fix me
|
u64, err = strconv.ParseUint(strValue, 0, 64)
|
||||||
case reflect.Float32:
|
|
||||||
f64, err = strconv.ParseFloat(strValue, 32)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return reflect.Value{}, fmt.Errorf("ParseFloat 32: %+v", err)
|
return reflect.Value{}, fmt.Errorf("ParseUint %s: %+v", strValue, err)
|
||||||
}
|
}
|
||||||
fieldVal.SetFloat(f64)
|
|
||||||
case reflect.Float64:
|
if fieldVal.OverflowUint(u64) {
|
||||||
|
return reflect.Value{}, fmt.Errorf("uint value overflow %s", strValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
fieldVal.SetUint(u64)
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
f64, err = strconv.ParseFloat(strValue, 64)
|
f64, err = strconv.ParseFloat(strValue, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return reflect.Value{}, fmt.Errorf("ParseFloat 64: %+v", err)
|
return reflect.Value{}, fmt.Errorf("ParseFloat %s: %+v", strValue, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if fieldVal.OverflowFloat(f64) {
|
||||||
|
return reflect.Value{}, fmt.Errorf("float value overflow %s", strValue)
|
||||||
}
|
}
|
||||||
fieldVal.SetFloat(f64)
|
fieldVal.SetFloat(f64)
|
||||||
default:
|
default:
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
HeaderRead = errors.New("column headers successfully read")
|
ErrHeaderRead = errors.New("column headers successfully read")
|
||||||
)
|
)
|
||||||
|
|
||||||
type FieldsConfig struct {
|
type FieldsConfig struct {
|
||||||
@ -163,7 +163,7 @@ func (u *Unmarshaler) Unmarshal(data []byte, v any, options ...CsvOption) error
|
|||||||
|
|
||||||
err = u.recordHandler.HandleRecord(val.Interface(), record)
|
err = u.recordHandler.HandleRecord(val.Interface(), record)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == HeaderRead {
|
if err == ErrHeaderRead {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
return fmt.Errorf("error Unmarshal: RecordHandler.HandleRecord: %+v", err)
|
return fmt.Errorf("error Unmarshal: RecordHandler.HandleRecord: %+v", err)
|
||||||
@ -172,10 +172,9 @@ func (u *Unmarshaler) Unmarshal(data []byte, v any, options ...CsvOption) error
|
|||||||
slice.Set(reflect.Append(slice, val.Elem()))
|
slice.Set(reflect.Append(slice, val.Elem()))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
//TODO: implementation
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: implementation
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,5 +214,5 @@ func (u *Unmarshaler) newElem(typ reflect.Type) (reflect.Value, error) {
|
|||||||
//TODO: implementation
|
//TODO: implementation
|
||||||
}
|
}
|
||||||
|
|
||||||
return reflect.Zero(typ), errors.New("invalid impelementation") //TODO: placeholder; update me
|
return reflect.Zero(typ), errors.New("invalid implementation") //TODO: placeholder; update me
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user