I am trying to write a csv parser using the example provided here. It works great for all native types but I am having trouble with any structs that contain a timestamp of type time.Time. It exits with an error of "cannot convert this type".
This is the code.
//For each field in a given struct... //Get a field val := sv.Field(i) // this is necessary because Kind can't tell // distinguish between a primitive type // and a type derived from it. We're looking // for a Value interface defined on // the pointer to this value _, ok := val.Addr().Interface().(Value) if ok { val = val.Addr() kind = value_k } else { switch Kind { case reflect.Int, reflect.Int16, reflect.Int8, reflect.Int32, reflect.Int64: kind = int_k case reflect.Uint, reflect.Uint16, reflect.Uint8, reflect.Uint32, reflect.Uint64: kind = uint_k case reflect.Float32, reflect.Float64: kind = float_k case reflect.String: kind = string_k default: // Kind is Struct here kind = value_k _, ok := val.Interface().(Value) if !ok { err = os.NewError("cannot convert this type ") this = nil return } } }
What this code does is take an interface and a reader. It attempts to match the field headers in the reader (csv file) with field names in the interface. It also reflects on the interface (struct) and collects positional a type information for later setting the fields in the iterator. It is this step that is failing for non-native types.
I've tried a few methods to work around this but the only thing that seems to work is changing the timestamp to a string. I am undoubtedly missing something and would greatly appreciate some guidance.