DEV Community

Cover image for Go Transformers
Mahadev K
Mahadev K

Posted on

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
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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)

}
Enter fullscreen mode Exit fullscreen mode

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 TestFilterIt(t *testing.T) {
    // Create a map with some values
    floatingStrings := []string{"0.1", "0.2", "22", "22.1"}

    res, err := NewTransformer[string, int64](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 })).
        Map(MapIt[float64, int64](func(item float64) (int64, error) { return int64(item), nil })).
        Map(FilterIt[int64](func(item int64) (bool, error) { return item%2 == 0, nil })).
        Result()
    if err != nil {
        t.Errorf("Testcase failed with error : %v", err)
        return
    }
    // Output: [2 220]
    t.Logf("Result: %v", res)
    assert.ElementsMatch(t, []any{int64(2), int64(220)}, res)   
}
Enter fullscreen mode Exit fullscreen mode

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.

module examples

go 1.23.2

require github.com/stretchr/testify v1.9.0

require (
    github.com/davecgh/go-spew v1.1.1 // indirect
    github.com/mahadev-k/go-utils v1.0.1 // indirect *go-utils*
    github.com/pmezard/go-difflib v1.0.0 // indirect
    gopkg.in/yaml.v3 v3.0.1 // indirect
)
Enter fullscreen mode Exit fullscreen mode

Simple example

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

    res, err := streams.NewTransformer[string, int64](floatingStrings).
        Map(streams.MapIt[string, float64](func(item string) (float64, error) { return strconv.ParseFloat(item, 64) })).
        Map(streams.MapIt[float64, float64](func(item float64) (float64, error) { return item * 10, nil })).
        Map(streams.MapIt[float64, int64](func(item float64) (int64, error) { return int64(item), nil })).
        Map(streams.FilterIt[int64](func(item int64) (bool, error) { return item%2 == 0, nil })).
        Result()
    if err != nil {
        t.Errorf("Testcase failed with error : %v", err)
        return
    }
    // Output: [2 220]
    t.Logf("Result: %v", res)
    assert.ElementsMatch(t, []any{int64(2), int64(220)}, res)
}
Enter fullscreen mode Exit fullscreen mode

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 🎉🥳

Top comments (1)

Collapse
 
v4vivekss profile image
Vivek. S. S

Interesting read !