github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/flex_unmarshal.go (about)

     1  // Copyright 2018 LINE Corporation
     2  //
     3  // LINE Corporation licenses this file to you under the Apache License,
     4  // version 2.0 (the "License"); you may not use this file except in compliance
     5  // with the License. You may obtain a copy of the License at:
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package linebot
    16  
    17  import (
    18  	"encoding/json"
    19  	"errors"
    20  )
    21  
    22  // UnmarshalFlexMessageJSON function
    23  func UnmarshalFlexMessageJSON(data []byte) (FlexContainer, error) {
    24  	raw := rawFlexContainer{}
    25  	if err := json.Unmarshal(data, &raw); err != nil {
    26  		return nil, err
    27  	}
    28  	return raw.Container, nil
    29  }
    30  
    31  type rawFlexContainer struct {
    32  	Type      FlexContainerType `json:"type"`
    33  	Container FlexContainer     `json:"-"`
    34  }
    35  
    36  func (c *rawFlexContainer) UnmarshalJSON(data []byte) error {
    37  	type alias rawFlexContainer
    38  	raw := alias{}
    39  	if err := json.Unmarshal(data, &raw); err != nil {
    40  		return err
    41  	}
    42  	var container FlexContainer
    43  	switch raw.Type {
    44  	case FlexContainerTypeBubble:
    45  		container = &BubbleContainer{}
    46  	case FlexContainerTypeCarousel:
    47  		container = &CarouselContainer{}
    48  	default:
    49  		return errors.New("invalid container type")
    50  	}
    51  	if err := json.Unmarshal(data, container); err != nil {
    52  		return err
    53  	}
    54  	c.Type = raw.Type
    55  	c.Container = container
    56  	return nil
    57  }
    58  
    59  type rawFlexComponent struct {
    60  	Type      FlexComponentType `json:"type"`
    61  	Component FlexComponent     `json:"-"`
    62  }
    63  
    64  func (c *rawFlexComponent) UnmarshalJSON(data []byte) error {
    65  	type alias rawFlexComponent
    66  	raw := alias{}
    67  	if err := json.Unmarshal(data, &raw); err != nil {
    68  		return err
    69  	}
    70  	var component FlexComponent
    71  	switch raw.Type {
    72  	case FlexComponentTypeBox:
    73  		component = &BoxComponent{}
    74  	case FlexComponentTypeButton:
    75  		component = &ButtonComponent{}
    76  	case FlexComponentTypeFiller:
    77  		component = &FillerComponent{}
    78  	case FlexComponentTypeIcon:
    79  		component = &IconComponent{}
    80  	case FlexComponentTypeImage:
    81  		component = &ImageComponent{}
    82  	case FlexComponentTypeSeparator:
    83  		component = &SeparatorComponent{}
    84  	case FlexComponentTypeSpacer:
    85  		component = &SpacerComponent{}
    86  	case FlexComponentTypeText:
    87  		component = &TextComponent{}
    88  	case FlexComponentTypeVideo:
    89  		component = &VideoComponent{}
    90  	default:
    91  		return errors.New("invalid flex component type")
    92  	}
    93  	if err := json.Unmarshal(data, component); err != nil {
    94  		return err
    95  	}
    96  	c.Type = raw.Type
    97  	c.Component = component
    98  	return nil
    99  }
   100  
   101  type rawAction struct {
   102  	Type   ActionType     `json:"type"`
   103  	Action TemplateAction `json:"-"`
   104  }
   105  
   106  func (c *rawAction) UnmarshalJSON(data []byte) error {
   107  	type alias rawAction
   108  	raw := alias{}
   109  	if err := json.Unmarshal(data, &raw); err != nil {
   110  		return err
   111  	}
   112  	var action TemplateAction
   113  	switch raw.Type {
   114  	case ActionTypeURI:
   115  		action = &URIAction{}
   116  	case ActionTypeMessage:
   117  		action = &MessageAction{}
   118  	case ActionTypePostback:
   119  		action = &PostbackAction{}
   120  	case ActionTypeDatetimePicker:
   121  		action = &DatetimePickerAction{}
   122  	default:
   123  		return errors.New("invalid action type")
   124  	}
   125  	if err := json.Unmarshal(data, action); err != nil {
   126  		return err
   127  	}
   128  	c.Type = raw.Type
   129  	c.Action = action
   130  	return nil
   131  }
   132  
   133  // UnmarshalJSON method for BoxComponent
   134  func (c *BoxComponent) UnmarshalJSON(data []byte) error {
   135  	type alias BoxComponent
   136  	raw := struct {
   137  		Contents []rawFlexComponent `json:"contents"`
   138  		Action   rawAction          `json:"action"`
   139  		*alias
   140  	}{
   141  		alias: (*alias)(c),
   142  	}
   143  	if err := json.Unmarshal(data, &raw); err != nil {
   144  		return err
   145  	}
   146  	components := make([]FlexComponent, len(raw.Contents))
   147  	for i, content := range raw.Contents {
   148  		components[i] = content.Component
   149  	}
   150  	c.Contents = components
   151  	c.Action = raw.Action.Action
   152  	return nil
   153  }
   154  
   155  // UnmarshalJSON method for ButtonComponent
   156  func (c *ButtonComponent) UnmarshalJSON(data []byte) error {
   157  	type alias ButtonComponent
   158  	raw := struct {
   159  		Action rawAction `json:"action"`
   160  		*alias
   161  	}{
   162  		alias: (*alias)(c),
   163  	}
   164  	if err := json.Unmarshal(data, &raw); err != nil {
   165  		return err
   166  	}
   167  	c.Action = raw.Action.Action
   168  	return nil
   169  }
   170  
   171  // UnmarshalJSON method for ImageComponent
   172  func (c *ImageComponent) UnmarshalJSON(data []byte) error {
   173  	type alias ImageComponent
   174  	raw := struct {
   175  		Action rawAction `json:"action"`
   176  		*alias
   177  	}{
   178  		alias: (*alias)(c),
   179  	}
   180  	if err := json.Unmarshal(data, &raw); err != nil {
   181  		return err
   182  	}
   183  	c.Action = raw.Action.Action
   184  	return nil
   185  }
   186  
   187  // UnmarshalJSON method for TextComponent
   188  func (c *TextComponent) UnmarshalJSON(data []byte) error {
   189  	type alias TextComponent
   190  	raw := struct {
   191  		Action rawAction `json:"action"`
   192  		*alias
   193  	}{
   194  		alias: (*alias)(c),
   195  	}
   196  	if err := json.Unmarshal(data, &raw); err != nil {
   197  		return err
   198  	}
   199  	c.Action = raw.Action.Action
   200  	return nil
   201  }
   202  
   203  // UnmarshalJSON method for VideoComponent
   204  func (c *VideoComponent) UnmarshalJSON(data []byte) error {
   205  	type alias VideoComponent
   206  	raw := struct {
   207  		AltContent rawFlexComponent `json:"altContent"`
   208  		*alias
   209  	}{
   210  		alias: (*alias)(c),
   211  	}
   212  	if err := json.Unmarshal(data, &raw); err != nil {
   213  		return err
   214  	}
   215  	c.AltContent = raw.AltContent.Component
   216  	return nil
   217  }