mirror of
https://github.com/pbnjay/grate.git
synced 2025-03-04 16:16:03 +02:00
expose data types through interface
This commit is contained in:
parent
35c8ec73bc
commit
35d03d67eb
@ -40,7 +40,9 @@ func main() {
|
||||
}
|
||||
|
||||
for sheet.Next() {
|
||||
//dtypes := sheet.Types()
|
||||
row := sheet.Strings()
|
||||
//fmt.Println(strings.Join(dtypes, "\t"))
|
||||
fmt.Println(strings.Join(row, "\t"))
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,26 @@ const (
|
||||
StaticCell // placeholder, internal use only
|
||||
)
|
||||
|
||||
// String returns a string description of the cell data type.
|
||||
func (c CellType) String() string {
|
||||
switch c {
|
||||
case BlankCell:
|
||||
return "blank"
|
||||
case IntegerCell:
|
||||
return "integer"
|
||||
case FloatCell:
|
||||
return "float"
|
||||
case BooleanCell:
|
||||
return "boolean"
|
||||
case DateCell:
|
||||
return "date"
|
||||
case HyperlinkStringCell:
|
||||
return "hyperlink"
|
||||
default: // StringCell, StaticCell
|
||||
return "string"
|
||||
}
|
||||
}
|
||||
|
||||
// Cell represents a single cell value.
|
||||
type Cell []interface{}
|
||||
|
||||
@ -85,7 +105,7 @@ var boolStrings = map[string]bool{
|
||||
}
|
||||
|
||||
// NewCellWithType creates a new cell value with the given type, coercing as necessary.
|
||||
func NewCellWithType(value interface{}, t CellType) Cell {
|
||||
func NewCellWithType(value interface{}, t CellType, f *Formatter) Cell {
|
||||
c := NewCell(value)
|
||||
if c[1] == t {
|
||||
// fast path if it was already typed correctly
|
||||
@ -154,7 +174,12 @@ func NewCellWithType(value interface{}, t CellType) Cell {
|
||||
c[1] = StringCell
|
||||
}
|
||||
if t == DateCell {
|
||||
/// DO THE MAGIC CONVERSION HERE
|
||||
if c[1] == FloatCell {
|
||||
c[0] = f.ConvertToDate(c[0].(float64))
|
||||
} else if c[1] == IntegerCell {
|
||||
c[0] = f.ConvertToDate(float64(c[0].(int64)))
|
||||
}
|
||||
c[1] = DateCell
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ func (s *Sheet) Put(row, col int, value interface{}, fmtNum uint16) {
|
||||
if !ok || fmtNum == 0 {
|
||||
s.Rows[row][col] = NewCell(value)
|
||||
} else {
|
||||
s.Rows[row][col] = NewCellWithType(value, ct)
|
||||
s.Rows[row][col] = NewCellWithType(value, ct, s.Formatter)
|
||||
}
|
||||
s.Rows[row][col].SetFormatNumber(fmtNum)
|
||||
}
|
||||
@ -110,6 +110,17 @@ func (s *Sheet) Strings() []string {
|
||||
return res
|
||||
}
|
||||
|
||||
// Types extracts the data types from the current record into a list.
|
||||
// options: "boolean", "integer", "float", "string", "date",
|
||||
// and special cases: "blank", "hyperlink" which are string types
|
||||
func (s *Sheet) Types() []string {
|
||||
res := make([]string, s.NumCols)
|
||||
for i, cell := range s.Rows[s.CurRow] {
|
||||
res[i] = cell.Type().String()
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// Scan extracts values from the current record into the provided arguments
|
||||
// Arguments must be pointers to one of 5 supported types:
|
||||
// bool, int64, float64, string, or time.Time
|
||||
|
5
grate.go
5
grate.go
@ -29,6 +29,11 @@ type Collection interface {
|
||||
// Strings extracts values from the current record into a list of strings.
|
||||
Strings() []string
|
||||
|
||||
// Types extracts the data types from the current record into a list.
|
||||
// options: "boolean", "integer", "float", "string", "date",
|
||||
// and special cases: "blank", "hyperlink" which are string types
|
||||
Types() []string
|
||||
|
||||
// Scan extracts values from the current record into the provided arguments
|
||||
// Arguments must be pointers to one of 5 supported types:
|
||||
// bool, int64, float64, string, or time.Time
|
||||
|
@ -44,6 +44,21 @@ func (t *simpleFile) Strings() []string {
|
||||
return t.rows[t.iterRow]
|
||||
}
|
||||
|
||||
// Types extracts the data types from the current record into a list.
|
||||
// options: "boolean", "integer", "float", "string", "date",
|
||||
// and special cases: "blank", "hyperlink" which are string types
|
||||
func (t *simpleFile) Types() []string {
|
||||
res := make([]string, len(t.rows[t.iterRow]))
|
||||
for i, v := range t.rows[t.iterRow] {
|
||||
if v == "" {
|
||||
res[i] = "blank"
|
||||
} else {
|
||||
res[i] = "string"
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// Scan extracts values from the current record into the provided arguments
|
||||
// Arguments must be pointers to one of 5 supported types:
|
||||
// bool, int, float64, string, or time.Time
|
||||
|
Loading…
x
Reference in New Issue
Block a user