Skip to content

Commit

Permalink
Don't use pointers when you need them not!
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio committed Aug 31, 2018
1 parent 1370ca6 commit 122ed3a
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 112 deletions.
14 changes: 7 additions & 7 deletions client/utils/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ const (

// WriteErrorResponse prepares and writes a HTTP error
// given a status code and an error message.
func WriteErrorResponse(w *http.ResponseWriter, status int, msg string) {
(*w).WriteHeader(status)
(*w).Write([]byte(msg))
func WriteErrorResponse(w http.ResponseWriter, status int, msg string) {
w.WriteHeader(status)
w.Write([]byte(msg))
}

// WriteGasEstimateResponse prepares and writes an HTTP
// response for transactions simulations.
func WriteSimulationResponse(w *http.ResponseWriter, gas int64) {
(*w).WriteHeader(http.StatusOK)
(*w).Write([]byte(fmt.Sprintf(`{"gas_estimate":%v}`, gas)))
func WriteSimulationResponse(w http.ResponseWriter, gas int64) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf(`{"gas_estimate":%v}`, gas)))
}

// HasDryRunArg returns true if the request's URL query contains
Expand All @@ -32,7 +32,7 @@ func HasDryRunArg(r *http.Request) bool {

// ParseFloat64OrReturnBadRequest converts s to a float64 value. It returns a default
// value if the string is empty. Write
func ParseFloat64OrReturnBadRequest(w *http.ResponseWriter, s string, defaultIfEmpty float64) (n float64, ok bool) {
func ParseFloat64OrReturnBadRequest(w http.ResponseWriter, s string, defaultIfEmpty float64) (n float64, ok bool) {
if len(s) == 0 {
return defaultIfEmpty, true
}
Expand Down
8 changes: 4 additions & 4 deletions x/auth/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func QueryAccountRequestHandlerFn(

addr, err := sdk.AccAddressFromBech32(bech32addr)
if err != nil {
utils.WriteErrorResponse(&w, http.StatusInternalServerError, err.Error())
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

res, err := cliCtx.QueryStore(auth.AddressStoreKey(addr), storeName)
if err != nil {
utils.WriteErrorResponse(&w, http.StatusInternalServerError, fmt.Sprintf("couldn't query account. Error: %s", err.Error()))
utils.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't query account. Error: %s", err.Error()))
return
}

Expand All @@ -52,14 +52,14 @@ func QueryAccountRequestHandlerFn(
// decode the value
account, err := decoder(res)
if err != nil {
utils.WriteErrorResponse(&w, http.StatusInternalServerError, fmt.Sprintf("couldn't parse query result. Result: %s. Error: %s", res, err.Error()))
utils.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't parse query result. Result: %s. Error: %s", res, err.Error()))
return
}

// print out whole account
output, err := cdc.MarshalJSON(account)
if err != nil {
utils.WriteErrorResponse(&w, http.StatusInternalServerError, fmt.Sprintf("couldn't marshall query result. Error: %s", err.Error()))
utils.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't marshall query result. Error: %s", err.Error()))
return
}

Expand Down
22 changes: 11 additions & 11 deletions x/bank/client/rest/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,32 @@ func SendRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx context.CLICo

to, err := sdk.AccAddressFromBech32(bech32addr)
if err != nil {
utils.WriteErrorResponse(&w, http.StatusBadRequest, err.Error())
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

var m sendBody
body, err := ioutil.ReadAll(r.Body)
if err != nil {
utils.WriteErrorResponse(&w, http.StatusBadRequest, err.Error())
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
err = msgCdc.UnmarshalJSON(body, &m)
if err != nil {
utils.WriteErrorResponse(&w, http.StatusBadRequest, err.Error())
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

info, err := kb.Get(m.LocalAccountName)
if err != nil {
utils.WriteErrorResponse(&w, http.StatusUnauthorized, err.Error())
utils.WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
return
}

// build message
msg := client.BuildMsg(sdk.AccAddress(info.GetPubKey().Address()), to, m.Amount)
if err != nil { // XXX rechecking same error ?
utils.WriteErrorResponse(&w, http.StatusInternalServerError, err.Error())
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

Expand All @@ -88,7 +88,7 @@ func SendRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx context.CLICo
Sequence: m.Sequence,
}

adjustment, ok := utils.ParseFloat64OrReturnBadRequest(&w, m.GasAdjustment, cliclient.DefaultGasAdjustment)
adjustment, ok := utils.ParseFloat64OrReturnBadRequest(w, m.GasAdjustment, cliclient.DefaultGasAdjustment)
if !ok {
return
}
Expand All @@ -97,31 +97,31 @@ func SendRequestHandlerFn(cdc *wire.Codec, kb keys.Keybase, cliCtx context.CLICo
if utils.HasDryRunArg(r) || m.Gas == 0 {
newCtx, err := utils.EnrichCtxWithGas(txCtx, cliCtx, m.LocalAccountName, []sdk.Msg{msg})
if err != nil {
utils.WriteErrorResponse(&w, http.StatusInternalServerError, err.Error())
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
if utils.HasDryRunArg(r) {
utils.WriteSimulationResponse(&w, txCtx.Gas)
utils.WriteSimulationResponse(w, txCtx.Gas)
return
}
txCtx = newCtx
}

txBytes, err := txCtx.BuildAndSign(m.LocalAccountName, m.Password, []sdk.Msg{msg})
if err != nil {
utils.WriteErrorResponse(&w, http.StatusUnauthorized, err.Error())
utils.WriteErrorResponse(w, http.StatusUnauthorized, err.Error())
return
}

res, err := cliCtx.BroadcastTx(txBytes)
if err != nil {
utils.WriteErrorResponse(&w, http.StatusInternalServerError, err.Error())
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

output, err := wire.MarshalJSONIndent(cdc, res)
if err != nil {
utils.WriteErrorResponse(&w, http.StatusInternalServerError, err.Error())
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

Expand Down
Loading

0 comments on commit 122ed3a

Please sign in to comment.