-
Notifications
You must be signed in to change notification settings - Fork 46
/
field.go
79 lines (66 loc) · 2.13 KB
/
field.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package godbf
// FieldDescriptor describes one field/column in a DbfTable as per https://www.dbase.com/Knowledgebase/INT/db7_file_fmt.htm, Heading 1.2.
type FieldDescriptor struct {
name string
fieldType DbaseDataType
length byte
decimalPlaces byte // Field decimal count in binary
fieldStore [32]byte
}
// Name returns the column name of the field
func (fd *FieldDescriptor) Name() string {
return fd.name
}
// FieldType returns the type of data stored for the field as a DbaseDataType
func (fd *FieldDescriptor) FieldType() DbaseDataType {
return fd.fieldType
}
// Length returns the length of data stored for the field
func (fd *FieldDescriptor) Length() byte {
return fd.length
}
// DecimalPlaces returns the count of decimal places for the field
func (fd *FieldDescriptor) DecimalPlaces() byte {
return fd.decimalPlaces
}
func (fd FieldDescriptor) usesDecimalPlaces() bool {
return fd.fieldType.usesDecimalCount()
}
// DbaseDataType is dBase data type, as per https://www.dbase.com/Knowledgebase/INT/db7_file_fmt.htm, under heading "Storage of dBASE Data Types".
type DbaseDataType byte
const (
Character DbaseDataType = 'C'
Logical DbaseDataType = 'L'
Date DbaseDataType = 'D'
Numeric DbaseDataType = 'N'
Float DbaseDataType = 'F'
)
func (ddt DbaseDataType) byte() byte {
return byte(ddt)
}
const notApplicable = 0x00
// fixedFieldLength returns the length in bytes in for the data type if it describes a fixed-length field.
func (ddt DbaseDataType) fixedFieldLength() byte {
switch ddt {
case Logical:
return 1
case Date:
return 8
default:
return notApplicable
}
}
// usesDecimalCount indicates whether the data type describes a field that makes use of a field's decimal count setting.
func (ddt DbaseDataType) usesDecimalCount() bool {
switch ddt {
case Float, Numeric:
return true
default:
return false
}
}
// decimalCountNotApplicable is a convenience decorator supplying a 0-valued byte. THis is used indicate that the data
// type describes a field that does not make use its decimal count setting.
func (ddt DbaseDataType) decimalCountNotApplicable() byte {
return notApplicable
}