From 1480f58cac1f47e60cf87c82e4368af18aa47187 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Fri, 18 Feb 2022 00:13:45 +0500 Subject: [PATCH] :art: Format session logic code --- internal/domain/profile.go | 10 +++++----- internal/domain/session.go | 17 +++++++++-------- .../profile/repository/http/http_profile.go | 4 ++++ .../repository/sqlite3/sqlite3_session.go | 6 +++--- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/internal/domain/profile.go b/internal/domain/profile.go index d6a03ca..c002854 100644 --- a/internal/domain/profile.go +++ b/internal/domain/profile.go @@ -6,10 +6,10 @@ import ( // Profile describes the data about the user. type Profile struct { - Photo []*URL - URL []*URL - Email []*Email - Name []string + Photo []*URL `json:"photo"` + URL []*URL `json:"url"` + Email []*Email `json:"email"` + Name []string `json:"name"` } func NewProfile() *Profile { @@ -67,4 +67,4 @@ func (p Profile) GetEmail() *Email { } return p.Email[0] -} \ No newline at end of file +} diff --git a/internal/domain/session.go b/internal/domain/session.go index d42441d..292f01b 100644 --- a/internal/domain/session.go +++ b/internal/domain/session.go @@ -6,15 +6,16 @@ import ( "source.toby3d.me/website/indieauth/internal/random" ) +//nolint: tagliatelle type Session struct { - ClientID *ClientID - RedirectURI *URL - Me *Me - Profile *Profile - Scope Scopes - CodeChallengeMethod CodeChallengeMethod - CodeChallenge string - Code string + ClientID *ClientID `json:"client_id"` + RedirectURI *URL `json:"redirect_uri"` + Me *Me `json:"me"` + Profile *Profile `json:"profile,omitempty"` + Scope Scopes `json:"scope"` + CodeChallengeMethod CodeChallengeMethod `json:"code_challenge_method,omitempty"` + CodeChallenge string `json:"code_challenge,omitempty"` + Code string `json:"-"` } // TestSession returns valid random generated session for tests. diff --git a/internal/profile/repository/http/http_profile.go b/internal/profile/repository/http/http_profile.go index 8d72140..040c8c6 100644 --- a/internal/profile/repository/http/http_profile.go +++ b/internal/profile/repository/http/http_profile.go @@ -86,5 +86,9 @@ func (repo *httpProfileRepository) Get(ctx context.Context, me *domain.Me) (*dom } } + if result.GetName() == "" && result.GetURL() == nil && result.GetPhoto() == nil && result.GetEmail() == nil { + return nil, profile.ErrNotExist + } + return result, nil } diff --git a/internal/session/repository/sqlite3/sqlite3_session.go b/internal/session/repository/sqlite3/sqlite3_session.go index 2324dae..d828976 100644 --- a/internal/session/repository/sqlite3/sqlite3_session.go +++ b/internal/session/repository/sqlite3/sqlite3_session.go @@ -124,7 +124,7 @@ func (repo *sqlite3SessionRepository) GC() {} func NewSession(src *domain.Session) (*Session, error) { data, err := json.Marshal(src) if err != nil { - return nil, err + return nil, fmt.Errorf("cannot encode data to JSON: %w", err) } return &Session{ @@ -142,11 +142,11 @@ func (t *Session) Populate(src []byte, dst *domain.Session) error { n, err := base64.StdEncoding.Decode(tmp, src) if err != nil { - return err + return fmt.Errorf("cannot decode base64 data: %w", err) } if err = json.Unmarshal(tmp[:n], dst); err != nil { - return err + return fmt.Errorf("cannot decode JSON data: %w", err) } return nil