summaryrefslogtreecommitdiff
path: root/view
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-03-03 07:56:39 +0100
committerxengineering <me@xengineering.eu>2024-03-03 13:50:58 +0100
commit110f9db31a2a2b72031292558f7b1d5bab7649c3 (patch)
treef34d8bb30322e40a696dd6f601a65520f78e6686 /view
parent38ee758c3d553faae683169eb74f2e6de0ab4207 (diff)
downloadceres-110f9db31a2a2b72031292558f7b1d5bab7649c3.tar
ceres-110f9db31a2a2b72031292558f7b1d5bab7649c3.tar.zst
ceres-110f9db31a2a2b72031292558f7b1d5bab7649c3.zip
model: Use only string types for models
When a HTML form is converted to JSON by JavaScript using `FormData()`, `Object.fromEntries()` and `JSON.stringify` the data type is always `string`. This does not match the Go struct definitions using multiple types including e.g. `int`. There are several options to solve this conflict: 1. use only strings in Go struct definitions 2. write custom functions to parse string-based JSONs to Go structs 3. implement custom functions in JS to use `number` type if possible Option 3 seems to be a very clean solution. Nevertheless it is limited by the fact that JSON anyway has a way more limited type system than Go. So the types used in Go cannot be used and this would reduce this option to a variant of option 2. Option 2 requires significant effort per struct inside the model package. Every object which is transferred via JSON and serialized into Go structs would require a second struct definition with string types and a conversion function. This does not scale. Thus option 1 seems to be the best fit. The reasons for using types like `int` or `bool` are: - less memory consumption than `string` in most cases - implicit data validation (e.g. enforcing positive numbers with `uint`) - better compatibility with certain APIs which rely on e.g. `int` The first argument is not so relevant in this use case. The amount of required memory is still quite small for servers. Implicit data validation is a good thing but not enough. There should anyway be validation method which has to be called on CRUD methods and JSON deserialization.
Diffstat (limited to 'view')
-rw-r--r--view/recipe.go6
1 files changed, 1 insertions, 5 deletions
diff --git a/view/recipe.go b/view/recipe.go
index b0525a1..1dd6045 100644
--- a/view/recipe.go
+++ b/view/recipe.go
@@ -2,7 +2,6 @@ package view
import (
"net/http"
- "strconv"
"xengineering.eu/ceres/model"
@@ -10,11 +9,8 @@ import (
)
func Recipe(w http.ResponseWriter, r *http.Request) {
- id_str := mux.Vars(r)[`id`]
- id, _ := strconv.Atoi(id_str)
-
recipe := model.Recipe{}
- recipe.Id = int64(id)
+ recipe.Id = mux.Vars(r)[`id`]
err := recipe.Read()
if err != nil {