Name all storage interface params, return slices of pointers

This commit is contained in:
Ken-Håvard Lieng 2018-06-01 04:16:38 +02:00
parent 09248edd59
commit e0200a2b2a
11 changed files with 65 additions and 52 deletions

View File

@ -24,7 +24,7 @@ type connectDefaults struct {
type indexData struct {
Defaults connectDefaults
Servers []Server
Channels []storage.Channel
Channels []*storage.Channel
// Users in the selected channel
Users *Userlist
@ -116,7 +116,7 @@ func (d *indexData) addUsersAndMessages(server, channel string, state *State) {
}
}
func isInChannel(channels []storage.Channel, server, channel string) bool {
func isInChannel(channels []*storage.Channel, server, channel string) bool {
if channel != "" {
for _, ch := range channels {
if server == ch.Server && channel == ch.Name {

View File

@ -74,16 +74,24 @@ func easyjson7e607aefDecodeGithubComKhliengDispatchServer(in *jlexer.Lexer, out
in.Delim('[')
if out.Channels == nil {
if !in.IsDelim(']') {
out.Channels = make([]storage.Channel, 0, 1)
out.Channels = make([]*storage.Channel, 0, 8)
} else {
out.Channels = []storage.Channel{}
out.Channels = []*storage.Channel{}
}
} else {
out.Channels = (out.Channels)[:0]
}
for !in.IsDelim(']') {
var v2 storage.Channel
easyjson7e607aefDecodeGithubComKhliengDispatchStorage(in, &v2)
var v2 *storage.Channel
if in.IsNull() {
in.Skip()
v2 = nil
} else {
if v2 == nil {
v2 = new(storage.Channel)
}
easyjson7e607aefDecodeGithubComKhliengDispatchStorage(in, &*v2)
}
out.Channels = append(out.Channels, v2)
in.WantComma()
}
@ -170,7 +178,11 @@ func easyjson7e607aefEncodeGithubComKhliengDispatchServer(out *jwriter.Writer, i
if v5 > 0 {
out.RawByte(',')
}
easyjson7e607aefEncodeGithubComKhliengDispatchStorage(out, v6)
if v6 == nil {
out.RawString("null")
} else {
easyjson7e607aefEncodeGithubComKhliengDispatchStorage(out, *v6)
}
}
out.RawByte(']')
}

View File

@ -20,7 +20,7 @@ type WSResponse struct {
}
type Server struct {
storage.Server
*storage.Server
Status ConnectionUpdate
}

View File

@ -766,6 +766,7 @@ func easyjson42239ddeDecodeGithubComKhliengDispatchServer7(in *jlexer.Lexer, out
in.Skip()
return
}
out.Server = new(storage.Server)
in.Delim('{')
for !in.IsDelim('}') {
key := in.UnsafeString()

View File

@ -59,8 +59,8 @@ func (d *Dispatch) loadUsers() {
log.Printf("Loading %d user(s)", len(users))
for i := range users {
go d.loadUser(&users[i])
for _, user := range users {
go d.loadUser(user)
}
}
@ -92,7 +92,7 @@ func (d *Dispatch) loadUser(user *storage.User) {
}
for _, server := range servers {
i := connectIRC(&server, state)
i := connectIRC(server, state)
var joining []string
for _, channel := range channels {

View File

@ -247,8 +247,8 @@ func newStateStore(sessionStore storage.SessionStore) *stateStore {
for _, session := range sessions {
if !session.Expired() {
session.Init()
store.sessions[session.Key()] = &session
go deleteSessionWhenExpired(&session, store)
store.sessions[session.Key()] = session
go deleteSessionWhenExpired(session, store)
} else {
go sessionStore.DeleteSession(session.Key())
}

View File

@ -90,9 +90,9 @@ func (h *wsHandler) connect(b []byte) {
if _, ok := h.state.getIRC(data.Host); !ok {
log.Println(h.addr, "[IRC] Add server", data.Host)
connectIRC(&data.Server, h.state)
connectIRC(data.Server, h.state)
go h.state.user.AddServer(&data.Server)
go h.state.user.AddServer(data.Server)
} else {
log.Println(h.addr, "[IRC]", data.Host, "already added")
}

View File

@ -48,8 +48,8 @@ func (s *BoltStore) Close() {
s.db.Close()
}
func (s *BoltStore) GetUsers() ([]storage.User, error) {
var users []storage.User
func (s *BoltStore) GetUsers() ([]*storage.User, error) {
var users []*storage.User
s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketUsers)
@ -63,7 +63,7 @@ func (s *BoltStore) GetUsers() ([]storage.User, error) {
}
copy(user.IDBytes, k)
users = append(users, user)
users = append(users, &user)
return nil
})
@ -109,8 +109,8 @@ func (s *BoltStore) DeleteUser(user *storage.User) error {
})
}
func (s *BoltStore) GetServers(user *storage.User) ([]storage.Server, error) {
var servers []storage.Server
func (s *BoltStore) GetServers(user *storage.User) ([]*storage.Server, error) {
var servers []*storage.Server
s.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(bucketServers).Cursor()
@ -118,7 +118,7 @@ func (s *BoltStore) GetServers(user *storage.User) ([]storage.Server, error) {
for k, v := c.Seek(user.IDBytes); bytes.HasPrefix(k, user.IDBytes); k, v = c.Next() {
server := storage.Server{}
server.Unmarshal(v)
servers = append(servers, server)
servers = append(servers, &server)
}
return nil
@ -190,8 +190,8 @@ func (s *BoltStore) SetServerName(user *storage.User, name, address string) erro
})
}
func (s *BoltStore) GetChannels(user *storage.User) ([]storage.Channel, error) {
var channels []storage.Channel
func (s *BoltStore) GetChannels(user *storage.User) ([]*storage.Channel, error) {
var channels []*storage.Channel
s.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(bucketChannels).Cursor()
@ -199,7 +199,7 @@ func (s *BoltStore) GetChannels(user *storage.User) ([]storage.Channel, error) {
for k, v := c.Seek(user.IDBytes); bytes.HasPrefix(k, user.IDBytes); k, v = c.Next() {
channel := storage.Channel{}
channel.Unmarshal(v)
channels = append(channels, channel)
channels = append(channels, &channel)
}
return nil
@ -298,8 +298,8 @@ func (s *BoltStore) GetMessagesByID(server, channel string, ids []string) ([]sto
return messages, err
}
func (s *BoltStore) GetSessions() ([]session.Session, error) {
var sessions []session.Session
func (s *BoltStore) GetSessions() ([]*session.Session, error) {
var sessions []*session.Session
err := s.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketSessions)
@ -307,7 +307,7 @@ func (s *BoltStore) GetSessions() ([]session.Session, error) {
return b.ForEach(func(_ []byte, v []byte) error {
session := session.Session{}
_, err := session.Unmarshal(v)
sessions = append(sessions, session)
sessions = append(sessions, &session)
return err
})
})

View File

@ -11,23 +11,23 @@ func Initialize(dir string) {
}
type Store interface {
GetUsers() ([]User, error)
SaveUser(*User) error
DeleteUser(*User) error
GetUsers() ([]*User, error)
SaveUser(user *User) error
DeleteUser(user *User) error
GetServers(*User) ([]Server, error)
AddServer(*User, *Server) error
RemoveServer(*User, string) error
SetNick(*User, string, string) error
SetServerName(*User, string, string) error
GetServers(user *User) ([]*Server, error)
AddServer(user *User, server *Server) error
RemoveServer(user *User, host string) error
SetNick(user *User, nick, host string) error
SetServerName(user *User, name, host string) error
GetChannels(*User) ([]Channel, error)
AddChannel(*User, *Channel) error
RemoveChannel(*User, string, string) error
GetChannels(user *User) ([]*Channel, error)
AddChannel(user *User, channel *Channel) error
RemoveChannel(user *User, server, channel string) error
}
type SessionStore interface {
GetSessions() ([]session.Session, error)
GetSessions() ([]*session.Session, error)
SaveSession(session *session.Session) error
DeleteSession(key string) error
}

View File

@ -35,15 +35,15 @@ func NewUser(store Store) (*User, error) {
return user, nil
}
func LoadUsers(store Store) ([]User, error) {
func LoadUsers(store Store) ([]*User, error) {
users, err := store.GetUsers()
if err != nil {
return nil, err
}
for i := range users {
users[i].store = store
users[i].loadCertificate()
for _, user := range users {
user.store = store
user.loadCertificate()
}
return users, nil
@ -79,7 +79,7 @@ type Server struct {
Realname string
}
func (u *User) GetServers() ([]Server, error) {
func (u *User) GetServers() ([]*Server, error) {
return u.store.GetServers(u)
}
@ -105,7 +105,7 @@ type Channel struct {
Topic string
}
func (u *User) GetChannels() ([]Channel, error) {
func (u *User) GetChannels() ([]*Channel, error) {
return u.store.GetChannels(u)
}

View File

@ -27,29 +27,29 @@ func TestUser(t *testing.T) {
user, err := storage.NewUser(db)
assert.Nil(t, err)
srv := storage.Server{
srv := &storage.Server{
Name: "Freenode",
Host: "irc.freenode.net",
Nick: "test",
}
chan1 := storage.Channel{
chan1 := &storage.Channel{
Server: srv.Host,
Name: "#test",
}
chan2 := storage.Channel{
chan2 := &storage.Channel{
Server: srv.Host,
Name: "#testing",
}
user.AddServer(&srv)
user.AddChannel(&chan1)
user.AddChannel(&chan2)
user.AddServer(srv)
user.AddChannel(chan1)
user.AddChannel(chan2)
users, err := storage.LoadUsers(db)
assert.Nil(t, err)
assert.Len(t, users, 1)
user = &users[0]
user = users[0]
assert.Equal(t, uint64(1), user.ID)
servers, err := user.GetServers()