Skip to content

Transforming return type when using map #66

@Luviz

Description

@Luviz

Hi I have been looking to use this package in my solution however every time I have tried to so I feel like something is missing.

In our solution we want to use Options and Results every time we are uncertain of the return value.

I do use monads in python and node (typescript) and it's great there is a a lot that we can do with elegantly, and I do like MarshalJSON function that you have implemented.

My issue with mo is that when we need to map a type to another type, see example below.

this code is more like a pseudocode

type UserRequestForm struct {
	Email     string
	FirstName string
	LastName  string
	Address   string // can be empty
	Title     string // can be empty
}

type User struct {
	Id        string // uuid/guid
	Email     string
	Name      string
	FirstName string
	LastName  string
	Address   string // can be empty
	Title     string // can be empty
}

func (data UserRequestForm) TDO() User {
	return User{
		Id:        uuidx.NewV4().String(),
		Email:     data.Email,
		Name:      fmt.Sprintf("%v %v", data.FirstName, data.LastName),
		FirstName: data.FirstName,
		LastName:  data.LastName,
		Address:   data.Address,
		Title:     data.Title,
	}
}

func UserRequestFormToDTO(value UserRequestForm) mo.Option[User] {
	return mo.Some(value.TDO())
}

Here I want to get a option of UserRequestForm and if it has a value I wan't to convert it to a TDO and push it to a DB.

So something looking like this:

func CreateUser(user mo.Option[UserRequestForm]) error {
	return user.FlatMap(UserRequestFormToDTO).Match(
		func(value User) {
			db.CreateUser(value) // Value should be of type User
		},
		func() (error) {
			return fmt.Errorf("Form data could to be converted to User database object ")
		})
}

I'm kind new to GO thereby I might be a bit ignorerat, so there might be that writing something like the code above is might not be a good idea.
However I know that there is good support generics and type inference in GO. So I don't see any issues with creating functions looking like this:

func Map[T any, R any](value T, callback func(T) R) R {
	return callback(value)
}

func main() {
       u := Map(data, UserRequestFormToDTO) // typeof u is User
}

Here is my question, is there a reson why the Map and FlatMap function is hard code as generic. Or is just that you haven't implemented support for it yet, and in that case can I try to make PR?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions