Skip to content

Commit

Permalink
Return new relation field in QueryResult JSON response (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkajla12 authored Mar 14, 2024
1 parent 993e916 commit 9542553
Show file tree
Hide file tree
Showing 6 changed files with 733 additions and 29 deletions.
22 changes: 14 additions & 8 deletions pkg/authz/query/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,29 @@ func (parser QueryListParamParser) ParseValue(val string, sortBy string) (interf
}
}

type ByObjectTypeAndObjectIdAsc []QueryResult
type ByObjectTypeAndObjectIdAndRelationAsc []QueryResult

func (res ByObjectTypeAndObjectIdAsc) Len() int { return len(res) }
func (res ByObjectTypeAndObjectIdAsc) Swap(i, j int) { res[i], res[j] = res[j], res[i] }
func (res ByObjectTypeAndObjectIdAsc) Less(i, j int) bool {
func (res ByObjectTypeAndObjectIdAndRelationAsc) Len() int { return len(res) }
func (res ByObjectTypeAndObjectIdAndRelationAsc) Swap(i, j int) { res[i], res[j] = res[j], res[i] }
func (res ByObjectTypeAndObjectIdAndRelationAsc) Less(i, j int) bool {
if res[i].ObjectType == res[j].ObjectType {
if res[i].ObjectId == res[j].ObjectId {
return res[i].Relation < res[j].Relation
}
return res[i].ObjectId < res[j].ObjectId
}
return res[i].ObjectType < res[j].ObjectType
}

type ByObjectTypeAndObjectIdDesc []QueryResult
type ByObjectTypeAndObjectIdAndRelationDesc []QueryResult

func (res ByObjectTypeAndObjectIdDesc) Len() int { return len(res) }
func (res ByObjectTypeAndObjectIdDesc) Swap(i, j int) { res[i], res[j] = res[j], res[i] }
func (res ByObjectTypeAndObjectIdDesc) Less(i, j int) bool {
func (res ByObjectTypeAndObjectIdAndRelationDesc) Len() int { return len(res) }
func (res ByObjectTypeAndObjectIdAndRelationDesc) Swap(i, j int) { res[i], res[j] = res[j], res[i] }
func (res ByObjectTypeAndObjectIdAndRelationDesc) Less(i, j int) bool {
if res[i].ObjectType == res[j].ObjectType {
if res[i].ObjectId == res[j].ObjectId {
return res[i].Relation > res[j].Relation
}
return res[i].ObjectId > res[j].ObjectId
}
return res[i].ObjectType > res[j].ObjectType
Expand Down
31 changes: 16 additions & 15 deletions pkg/authz/query/resultset.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,33 @@ func (rs *ResultSet) List() *ResultSetNode {
}

func (rs *ResultSet) Add(objectType string, objectId string, relation string, warrant warrant.WarrantSpec, isImplicit bool) {
newNode := &ResultSetNode{
ObjectType: objectType,
ObjectId: objectId,
Relation: relation,
Warrant: warrant,
IsImplicit: isImplicit,
next: nil,
}

existingRes, exists := rs.m[key(objectType, objectId, relation)]
if !exists {
newNode := ResultSetNode{
ObjectType: objectType,
ObjectId: objectId,
Relation: relation,
Warrant: warrant,
IsImplicit: isImplicit,
next: nil,
}

// Add warrant to list
if rs.head == nil {
rs.head = newNode
rs.head = &newNode
}

if rs.tail != nil {
rs.tail.next = newNode
rs.tail.next = &newNode
}

rs.tail = newNode
}
rs.tail = &newNode

if !exists || (existingRes.IsImplicit && !isImplicit) {
// Add result node to map for O(1) lookups
rs.m[key(objectType, objectId, relation)] = newNode
rs.m[key(objectType, objectId, relation)] = &newNode
} else if existingRes.IsImplicit && !isImplicit { // favor explicit results
existingRes.IsImplicit = isImplicit
existingRes.Warrant = warrant
}
}

Expand Down
14 changes: 9 additions & 5 deletions pkg/authz/query/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewService(env service.Env, objectTypeSvc objecttype.Service, warrantSvc wa

func (svc QueryService) Query(ctx context.Context, query Query, listParams service.ListParams) ([]QueryResult, *service.Cursor, *service.Cursor, error) {
queryResults := make([]QueryResult, 0)
resultMap := make(map[string]int)
resultMap := make(map[string][]int)
objects := make(map[string][]string)
selectedObjectTypes := make(map[string]bool)

Expand Down Expand Up @@ -198,9 +198,9 @@ func (svc QueryService) Query(ctx context.Context, query Query, listParams servi
case PrimarySortKey:
switch listParams.SortOrder {
case service.SortOrderAsc:
sort.Sort(ByObjectTypeAndObjectIdAsc(queryResults))
sort.Sort(ByObjectTypeAndObjectIdAndRelationAsc(queryResults))
case service.SortOrderDesc:
sort.Sort(ByObjectTypeAndObjectIdDesc(queryResults))
sort.Sort(ByObjectTypeAndObjectIdAndRelationDesc(queryResults))
}
case "createdAt":
switch listParams.SortOrder {
Expand Down Expand Up @@ -285,7 +285,9 @@ func (svc QueryService) Query(ctx context.Context, query Query, listParams servi
paginatedQueryResults = append(paginatedQueryResults, paginatedQueryResult)
selectedObjectTypes[paginatedQueryResult.ObjectType] = true
objects[paginatedQueryResult.ObjectType] = append(objects[paginatedQueryResult.ObjectType], paginatedQueryResult.ObjectId)
resultMap[objectKey(paginatedQueryResult.ObjectType, paginatedQueryResult.ObjectId)] = len(paginatedQueryResults) - 1

objKey := objectKey(paginatedQueryResult.ObjectType, paginatedQueryResult.ObjectId)
resultMap[objKey] = append(resultMap[objKey], len(paginatedQueryResults)-1)
start++
}

Expand All @@ -297,7 +299,9 @@ func (svc QueryService) Query(ctx context.Context, query Query, listParams servi
}

for _, objectSpec := range objectSpecs {
paginatedQueryResults[resultMap[objectKey(selectedObjectType, objectSpec.ObjectId)]].Meta = objectSpec.Meta
for _, resultIdx := range resultMap[objectKey(selectedObjectType, objectSpec.ObjectId)] {
paginatedQueryResults[resultIdx].Meta = objectSpec.Meta
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/authz/query/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type QueryHaving struct {
type QueryResult struct {
ObjectType string `json:"objectType"`
ObjectId string `json:"objectId"`
Relation string `json:"-"`
Relation string `json:"relation"`
Warrant baseWarrant.WarrantSpec `json:"warrant"`
IsImplicit bool `json:"isImplicit"`
Meta map[string]interface{} `json:"meta,omitempty"`
Expand Down
Loading

0 comments on commit 9542553

Please sign in to comment.