From 832a0be91a00809ac9c2f399f47394e7948488dc Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Sat, 8 Jan 2022 15:50:49 +0500 Subject: [PATCH] :label: Added MarshalJSON for Scopes domain --- internal/domain/scope.go | 13 +++++++++++++ internal/domain/scope_test.go | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/domain/scope.go b/internal/domain/scope.go index e1d40f0..f8fb7ea 100644 --- a/internal/domain/scope.go +++ b/internal/domain/scope.go @@ -3,6 +3,7 @@ package domain import ( "errors" "fmt" + "sort" "strconv" "strings" ) @@ -133,6 +134,18 @@ func (s *Scopes) UnmarshalJSON(v []byte) error { 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. func (s Scope) String() string { return s.slug diff --git a/internal/domain/scope_test.go b/internal/domain/scope_test.go index acfeaf1..f765821 100644 --- a/internal/domain/scope_test.go +++ b/internal/domain/scope_test.go @@ -1,16 +1,16 @@ package domain_test import ( - "encoding/json" "testing" + "github.com/goccy/go-json" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "source.toby3d.me/website/indieauth/internal/domain" ) -func TestScopesUnmarshalJSON(t *testing.T) { +func TestScopes_UnmarshalJSON(t *testing.T) { t.Parallel() result := &struct {