🏷️ Added MarshalJSON for Scopes domain
This commit is contained in:
parent
a8ba34b905
commit
832a0be91a
|
@ -3,6 +3,7 @@ package domain
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -133,6 +134,18 @@ func (s *Scopes) UnmarshalJSON(v []byte) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Scopes) MarshalJSON() ([]byte, error) {
|
||||||
|
scopes := make([]string, len(s))
|
||||||
|
|
||||||
|
for i := range s {
|
||||||
|
scopes[i] = s[i].String()
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(scopes)
|
||||||
|
|
||||||
|
return []byte(strconv.Quote(strings.Join(scopes, " "))), nil
|
||||||
|
}
|
||||||
|
|
||||||
// String returns scope slug as string.
|
// String returns scope slug as string.
|
||||||
func (s Scope) String() string {
|
func (s Scope) String() string {
|
||||||
return s.slug
|
return s.slug
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
package domain_test
|
package domain_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/goccy/go-json"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"source.toby3d.me/website/indieauth/internal/domain"
|
"source.toby3d.me/website/indieauth/internal/domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestScopesUnmarshalJSON(t *testing.T) {
|
func TestScopes_UnmarshalJSON(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
result := &struct {
|
result := &struct {
|
||||||
|
|
Loading…
Reference in New Issue