aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-03-12 14:39:21 +0000
committerMatthew Lemon <y@yulqen.org>2024-03-12 14:40:06 +0000
commitcb1a75bd57d5a240a80f3fbcf980d386878c0530 (patch)
tree2015de9beed8d6b60cd386ef7daefdbb061e8fb8
parent9b2f5ce230900f799f54d872ffaf43790be55f14 (diff)
wip: Indent the JSON
-rw-r--r--cmd/api/datamaps.go16
-rw-r--r--cmd/api/helpers.go30
2 files changed, 43 insertions, 3 deletions
diff --git a/cmd/api/datamaps.go b/cmd/api/datamaps.go
index 3cec38e..5133911 100644
--- a/cmd/api/datamaps.go
+++ b/cmd/api/datamaps.go
@@ -8,12 +8,16 @@ import (
"time"
)
+// datamapLine holds the data parsed from each line of a submitted datamap CSV file.
+// The fields need to be exported otherwise they won't be included when encoding
+// the struct to json.
type datamapLine struct {
Key string `json:"key"`
Sheet string `json:"sheet"`
Cellref string `json:"cellref"`
}
+// datamap includes a slice of datamapLine objects alongside header metadata
type datamap struct {
Name string `json:"name"`
Description string `json:"description"`
@@ -29,7 +33,13 @@ func (app *application) createDatamapHandler(w http.ResponseWriter, r *http.Requ
return
}
- // get the uploaded file
+ // Get form values
+ dmName := r.FormValue("name")
+ app.logger.Info("obtain value from form", "name", dmName)
+ dmDesc := r.FormValue("description")
+ app.logger.Info("obtain value from form", "description", dmDesc)
+
+ // Get the uploaded file and name
file, _, err := r.FormFile("file")
if err != nil {
http.Error(w, "Missing file", http.StatusBadRequest)
@@ -62,9 +72,9 @@ func (app *application) createDatamapHandler(w http.ResponseWriter, r *http.Requ
Cellref: line[2],
})
}
- dm = datamap{Name: "test-datamap", Description: "test description", Created: time.Now(), DMLs: dmls}
+ dm = datamap{Name: dmName, Description: dmDesc, Created: time.Now(), DMLs: dmls}
- err = app.writeJSON(w, http.StatusOK, dm, nil)
+ err = app.writeJSONPretty(w, http.StatusOK, dm, nil)
if err != nil {
app.logger.Debug("writing out csv", "err", err)
http.Error(w, "Cannot write output from parsed CSV", http.StatusInternalServerError)
diff --git a/cmd/api/helpers.go b/cmd/api/helpers.go
index 77fbf0c..61aeb4f 100644
--- a/cmd/api/helpers.go
+++ b/cmd/api/helpers.go
@@ -5,6 +5,36 @@ import (
"net/http"
)
+// writeJSON)Pretty() helper for sending responses - pretty prints output. This takes the destination http.ResponseWriter, the
+// HTTP status code to sned, the data to encode to JSON and a header map containing any additional
+// HTTP headers we want to include in the response.
+func (app *application) writeJSONPretty(w http.ResponseWriter, status int, data any, headers http.Header) error {
+ // Encode the data to JSON, returing the error if there was one.
+ js, err := json.MarshalIndent(data, "", "\t")
+ if err != nil {
+ return err
+ }
+
+ // Append a newline to make it easier tro view in terminal applications
+ js = append(js, '\n')
+
+ // We know now that we won't encounter any more errors before writing the response,
+ // so it's safe to add any headers that we want to include. We loop through the
+ // header map and add each header to the http.ResponseWriter header map.
+ // Note that it's okay if the provided header map is nil. Go doesn't throw an error
+ // if you wanmt to try to range over (or more generally reader from) a nil map.
+ for key, value := range headers {
+ w.Header()[key] = value
+ }
+ // Add the "Content-Type: application/json" header, then write the status code and
+ // JSON response.
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(status)
+ w.Write(js)
+
+ return nil
+}
+
// writeJSON() helper for sending responses. This takes the destination http.ResponseWriter, the
// HTTP status code to sned, the data to encode to JSON and a header map containing any additional
// HTTP headers we want to include in the response.