Skip to content

Commit

Permalink
GetRelation(s) with_objects (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
gertd authored Nov 13, 2023
1 parent a0165d7 commit 0f76237
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions pkg/directory/v3/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ func (s *Reader) GetObjects(ctx context.Context, req *dsr3.GetObjectsRequest) (*

// GetRelation, get a single relation instance based on subject, relation, object filter.
func (s *Reader) GetRelation(ctx context.Context, req *dsr3.GetRelationRequest) (*dsr3.GetRelationResponse, error) {
resp := &dsr3.GetRelationResponse{Result: &dsc3.Relation{}, Objects: map[string]*dsc3.Object{}}
resp := &dsr3.GetRelationResponse{
Result: &dsc3.Relation{},
Objects: map[string]*dsc3.Object{},
}

if err := s.v.Validate(req); err != nil {
return resp, derr.ErrProtoValidate.Msg(err.Error())
Expand Down Expand Up @@ -176,13 +179,13 @@ func (s *Reader) GetRelation(ctx context.Context, req *dsr3.GetRelationRequest)

sub, err := bdb.Get[dsc3.Object](ctx, tx, bdb.ObjectsPath, ds.ObjectIdentifier(rel.Subject()).Key())
if err != nil {
return err
sub = &dsc3.Object{Type: rel.SubjectType, Id: rel.SubjectId}
}
objects[ds.Object(sub).Key()] = sub

obj, err := bdb.Get[dsc3.Object](ctx, tx, bdb.ObjectsPath, ds.ObjectIdentifier(rel.Object()).Key())
if err != nil {
return err
obj = &dsc3.Object{Type: rel.ObjectType, Id: rel.ObjectId}
}
objects[ds.Object(obj).Key()] = obj

Expand All @@ -197,7 +200,11 @@ func (s *Reader) GetRelation(ctx context.Context, req *dsr3.GetRelationRequest)

// GetRelations, gets paginated set of relation instances based on subject, relation, object filter.
func (s *Reader) GetRelations(ctx context.Context, req *dsr3.GetRelationsRequest) (*dsr3.GetRelationsResponse, error) {
resp := &dsr3.GetRelationsResponse{Results: []*dsc3.Relation{}, Page: &dsc3.PaginationResponse{}}
resp := &dsr3.GetRelationsResponse{
Results: []*dsc3.Relation{},
Objects: map[string]*dsc3.Object{},
Page: &dsc3.PaginationResponse{},
}

if err := s.v.Validate(req); err != nil {
return resp, derr.ErrProtoValidate.Msg(err.Error())
Expand Down Expand Up @@ -234,6 +241,28 @@ func (s *Reader) GetRelations(ctx context.Context, req *dsr3.GetRelationsRequest
}
}

if req.GetWithObjects() {
objects := map[string]*dsc3.Object{}

for _, r := range resp.Results {
rel := ds.Relation(r)

sub, err := bdb.Get[dsc3.Object](ctx, tx, bdb.ObjectsPath, ds.ObjectIdentifier(rel.Subject()).Key())
if err != nil {
sub = &dsc3.Object{Type: rel.SubjectType, Id: rel.SubjectId}
}
objects[ds.Object(sub).Key()] = sub

obj, err := bdb.Get[dsc3.Object](ctx, tx, bdb.ObjectsPath, ds.ObjectIdentifier(rel.Object()).Key())
if err != nil {
obj = &dsc3.Object{Type: rel.ObjectType, Id: rel.ObjectId}
}
objects[ds.Object(obj).Key()] = obj
}

resp.Objects = objects
}

return nil
})

Expand Down

0 comments on commit 0f76237

Please sign in to comment.