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