Skip to content

Commit

Permalink
fix(tools/site): brick unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
bububa committed Jul 28, 2022
1 parent 3116c2b commit dea98bd
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 6 deletions.
4 changes: 4 additions & 0 deletions marketing-api/model/tools/site/brick.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ type BaseBrick struct {
// Float 站点组件浮动方式: none(默认为无);其他值可选:top(置顶);right(居右);bottom(置底)
Float string `json:"float,omitempty"`
}

func (b BaseBrick) Type() BrickType {
return BrickType(b.Name)
}
44 changes: 44 additions & 0 deletions marketing-api/model/tools/site/button_brick.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package site

import (
"encoding/json"
)

// IconType 图标信息
type IconType string

Expand Down Expand Up @@ -68,6 +72,46 @@ type ButtonEvent struct {
Behavior Event `json:"behavior,omitempty"`
}

type tmpButtonEvent struct {
// Trigger 触发
Trigger *EventTrigger `json:"trigger,omitempty"`
// Behavior 事件行为描述,behavior目前已开放DownloadEvent 和LinkEvent以及TelephoneEvent
Behavior json.RawMessage `json:"behavior,omitempty"`
}

func (e *ButtonEvent) UnmarshalJSON(b []byte) error {
var tmp tmpButtonEvent
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
event := ButtonEvent{
Trigger: tmp.Trigger,
}
var base BaseEvent
if err := json.Unmarshal(tmp.Behavior, &base); err != nil {
return err
}
switch base.Type() {
case EventType_DownloadEvent:
var data DownloadEvent
if err := json.Unmarshal(tmp.Behavior, &data); err != nil {
return err
}
case EventType_LinkEvent:
var data LinkEvent
if err := json.Unmarshal(tmp.Behavior, &data); err != nil {
return err
}
case EventType_TelephoneEvent:
var data TelephoneEvent
if err := json.Unmarshal(tmp.Behavior, &data); err != nil {
return err
}
}
*e = event
return nil
}

// TriggerType 触发事件
type TriggerType string

Expand Down
3 changes: 1 addition & 2 deletions marketing-api/model/tools/site/download_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package site
// DownloadEvent DownloadEvent事件行为描述
// 特殊说明:基于应用下载安全要求,在创建和修改站点接口中,如果希望添加应用下载链接,有两种情况:①DownloadEvent类型的按钮组件可以填入安卓/IOS下载链接;②LinkEvent类型的按钮组件的URL支持落地页链接和IOS下载链接,不支持安卓下载链接,仅DownloadEvent事件下的安卓URL参数下可填写安卓下载链接;综上,除了这两种情况外其他建站组件中的URL参数都不允许填入应用下载链接,如果填入将会创建失败。
type DownloadEvent struct {
// Name 事件名称,传“DownloadEvent”
Name string `json:"name,omitempty"`
BaseEvent
// IOSLink ios链接信息
IOSLink *Link `json:"ios_link,omitempty"`
// AndroidLink 安卓链接信息
Expand Down
8 changes: 8 additions & 0 deletions marketing-api/model/tools/site/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ const (
type Event interface {
Type() EventType
}

type BaseEvent struct {
Name string `json:"name,omitempty"`
}

func (e BaseEvent) Type() EventType {
return EventType(e.Name)
}
3 changes: 1 addition & 2 deletions marketing-api/model/tools/site/link_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package site

// LinkEvent LinkEvent事件行为描述
type LinkEvent struct {
// Name 事件名称,传LinkEvent
Name string `json:"name,omitempty"`
BaseEvent
// Link 链接信息(支持落地页链接和IOS下载链接,不支持安卓下载链接,仅DownloadEvent事件下的安卓URL参数下可填写安卓下载链接)
Link *Link `json:"link,omitempty"`
}
Expand Down
81 changes: 81 additions & 0 deletions marketing-api/model/tools/site/site.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package site

import (
"encoding/json"

"github.com/bububa/oceanengine/marketing-api/enum"
"github.com/bububa/oceanengine/marketing-api/model"
)
Expand All @@ -19,6 +21,19 @@ type SiteDetail struct {
Thumbnail string `json:"thumbnail,omitempty"`
}

type tmpSiteDetail struct {
// Bricks 具体见返回示例业务数据(新建或更新时传递的数据)
Bricks []json.RawMessage `json:"bricks,omitempty"`
// ID 站点ID
ID model.FlexUint64 `json:"id,omitempty"`
// Status 站点状态
Status enum.SiteStatus `json:"status,omitempty"`
// SiteType 建站类型
SiteType enum.SiteType `json:"site_type,omitempty"`
// Thumbnail 缩略图
Thumbnail string `json:"thumbnail,omitempty"`
}

// Site 站点信息
type Site struct {
// SiteID 站点ID
Expand All @@ -34,3 +49,69 @@ type Site struct {
// Thumbnail 缩略图
Thumbnail string `json:"thumbnail,omitempty"`
}

// UnmarshalJSON implement json Unmarshal interface
func (s *SiteDetail) UnmarshalJSON(b []byte) (err error) {
var tmp tmpSiteDetail
if err = json.Unmarshal(b, &tmp); err != nil {
return
}
detail := SiteDetail{
ID: tmp.ID,
Status: tmp.Status,
SiteType: tmp.SiteType,
Thumbnail: tmp.Thumbnail,
}
for _, b := range tmp.Bricks {
var base BaseBrick
if err := json.Unmarshal(b, &base); err != nil {
return err
}
switch base.Type() {
case XrVideo:
var data VideoBrick
if err := json.Unmarshal(b, &data); err != nil {
return err
}
detail.Bricks = append(detail.Bricks, data)
case XrPicture:
var data ImageBrick
if err := json.Unmarshal(b, &data); err != nil {
return err
}
detail.Bricks = append(detail.Bricks, data)
case XrPictureGroup:
var data ImagesBrick
if err := json.Unmarshal(b, &data); err != nil {
return err
}
detail.Bricks = append(detail.Bricks, data)
case XrSimpleText:
var data TextBrick
if err := json.Unmarshal(b, &data); err != nil {
return err
}
detail.Bricks = append(detail.Bricks, data)
case XrRichText:
var data RichTextBrick
if err := json.Unmarshal(b, &data); err != nil {
return err
}
detail.Bricks = append(detail.Bricks, data)
case XrForm:
var data FormBrick
if err := json.Unmarshal(b, &data); err != nil {
return err
}
detail.Bricks = append(detail.Bricks, data)
case XrButton:
var data ButtonBrick
if err := json.Unmarshal(b, &data); err != nil {
return err
}
detail.Bricks = append(detail.Bricks, data)
}
}
*s = detail
return nil
}
3 changes: 1 addition & 2 deletions marketing-api/model/tools/site/telephone_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package site

// TelephoneEvent TelephoneEvent事件行为描述
type TelephoneEvent struct {
// Name 事件名称,传“TelephoneEvent” 1 <= name长度值 <= 20
Name string `json:"name,omitempty"`
BaseEvent
// SmartPhone 智能电话信息
SmartPhone *SmartPhone `json:"smart_phone,omitempty"`
}
Expand Down

0 comments on commit dea98bd

Please sign in to comment.