Skip to content

Commit 4a5fe14

Browse files
authored
csv: Handle with quoted strings in setCell (#81)
1 parent 32d82c6 commit 4a5fe14

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

csv/convert.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func convert(t reflect.Type, s string) (reflect.Value, error) {
9191
return v, nil
9292
}
9393
}
94-
return v, errNotSet
94+
return reflect.Value{}, errNotSet
9595
}
9696

9797
// setCell copies to dest the value in src, converting it if possible.
@@ -132,7 +132,10 @@ func setCell(dest any, s string) error {
132132
if d == nil {
133133
return errNilPtr
134134
}
135-
return d.UnmarshalJSON([]byte(s))
135+
if err := d.UnmarshalJSON([]byte(strconv.Quote(s))); err != nil {
136+
return d.UnmarshalJSON([]byte(s))
137+
}
138+
return nil
136139
case encoding.TextUnmarshaler:
137140
if d == nil {
138141
return errNilPtr
@@ -159,7 +162,10 @@ func setCell(dest any, s string) error {
159162

160163
if v, err := convert(dv.Type(), s); err != nil {
161164
if err == errNotSet {
162-
return json.Unmarshal([]byte(s), dest)
165+
if err := json.Unmarshal([]byte(strconv.Quote(s)), dest); err != nil {
166+
return json.Unmarshal([]byte(s), dest)
167+
}
168+
return nil
163169
}
164170
return err
165171
} else {

0 commit comments

Comments
 (0)