MCPcopy Create free account
hub / github.com/pace/bricks / Unmarshal

Function Unmarshal

http/jsonapi/runtime/marshalling.go:19–52  ·  view source on GitHub ↗

Unmarshal processes the request content and fills passed data struct with the correct jsonapi content. After un-marshaling the struct will be validated with specified go-validator struct tags. In case of an error, an jsonapi error message will be directly send to the client

(w http.ResponseWriter, r *http.Request, data interface{})

Source from the content-addressed store, hash-verified

17// specified go-validator struct tags.
18// In case of an error, an jsonapi error message will be directly send to the client
19func Unmarshal(w http.ResponseWriter, r *http.Request, data interface{}) bool {
20 // don't leak , but error can't be handled
21 defer r.Body.Close() // nolint: errcheck
22
23 // verify that the client accepts our response
24 // Note: logically this would be done before marshalling,
25 // to prevent stale backend/frontend state we respond before
26 // Additionally, marshal has no access to the request struct
27 accept := r.Header.Get("Accept")
28 if accept != JSONAPIContentType {
29 WriteError(w, http.StatusNotAcceptable,
30 fmt.Errorf("request needs to be send with %q header, containing value: %q", "Accept", JSONAPIContentType))
31 return false
32 }
33
34 // if the client didn't send a content type, don't verify
35 contentType := r.Header.Get("Content-Type")
36 if contentType != JSONAPIContentType {
37 WriteError(w, http.StatusUnsupportedMediaType,
38 fmt.Errorf("request needs to be send with %q header, containing value: %q", "Content-Type", JSONAPIContentType))
39 return false
40 }
41
42 // parse request
43 err := jsonapi.UnmarshalPayload(r.Body, data)
44 if err != nil {
45 WriteError(w, http.StatusUnprocessableEntity,
46 fmt.Errorf("can't parse content: %v", err))
47 return false
48 }
49
50 // validate request
51 return ValidateRequest(w, r, data)
52}
53
54// UnmarshalMany processes the request content that has an array of objects and fills passed data struct with the
55// correct jsonapi content. After un-marshaling the struct will be validated with

Callers 15

ProcessPaymentHandlerFunction · 0.92
DeduplicatePoiHandlerFunction · 0.92
MovePoiAtPositionHandlerFunction · 0.92
CreateAppHandlerFunction · 0.92
UpdateAppHandlerFunction · 0.92
ChangePoiHandlerFunction · 0.92
CreatePolicyHandlerFunction · 0.92
CreateSourceHandlerFunction · 0.92
UpdateSourceHandlerFunction · 0.92

Calls 6

UnmarshalPayloadFunction · 0.92
WriteErrorFunction · 0.85
ValidateRequestFunction · 0.85
CloseMethod · 0.80
ErrorfMethod · 0.80
GetMethod · 0.65

Tested by 15

ProcessPaymentHandlerFunction · 0.74
DeduplicatePoiHandlerFunction · 0.74
MovePoiAtPositionHandlerFunction · 0.74
CreateAppHandlerFunction · 0.74
UpdateAppHandlerFunction · 0.74
ChangePoiHandlerFunction · 0.74
CreatePolicyHandlerFunction · 0.74
CreateSourceHandlerFunction · 0.74
UpdateSourceHandlerFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…