scopes

Paddy 2015-12-05 Parent:b2ab1ab8f157 Child:a64a25ae2db1

1:b93938562a17 Go to Latest

scopes/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 --- a/scope.go	Sat Dec 05 14:54:57 2015 -0800
     1.2 +++ b/scope.go	Sat Dec 05 15:00:34 2015 -0800
     1.3 @@ -4,6 +4,8 @@
     1.4  	"errors"
     1.5  	"fmt"
     1.6  
     1.7 +	"code.secondbit.org/scopes.hg/types"
     1.8 +
     1.9  	"golang.org/x/net/context"
    1.10  )
    1.11  
    1.12 @@ -18,69 +20,18 @@
    1.13  	return fmt.Sprintf("scope %s already exists", string(e))
    1.14  }
    1.15  
    1.16 -// Scope represents a limit on the access that a grant provides.
    1.17 -type Scope struct {
    1.18 -	ID          string
    1.19 -	Name        string
    1.20 -	Description string
    1.21 -}
    1.22 -
    1.23 -func ApplyChange(change ScopeChange, scope Scope) Scope {
    1.24 -	changed := scope
    1.25 -	if change.Name != nil {
    1.26 -		changed.Name = *change.Name
    1.27 -	}
    1.28 -	if change.Description != nil {
    1.29 -		changed.Description = *change.Description
    1.30 -	}
    1.31 -	return changed
    1.32 -}
    1.33 -
    1.34 -type Scopes []Scope
    1.35 -
    1.36 -func (s Scopes) Len() int {
    1.37 -	return len(s)
    1.38 -}
    1.39 -
    1.40 -func (s Scopes) Swap(i, j int) {
    1.41 -	s[i], s[j] = s[j], s[i]
    1.42 -}
    1.43 -
    1.44 -func (s Scopes) Less(i, j int) bool {
    1.45 -	return s[i].ID < s[j].ID
    1.46 -}
    1.47 -
    1.48 -func (s Scopes) Strings() []string {
    1.49 -	res := make([]string, len(s))
    1.50 +func stringsToScopes(s []string) scopeTypes.Scopes {
    1.51 +	res := make(scopeTypes.Scopes, len(s))
    1.52  	for pos, scope := range s {
    1.53 -		res[pos] = scope.ID
    1.54 +		res[pos] = scopeTypes.Scope{ID: scope}
    1.55  	}
    1.56  	return res
    1.57  }
    1.58  
    1.59 -func stringsToScopes(s []string) Scopes {
    1.60 -	res := make(Scopes, len(s))
    1.61 -	for pos, scope := range s {
    1.62 -		res[pos] = Scope{ID: scope}
    1.63 -	}
    1.64 -	return res
    1.65 +type Storer interface {
    1.66 +	CreateScopes(scopes []scopeTypes.Scope, ctx context.Context) error
    1.67 +	GetScopes(ids []string, ctx context.Context) (map[string]scopeTypes.Scope, error)
    1.68 +	UpdateScope(change scopeTypes.ScopeChange, ctx context.Context) error
    1.69 +	RemoveScopes(ids []string, ctx context.Context) error
    1.70 +	ListScopes(ctx context.Context) ([]scopeTypes.Scope, error)
    1.71  }
    1.72 -
    1.73 -// ScopeChange represents a change to a Scope.
    1.74 -type ScopeChange struct {
    1.75 -	ScopeID     string
    1.76 -	Name        *string
    1.77 -	Description *string
    1.78 -}
    1.79 -
    1.80 -func (s ScopeChange) Empty() bool {
    1.81 -	return s.Name == nil && s.Description == nil
    1.82 -}
    1.83 -
    1.84 -type Storer interface {
    1.85 -	CreateScopes(scopes []Scope, ctx context.Context) error
    1.86 -	GetScopes(ids []string, ctx context.Context) (map[string]Scope, error)
    1.87 -	UpdateScope(change ScopeChange, ctx context.Context) error
    1.88 -	RemoveScopes(ids []string, ctx context.Context) error
    1.89 -	ListScopes(ctx context.Context) ([]Scope, error)
    1.90 -}