Skip to content

Commit

Permalink
feat: use If and remove Nil node let card easy to use
Browse files Browse the repository at this point in the history
  • Loading branch information
chyroc committed Dec 4, 2023
1 parent 6c3220b commit d5b2100
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 11 deletions.
2 changes: 1 addition & 1 deletion card/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Card(modules ...lark.MessageContentCardModule) *lark.MessageContentCard {
return &lark.MessageContentCard{
Header: nil,
Config: Config(),
Modules: modules,
Modules: removeNilMessageContentCardModule(modules),
I18NModules: nil,
}
}
Expand Down
2 changes: 1 addition & 1 deletion card/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func Column(modules ...lark.MessageContentCardModule) *lark.MessageContentCardModuleColumn {
return &lark.MessageContentCardModuleColumn{
Modules: modules,
Modules: removeNilMessageContentCardModule(modules),
Width: nil,
Weight: nil,
VerticalAlign: nil,
Expand Down
2 changes: 1 addition & 1 deletion card/element_image_combination.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ func ElementImageCombination(mode lark.MessageContentCardElementCombinationMode,
res := &lark.MessageContentCardElementImageCombination{
CombinationMode: mode,
}
res.SetImageList(images...)
res.SetImageList(removeNilString(images)...)
return res
}
4 changes: 2 additions & 2 deletions card/element_select_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import (
func StaticSelectMenu(options ...*lark.MessageContentCardObjectOption) *lark.MessageContentCardElementSelectMenu {
return &lark.MessageContentCardElementSelectMenu{
Tag: lark.MessageContentCardElementTagSelectStatic,
Options: options,
Options: removeNilMessageContentCardObjectOption(options),
Value: nil,
}
}

func PersonSelectMenuForIDs(ids ...string) *lark.MessageContentCardElementSelectMenu {
var options []*lark.MessageContentCardObjectOption
for _, v := range ids {
for _, v := range removeNilString(ids) {
options = append(options, PersonOption(v))
}
return &lark.MessageContentCardElementSelectMenu{
Expand Down
71 changes: 71 additions & 0 deletions card/if.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package card

import (
"unsafe"

"github.com/chyroc/lark"
)

type face struct {
x uintptr
data unsafe.Pointer
}

func isNil(v any) bool {

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any

Check failure on line 14 in card/if.go

View workflow job for this annotation

GitHub Actions / run

undefined: any
return (*face)(unsafe.Pointer(&v)).data == nil
}

func removeNilMessageContentCardModule(data []lark.MessageContentCardModule) []lark.MessageContentCardModule {
res := make([]lark.MessageContentCardModule, 0, len(data))
for _, v := range data {
if isNil(v) {
continue
}
res = append(res, v)
}
return res
}

func removeNilMessageContentCardElement(data []lark.MessageContentCardElement) []lark.MessageContentCardElement {
res := make([]lark.MessageContentCardElement, 0, len(data))
for _, v := range data {
if isNil(v) {
continue
}
res = append(res, v)
}
return res
}

func removeNilMessageContentCardObjectOption(data []*lark.MessageContentCardObjectOption) []*lark.MessageContentCardObjectOption {
res := make([]*lark.MessageContentCardObjectOption, 0, len(data))
for _, v := range data {
if isNil(v) {
continue
}
res = append(res, v)
}
return res
}

func removeNilMessageContentCardModuleColumn(data []*lark.MessageContentCardModuleColumn) []*lark.MessageContentCardModuleColumn {
res := make([]*lark.MessageContentCardModuleColumn, 0, len(data))
for _, v := range data {
if isNil(v) {
continue
}
res = append(res, v)
}
return res
}

func removeNilString(data []string) []string {
res := make([]string, 0, len(data))
for _, v := range data {
if isNil(v) {
continue
}
res = append(res, v)
}
return res
}
17 changes: 17 additions & 0 deletions card/if_generics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build go1.18

package card

func If[T any](cond bool, val T, otherwise T) T {
if cond {
return val
}
return otherwise
}

func IfLay[T any](cond bool, val func() T, otherwise func() T) T {
if cond {
return val()
}
return otherwise()
}
2 changes: 1 addition & 1 deletion card/module_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import (
// Action https://open.feishu.cn/document/ukTMukTMukTM/uYjNwUjL2YDM14iN2ATN
func Action(actions ...lark.MessageContentCardElement) *lark.MessageContentCardModuleAction {
return &lark.MessageContentCardModuleAction{
Actions: actions,
Actions: removeNilMessageContentCardElement(actions),
}
}
2 changes: 1 addition & 1 deletion card/module_column_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func ColumnSet(columns ...*lark.MessageContentCardModuleColumn) *lark.MessageCon
bgStyle := "default"
hSpacing := "default"
return &lark.MessageContentCardModuleColumnSet{
Columns: columns,
Columns: removeNilMessageContentCardModuleColumn(columns),
FlexMode: &flexMode,
BackgroundStyle: &bgStyle,
HorizontalSpacing: &hSpacing,
Expand Down
2 changes: 1 addition & 1 deletion card/module_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ import (
func Form(name string, elements ...lark.MessageContentCardElement) *lark.MessageContentCardModuleForm {
return &lark.MessageContentCardModuleForm{
Name: name,
Elements: elements,
Elements: removeNilMessageContentCardElement(elements),
}
}
2 changes: 1 addition & 1 deletion card/module_note.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import (
// Note 注意:note 只能填充 text 和 image 对象
func Note(elements ...lark.MessageContentCardElement) *lark.MessageContentCardModuleNote {
return &lark.MessageContentCardModuleNote{
Elements: elements,
Elements: removeNilMessageContentCardElement(elements),
}
}
2 changes: 1 addition & 1 deletion card/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ import (

// Modules 构造模块列表
func Modules(modules ...lark.MessageContentCardModule) []lark.MessageContentCardModule {
return modules
return removeNilMessageContentCardModule(modules)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/chyroc/lark

go 1.16
go 1.18

require github.com/stretchr/testify v1.8.2

0 comments on commit d5b2100

Please sign in to comment.