scopes

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

1:b93938562a17 Browse Files

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.

memstore.go scope.go types/scope.go

     1.1 --- a/memstore.go	Sat Dec 05 14:54:57 2015 -0800
     1.2 +++ b/memstore.go	Sat Dec 05 15:00:34 2015 -0800
     1.3 @@ -4,15 +4,17 @@
     1.4  	"sort"
     1.5  	"sync"
     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  type Memstore struct {
    1.13 -	scopes map[string]Scope
    1.14 +	scopes map[string]scopeTypes.Scope
    1.15  	lock   sync.RWMutex
    1.16  }
    1.17  
    1.18 -func (m *Memstore) CreateScopes(scopes []Scope, ctx context.Context) error {
    1.19 +func (m *Memstore) CreateScopes(scopes []scopeTypes.Scope, ctx context.Context) error {
    1.20  	m.lock.Lock()
    1.21  	defer m.lock.Unlock()
    1.22  
    1.23 @@ -27,11 +29,11 @@
    1.24  	return nil
    1.25  }
    1.26  
    1.27 -func (m *Memstore) GetScopes(ids []string, ctx context.Context) (map[string]Scope, error) {
    1.28 +func (m *Memstore) GetScopes(ids []string, ctx context.Context) (map[string]scopeTypes.Scope, error) {
    1.29  	m.lock.RLock()
    1.30  	defer m.lock.RUnlock()
    1.31  
    1.32 -	scopes := map[string]Scope{}
    1.33 +	scopes := map[string]scopeTypes.Scope{}
    1.34  	for _, id := range ids {
    1.35  		scope, ok := m.scopes[id]
    1.36  		if !ok {
    1.37 @@ -42,7 +44,7 @@
    1.38  	return scopes, nil
    1.39  }
    1.40  
    1.41 -func (m *Memstore) UpdateScope(change ScopeChange, ctx context.Context) error {
    1.42 +func (m *Memstore) UpdateScope(change scopeTypes.ScopeChange, ctx context.Context) error {
    1.43  	m.lock.Lock()
    1.44  	defer m.lock.Unlock()
    1.45  
    1.46 @@ -50,7 +52,7 @@
    1.47  	if !ok {
    1.48  		return ErrScopeNotFound
    1.49  	}
    1.50 -	scope = ApplyChange(change, scope)
    1.51 +	scope = scopeTypes.ApplyChange(change, scope)
    1.52  	m.scopes[change.ScopeID] = scope
    1.53  	return nil
    1.54  }
    1.55 @@ -65,15 +67,15 @@
    1.56  	return nil
    1.57  }
    1.58  
    1.59 -func (m *Memstore) ListScopes(ctx context.Context) ([]Scope, error) {
    1.60 +func (m *Memstore) ListScopes(ctx context.Context) ([]scopeTypes.Scope, error) {
    1.61  	m.lock.RLock()
    1.62  	defer m.lock.RUnlock()
    1.63  
    1.64 -	scopes := []Scope{}
    1.65 +	scopes := []scopeTypes.Scope{}
    1.66  	for _, scope := range m.scopes {
    1.67  		scopes = append(scopes, scope)
    1.68  	}
    1.69 -	sorted := Scopes(scopes)
    1.70 +	sorted := scopeTypes.Scopes(scopes)
    1.71  	sort.Sort(sorted)
    1.72  	scopes = sorted
    1.73  	return scopes, nil
     2.1 --- a/scope.go	Sat Dec 05 14:54:57 2015 -0800
     2.2 +++ b/scope.go	Sat Dec 05 15:00:34 2015 -0800
     2.3 @@ -4,6 +4,8 @@
     2.4  	"errors"
     2.5  	"fmt"
     2.6  
     2.7 +	"code.secondbit.org/scopes.hg/types"
     2.8 +
     2.9  	"golang.org/x/net/context"
    2.10  )
    2.11  
    2.12 @@ -18,69 +20,18 @@
    2.13  	return fmt.Sprintf("scope %s already exists", string(e))
    2.14  }
    2.15  
    2.16 -// Scope represents a limit on the access that a grant provides.
    2.17 -type Scope struct {
    2.18 -	ID          string
    2.19 -	Name        string
    2.20 -	Description string
    2.21 -}
    2.22 -
    2.23 -func ApplyChange(change ScopeChange, scope Scope) Scope {
    2.24 -	changed := scope
    2.25 -	if change.Name != nil {
    2.26 -		changed.Name = *change.Name
    2.27 -	}
    2.28 -	if change.Description != nil {
    2.29 -		changed.Description = *change.Description
    2.30 -	}
    2.31 -	return changed
    2.32 -}
    2.33 -
    2.34 -type Scopes []Scope
    2.35 -
    2.36 -func (s Scopes) Len() int {
    2.37 -	return len(s)
    2.38 -}
    2.39 -
    2.40 -func (s Scopes) Swap(i, j int) {
    2.41 -	s[i], s[j] = s[j], s[i]
    2.42 -}
    2.43 -
    2.44 -func (s Scopes) Less(i, j int) bool {
    2.45 -	return s[i].ID < s[j].ID
    2.46 -}
    2.47 -
    2.48 -func (s Scopes) Strings() []string {
    2.49 -	res := make([]string, len(s))
    2.50 +func stringsToScopes(s []string) scopeTypes.Scopes {
    2.51 +	res := make(scopeTypes.Scopes, len(s))
    2.52  	for pos, scope := range s {
    2.53 -		res[pos] = scope.ID
    2.54 +		res[pos] = scopeTypes.Scope{ID: scope}
    2.55  	}
    2.56  	return res
    2.57  }
    2.58  
    2.59 -func stringsToScopes(s []string) Scopes {
    2.60 -	res := make(Scopes, len(s))
    2.61 -	for pos, scope := range s {
    2.62 -		res[pos] = Scope{ID: scope}
    2.63 -	}
    2.64 -	return res
    2.65 +type Storer interface {
    2.66 +	CreateScopes(scopes []scopeTypes.Scope, ctx context.Context) error
    2.67 +	GetScopes(ids []string, ctx context.Context) (map[string]scopeTypes.Scope, error)
    2.68 +	UpdateScope(change scopeTypes.ScopeChange, ctx context.Context) error
    2.69 +	RemoveScopes(ids []string, ctx context.Context) error
    2.70 +	ListScopes(ctx context.Context) ([]scopeTypes.Scope, error)
    2.71  }
    2.72 -
    2.73 -// ScopeChange represents a change to a Scope.
    2.74 -type ScopeChange struct {
    2.75 -	ScopeID     string
    2.76 -	Name        *string
    2.77 -	Description *string
    2.78 -}
    2.79 -
    2.80 -func (s ScopeChange) Empty() bool {
    2.81 -	return s.Name == nil && s.Description == nil
    2.82 -}
    2.83 -
    2.84 -type Storer interface {
    2.85 -	CreateScopes(scopes []Scope, ctx context.Context) error
    2.86 -	GetScopes(ids []string, ctx context.Context) (map[string]Scope, error)
    2.87 -	UpdateScope(change ScopeChange, ctx context.Context) error
    2.88 -	RemoveScopes(ids []string, ctx context.Context) error
    2.89 -	ListScopes(ctx context.Context) ([]Scope, error)
    2.90 -}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/types/scope.go	Sat Dec 05 15:00:34 2015 -0800
     3.3 @@ -0,0 +1,52 @@
     3.4 +package scopeTypes
     3.5 +
     3.6 +// Scope represents a limit on the access that a grant provides.
     3.7 +type Scope struct {
     3.8 +	ID          string
     3.9 +	Name        string
    3.10 +	Description string
    3.11 +}
    3.12 +
    3.13 +func ApplyChange(change ScopeChange, scope Scope) Scope {
    3.14 +	changed := scope
    3.15 +	if change.Name != nil {
    3.16 +		changed.Name = *change.Name
    3.17 +	}
    3.18 +	if change.Description != nil {
    3.19 +		changed.Description = *change.Description
    3.20 +	}
    3.21 +	return changed
    3.22 +}
    3.23 +
    3.24 +type Scopes []Scope
    3.25 +
    3.26 +func (s Scopes) Len() int {
    3.27 +	return len(s)
    3.28 +}
    3.29 +
    3.30 +func (s Scopes) Swap(i, j int) {
    3.31 +	s[i], s[j] = s[j], s[i]
    3.32 +}
    3.33 +
    3.34 +func (s Scopes) Less(i, j int) bool {
    3.35 +	return s[i].ID < s[j].ID
    3.36 +}
    3.37 +
    3.38 +func (s Scopes) Strings() []string {
    3.39 +	res := make([]string, len(s))
    3.40 +	for pos, scope := range s {
    3.41 +		res[pos] = scope.ID
    3.42 +	}
    3.43 +	return res
    3.44 +}
    3.45 +
    3.46 +// ScopeChange represents a change to a Scope.
    3.47 +type ScopeChange struct {
    3.48 +	ScopeID     string
    3.49 +	Name        *string
    3.50 +	Description *string
    3.51 +}
    3.52 +
    3.53 +func (s ScopeChange) Empty() bool {
    3.54 +	return s.Name == nil && s.Description == nil
    3.55 +}