-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsv.go
84 lines (71 loc) · 1.85 KB
/
dsv.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
80
81
82
83
84
package dsv
import (
"errors"
"strings"
)
// StructToDSV returns a dsv string from the struct
func StructToDSV(i interface{}, separator string) (string, error) {
// Validating inputs
if i == nil {
return "", errors.New("The struct in the parameters can not be null")
}
if len(separator) == 0 {
return "", errors.New("Separator can not be empty")
}
// Processing data
fields, err := getFields(i)
if err != nil {
return "", err
}
sortedFields, err := sortFields(fields)
if err != nil {
return "", err
}
var responseLine string
for index := 0; index < len(sortedFields); index++ {
finalValue, err := padValue(sortedFields[index])
if err != nil {
return "", err
}
if index > 0 {
responseLine += separator
}
responseLine += finalValue
}
return responseLine, nil
}
// ToStruct returns an interface based in the dsv data, the struct interface and the separator
func ToStruct(dsv string, i interface{}, separator string) (interface{}, error) {
// Validating inputs
if len(dsv) == 0 {
return nil, errors.New("DSV data can not be empty")
}
if i == nil {
return nil, errors.New("Interface of struct can not be null")
}
if len(separator) == 0 {
return nil, errors.New("Separator can not be empty")
}
// Processing data
fields, err := getFields(i)
if err != nil {
return nil, err
}
fieldsLength := len(fields)
splittedData := strings.Split(dsv, separator)
splittedDataLength := len(splittedData)
// Put data in the fields
for dataIndex := 0; dataIndex < splittedDataLength; dataIndex++ {
for fieldIndex := 0; fieldIndex < fieldsLength; fieldIndex++ {
if fields[fieldIndex].index != dataIndex {
continue
}
fields[fieldIndex].value = strings.Trim(splittedData[dataIndex], " ")
}
}
interfaceWithData, err := setFieldsToStruct(fields, i)
if err != nil {
return nil, err
}
return interfaceWithData, nil
}