forked from mongodb/mongo-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_export_document.go
54 lines (50 loc) · 1.93 KB
/
custom_export_document.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package mongoexport // ExportDocument writes a line to output with the CSV representation of a document.
import (
"fmt"
"reflect"
"strconv"
"github.com/mongodb/mongo-tools/common/bsonutil"
"github.com/mongodb/mongo-tools/common/json"
"go.mongodb.org/mongo-driver/bson"
)
// ExportDocumentCustom differs from ExportDocument method of *CSVExportOutput
// while formatting floats. ExportDocument uses fmt.Sprint to format all values,
// whereas ExportDocumentCustom uses strconv.FormatFloat to format float values.
// fmt.Sprint method converts floats to scientific notation while formatting.
// We wanted to avoid this, so we're using strconv.FormatFloat to format floats
// in standard notation.
func (csvExporter *CSVExportOutput) ExportDocumentCustom(document bson.D) error {
rowOut := make([]string, 0, len(csvExporter.Fields))
extendedDoc, err := bsonutil.ConvertBSONValueToLegacyExtJSON(document)
if err != nil {
return err
}
for _, fieldName := range csvExporter.Fields {
fieldVal := extractFieldByName(fieldName, extendedDoc)
if fieldVal == nil {
rowOut = append(rowOut, "")
} else if reflect.TypeOf(fieldVal) == reflect.TypeOf(bson.M{}) ||
reflect.TypeOf(fieldVal) == reflect.TypeOf(bson.D{}) ||
reflect.TypeOf(fieldVal) == marshalDType ||
reflect.TypeOf(fieldVal) == reflect.TypeOf([]interface{}{}) {
buf, err := json.Marshal(fieldVal)
if err != nil {
rowOut = append(rowOut, "")
} else {
rowOut = append(rowOut, string(buf))
}
} else if kind := reflect.TypeOf(fieldVal).Kind(); kind == reflect.Float32 || kind == reflect.Float64 {
f, ok := fieldVal.(json.NumberFloat)
if !ok {
rowOut = append(rowOut, fmt.Sprintf("%v", fieldVal))
} else {
rowOut = append(rowOut, strconv.FormatFloat(float64(f), 'f', -1, 64))
}
} else {
rowOut = append(rowOut, fmt.Sprintf("%v", fieldVal))
}
}
csvExporter.csvWriter.Write(rowOut)
csvExporter.NumExported++
return csvExporter.csvWriter.Error()
}