Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping context values #68

Open
jmattheis opened this issue Mar 13, 2023 · 2 comments
Open

Mapping context values #68

jmattheis opened this issue Mar 13, 2023 · 2 comments
Labels
feature New feature or request

Comments

@jmattheis
Copy link
Owner

jmattheis commented Mar 13, 2023

Goverter should allow passing a mapping context through converter methods for custom conversions. This allows more customizability when the conversion requires other state. See also https://mapstruct.org/documentation/stable/api/org/mapstruct/Context.html

Example:

// goverter:converter
// goverter:extend LookupUser
type Converter interface {
    Convert(source Input, context UserContext) (Output, error)
}

type UserContext struct {
    UserNames map[int]string
}

func LookupUser(userID int, context UserContext) (User, error) {
    user := User{ID: userID}
    name, ok := context.UserNames[userID]
    if !ok {
        return user, fmt.Errorf("user with id %d not found", userID)
    }
    user.Name = name
    return user, nil
}

type Input struct {  UserIDs []int }
type Output struct { Users []User  }
type User struct {
    ID int
    Name string
}

Conversion contexts are additional parameters that are passed into the conversion methods (See Convert method from Converter), and they should be passed into custom methods (See LookupUser).

Please 👍 this issue if you like this functionality. If you have a specific use-case in mind, feel free to comment it.

@pavelpatrin
Copy link
Contributor

@jmattheis can I hope that this feature will be implemented once upon a time?

@jmattheis
Copy link
Owner Author

@pavelpatrin Yes, but it doesn't have a high priority on my to-do list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants