From 0f76237cdce849e7f2ce2519dfe02440034fde27 Mon Sep 17 00:00:00 2001 From: Gert Drapers Date: Mon, 13 Nov 2023 05:18:29 +0000 Subject: [PATCH] GetRelation(s) with_objects (#56) --- pkg/directory/v3/reader.go | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/pkg/directory/v3/reader.go b/pkg/directory/v3/reader.go index 182b964..ff009de 100644 --- a/pkg/directory/v3/reader.go +++ b/pkg/directory/v3/reader.go @@ -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()) @@ -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 @@ -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()) @@ -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 })