ToSchema to export the datatable to a schema struct
(opt ...ExportOption)
| 102 | |
| 103 | // ToSchema to export the datatable to a schema struct |
| 104 | func (t *DataTable) ToSchema(opt ...ExportOption) *Schema { |
| 105 | if t == nil { |
| 106 | return nil |
| 107 | } |
| 108 | |
| 109 | opts := newExportOptions(opt...) |
| 110 | if err := t.evaluateExpressions(); err != nil { |
| 111 | panic(err) |
| 112 | } |
| 113 | |
| 114 | schema := &Schema{ |
| 115 | Name: t.name, |
| 116 | Rows: make([][]interface{}, 0, t.nrows), |
| 117 | } |
| 118 | |
| 119 | // visible columns |
| 120 | var cols []int |
| 121 | for i, col := range t.cols { |
| 122 | if opts.WithHiddenCols || col.IsVisible() { |
| 123 | cols = append(cols, i) |
| 124 | schema.Columns = append(schema.Columns, SchemaColumn{Type: col.UnderlyingType().Name(), Name: col.Name()}) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | for i := 0; i < t.nrows; i++ { |
| 129 | r := make([]interface{}, 0, len(cols)) |
| 130 | for _, pos := range cols { |
| 131 | r = append(r, t.cols[pos].serie.Get(i)) |
| 132 | } |
| 133 | schema.Rows = append(schema.Rows, r) |
| 134 | } |
| 135 | |
| 136 | return schema |
| 137 | } |