-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
treat message by pointer #38
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,29 @@ import ( | |
"strings" | ||
) | ||
|
||
// Messages is an array of `*Message`. | ||
type Messages []*Message | ||
|
||
// Sort sorts messages by `Message.Ts` ascendant order. | ||
func (msgs Messages) Sort() { | ||
sort.SliceStable(msgs, func(i, j int) bool { | ||
// must be the same digits, so no need to convert the timestamp to a number | ||
return msgs[i].Ts < msgs[j].Ts | ||
}) | ||
} | ||
|
||
// MessagesMap is a map, maps MessageMonthKey to Messages. | ||
type MessagesMap map[MessageMonthKey]Messages | ||
|
||
// Keys returns all keys in the map. | ||
func (mm MessagesMap) Keys() []MessageMonthKey { | ||
keys := make([]MessageMonthKey, 0, len(mm)) | ||
for key := range mm { | ||
keys = append(keys, key) | ||
} | ||
return keys | ||
} | ||
|
||
// MessageTable : メッセージデータを保持する | ||
// スレッドは投稿時刻からどのスレッドへの返信かが判断できるためThreadMapのキー | ||
// はtsである。 | ||
|
@@ -20,7 +43,7 @@ import ( | |
type MessageTable struct { | ||
// key: thread timestamp | ||
ThreadMap map[string]*Thread | ||
MsgsMap map[MessageMonthKey][]Message | ||
MsgsMap MessagesMap | ||
// key: file path | ||
loadedFiles map[string]struct{} | ||
} | ||
|
@@ -32,7 +55,7 @@ type MessageTable struct { | |
func NewMessageTable() *MessageTable { | ||
return &MessageTable{ | ||
ThreadMap: map[string]*Thread{}, | ||
MsgsMap: map[MessageMonthKey][]Message{}, | ||
MsgsMap: MessagesMap{}, | ||
loadedFiles: map[string]struct{}{}, | ||
} | ||
} | ||
|
@@ -79,13 +102,15 @@ func (m *MessageTable) ReadLogFile(path string) error { | |
return nil | ||
} | ||
|
||
var msgs []Message | ||
var visibleMsgs []Message | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 少し後ろの、本当に必要になる直前で宣言するように移動した。 |
||
var msgs Messages | ||
err = ReadFileAsJSON(path, &msgs) | ||
if err != nil { | ||
return fmt.Errorf("failed to unmarshal %s: %w", path, err) | ||
} | ||
for i, msg := range msgs { | ||
|
||
// assort messages, visible and threaded. | ||
var visibleMsgs Messages | ||
for _, msg := range msgs { | ||
if !msg.IsVisible() { | ||
continue | ||
} | ||
|
@@ -97,18 +122,12 @@ func (m *MessageTable) ReadLogFile(path string) error { | |
visibleMsgs = append(visibleMsgs, msg) | ||
} | ||
if threadTs != "" { | ||
if m.ThreadMap[threadTs] == nil { | ||
m.ThreadMap[threadTs] = &Thread{} | ||
} | ||
if msg.IsRootOfThread() { | ||
m.ThreadMap[threadTs].rootMsg = &msgs[i] | ||
} else { | ||
if m.ThreadMap[threadTs].replies == nil { | ||
m.ThreadMap[threadTs].replies = []Message{msg} | ||
} else { | ||
m.ThreadMap[threadTs].replies = append(m.ThreadMap[threadTs].replies, msg) | ||
} | ||
thread, ok := m.ThreadMap[threadTs] | ||
if !ok { | ||
thread = &Thread{} | ||
m.ThreadMap[threadTs] = thread | ||
} | ||
thread.Put(msg) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mapの値がポインタならば以下の3ステップで書くのが見通しが良くなるコツ。
|
||
} | ||
|
||
|
@@ -117,22 +136,15 @@ func (m *MessageTable) ReadLogFile(path string) error { | |
return err | ||
} | ||
if len(visibleMsgs) != 0 { | ||
if _, ok := m.MsgsMap[key]; !ok { | ||
m.MsgsMap[key] = visibleMsgs | ||
} else { | ||
m.MsgsMap[key] = append(m.MsgsMap[key], visibleMsgs...) | ||
} | ||
Comment on lines
-120
to
-124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここは上記のスライス版。
のコンボでこうなる。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. あー、nil スライス append に渡せないと勘違いしてました |
||
m.MsgsMap[key] = append(m.MsgsMap[key], visibleMsgs...) | ||
} | ||
|
||
for key, msgs := range m.MsgsMap { | ||
sort.SliceStable(m.MsgsMap[key], func(i, j int) bool { | ||
// must be the same digits, so no need to convert the timestamp to a number | ||
return m.MsgsMap[key][i].Ts < m.MsgsMap[key][j].Ts | ||
}) | ||
Comment on lines
-128
to
-131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
for _, msgs := range m.MsgsMap { | ||
msgs.Sort() | ||
var lastUser string | ||
for i, msg := range msgs { | ||
for _, msg := range msgs { | ||
if lastUser == msg.User { | ||
(&msgs[i]).Trail = true | ||
msg.Trail = true | ||
} else { | ||
lastUser = msg.User | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,21 +9,30 @@ import ( | |
// repliesにはそのスレッドへの返信メッセージが入る。先頭メッセージは含まない。 | ||
type Thread struct { | ||
rootMsg *Message | ||
replies []Message | ||
replies Messages | ||
} | ||
|
||
func (t Thread) LastReplyTime() time.Time { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 以下レシーバーの名前を テストを書くと There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // 丁度別件で踏みました... |
||
return TsToDateTime(t.replies[len(t.replies)-1].Ts) | ||
func (th Thread) LastReplyTime() time.Time { | ||
return TsToDateTime(th.replies[len(th.replies)-1].Ts) | ||
} | ||
|
||
func (t Thread) ReplyCount() int { | ||
return len(t.replies) | ||
func (th Thread) ReplyCount() int { | ||
return len(th.replies) | ||
} | ||
|
||
func (t Thread) RootText() string { | ||
return t.rootMsg.Text | ||
func (th Thread) RootText() string { | ||
return th.rootMsg.Text | ||
} | ||
|
||
func (t Thread) Replies() []Message { | ||
return t.replies | ||
func (th Thread) Replies() Messages { | ||
return th.replies | ||
} | ||
|
||
// Put puts a message to the thread as "root" or "reply". | ||
func (th *Thread) Put(m *Message) { | ||
if m.IsRootOfThread() { | ||
th.rootMsg = m | ||
} else { | ||
th.replies = append(th.replies, m) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MessageMap.Keys()
へ移動した。