From c287f65eef3bfa5c83d157f0442da78f66e15c43 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Mon, 26 Dec 2022 22:23:57 +0600 Subject: [PATCH] :truck: Split scope.go on scope.go and scopes.go --- internal/domain/scope.go | 116 +++----------------------------------- internal/domain/scopes.go | 107 +++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 109 deletions(-) create mode 100644 internal/domain/scopes.go diff --git a/internal/domain/scope.go b/internal/domain/scope.go index 6c7adb3..7367b32 100644 --- a/internal/domain/scope.go +++ b/internal/domain/scope.go @@ -8,18 +8,13 @@ import ( "source.toby3d.me/toby3d/auth/internal/common" ) -type ( - // Scope represent single token scope supported by IndieAuth. - // - // NOTE(toby3d): Encapsulate enums in structs for extra compile-time safety: - // https://threedots.tech/post/safer-enums-in-go/#struct-based-enums - Scope struct { - uid string - } - - // Scopes represent set of Scope domains. - Scopes []Scope -) +// Scope represent single token scope supported by IndieAuth. +// +// NOTE(toby3d): Encapsulate enums in structs for extra compile-time safety: +// https://threedots.tech/post/safer-enums-in-go/#struct-based-enums +type Scope struct { + uid string +} var ErrScopeUnknown error = NewError(ErrorCodeInvalidRequest, "unknown scope", "https://indieweb.org/scope") @@ -101,100 +96,3 @@ func (s Scope) String() string { func (s Scope) GoString() string { return "domain.Scope(" + s.String() + ")" } - -// UnmarshalForm implements custom unmarshler for form values. -func (s *Scopes) UnmarshalForm(v []byte) error { - scopes := make(Scopes, 0) - - for _, rawScope := range strings.Fields(string(v)) { - scope, err := ParseScope(rawScope) - if err != nil { - return fmt.Errorf("Scopes: UnmarshalForm: %w", err) - } - - if scopes.Has(scope) { - continue - } - - scopes = append(scopes, scope) - } - - *s = scopes - - return nil -} - -// UnmarshalJSON implements custom unmarshler for JSON. -func (s *Scopes) UnmarshalJSON(v []byte) error { - src, err := strconv.Unquote(string(v)) - if err != nil { - return fmt.Errorf("Scopes: UnmarshalJSON: %w", err) - } - - result := make(Scopes, 0) - - for _, rawScope := range strings.Fields(src) { - scope, err := ParseScope(rawScope) - if err != nil { - return fmt.Errorf("Scopes: UnmarshalJSON: %w", err) - } - - if result.Has(scope) { - continue - } - - result = append(result, scope) - } - - *s = result - - return nil -} - -// UnmarshalJSON implements custom marshler for JSON. -func (s Scopes) MarshalJSON() ([]byte, error) { - scopes := make([]string, len(s)) - - for i := range s { - scopes[i] = s[i].String() - } - - return []byte(strconv.Quote(strings.Join(scopes, " "))), nil -} - -// String returns string representation of scopes. -func (s Scopes) String() string { - scopes := make([]string, len(s)) - - for i := range s { - scopes[i] = s[i].String() - } - - return strings.Join(scopes, " ") -} - -// IsEmpty returns true if the set does not contain valid scope. -func (s Scopes) IsEmpty() bool { - for i := range s { - if s[i] == ScopeUnd { - continue - } - - return false - } - - return true -} - -// Has check what input scope contains in current scopes collection. -func (s Scopes) Has(scope Scope) bool { - for i := range s { - if s[i] != scope { - continue - } - - return true - } - - return false -} diff --git a/internal/domain/scopes.go b/internal/domain/scopes.go new file mode 100644 index 0000000..9ff2146 --- /dev/null +++ b/internal/domain/scopes.go @@ -0,0 +1,107 @@ +package domain + +import ( + "fmt" + "strconv" + "strings" +) + +// Scopes represent set of Scope domains. +type Scopes []Scope + +// UnmarshalForm implements custom unmarshler for form values. +func (s *Scopes) UnmarshalForm(v []byte) error { + scopes := make(Scopes, 0) + + for _, rawScope := range strings.Fields(string(v)) { + scope, err := ParseScope(rawScope) + if err != nil { + return fmt.Errorf("Scopes: UnmarshalForm: %w", err) + } + + if scopes.Has(scope) { + continue + } + + scopes = append(scopes, scope) + } + + *s = scopes + + return nil +} + +// UnmarshalJSON implements custom unmarshler for JSON. +func (s *Scopes) UnmarshalJSON(v []byte) error { + src, err := strconv.Unquote(string(v)) + if err != nil { + return fmt.Errorf("Scopes: UnmarshalJSON: %w", err) + } + + result := make(Scopes, 0) + + for _, rawScope := range strings.Fields(src) { + scope, err := ParseScope(rawScope) + if err != nil { + return fmt.Errorf("Scopes: UnmarshalJSON: %w", err) + } + + if result.Has(scope) { + continue + } + + result = append(result, scope) + } + + *s = result + + return nil +} + +// UnmarshalJSON implements custom marshler for JSON. +func (s Scopes) MarshalJSON() ([]byte, error) { + scopes := make([]string, len(s)) + + for i := range s { + scopes[i] = s[i].String() + } + + return []byte(strconv.Quote(strings.Join(scopes, " "))), nil +} + +// String returns string representation of scopes. +func (s Scopes) String() string { + scopes := make([]string, len(s)) + + for i := range s { + scopes[i] = s[i].String() + } + + return strings.Join(scopes, " ") +} + +// IsEmpty returns true if the set does not contain valid scope. +func (s Scopes) IsEmpty() bool { + for i := range s { + if s[i] == ScopeUnd { + continue + } + + return false + } + + return true +} + +// Has check what input scope contains in current scopes collection. +func (s Scopes) Has(scope Scope) bool { + for i := range s { + if s[i] != scope { + continue + } + + return true + } + + return false +}