-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathffparser.go
More file actions
184 lines (147 loc) · 4.25 KB
/
ffparser.go
File metadata and controls
184 lines (147 loc) · 4.25 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package ffparser
import (
"bytes"
"fmt"
"reflect"
"sort"
"strings"
"time"
"github.com/helloticket/ffparser/decorator"
)
//FFParser flat file parser
type FFParser struct {
decorators map[string]decorator.FieldDecorator
}
func NewSimpleParser() *FFParser {
instance := FFParser{decorators: map[string]decorator.FieldDecorator{}}
instance.decorators["Default"] = &decorator.DefaultDecorator{}
instance.decorators["IntDecorator"] = &decorator.IntDecorator{}
instance.decorators["Int64Decorator"] = &decorator.Int64Decorator{}
instance.decorators["Float64Decorator"] = &decorator.Float64Decorator{}
instance.decorators["BrazilDateDecorator"] = &decorator.BrazilDateDecorator{}
instance.decorators["BrazilSmallDateDecorator"] = &decorator.BrazilSmallDateDecorator{}
instance.decorators["BrazilMoneyDecorator"] = &decorator.BrazilMoneyDecorator{}
instance.decorators["DateTimeDecorator"] = &decorator.DateTimeDecorator{}
return &instance
}
func (f *FFParser) ParseToText(src interface{}) (string, error) {
mapFields, _ := tags(src, "record")
if len(mapFields) <= 0 {
return "", fmt.Errorf("Could not fields public")
}
var buffer bytes.Buffer
recordsField, _ := f.handlerRecordFieldsAndSort(src, mapFields)
for _, record := range recordsField {
content, err := getField(src, record.FieldName)
if err != nil {
return "", err
}
decorator, err := f.factoryDecorator(content, record)
if err != nil {
return "", err
}
value, err := decorator.ToString(content)
if err != nil {
return "", err
}
result, err := record.ApplyFormat(value)
if err != nil {
return "", err
}
buffer.WriteString(result)
}
return buffer.String(), nil
}
func (f *FFParser) CreateFromText(src interface{}, text string) error {
mapFields, _ := tags(src, "record")
if len(mapFields) <= 0 {
return fmt.Errorf("Could not fields public")
}
recordsField, _ := f.handlerRecordFieldsAndSort(src, mapFields)
for _, record := range recordsField {
decorator, err := f.factoryDecorator(src, record)
if err != nil {
return err
}
value, err := decorator.FromString(strings.TrimSpace(text[record.Start:record.End]))
if err != nil {
return err
}
if err := setField(src, record.FieldName, value); err != nil {
return err
}
}
return nil
}
func (f *FFParser) mapperTags(tagName string) map[string]string {
tags := map[string]string{}
for _, tagValue := range strings.Split(tagName, ",") {
entry := strings.Split(tagValue, "=")
if len(entry) >= 2 {
tags[entry[0]] = entry[1]
} else {
tags[entry[0]] = ""
}
}
return tags
}
func (f *FFParser) factoryDecorator(obj interface{}, record RecordField) (decorator.FieldDecorator, error) {
if record.Decorator != "" {
if decorator := f.decorators[record.Decorator]; decorator != nil {
return decorator, nil
}
}
if record.FieldType == reflect.Int ||
record.FieldType == reflect.Int8 ||
record.FieldType == reflect.Int16 ||
record.FieldType == reflect.Int32 {
return f.decorators["IntDecorator"], nil
}
if record.FieldType == reflect.Int64 {
return f.decorators["Int64Decorator"], nil
}
if record.FieldType == reflect.Float32 ||
record.FieldType == reflect.Float64 {
return f.decorators["Float64Decorator"], nil
}
if record.FieldType == reflect.Struct {
if _, ok := obj.(time.Time); ok {
return f.decorators["DateTimeDecorator"], nil
}
}
return f.decorators["Default"], nil
}
func (f *FFParser) handlerRecordFieldsAndSort(src interface{}, fields map[string]string) ([]RecordField, error) {
records := []RecordField{}
for fieldName, tagName := range fields {
if tagName == "" {
continue
}
tags := f.mapperTags(tagName)
start := toInteger(tags["start"]) - 1
end := toInteger(tags["end"])
size := (end - start)
decorator := tags["decorator"]
padChar := ""
padAlign := ""
fieldType, _ := getFieldKind(src, fieldName)
if tags["padchar"] != "" {
padChar = tags["padchar"]
}
if tags["padalign"] != "" {
padAlign = tags["padalign"]
}
records = append(records, RecordField{
FieldName: fieldName,
Start: start,
End: end,
Size: size,
Decorator: decorator,
PaddingChar: padChar,
PaddingAlign: padAlign,
FieldType: fieldType,
})
}
sort.Sort(RecordFieldSorted(records))
return records, nil
}