Browse Source

ensure entitlements and metadata are returned in a consistent order

Mark deVilliers 2 years ago
parent
commit
3b66d80313
2 changed files with 17 additions and 1 deletions
  1. 9 0
      node/api/entitlement_store.go
  2. 8 1
      node/api/metadata_store.go

+ 9 - 0
node/api/entitlement_store.go

@@ -2,6 +2,7 @@ package api
2 2
 
3 3
 import (
4 4
 	"errors"
5
+	"sort"
5 6
 	"sync"
6 7
 
7 8
 	"gogs.dyne.org/DECODE/decode-prototype-da/utils"
@@ -116,6 +117,8 @@ func (e entitlementMap) All() []Entitlement {
116 117
 		list = append(list, each)
117 118
 	}
118 119
 
120
+	sort.Sort(byEntSubject(list))
121
+
119 122
 	return list
120 123
 }
121 124
 
@@ -143,3 +146,9 @@ func (e entitlementMap) AppendOrReplaceOnSubject(ent Entitlement) {
143 146
 	e.store[ent.UID] = ent
144 147
 
145 148
 }
149
+
150
+type byEntSubject []Entitlement
151
+
152
+func (a byEntSubject) Len() int           { return len(a) }
153
+func (a byEntSubject) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
154
+func (a byEntSubject) Less(i, j int) bool { return a[i].Subject < a[j].Subject }

+ 8 - 1
node/api/metadata_store.go

@@ -1,6 +1,7 @@
1 1
 package api
2 2
 
3 3
 import (
4
+	"sort"
4 5
 	"sync"
5 6
 
6 7
 	"gogs.dyne.org/DECODE/decode-prototype-da/utils"
@@ -47,7 +48,6 @@ func (m *MetadataStore) FindBySubject(subject utils.Subject) (Metadata, bool) {
47 48
 			}
48 49
 		}
49 50
 	}
50
-
51 51
 	return Metadata{}, false
52 52
 }
53 53
 
@@ -66,6 +66,13 @@ func (m *MetadataStore) All() []Metadata {
66 66
 		}
67 67
 	}
68 68
 
69
+	sort.Sort(byMetSubject(list))
69 70
 	return list
70 71
 
71 72
 }
73
+
74
+type byMetSubject []Metadata
75
+
76
+func (a byMetSubject) Len() int           { return len(a) }
77
+func (a byMetSubject) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
78
+func (a byMetSubject) Less(i, j int) bool { return a[i].Subject < a[j].Subject }