scopes

Paddy 2015-12-13 Parent:b93938562a17

2:a64a25ae2db1 Go to Latest

scopes/types/scope.go

Port over Postgres storage and tests. Do the minimum possible port of the code from auth to get Postgres working and make the tests run and pass again. This leaves a bug where the ErrScopeAlreadyExists type, when populatd from Postgres, contains the entire error string returned from the database, instead of parsing the ID itself out. Which is a thing we should do and add a test for.

History
1 package scopeTypes
3 // Scope represents a limit on the access that a grant provides.
4 type Scope struct {
5 ID string
6 Name string
7 Description string
8 }
10 func ApplyChange(change ScopeChange, scope Scope) Scope {
11 changed := scope
12 if change.Name != nil {
13 changed.Name = *change.Name
14 }
15 if change.Description != nil {
16 changed.Description = *change.Description
17 }
18 return changed
19 }
21 type Scopes []Scope
23 func (s Scopes) Len() int {
24 return len(s)
25 }
27 func (s Scopes) Swap(i, j int) {
28 s[i], s[j] = s[j], s[i]
29 }
31 func (s Scopes) Less(i, j int) bool {
32 return s[i].ID < s[j].ID
33 }
35 func (s Scopes) Strings() []string {
36 res := make([]string, len(s))
37 for pos, scope := range s {
38 res[pos] = scope.ID
39 }
40 return res
41 }
43 // ScopeChange represents a change to a Scope.
44 type ScopeChange struct {
45 ScopeID string
46 Name *string
47 Description *string
48 }
50 func (s ScopeChange) Empty() bool {
51 return s.Name == nil && s.Description == nil
52 }
54 func StringsToScopes(s []string) Scopes {
55 res := make(Scopes, len(s))
56 for pos, scope := range s {
57 res[pos] = Scope{ID: scope}
58 }
59 return res
60 }