New golang package to reduce code duplication in APIs

Hi Golang developers!

I just released a new package that will help you develop APIs in go.

Look at this code :

func (a *API) handleChangePassword(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
userID := vars[“userID”]


This content originally appeared on DEV Community and was authored by Alexis Viscogliosi

Hi Golang developers!

I just released a new package that will help you develop APIs in go.

Look at this code :

func (a *API) handleChangePassword(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)
    userID := vars["userID"]

    requestBody, err := ioutil.ReadAll(r.Body)
    if err != nil {
        errorResponse(w, http.StatusInternalServerError, "", err) // <-- your custom error logic that you write for 
                                                                  //      each project
        return
    }

    var requestData ChangePasswordRequest
    if err := json.Unmarshal(requestBody, &requestData); err != nil {
        errorResponse(w, http.StatusInternalServerError, "", err)
        return
    }

    if err = requestData.IsValid(); err != nil {
        errorResponse(w, http.StatusBadRequest, err.Error(), err)
        return
    }

    if err = a.app().ChangePassword(userID, requestData.OldPassword, requestData.NewPassword); err != nil {
        errorResponse(w, http.StatusBadRequest, err.Error(), err)
        return
    }

    jsonStringResponse(w, http.StatusOK, "{}") // <-- your custom code that your are write for each project
}

This is often the code I see on Go projects. There is nothing wrong with writing this because it is very efficient and declarative.

But sometimes we would still like to focus a little more on what matters: business logic.

The version with KCD of this code now:

type ChangePasswordRequest struct {
    UserID string `path:"userID"`
    // ...
}

func (c *ChangePasswordRequest) Validate() error {
    // ...
}

func (a *API) handleChangePassword(request *ChangePasswordRequest) error {
    if err = a.app().ChangePassword(request.UserID, request.OldPassword, request.NewPassword); err != nil {
        return errors.Wrap(err, "unable to change password").WithKind(errors.KindInvalidArgument)
    }

    return nil
}

As you can see the code is more concise and has less boilerplate, it is not more efficient because kcd uses reflection to achieve this simplicity.

If you want to play with this new toy this is the link of the package https://github.com/alexisvisco/kcd


This content originally appeared on DEV Community and was authored by Alexis Viscogliosi


Print Share Comment Cite Upload Translate Updates
APA

Alexis Viscogliosi | Sciencx (2021-05-01T08:56:58+00:00) New golang package to reduce code duplication in APIs. Retrieved from https://www.scien.cx/2021/05/01/new-golang-package-to-reduce-code-duplication-in-apis/

MLA
" » New golang package to reduce code duplication in APIs." Alexis Viscogliosi | Sciencx - Saturday May 1, 2021, https://www.scien.cx/2021/05/01/new-golang-package-to-reduce-code-duplication-in-apis/
HARVARD
Alexis Viscogliosi | Sciencx Saturday May 1, 2021 » New golang package to reduce code duplication in APIs., viewed ,<https://www.scien.cx/2021/05/01/new-golang-package-to-reduce-code-duplication-in-apis/>
VANCOUVER
Alexis Viscogliosi | Sciencx - » New golang package to reduce code duplication in APIs. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/01/new-golang-package-to-reduce-code-duplication-in-apis/
CHICAGO
" » New golang package to reduce code duplication in APIs." Alexis Viscogliosi | Sciencx - Accessed . https://www.scien.cx/2021/05/01/new-golang-package-to-reduce-code-duplication-in-apis/
IEEE
" » New golang package to reduce code duplication in APIs." Alexis Viscogliosi | Sciencx [Online]. Available: https://www.scien.cx/2021/05/01/new-golang-package-to-reduce-code-duplication-in-apis/. [Accessed: ]
rf:citation
» New golang package to reduce code duplication in APIs | Alexis Viscogliosi | Sciencx | https://www.scien.cx/2021/05/01/new-golang-package-to-reduce-code-duplication-in-apis/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.