Skip to content

Commit c1eb8a6

Browse files
committed
incus-osd/rest: Cleanup some json unmarshalling logic
Signed-off-by: Mathias Gibbens <mathias.gibbens@futurfusion.io>
1 parent 3cc852e commit c1eb8a6

File tree

2 files changed

+10
-47
lines changed

2 files changed

+10
-47
lines changed

incus-osd/internal/rest/api_system_security.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package rest
33
import (
44
"encoding/json"
55
"errors"
6-
"io"
76
"net/http"
87
"slices"
98

@@ -59,16 +58,9 @@ func (s *Server) apiSystemSecurity(w http.ResponseWriter, r *http.Request) {
5958
return
6059
}
6160

62-
b, err := io.ReadAll(r.Body)
63-
if err != nil {
64-
_ = response.InternalError(err).Render(w)
65-
66-
return
67-
}
68-
69-
securityStruct := api.SystemSecurity{}
61+
securityStruct := &api.SystemSecurity{}
7062

71-
err = json.Unmarshal(b, &securityStruct)
63+
err := json.NewDecoder(r.Body).Decode(securityStruct)
7264
if err != nil {
7365
_ = response.BadRequest(err).Render(w)
7466

incus-osd/internal/rest/api_system_storage.go

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package rest
33
import (
44
"encoding/json"
55
"errors"
6-
"io"
76
"net/http"
87

98
"github.com/lxc/incus-os/incus-osd/api"
@@ -34,16 +33,9 @@ func (s *Server) apiSystemStorage(w http.ResponseWriter, r *http.Request) {
3433
return
3534
}
3635

37-
b, err := io.ReadAll(r.Body)
38-
if err != nil {
39-
_ = response.InternalError(err).Render(w)
40-
41-
return
42-
}
43-
44-
storageStruct := api.SystemStorage{}
36+
storageStruct := &api.SystemStorage{}
4537

46-
err = json.Unmarshal(b, &storageStruct)
38+
err := json.NewDecoder(r.Body).Decode(storageStruct)
4739
if err != nil {
4840
_ = response.BadRequest(err).Render(w)
4941

@@ -95,20 +87,13 @@ func (*Server) apiSystemStorageDeletePool(w http.ResponseWriter, r *http.Request
9587
return
9688
}
9789

98-
b, err := io.ReadAll(r.Body)
99-
if err != nil {
100-
_ = response.InternalError(err).Render(w)
101-
102-
return
103-
}
104-
10590
type deleteStruct struct {
10691
Name string `json:"name"`
10792
}
10893

109-
config := deleteStruct{}
94+
config := &deleteStruct{}
11095

111-
err = json.Unmarshal(b, &config)
96+
err := json.NewDecoder(r.Body).Decode(config)
11297
if err != nil {
11398
_ = response.BadRequest(err).Render(w)
11499

@@ -148,16 +133,9 @@ func (*Server) apiSystemStorageWipeDrive(w http.ResponseWriter, r *http.Request)
148133
return
149134
}
150135

151-
b, err := io.ReadAll(r.Body)
152-
if err != nil {
153-
_ = response.InternalError(err).Render(w)
154-
155-
return
156-
}
157-
158-
wipeStruct := api.SystemStorageWipe{}
136+
wipeStruct := &api.SystemStorageWipe{}
159137

160-
err = json.Unmarshal(b, &wipeStruct)
138+
err := json.NewDecoder(r.Body).Decode(wipeStruct)
161139
if err != nil {
162140
_ = response.BadRequest(err).Render(w)
163141

@@ -196,16 +174,9 @@ func (*Server) apiSystemStorageImportEncryptionKey(w http.ResponseWriter, r *htt
196174
return
197175
}
198176

199-
b, err := io.ReadAll(r.Body)
200-
if err != nil {
201-
_ = response.InternalError(err).Render(w)
202-
203-
return
204-
}
205-
206-
poolStruct := api.SystemStoragePoolKey{}
177+
poolStruct := &api.SystemStoragePoolKey{}
207178

208-
err = json.Unmarshal(b, &poolStruct)
179+
err := json.NewDecoder(r.Body).Decode(poolStruct)
209180
if err != nil {
210181
_ = response.BadRequest(err).Render(w)
211182

0 commit comments

Comments
 (0)