Home > Backend Development > Golang > Go Transformers

Go Transformers

DDD
Release: 2024-11-15 16:10:03
Original
679 people have browsed it

Go Transformers

Go-Utils

go-utils is a library that is aimed provide useful libraries in go to reduce the developer efforts on building stuffs and increasing
productivity.

Few functionalities are mentioned below.

Task Runner

The basic problem that this is trying to solve is how you want to run multiple tasks based on a request that you recieved.
Consider the following scenario.

  • processA processes one scenario and can produce error.
  • processB processes second scenario and can produce error.

As you can see all these process can result in an error. Inherently Golang is very verbose in error handling. Sometimes we don't want to see that redundant code.
Also this reduces the readability.

Now consider another scenario -
Once you handle the error for processA and you forgot for processB Golang wont throw a compile time error causing you to miss this case. A small miss can cause havoc. Though we are supposed to follow a lot of process before shipping to prod ask yourselves do you follow always or not?

To solve this I have developed an approach where you will be
more focussed on writing what matters and how easy would it be to look at a fn and understand what it does. This will also help in overcoming variable shadowing. Instances where we have multiple errors being assinged an error shadowing can occur and this can be bad. Following this pattern and right coding can help in avoiding such weird scenarios.

Examples -

A code with redundant error handling and reduced readability.

func FooBar() error {
    req := struct{
        isFoo bool
        isBar bool
    }{}
    ctx := context.TODO()
    err := processFoo(ctx, &req)
    if err != nil {
        return err
    }
    err = processBar(ctx, &req)
    if err != nil {
        return err
    }
    return nil
}
Copy after login
Copy after login

A code with the task runner

func FooBar() error {
    req := struct{
        isFoo bool
        isBar bool
    }{}
    ctx := context.TODO()
    runner := NewSimpleTaskRunner(ctx, req)
    _, err := runner.
        Then(processFoo).
        Then(processBar).
        Result()
    return err
}
Copy after login
Copy after login

As you can observe how better the code is readable and executable. This thought process and framework can improve the readability of the code.

Go-routine Enthusiasts

func FooBar() error {
    req := struct{
        isFoo bool
        isBar bool
    }{}
    ctx := context.TODO()
    runner := NewSimpleTaskRunner(ctx, req)
    _, err := runner.
        Parallel(processFooParallel).
        Parallel(processBarParallel).
        Result()
    return err
}
Copy after login
Copy after login

Stream Utils

We all know the famous lambdas and arrow functions. Golang
inherently doesnt support the arrow syntax. It would be nice to have
that in golang. For now suppose we need to do some Map operation that
is were things get hard. Well you are in for a cool implementation
from me to solve that for you. After this below implementation I would
ask you to think a soln of your own how this would have been implemented.

func TestMapRunner(t *testing.T) {
    // Create a map with some values
    floatingStrings := []string{"0.1", "0.2", "22", "22.1"}

    res, err := NewTransformer[string, float64](floatingStrings).
        Map(MapIt[string, float64](func(item string) (float64, error) { return strconv.ParseFloat(item, 64) })).
        Map(MapIt[float64, float64](func(item float64) (float64, error) { return item * 10, nil })).
        Result()
    if err != nil {
        t.Errorf("Testcase failed with error : %v", err)
        return
    }
    // Output: [0.1 0.2 22 22.1]
    t.Logf("Result: %v", res)
    assert.ElementsMatch(t, []any{float64(1), float64(2), float64(220), float64(221)}, res)

}
Copy after login

The above example is for a conversion of string to float64.
This will handle the errors for you if there are any. The only exception will be there can be runtime errors if there is any
Type Cast Issues So be careful with this. Try to write the testcases
which should avoid this issue.

Filter and Mapper Deadly Combo

An addition to the functionality is made now, filteration also works.
Happy time folks!!

func FooBar() error {
    req := struct{
        isFoo bool
        isBar bool
    }{}
    ctx := context.TODO()
    err := processFoo(ctx, &req)
    if err != nil {
        return err
    }
    err = processBar(ctx, &req)
    if err != nil {
        return err
    }
    return nil
}
Copy after login
Copy after login

Import library to your project to build cool stuff.

go get -u github.com/mahadev-k/go-utils@v1.0.1

Add this to your go.mod.
Use it as done in the examples module.

func FooBar() error {
    req := struct{
        isFoo bool
        isBar bool
    }{}
    ctx := context.TODO()
    runner := NewSimpleTaskRunner(ctx, req)
    _, err := runner.
        Then(processFoo).
        Then(processBar).
        Result()
    return err
}
Copy after login
Copy after login

Simple example

func FooBar() error {
    req := struct{
        isFoo bool
        isBar bool
    }{}
    ctx := context.TODO()
    runner := NewSimpleTaskRunner(ctx, req)
    _, err := runner.
        Parallel(processFooParallel).
        Parallel(processBarParallel).
        Result()
    return err
}
Copy after login
Copy after login

Checkout the repo:
Github: https://github.com/mahadev-k/go-utils

Considering you had read this through and liked it. If you would like to connect with me
Follow me on X - https://x.com/mahadev_k_
Linkedin - https://in.linkedin.com/in/mahadev-k-934520223
Do propose more ideas for contributions to this repo if interested.
Thank you ??

The above is the detailed content of Go Transformers. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template