Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc140583 committed Jun 27, 2020
1 parent 39c2dee commit a2c2fe3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 34 deletions.
9 changes: 8 additions & 1 deletion export/delimiter_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ func GetIndexesByTag(modelType reflect.Type, tagName string, skipTag string) (ma
tagValue := field.Tag.Get(tagName)
skipValue := field.Tag.Get(skipTag)
v := Delimiter{}
tagScale, sOk := field.Tag.Lookup("scale")
if sOk {
scale, err := strconv.Atoi(tagScale)
if err == nil {
v.Scale = scale
}
}
if len(skipValue) > 0 {
if len(tagValue) > 0 {
if strings.Contains(tagValue, "dateFormat:") {
Expand All @@ -131,7 +138,7 @@ func GetIndexesByTag(modelType reflect.Type, tagName string, skipTag string) (ma
if strings.Contains(tagValue, "dateFormat:") {
tagValue = strings.ReplaceAll(tagValue, "dateFormat:", "")
v.Format = tagValue
} else if strings.Contains(tagValue, "scale:") {
} else if sOk == false && strings.Contains(tagValue, "scale:") {
tagValue = strings.ReplaceAll(tagValue, "scale:", "")
scale, err1 := strconv.Atoi(tagValue)
if err1 != nil {
Expand Down
76 changes: 47 additions & 29 deletions export/fixedlength_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ func GetIndexes(modelType reflect.Type, tagName string) (map[int]*FixedLength, e
return ma, err
}
v := &FixedLength{Length: length}
tagScale, sOk := field.Tag.Lookup("scale")
if sOk {
scale, err := strconv.Atoi(tagScale)
if err == nil {
v.Scale = scale
}
}
if len(tagValue) > 0 {
if strings.Contains(tagValue, "dateFormat:") {
tagValue = strings.ReplaceAll(tagValue, "dateFormat:", "")
} else if strings.Contains(tagValue, "scale:") {
} else if sOk == false && strings.Contains(tagValue, "scale:") {
tagValue = strings.ReplaceAll(tagValue, "scale:", "")
scale, err1 := strconv.Atoi(tagValue)
if err1 != nil {
Expand Down Expand Up @@ -83,41 +90,52 @@ func ToFixedLength(model interface{}, formatCols map[int]*FixedLength) string {
v = reflect.Indirect(reflect.ValueOf(v)).Interface()
kind = field.Elem().Kind()
}

if d, okD := v.(time.Time); okD {
value = d.Format(format.Format)
} else {
if kind == reflect.Struct {
if v2 := reflect.Indirect(reflect.ValueOf(v)); v2.NumField() == 1 {
f := v2.Field(0)
fv := f.Interface()
k := f.Kind()
if k == reflect.Ptr {
fv = reflect.Indirect(reflect.ValueOf(fv)).Interface()
}
if sv, ok := fv.(big.Float); ok {
prec := 2
if f, err := strconv.Atoi(format.Format); err == nil {
prec = f
if s, okS := v.(string); okS {
value = FixedLengthString(format.Length, s)
} else {
if d, okD := v.(time.Time); okD {
value = d.Format(format.Format)
} else {
if kind == reflect.Struct {
if v2 := reflect.Indirect(reflect.ValueOf(v)); v2.NumField() == 1 {
f := v2.Field(0)
fv := f.Interface()
k := f.Kind()
if k == reflect.Ptr {
fv = reflect.Indirect(reflect.ValueOf(fv)).Interface()
}
if sv, ok := fv.(big.Float); ok {
prec := 2
if format.Scale > 0 {
prec = format.Scale
}
value = sv.Text('f', prec)
} else if svi, ok := fv.(big.Int); ok {
value = svi.Text(10)
} else {
value = fmt.Sprint(v)
}
value = sv.Text('f', prec)
} else if svi, ok := fv.(big.Int); ok {
value = svi.Text(10)
} else {
value = fmt.Sprint(v)
if len(value) > format.Length {
value = strings.TrimSpace(value)
}
if len(format.Format) > 0 {
value = fmt.Sprintf(format.Format, value)
}
}
} else {
value = fmt.Sprint(v)
if len(value) > format.Length {
value = strings.TrimSpace(value)
}
if len(format.Format) > 0 {
value = fmt.Sprintf(format.Format, value)
}
}
}else{
value = fmt.Sprint(v)
if len(value) > format.Length {
value = strings.TrimSpace(value)
}
if len(format.Format) > 0 {
value = fmt.Sprintf(format.Format, value)
}
}
value = FixedLengthString(format.Length, value)
}
value = FixedLengthString(format.Length, value)
}
arr = append(arr, value)
}
Expand Down
2 changes: 1 addition & 1 deletion export/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func GetColumnsSelect(modelType reflect.Type) []string {
str2 := strings.Split(str1[i], ":")
for j := 0; j < len(str2); j++ {
if str2[j] == "column" {
columnName := str2[j+1]
columnName := strings.ToLower(str2[j+1])
columnNameKeys = append(columnNameKeys, columnName)
}
}
Expand Down
10 changes: 8 additions & 2 deletions import/delimiter_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ func GetIndexesByTag(modelType reflect.Type, tagName string) (map[int]Delimiter,
field := modelType.Field(i)
tagValue := field.Tag.Get(tagName)
v := Delimiter{}
tagScale, sOk := field.Tag.Lookup("scale")
if sOk {
scale, err := strconv.Atoi(tagScale)
if err == nil {
v.Scale = scale
}
}
if len(tagValue) > 0 {
if strings.Contains(tagValue, "dateFormat:") {
tagValue = strings.ReplaceAll(tagValue, "dateFormat:", "")
} else if strings.Contains(tagValue, "scale:") {
} else if sOk == false && strings.Contains(tagValue, "scale:") {
tagValue = strings.ReplaceAll(tagValue, "scale:", "")
scale, err1 := strconv.Atoi(tagValue)
if err1 != nil {
Expand Down Expand Up @@ -132,7 +139,6 @@ func ScanLine(lines []string, modelType reflect.Type, record interface{}, format
f.Set(reflect.ValueOf(fieldDate))
}
}

case "big.Float", "*big.Float":
if formatf, ok := formatCols[i]; ok {
bf := new(big.Float)
Expand Down
9 changes: 8 additions & 1 deletion import/fixedlength_formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,17 @@ func GetIndexes(modelType reflect.Type, tagName string) (map[int]*FixedLength, e
return ma, err
}
v := &FixedLength{Length: length}
tagScale, sOk := field.Tag.Lookup("scale")
if sOk {
scale, err := strconv.Atoi(tagScale)
if err == nil {
v.Scale = scale
}
}
if len(tagValue) > 0 {
if strings.Contains(tagValue, "dateFormat:") {
tagValue = strings.ReplaceAll(tagValue, "dateFormat:", "")
} else if strings.Contains(tagValue, "scale:") {
} else if sOk == false && strings.Contains(tagValue, "scale:") {
tagValue = strings.ReplaceAll(tagValue, "scale:", "")
scale, err1 := strconv.Atoi(tagValue)
if err1 != nil {
Expand Down

0 comments on commit a2c2fe3

Please sign in to comment.