add base handler and refactor H

This commit is contained in:
2025-10-28 21:38:58 +01:00
parent fb4d31afba
commit c7024d7f4e
7 changed files with 118 additions and 22 deletions
+9 -8
View File
@@ -4,6 +4,7 @@ import (
"encoding/csv"
"fmt"
"io"
"servicea/internal/app"
"strconv"
"strings"
"time"
@@ -69,7 +70,7 @@ func (c *CSV) Parse(r io.Reader) ([]MeteoData, []RejectedMeteoData, error) {
rowValue := strings.Join(row, ";")
record := make(H)
record := make(app.H)
for i, value := range row {
if i < len(header) {
record[header[i]] = value
@@ -99,17 +100,17 @@ func (c *CSV) Parse(r io.Reader) ([]MeteoData, []RejectedMeteoData, error) {
return meteoDataList, rejectedDataList, nil
}
func normalize(record H) (*MeteoData, error) {
func normalize(record app.H) (*MeteoData, error) {
meteoData := &MeteoData{}
var err error
meteoData.Timestamp, err = parseDate(record, "Fecha", ErrMissingOrInvalidDateField)
meteoData.Timestamp, err = parseDate(record, "Fecha", ErrMissingOrInvalidDate)
if err != nil {
return nil, err
}
meteoData.Location, err = parseString(record, "Ciudad", ErrMissingOrInvalidCityField)
meteoData.Location, err = parseString(record, "Ciudad", ErrMissingOrInvalidLocation)
if err != nil {
return nil, err
}
@@ -137,7 +138,7 @@ func normalize(record H) (*MeteoData, error) {
return meteoData, nil
}
func parseDate(record H, key string, errMissing error) (time.Time, error) {
func parseDate(record app.H, key string, errMissing error) (time.Time, error) {
if str, ok := record[key].(string); ok && str != "" {
t, err := time.Parse("2006/01/02", str)
if err != nil {
@@ -148,14 +149,14 @@ func parseDate(record H, key string, errMissing error) (time.Time, error) {
return time.Time{}, errMissing
}
func parseString(record H, key string, errMissing error) (string, error) {
func parseString(record app.H, key string, errMissing error) (string, error) {
if str, ok := record[key].(string); ok && str != "" {
return str, nil
}
return "", errMissing
}
func parseFloatField(record H, key string, errMissing error) (float32, error) {
func parseFloatField(record app.H, key string, errMissing error) (float32, error) {
if str, ok := record[key].(string); ok && str != "" {
str = strings.Replace(str, ",", ".", 1)
f, err := strconv.ParseFloat(str, 32)
@@ -167,7 +168,7 @@ func parseFloatField(record H, key string, errMissing error) (float32, error) {
return 0, errMissing
}
func parseIntField(record H, key string, errMissing error) (int, error) {
func parseIntField(record app.H, key string, errMissing error) (int, error) {
if str, ok := record[key].(string); ok && str != "" {
str = strings.TrimSpace(str)
i, err := strconv.Atoi(str)