From 84ec54a893a0a4eec41916ffa907f19e7324499d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20P=C3=A9rez?= Date: Thu, 30 Oct 2025 17:50:03 +0100 Subject: [PATCH] move H to pkg --- .../internal/app/app.go => pkg/models.go | 2 +- service_a/internal/domains/meteo/file.go | 14 +++++++------- service_a/internal/domains/meteo/handlers.go | 19 +++++++++---------- service_b/internal/app/app.go | 2 -- service_b/internal/domains/meteo/handlers.go | 5 ++--- 5 files changed, 19 insertions(+), 23 deletions(-) rename service_a/internal/app/app.go => pkg/models.go (65%) diff --git a/service_a/internal/app/app.go b/pkg/models.go similarity index 65% rename from service_a/internal/app/app.go rename to pkg/models.go index 93a6d9e..5e45fd1 100644 --- a/service_a/internal/app/app.go +++ b/pkg/models.go @@ -1,3 +1,3 @@ -package app +package pkg type H map[string]any diff --git a/service_a/internal/domains/meteo/file.go b/service_a/internal/domains/meteo/file.go index 0ea5a7e..c118961 100644 --- a/service_a/internal/domains/meteo/file.go +++ b/service_a/internal/domains/meteo/file.go @@ -4,7 +4,7 @@ import ( "encoding/csv" "fmt" "io" - "servicea/internal/app" + "pkg" "strconv" "strings" "time" @@ -74,7 +74,7 @@ func (c *CSV) Parse(r io.Reader) ([]MeteoData, []RejectedMeteoData, error) { rowValue := strings.Join(row, ";") - record := make(app.H) + record := make(pkg.H) for i, value := range row { if i < len(header) { record[header[i]] = value @@ -104,7 +104,7 @@ func (c *CSV) Parse(r io.Reader) ([]MeteoData, []RejectedMeteoData, error) { return meteoDataList, rejectedDataList, nil } -func normalize(record app.H) (*MeteoData, error) { +func normalize(record pkg.H) (*MeteoData, error) { meteoData := &MeteoData{} var err error @@ -142,7 +142,7 @@ func normalize(record app.H) (*MeteoData, error) { return meteoData, nil } -func parseDate(record app.H, key string, errMissing error) (time.Time, error) { +func parseDate(record pkg.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 { @@ -153,14 +153,14 @@ func parseDate(record app.H, key string, errMissing error) (time.Time, error) { return time.Time{}, errMissing } -func parseString(record app.H, key string, errMissing error) (string, error) { +func parseString(record pkg.H, key string, errMissing error) (string, error) { if str, ok := record[key].(string); ok && str != "" { return str, nil } return "", errMissing } -func parseFloatField(record app.H, key string, errMissing error) (float32, error) { +func parseFloatField(record pkg.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) @@ -172,7 +172,7 @@ func parseFloatField(record app.H, key string, errMissing error) (float32, error return 0, errMissing } -func parseIntField(record app.H, key string, errMissing error) (int, error) { +func parseIntField(record pkg.H, key string, errMissing error) (int, error) { if str, ok := record[key].(string); ok && str != "" { str = strings.TrimSpace(str) i, err := strconv.Atoi(str) diff --git a/service_a/internal/domains/meteo/handlers.go b/service_a/internal/domains/meteo/handlers.go index 4413d3c..2769107 100644 --- a/service_a/internal/domains/meteo/handlers.go +++ b/service_a/internal/domains/meteo/handlers.go @@ -9,7 +9,6 @@ import ( "log/slog" "net/http" "pkg" - "servicea/internal/app" "time" ) @@ -28,14 +27,14 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) { err := r.ParseMultipartForm(10 << 20) if err != nil { slog.Error(ErrParsingForm.Error(), "error", err) - h.ToJSON(w, http.StatusBadRequest, app.H{"error": ErrParsingForm}) + h.ToJSON(w, http.StatusBadRequest, pkg.H{"error": ErrParsingForm}) return } file, header, err := r.FormFile("file") if err != nil { slog.Error(ErrRetrievingFile.Error(), "error", err) - h.ToJSON(w, http.StatusBadRequest, app.H{"error": ErrRetrievingFile}) + h.ToJSON(w, http.StatusBadRequest, pkg.H{"error": ErrRetrievingFile}) return } defer file.Close() @@ -43,7 +42,7 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) { content, err := io.ReadAll(file) if err != nil { slog.Error(ErrReadingFile.Error(), "error", err) - h.ToJSON(w, http.StatusInternalServerError, app.H{"error": ErrReadingFile}) + h.ToJSON(w, http.StatusInternalServerError, pkg.H{"error": ErrReadingFile}) return } @@ -60,7 +59,7 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) { slog.Error(ErrCannotParseFile.Error(), "filename", header.Filename, "error", err) - h.ToJSON(w, http.StatusConflict, app.H{"error": err}) + h.ToJSON(w, http.StatusConflict, pkg.H{"error": err}) return } fileStats.ElapsedMS = int(time.Since(start).Milliseconds()) @@ -74,7 +73,7 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) { "file_checksum", fileStats.FileChecksum, ) - h.ToJSON(w, http.StatusOK, app.H{"stats": fileStats}) + h.ToJSON(w, http.StatusOK, pkg.H{"stats": fileStats}) } func (h *Handler) IngestExcel(w http.ResponseWriter, r *http.Request) { @@ -84,7 +83,7 @@ func (h *Handler) IngestExcel(w http.ResponseWriter, r *http.Request) { func (h *Handler) GetCities(w http.ResponseWriter, r *http.Request) { cities := h.s.GetCities(r.Context()) slog.Info("cities retrieved", "count", len(cities)) - h.ToJSON(w, http.StatusOK, app.H{"cities": cities}) + h.ToJSON(w, http.StatusOK, pkg.H{"cities": cities}) } func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) { @@ -100,18 +99,18 @@ func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) { if err := params.Validate(); err != nil { slog.Error("error validating struct", "error", err) - h.ToJSON(w, http.StatusBadRequest, app.H{"error": err.Error()}) + h.ToJSON(w, http.StatusBadRequest, pkg.H{"error": err.Error()}) return } meteoData, err := h.s.GetMeteoData(r.Context(), params) if err != nil { slog.Error(ErrReadingData.Error(), "error", err) - h.ToJSON(w, http.StatusNotFound, app.H{"error": ErrReadingData.Error()}) + h.ToJSON(w, http.StatusNotFound, pkg.H{"error": ErrReadingData.Error()}) return } slog.Info("data retrieved", "location", params.Location) - h.ToJSON(w, http.StatusOK, app.H{"meteo_data": meteoData}) + h.ToJSON(w, http.StatusOK, pkg.H{"meteo_data": meteoData}) } diff --git a/service_b/internal/app/app.go b/service_b/internal/app/app.go index 93a6d9e..4879f7a 100644 --- a/service_b/internal/app/app.go +++ b/service_b/internal/app/app.go @@ -1,3 +1 @@ package app - -type H map[string]any diff --git a/service_b/internal/domains/meteo/handlers.go b/service_b/internal/domains/meteo/handlers.go index 485251f..16b0bbb 100644 --- a/service_b/internal/domains/meteo/handlers.go +++ b/service_b/internal/domains/meteo/handlers.go @@ -4,7 +4,6 @@ import ( "log/slog" "net/http" "pkg" - "serviceb/internal/app" ) type Handler struct { @@ -32,14 +31,14 @@ func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) { if err := params.Validate(); err != nil { slog.Error("error validating struct", "error", err) - h.ToJSON(w, http.StatusBadRequest, app.H{"error": err.Error()}) + h.ToJSON(w, http.StatusBadRequest, pkg.H{"error": err.Error()}) return } data, err := h.s.GetWeatherByCity(r.Context(), params) if err != nil { slog.Error("error", "err", err) - h.ToJSON(w, http.StatusInternalServerError, app.H{"error": err}) + h.ToJSON(w, http.StatusInternalServerError, pkg.H{"error": err}) return }