Use json format for app and session repo

This commit is contained in:
r 2019-12-21 09:56:18 +00:00
parent a25d64a078
commit e73eb1162a
4 changed files with 34 additions and 56 deletions

View File

@ -2,7 +2,6 @@ package model
import ( import (
"errors" "errors"
"strings"
) )
var ( var (
@ -10,31 +9,13 @@ var (
) )
type App struct { type App struct {
InstanceDomain string InstanceDomain string `json:"instance_domain"`
InstanceURL string InstanceURL string `json:"instance_url"`
ClientID string ClientID string `json:"client_id"`
ClientSecret string ClientSecret string `json:"client_secret"`
} }
type AppRepository interface { type AppRepository interface {
Add(app App) (err error) Add(app App) (err error)
Get(instanceDomain string) (app App, err error) Get(instanceDomain string) (app App, err error)
} }
func (a *App) Marshal() []byte {
str := a.InstanceURL + "\n" + a.ClientID + "\n" + a.ClientSecret
return []byte(str)
}
func (a *App) Unmarshal(instanceDomain string, data []byte) error {
str := string(data)
lines := strings.Split(str, "\n")
if len(lines) != 3 {
return errors.New("invalid data")
}
a.InstanceDomain = instanceDomain
a.InstanceURL = lines[0]
a.ClientID = lines[1]
a.ClientSecret = lines[2]
return nil
}

View File

@ -2,7 +2,6 @@ package model
import ( import (
"errors" "errors"
"strings"
) )
var ( var (
@ -10,9 +9,9 @@ var (
) )
type Session struct { type Session struct {
ID string ID string `json:"id"`
InstanceDomain string InstanceDomain string `json:"instance_domain"`
AccessToken string AccessToken string `json:"access_token"`
} }
type SessionRepository interface { type SessionRepository interface {
@ -24,26 +23,3 @@ type SessionRepository interface {
func (s Session) IsLoggedIn() bool { func (s Session) IsLoggedIn() bool {
return len(s.AccessToken) > 0 return len(s.AccessToken) > 0
} }
func (s *Session) Marshal() []byte {
str := s.InstanceDomain + "\n" + s.AccessToken
return []byte(str)
}
func (s *Session) Unmarshal(id string, data []byte) error {
str := string(data)
lines := strings.Split(str, "\n")
size := len(lines)
if size == 1 {
s.InstanceDomain = lines[0]
} else if size == 2 {
s.InstanceDomain = lines[0]
s.AccessToken = lines[1]
} else {
return errors.New("invalid data")
}
s.ID = id
return nil
}

View File

@ -1,6 +1,7 @@
package repository package repository
import ( import (
"encoding/json"
"web/kv" "web/kv"
"web/model" "web/model"
) )
@ -16,7 +17,11 @@ func NewAppRepository(db *kv.Database) *appRepository {
} }
func (repo *appRepository) Add(a model.App) (err error) { func (repo *appRepository) Add(a model.App) (err error) {
err = repo.db.Set(a.InstanceDomain, a.Marshal()) data, err := json.Marshal(a)
if err != nil {
return
}
err = repo.db.Set(a.InstanceDomain, data)
return return
} }
@ -27,7 +32,10 @@ func (repo *appRepository) Get(instanceDomain string) (a model.App, err error) {
return return
} }
err = a.Unmarshal(instanceDomain, data) err = json.Unmarshal(data, &a)
if err != nil {
return
}
return return
} }

View File

@ -1,6 +1,7 @@
package repository package repository
import ( import (
"encoding/json"
"web/kv" "web/kv"
"web/model" "web/model"
) )
@ -16,7 +17,11 @@ func NewSessionRepository(db *kv.Database) *sessionRepository {
} }
func (repo *sessionRepository) Add(s model.Session) (err error) { func (repo *sessionRepository) Add(s model.Session) (err error) {
err = repo.db.Set(s.ID, s.Marshal()) data, err := json.Marshal(s)
if err != nil {
return
}
err = repo.db.Set(s.ID, data)
return return
} }
@ -27,14 +32,19 @@ func (repo *sessionRepository) Update(id string, accessToken string) (err error)
} }
var s model.Session var s model.Session
err = s.Unmarshal(id, data) err = json.Unmarshal(data, &s)
if err != nil { if err != nil {
return return
} }
s.AccessToken = accessToken s.AccessToken = accessToken
return repo.db.Set(id, s.Marshal()) data, err = json.Marshal(s)
if err != nil {
return
}
return repo.db.Set(id, data)
} }
func (repo *sessionRepository) Get(id string) (s model.Session, err error) { func (repo *sessionRepository) Get(id string) (s model.Session, err error) {
@ -44,7 +54,10 @@ func (repo *sessionRepository) Get(id string) (s model.Session, err error) {
return return
} }
err = s.Unmarshal(id, data) err = json.Unmarshal(data, &s)
if err != nil {
return
}
return return
} }