github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/template_unmarshal.go (about) 1 package linebot 2 3 import ( 4 "encoding/json" 5 "errors" 6 ) 7 8 // UnmarshalTemplateJSON function 9 func UnmarshalTemplateJSON(data []byte) (Template, error) { 10 raw := rawTemplate{} 11 if err := json.Unmarshal(data, &raw); err != nil { 12 return nil, err 13 } 14 return raw.Template, nil 15 } 16 17 type rawTemplate struct { 18 Type TemplateType `json:"type"` 19 Template Template `json:"-"` 20 } 21 22 func (t *rawTemplate) UnmarshalJSON(data []byte) error { 23 type alias rawTemplate 24 raw := alias{} 25 if err := json.Unmarshal(data, &raw); err != nil { 26 return err 27 } 28 var template Template 29 switch raw.Type { 30 case TemplateTypeButtons: 31 template = &ButtonsTemplate{} 32 case TemplateTypeConfirm: 33 template = &ConfirmTemplate{} 34 case TemplateTypeCarousel: 35 template = &CarouselTemplate{} 36 case TemplateTypeImageCarousel: 37 template = &ImageCarouselTemplate{} 38 default: 39 return errors.New("invalid template type") 40 } 41 if err := json.Unmarshal(data, template); err != nil { 42 return err 43 } 44 t.Type = raw.Type 45 t.Template = template 46 return nil 47 } 48 49 // UnmarshalJSON method for ButtonsTemplate 50 func (t *ButtonsTemplate) UnmarshalJSON(data []byte) error { 51 type alias ButtonsTemplate 52 raw := struct { 53 Actions []rawAction `json:"actions"` 54 *alias 55 }{ 56 alias: (*alias)(t), 57 } 58 if err := json.Unmarshal(data, &raw); err != nil { 59 return err 60 } 61 actions := make([]TemplateAction, len(raw.Actions)) 62 for i, action := range raw.Actions { 63 actions[i] = action.Action 64 } 65 t.Actions = actions 66 return nil 67 } 68 69 // UnmarshalJSON method for ConfirmTemplate 70 func (t *ConfirmTemplate) UnmarshalJSON(data []byte) error { 71 type alias ConfirmTemplate 72 raw := struct { 73 Actions []rawAction `json:"actions"` 74 *alias 75 }{ 76 alias: (*alias)(t), 77 } 78 if err := json.Unmarshal(data, &raw); err != nil { 79 return err 80 } 81 actions := make([]TemplateAction, len(raw.Actions)) 82 for i, action := range raw.Actions { 83 actions[i] = action.Action 84 } 85 t.Actions = actions 86 return nil 87 } 88 89 type rawColumn struct { 90 Column CarouselColumn `json:"-"` 91 } 92 93 func (c *rawColumn) UnmarshalJSON(data []byte) error { 94 type alias CarouselColumn 95 raw := struct { 96 DefaultAction rawAction `json:"defaultAction"` 97 Actions []rawAction `json:"actions"` 98 *alias 99 }{ 100 alias: (*alias)(&c.Column), 101 } 102 if err := json.Unmarshal(data, &raw); err != nil { 103 return err 104 } 105 c.Column.DefaultAction = raw.DefaultAction.Action 106 actions := make([]TemplateAction, len(raw.Actions)) 107 for i, action := range raw.Actions { 108 actions[i] = action.Action 109 } 110 c.Column.Actions = actions 111 return nil 112 } 113 114 // UnmarshalJSON method for CarouselTemplate 115 func (t *CarouselTemplate) UnmarshalJSON(data []byte) error { 116 type alias CarouselTemplate 117 raw := struct { 118 Columns []rawColumn `json:"columns"` 119 *alias 120 }{ 121 alias: (*alias)(t), 122 } 123 if err := json.Unmarshal(data, &raw); err != nil { 124 return err 125 } 126 columns := make([]*CarouselColumn, len(raw.Columns)) 127 for i, column := range raw.Columns { 128 c := column.Column 129 columns[i] = &c 130 } 131 t.Columns = columns 132 return nil 133 } 134 135 type rawImageColumn struct { 136 Column ImageCarouselColumn `json:"-"` 137 } 138 139 func (c *rawImageColumn) UnmarshalJSON(data []byte) error { 140 type alias ImageCarouselColumn 141 raw := struct { 142 Action rawAction `json:"action"` 143 *alias 144 }{ 145 alias: (*alias)(&c.Column), 146 } 147 if err := json.Unmarshal(data, &raw); err != nil { 148 return err 149 } 150 c.Column.Action = raw.Action.Action 151 return nil 152 } 153 154 // UnmarshalJSON method for ImageCarouselTemplate 155 func (t *ImageCarouselTemplate) UnmarshalJSON(data []byte) error { 156 type alias ImageCarouselTemplate 157 raw := struct { 158 Columns []rawImageColumn `json:"columns"` 159 *alias 160 }{ 161 alias: (*alias)(t), 162 } 163 if err := json.Unmarshal(data, &raw); err != nil { 164 return err 165 } 166 columns := make([]*ImageCarouselColumn, len(raw.Columns)) 167 for i, column := range raw.Columns { 168 c := column.Column 169 columns[i] = &c 170 } 171 t.Columns = columns 172 return nil 173 }