scopes

Paddy 2015-12-05 Child:a64a25ae2db1

1:b93938562a17 Go to Latest

scopes/types/scope.go

Separete out scopeTypes package. To avoid the same problem we had in the auth package (where importing the type required you to vendor all the database drivers, etc.) let's just separate out the scopeTypes package to hold the types, then everything can import that.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/types/scope.go	Sat Dec 05 15:00:34 2015 -0800
     1.3 @@ -0,0 +1,52 @@
     1.4 +package scopeTypes
     1.5 +
     1.6 +// Scope represents a limit on the access that a grant provides.
     1.7 +type Scope struct {
     1.8 +	ID          string
     1.9 +	Name        string
    1.10 +	Description string
    1.11 +}
    1.12 +
    1.13 +func ApplyChange(change ScopeChange, scope Scope) Scope {
    1.14 +	changed := scope
    1.15 +	if change.Name != nil {
    1.16 +		changed.Name = *change.Name
    1.17 +	}
    1.18 +	if change.Description != nil {
    1.19 +		changed.Description = *change.Description
    1.20 +	}
    1.21 +	return changed
    1.22 +}
    1.23 +
    1.24 +type Scopes []Scope
    1.25 +
    1.26 +func (s Scopes) Len() int {
    1.27 +	return len(s)
    1.28 +}
    1.29 +
    1.30 +func (s Scopes) Swap(i, j int) {
    1.31 +	s[i], s[j] = s[j], s[i]
    1.32 +}
    1.33 +
    1.34 +func (s Scopes) Less(i, j int) bool {
    1.35 +	return s[i].ID < s[j].ID
    1.36 +}
    1.37 +
    1.38 +func (s Scopes) Strings() []string {
    1.39 +	res := make([]string, len(s))
    1.40 +	for pos, scope := range s {
    1.41 +		res[pos] = scope.ID
    1.42 +	}
    1.43 +	return res
    1.44 +}
    1.45 +
    1.46 +// ScopeChange represents a change to a Scope.
    1.47 +type ScopeChange struct {
    1.48 +	ScopeID     string
    1.49 +	Name        *string
    1.50 +	Description *string
    1.51 +}
    1.52 +
    1.53 +func (s ScopeChange) Empty() bool {
    1.54 +	return s.Name == nil && s.Description == nil
    1.55 +}