MCPcopy Create free account
hub / github.com/bytebase/bytebase / JSONToWriter

Function JSONToWriter

backend/component/export/json.go:19–37  ·  view source on GitHub ↗

JSONToWriter streams query results as pretty-printed JSON directly to the writer.

(w io.Writer, result *v1pb.QueryResult)

Source from the content-addressed store, hash-verified

17
18// JSONToWriter streams query results as pretty-printed JSON directly to the writer.
19func JSONToWriter(w io.Writer, result *v1pb.QueryResult) error {
20 records := make([]map[string]any, 0, len(result.Rows))
21 for _, row := range result.Rows {
22 record := make(map[string]any, len(result.ColumnNames))
23 for i, value := range row.Values {
24 record[result.ColumnNames[i]] = convertValueToJSONValue(value)
25 }
26 records = append(records, record)
27 }
28
29 jsonBytes, err := json.MarshalIndent(records, "", " ")
30 if err != nil {
31 return errors.Errorf("failed to encode JSON: %v", err)
32 }
33 if _, err := w.Write(jsonBytes); err != nil {
34 return err
35 }
36 return nil
37}
38
39func convertValueToJSONValue(value *v1pb.RowValue) any {
40 if value == nil || value.Kind == nil {

Callers 2

formatExportToZipMethod · 0.92
formatExportToZipFunction · 0.92

Calls 2

convertValueToJSONValueFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected