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

feat: support ftl.Map on injected resources #3153

Open
wants to merge 3 commits into
base: worstell/20240930-database-resources
Choose a base branch
from

Conversation

worstell
Copy link
Contributor

@worstell worstell commented Oct 18, 2024

embed the FTL handle that needs mapping in a new mapper type, e.g.:

type DBMapper struct {
     ftl.DatabaseHandle[MyConfig]
}

then implement a map function on it:

func (DBMapper) Map(ctx context.Context, conn *sql.DB) (NewType, error) {
     return NewType{db}, nil
}

such that your new type implements this FTL interface:

type ResourceMapper[From, To any] interface {
     Handle[From]
     Map(ctx context.Context, f From) (To, error)
}

Then, you can plug your mapper into a verb signature via ftl.MappedHandle, e.g.:

//ftl:verb
func Insert(ctx context.Context, req Request, db ftl.MappedHandle[DBMapper, *sql.DB, NewType]) (Response, error) {
     ...
}

also fixes #2941

@worstell worstell requested review from a team and alecthomas as code owners October 18, 2024 00:03
@worstell worstell requested review from gak and matt2e and removed request for a team October 18, 2024 00:03
This was referenced Oct 17, 2024
@@ -34,22 +34,7 @@ func init() {
reflection.ProvideResourcesForVerb(
{{.TypeName}},
{{- range .Resources}}
{{- with getVerbClient . }}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer to leave these in the template for readability, but with the new MappedHandle type, we need to recurse back into this logic to get the string for the resource that will be mapped - so pulled it out into a Go function

@worstell worstell force-pushed the worstell/20241011-resource-mapper branch from ab0a4d2 to 4d9ad16 Compare October 18, 2024 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant