Skip to content

Commit 9542553

Browse files
authored
Return new relation field in QueryResult JSON response (#309)
1 parent 993e916 commit 9542553

File tree

6 files changed

+733
-29
lines changed

6 files changed

+733
-29
lines changed

pkg/authz/query/list.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,29 @@ func (parser QueryListParamParser) ParseValue(val string, sortBy string) (interf
4848
}
4949
}
5050

51-
type ByObjectTypeAndObjectIdAsc []QueryResult
51+
type ByObjectTypeAndObjectIdAndRelationAsc []QueryResult
5252

53-
func (res ByObjectTypeAndObjectIdAsc) Len() int { return len(res) }
54-
func (res ByObjectTypeAndObjectIdAsc) Swap(i, j int) { res[i], res[j] = res[j], res[i] }
55-
func (res ByObjectTypeAndObjectIdAsc) Less(i, j int) bool {
53+
func (res ByObjectTypeAndObjectIdAndRelationAsc) Len() int { return len(res) }
54+
func (res ByObjectTypeAndObjectIdAndRelationAsc) Swap(i, j int) { res[i], res[j] = res[j], res[i] }
55+
func (res ByObjectTypeAndObjectIdAndRelationAsc) Less(i, j int) bool {
5656
if res[i].ObjectType == res[j].ObjectType {
57+
if res[i].ObjectId == res[j].ObjectId {
58+
return res[i].Relation < res[j].Relation
59+
}
5760
return res[i].ObjectId < res[j].ObjectId
5861
}
5962
return res[i].ObjectType < res[j].ObjectType
6063
}
6164

62-
type ByObjectTypeAndObjectIdDesc []QueryResult
65+
type ByObjectTypeAndObjectIdAndRelationDesc []QueryResult
6366

64-
func (res ByObjectTypeAndObjectIdDesc) Len() int { return len(res) }
65-
func (res ByObjectTypeAndObjectIdDesc) Swap(i, j int) { res[i], res[j] = res[j], res[i] }
66-
func (res ByObjectTypeAndObjectIdDesc) Less(i, j int) bool {
67+
func (res ByObjectTypeAndObjectIdAndRelationDesc) Len() int { return len(res) }
68+
func (res ByObjectTypeAndObjectIdAndRelationDesc) Swap(i, j int) { res[i], res[j] = res[j], res[i] }
69+
func (res ByObjectTypeAndObjectIdAndRelationDesc) Less(i, j int) bool {
6770
if res[i].ObjectType == res[j].ObjectType {
71+
if res[i].ObjectId == res[j].ObjectId {
72+
return res[i].Relation > res[j].Relation
73+
}
6874
return res[i].ObjectId > res[j].ObjectId
6975
}
7076
return res[i].ObjectType > res[j].ObjectType

pkg/authz/query/resultset.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,33 @@ func (rs *ResultSet) List() *ResultSetNode {
4949
}
5050

5151
func (rs *ResultSet) Add(objectType string, objectId string, relation string, warrant warrant.WarrantSpec, isImplicit bool) {
52-
newNode := &ResultSetNode{
53-
ObjectType: objectType,
54-
ObjectId: objectId,
55-
Relation: relation,
56-
Warrant: warrant,
57-
IsImplicit: isImplicit,
58-
next: nil,
59-
}
60-
6152
existingRes, exists := rs.m[key(objectType, objectId, relation)]
6253
if !exists {
54+
newNode := ResultSetNode{
55+
ObjectType: objectType,
56+
ObjectId: objectId,
57+
Relation: relation,
58+
Warrant: warrant,
59+
IsImplicit: isImplicit,
60+
next: nil,
61+
}
62+
6363
// Add warrant to list
6464
if rs.head == nil {
65-
rs.head = newNode
65+
rs.head = &newNode
6666
}
6767

6868
if rs.tail != nil {
69-
rs.tail.next = newNode
69+
rs.tail.next = &newNode
7070
}
7171

72-
rs.tail = newNode
73-
}
72+
rs.tail = &newNode
7473

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

pkg/authz/query/service.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func NewService(env service.Env, objectTypeSvc objecttype.Service, warrantSvc wa
5252

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

@@ -198,9 +198,9 @@ func (svc QueryService) Query(ctx context.Context, query Query, listParams servi
198198
case PrimarySortKey:
199199
switch listParams.SortOrder {
200200
case service.SortOrderAsc:
201-
sort.Sort(ByObjectTypeAndObjectIdAsc(queryResults))
201+
sort.Sort(ByObjectTypeAndObjectIdAndRelationAsc(queryResults))
202202
case service.SortOrderDesc:
203-
sort.Sort(ByObjectTypeAndObjectIdDesc(queryResults))
203+
sort.Sort(ByObjectTypeAndObjectIdAndRelationDesc(queryResults))
204204
}
205205
case "createdAt":
206206
switch listParams.SortOrder {
@@ -285,7 +285,9 @@ func (svc QueryService) Query(ctx context.Context, query Query, listParams servi
285285
paginatedQueryResults = append(paginatedQueryResults, paginatedQueryResult)
286286
selectedObjectTypes[paginatedQueryResult.ObjectType] = true
287287
objects[paginatedQueryResult.ObjectType] = append(objects[paginatedQueryResult.ObjectType], paginatedQueryResult.ObjectId)
288-
resultMap[objectKey(paginatedQueryResult.ObjectType, paginatedQueryResult.ObjectId)] = len(paginatedQueryResults) - 1
288+
289+
objKey := objectKey(paginatedQueryResult.ObjectType, paginatedQueryResult.ObjectId)
290+
resultMap[objKey] = append(resultMap[objKey], len(paginatedQueryResults)-1)
289291
start++
290292
}
291293

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

299301
for _, objectSpec := range objectSpecs {
300-
paginatedQueryResults[resultMap[objectKey(selectedObjectType, objectSpec.ObjectId)]].Meta = objectSpec.Meta
302+
for _, resultIdx := range resultMap[objectKey(selectedObjectType, objectSpec.ObjectId)] {
303+
paginatedQueryResults[resultIdx].Meta = objectSpec.Meta
304+
}
301305
}
302306
}
303307
}

pkg/authz/query/spec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ type QueryHaving struct {
9797
type QueryResult struct {
9898
ObjectType string `json:"objectType"`
9999
ObjectId string `json:"objectId"`
100-
Relation string `json:"-"`
100+
Relation string `json:"relation"`
101101
Warrant baseWarrant.WarrantSpec `json:"warrant"`
102102
IsImplicit bool `json:"isImplicit"`
103103
Meta map[string]interface{} `json:"meta,omitempty"`

0 commit comments

Comments
 (0)