Skip to content

Commit d6e304f

Browse files
authored
chore: simplify unmarshaling (#25010)
1 parent 4a275b4 commit d6e304f

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

server/http_session_handler.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,22 @@ func updateSessionHandler(w http.ResponseWriter, r *http.Request) {
119119

120120
id := mux.Vars(r)["id"]
121121

122-
var data map[string]any
123-
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
124-
jsonError(w, http.StatusBadRequest, errors.New("invalid JSON"))
125-
return
122+
var data struct {
123+
Vehicle, Loadpoint *string
126124
}
127125

128-
updates := map[string]any{}
129-
for _, field := range []string{"vehicle", "loadpoint"} {
130-
if val, ok := data[field]; ok {
131-
updates[field] = val
132-
}
126+
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
127+
jsonError(w, http.StatusBadRequest, err)
128+
return
133129
}
134130

135-
if len(updates) == 0 {
136-
jsonError(w, http.StatusBadRequest, errors.New("no valid fields to update"))
131+
if data.Vehicle == nil && data.Loadpoint == nil {
132+
jsonError(w, http.StatusBadRequest, errors.New("nothing to update"))
137133
return
138134
}
139135

140136
// https://github.com/evcc-io/evcc/issues/13738#issuecomment-2094070362
141-
if txn := db.Instance.Table("sessions").Where("id = ?", id).Updates(updates); txn.Error != nil {
137+
if txn := db.Instance.Table("sessions").Where("id = ?", id).Updates(data); txn.Error != nil {
142138
jsonError(w, http.StatusBadRequest, txn.Error)
143139
return
144140
}

0 commit comments

Comments
 (0)