Skip to content

Commit

Permalink
Merge pull request #6 from WindowsSov8forUs/master
Browse files Browse the repository at this point in the history
feat: 添加 select 方法,调整列表格式
  • Loading branch information
WindowsSov8forUs authored Jun 18, 2024
2 parents f5b421e + d99df3b commit 63a4f1a
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pkg/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ type Channel struct {

// Channel 分页列表
type ChannelList struct {
Data []Channel `json:"data"` // 数据
Next string `json:"next,omitempty"` // 下一页的令牌
Data []*Channel `json:"data"` // 数据
Next string `json:"next,omitempty"` // 下一页的令牌
}
4 changes: 2 additions & 2 deletions pkg/guild/guild.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ type Guild struct {

// Guild 分页列表
type GuildList struct {
Data []Guild `json:"data"` // 数据
Next string `json:"next,omitempty"` // 下一页的令牌
Data []*Guild `json:"data"` // 数据
Next string `json:"next,omitempty"` // 下一页的令牌
}
4 changes: 2 additions & 2 deletions pkg/guildmember/guildmember.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ type GuildMember struct {

// GuildMember 分页列表
type GuildMemberList struct {
Data []GuildMember `json:"data"` // 数据
Next string `json:"next,omitempty"` // 下一页的令牌
Data []*GuildMember `json:"data"` // 数据
Next string `json:"next,omitempty"` // 下一页的令牌
}
4 changes: 2 additions & 2 deletions pkg/guildrole/guildrole.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ type GuildRole struct {

// GuildRole 分页列表
type GuildRoleList struct {
Data []GuildRole `json:"data"` // 数据
Next string `json:"next,omitempty"` // 下一页的令牌
Data []*GuildRole `json:"data"` // 数据
Next string `json:"next,omitempty"` // 下一页的令牌
}
34 changes: 31 additions & 3 deletions pkg/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ type Message struct {

// Message 双向分页列表
type MessageBidiList struct {
Data []Message `json:"data"` // 数据
Prev string `json:"prev,omitempty"` // 上一页的令牌
Next string `json:"next,omitempty"` // 下一页的令牌
Data []*Message `json:"data"` // 数据
Prev string `json:"prev,omitempty"` // 上一页的令牌
Next string `json:"next,omitempty"` // 下一页的令牌
}

func (m *Message) Decode(elements []MessageElement) error {
Expand All @@ -41,3 +41,31 @@ func (m *Message) Encode() ([]MessageElement, error) {
}
return Parse(m.Content)
}

// Select 选取特定的消息元素
func Select(element MessageElement, tag string) []MessageElement {
var selected []MessageElement

if element.Tag() == tag {
selected = append(selected, element)
}

selected = selectFromSlide(element.GetChildren(), tag, selected)

return selected
}

// selectFromSlide 从列表中选取特定的消息元素
func selectFromSlide(elements []MessageElement, tag string, selected []MessageElement) []MessageElement {
for _, element := range elements {
if element.Tag() == tag {
selected = append(selected, element)
}
}

for _, element := range elements {
selected = append(selected, selectFromSlide(element.GetChildren(), tag, selected)...)
}

return selected
}
1 change: 1 addition & 0 deletions pkg/message/message_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type MessageElement interface {
Tag() string
Stringify() string
Alias() []string
GetChildren() []MessageElement
}

type noAliasMessageElement struct {
Expand Down
4 changes: 4 additions & 0 deletions pkg/message/message_element_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func (e *MessageElementText) Parse(n *html.Node) (MessageElement, error) {
return nil, nil
}

func (e *MessageElementText) GetChildren() []MessageElement {
return nil
}

type MessageElementAt struct {
*noAliasMessageElement
*ChildrenMessageElement
Expand Down
4 changes: 4 additions & 0 deletions pkg/message/message_element_typography.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (e *MessageElmentBr) Parse(n *html.Node) (MessageElement, error) {
return &MessageElmentBr{}, nil
}

func (e *MessageElmentBr) GetChildren() []MessageElement {
return nil
}

type MessageElmentP struct {
*noAliasMessageElement
*ChildrenMessageElement
Expand Down
4 changes: 2 additions & 2 deletions pkg/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ type User struct {

// User 分页列表
type UserList struct {
Data []User `json:"data"` // 数据
Next string `json:"next,omitempty"` // 下一页的令牌
Data []*User `json:"data"` // 数据
Next string `json:"next,omitempty"` // 下一页的令牌
}

0 comments on commit 63a4f1a

Please sign in to comment.