diff --git a/app/v1/friend.go b/app/v1/friend.go index 4617571..e0e4acc 100644 --- a/app/v1/friend.go +++ b/app/v1/friend.go @@ -2,7 +2,6 @@ package v1 import ( "database/sql" - "encoding/json" "strconv" "time" @@ -149,7 +148,7 @@ type friendAddPOSTData struct { // FriendsAddPOST allows for adding friends. Yup. Easy as that. func FriendsAddPOST(md common.MethodData) (r common.Response) { d := friendAddPOSTData{} - err := json.Unmarshal(md.RequestData, &d) + err := md.RequestData.Unmarshal(&d) if err != nil { md.Err(err) r = Err500 @@ -220,7 +219,7 @@ func FriendsDelGET(md common.MethodData) common.Response { // FriendsDelPOST allows for deleting friends. func FriendsDelPOST(md common.MethodData) (r common.Response) { d := friendAddPOSTData{} - err := json.Unmarshal(md.RequestData, &d) + err := md.RequestData.Unmarshal(&d) if err != nil { md.Err(err) r = Err500 diff --git a/app/v1/token.go b/app/v1/token.go index 1f9cf1a..64608d2 100644 --- a/app/v1/token.go +++ b/app/v1/token.go @@ -3,7 +3,6 @@ package v1 import ( "crypto/md5" "database/sql" - "encoding/json" "fmt" "github.com/osuripple/api/common" @@ -31,7 +30,7 @@ type tokenNewOutData struct { // TokenNewPOST is the handler for POST /token/new. func TokenNewPOST(md common.MethodData) (r common.Response) { data := tokenNewInData{} - err := json.Unmarshal(md.RequestData, &data) + err := md.RequestData.Unmarshal(&data) if err != nil { r = ErrBadJSON return diff --git a/common/method_data.go b/common/method_data.go index ba819d1..7d4159f 100644 --- a/common/method_data.go +++ b/common/method_data.go @@ -2,6 +2,7 @@ package common import ( "database/sql" + "encoding/json" "github.com/gin-gonic/gin" ) @@ -10,7 +11,7 @@ import ( type MethodData struct { User Token DB *sql.DB - RequestData []byte + RequestData RequestData C *gin.Context } @@ -23,3 +24,14 @@ func (md MethodData) Err(err error) { func (md MethodData) ID() int { return md.User.UserID } + +// RequestData is the body of a request. It is wrapped into this type +// to implement the Unmarshal function, which is just a shorthand to +// json.Unmarshal. +type RequestData []byte + +// Unmarshal json-decodes Requestdata into a value. Basically a +// shorthand to json.Unmarshal. +func (r RequestData) Unmarshal(into interface{}) error { + return json.Unmarshal([]byte(r), into) +}