mtgbot/modules/apiact/line/component.go

173 lines
4.2 KiB
Go

package line
import (
"errors"
)
// Component -
type Component struct {
Type string `json:"type"`
}
// BoxComponent -
type BoxComponent struct {
Component
Layout string `json:"layout"`
Contents []interface{} `json:"contents"`
Spacing NullString `json:"spacing,omitempty"`
Action interface{} `json:"action,omitempty"`
Flex NullInt `json:"flex,omitempty"`
Margin NullString `json:"margin,omitempty"`
}
// SetLayout -
func (p *BoxComponent) SetLayout(layout string) {
switch layout {
case "horizontal":
case "vertical":
case "baseline":
p.Layout = layout
default:
p.Layout = "horizontal"
}
}
// SetContent -
func (p *BoxComponent) SetContent(content interface{}) (err error) {
if p.Layout == "horizontal" || p.Layout == "vertical" {
switch content.(type) {
case *BoxComponent:
case *ButtonComponent:
case *FillerComponent:
case *ImageComponent:
case *SeparatorComponent:
case *SpacerComponent:
case *TextComponent:
p.Contents = append(p.Contents, content)
default:
return errors.New("content type not allow")
}
} else if p.Layout == "baseline" {
switch content.(type) {
case *FillerComponent:
case *IconComponent:
case *SpacerComponent:
case *TextComponent:
p.Contents = append(p.Contents, content)
default:
return errors.New("content type not allow")
}
} else {
return errors.New("layout type not valid")
}
return
}
// ButtonComponent -
type ButtonComponent struct {
Component
Action interface{} `json:"action"`
Height NullString `json:"height,omitempty"`
Style NullString `json:"style,omitempty"`
Color NullString `json:"color,omitempty"`
Gravity NullString `json:"gravity,omitempty"`
Flex NullInt `json:"flex,omitempty"`
Margin NullString `json:"margin,omitempty"`
}
// FillerComponent -
type FillerComponent struct {
Component
}
// IconComponent -
type IconComponent struct {
Component
URL string `json:"url"`
Margin NullString `json:"margin,omitempty"`
Size NullString `json:"size,omitempty"`
AspectRatio NullString `json:"aspectRatio,omitempty"`
}
// ImageComponent -
type ImageComponent struct {
Component
URL string `json:"url"`
Flex NullInt `json:"flex,omitempty"`
Margin NullString `json:"margin,omitempty"`
Align NullString `json:"align,omitempty"`
Gravity NullString `json:"gravity,omitempty"`
Size NullString `json:"size,omitempty"`
AspectRatio NullString `json:"aspectRatio,omitempty"`
AspectMode NullString `json:"aspectMode,omitempty"`
BackgroundColor NullString `json:"backgroundColor,omitempty"`
Action interface{} `json:"action,omitempty"`
}
// SeparatorComponent -
type SeparatorComponent struct {
Component
Margin NullString `json:"margin,omitempty"`
Color NullString `json:"color,omitempty"`
}
// SpacerComponent -
type SpacerComponent struct {
Component
Size NullString `json:"size,omitempty"`
}
// TextComponent -
type TextComponent struct {
Component
Text string `json:"text"`
Flex NullInt `json:"flex,omitempty"`
Margin NullString `json:"margin,omitempty"`
Size NullString `json:"size,omitempty"`
Align NullString `json:"align,omitempty"`
Gravity NullString `json:"gravity,omitempty"`
Wrap bool `json:"wrap"`
MaxLines NullInt `json:"maxLines,omitempty"`
Weight NullString `json:"weight,omitempty"`
Color NullString `json:"color,omitempty"`
Action interface{} `json:"action,omitempty"`
}
// NewBoxComponent -
func NewBoxComponent(layout string) (box *BoxComponent) {
box = &BoxComponent{}
box.Type = "box"
box.SetLayout(layout)
return
}
// NewButtonComponent -
func NewButtonComponent() (btn *ButtonComponent) {
btn = &ButtonComponent{}
btn.Type = "button"
return
}
// NewFillerComponent -
func NewFillerComponent() (filler *FillerComponent) {
filler = &FillerComponent{}
filler.Type = "filler"
return
}
// NewIconComponent -
func NewIconComponent(imgUrl string) (icon *IconComponent) {
icon = &IconComponent{}
icon.Type = "icon"
icon.URL = imgUrl
return
}
// NewImageComponent -
func NewImageComponent(imgUrl string) (image *ImageComponent) {
image = &ImageComponent{}
image.Type = "image"
image.URL = imgUrl
return
}