-
Notifications
You must be signed in to change notification settings - Fork 0
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
Backend for Portfolio Groups #83
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package pactasrv | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/RMI/pacta/oapierr" | ||
"github.com/RMI/pacta/pacta" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (s *Server) populatePortfoliosInPortfolioGroups( | ||
ctx context.Context, | ||
ts []*pacta.PortfolioGroup, | ||
) error { | ||
getFn := func(pg *pacta.PortfolioGroup) ([]*pacta.Portfolio, error) { | ||
result := []*pacta.Portfolio{} | ||
for _, member := range pg.Members { | ||
result = append(result, member.Portfolio) | ||
} | ||
return result, nil | ||
} | ||
lookupFn := func(ids []pacta.PortfolioID) (map[pacta.PortfolioID]*pacta.Portfolio, error) { | ||
return s.DB.Portfolios(s.DB.NoTxn(ctx), ids) | ||
} | ||
getIDFn := func(p *pacta.Portfolio) pacta.PortfolioID { | ||
return p.ID | ||
} | ||
if err := populateAll(ts, getFn, getIDFn, lookupFn); err != nil { | ||
return oapierr.Internal("populating portfolios in portfolio groups failed", zap.Error(err)) | ||
} | ||
return nil | ||
} | ||
|
||
func (s *Server) populatePortfolioGroupsInPortfolios( | ||
ctx context.Context, | ||
ts []*pacta.Portfolio, | ||
) error { | ||
getFn := func(pg *pacta.Portfolio) ([]*pacta.PortfolioGroup, error) { | ||
result := []*pacta.PortfolioGroup{} | ||
for _, member := range pg.MemberOf { | ||
result = append(result, member.PortfolioGroup) | ||
} | ||
return result, nil | ||
} | ||
lookupFn := func(ids []pacta.PortfolioGroupID) (map[pacta.PortfolioGroupID]*pacta.PortfolioGroup, error) { | ||
return s.DB.PortfolioGroups(s.DB.NoTxn(ctx), ids) | ||
} | ||
getIDFn := func(p *pacta.PortfolioGroup) pacta.PortfolioGroupID { | ||
return p.ID | ||
} | ||
if err := populateAll(ts, getFn, getIDFn, lookupFn); err != nil { | ||
return oapierr.Internal("populating portfolio groups in portfolios failed", zap.Error(err)) | ||
} | ||
return nil | ||
} | ||
|
||
// This helper function populates the given targets in the given sources, | ||
// to allow for generic population of nested data structures. | ||
// sources = entities that you want to populate sub-entity references in. | ||
// the sub-entities should be pointers to structs with an ID populated. | ||
// getTargetsFn = function that takes a source and returns zero or more sub-entities to populate. | ||
// getTargetIDFn = function that takes a sub-entity and returns its ID. | ||
// lookupTargetsFn = function that takes a list of sub-entity IDs and returns a map of ID -> sub-entity. | ||
func populateAll[Source any, TargetID ~string, Target any]( | ||
sources []*Source, | ||
getTargetsFn func(*Source) ([]*Target, error), | ||
getTargetIDFn func(*Target) TargetID, | ||
lookupTargetsFn func([]TargetID) (map[TargetID]*Target, error), | ||
) error { | ||
allTargets := []*Target{} | ||
for i, source := range sources { | ||
targets, err := getTargetsFn(source) | ||
if err != nil { | ||
return fmt.Errorf("getting %d-th targets: %w", i, err) | ||
} | ||
allTargets = append(allTargets, targets...) | ||
} | ||
|
||
seen := map[TargetID]bool{} | ||
uniqueIds := []TargetID{} | ||
for _, target := range allTargets { | ||
id := getTargetIDFn(target) | ||
if _, ok := seen[id]; !ok { | ||
uniqueIds = append(uniqueIds, id) | ||
seen[id] = true | ||
} | ||
} | ||
|
||
populatedTargets, err := lookupTargetsFn(uniqueIds) | ||
if err != nil { | ||
return fmt.Errorf("looking up populated: %w", err) | ||
} | ||
for i, source := range sources { | ||
targets, err := getTargetsFn(source) | ||
if err != nil { | ||
return fmt.Errorf("re-getting %d-th targets: %w", i, err) | ||
} | ||
for _, target := range targets { | ||
id := getTargetIDFn(target) | ||
if populated, ok := populatedTargets[id]; ok { | ||
*target = *populated | ||
} else { | ||
return fmt.Errorf("can't find populated target %s", id) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a clever use of generics, but it's also hard to understand what it's doing, both here and where its called. I'd add a comment or similar.
Also, it looks like we're doing that "populate a pre-existing pointer" thing, which I begrudgingly accepted was useful in a GraphQL system for populating arbitrary recursive responses, but in a standard RESTful server, I can't see the motivation for such gymnastics when populating a static, non-recursive structure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GC on commenting this to make it clearer.
Not sure of the distinction you're drawing on that one: we have recursive structures in our data model here too:
Portfolio.MemberOf
is aPortfolioGroupMembership
,which has aPortfolio
field (same withPortfolioGroup
). The value of these selsective population things is exactly that we can build a DB layer that is "dumb", and then populate the data we want selectively based on the context.This is the ~the same pattern we used for Abound, ex, because the data model is actually really similar. Portfolio:Art, Gallery:PortfolioGroup, ex.