diff --git a/README.md b/README.md index 0103dbca..1e876bab 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ if err != nil { ## Requirements -This library requires Go 1.7 or later. +This library requires Go 1.10 or later. ## LICENSE diff --git a/linebot/flex.go b/linebot/flex.go index aeed7a48..458a3d6d 100644 --- a/linebot/flex.go +++ b/linebot/flex.go @@ -17,7 +17,12 @@ package linebot import "encoding/json" // FlexContainerType type -type FlexContainerType string +type FlexContainerType = string // alias type + +// IntPtr is a helper function for using *int values +func IntPtr(v int) *int { + return &v +} // FlexContainerType constants const ( @@ -323,7 +328,7 @@ type BoxComponent struct { Type FlexComponentType Layout FlexBoxLayoutType Contents []FlexComponent - Flex int + Flex *int Spacing FlexComponentSpacingType Margin FlexComponentMarginType } @@ -334,7 +339,7 @@ func (c *BoxComponent) MarshalJSON() ([]byte, error) { Type FlexComponentType `json:"type"` Layout FlexBoxLayoutType `json:"layout"` Contents []FlexComponent `json:"contents"` - Flex int `json:"flex,omitempty"` + Flex *int `json:"flex,omitempty"` Spacing FlexComponentSpacingType `json:"spacing,omitempty"` Margin FlexComponentMarginType `json:"margin,omitempty"` }{ @@ -351,7 +356,7 @@ func (c *BoxComponent) MarshalJSON() ([]byte, error) { type ButtonComponent struct { Type FlexComponentType Action TemplateAction - Flex int + Flex *int Margin FlexComponentMarginType Height FlexButtonHeightType Style FlexButtonStyleType @@ -364,7 +369,7 @@ func (c *ButtonComponent) MarshalJSON() ([]byte, error) { return json.Marshal(&struct { Type FlexComponentType `json:"type"` Action TemplateAction `json:"action"` - Flex int `json:"flex,omitempty"` + Flex *int `json:"flex,omitempty"` Margin FlexComponentMarginType `json:"margin,omitempty"` Height FlexButtonHeightType `json:"height,omitempty"` Style FlexButtonStyleType `json:"style,omitempty"` @@ -426,7 +431,7 @@ func (c *IconComponent) MarshalJSON() ([]byte, error) { type ImageComponent struct { Type FlexComponentType URL string - Flex int + Flex *int Margin FlexComponentMarginType Align FlexComponentAlignType Gravity FlexComponentGravityType @@ -442,7 +447,7 @@ func (c *ImageComponent) MarshalJSON() ([]byte, error) { return json.Marshal(&struct { Type FlexComponentType `json:"type"` URL string `json:"url"` - Flex int `json:"flex,omitempty"` + Flex *int `json:"flex,omitempty"` Margin FlexComponentMarginType `json:"margin,omitempty"` Align FlexComponentAlignType `json:"align,omitempty"` Gravity FlexComponentGravityType `json:"gravity,omitempty"` @@ -507,7 +512,7 @@ func (c *SpacerComponent) MarshalJSON() ([]byte, error) { type TextComponent struct { Type FlexComponentType Text string - Flex int + Flex *int Margin FlexComponentMarginType Size FlexTextSizeType Align FlexComponentAlignType @@ -523,7 +528,7 @@ func (c *TextComponent) MarshalJSON() ([]byte, error) { return json.Marshal(&struct { Type FlexComponentType `json:"type"` Text string `json:"text"` - Flex int `json:"flex,omitempty"` + Flex *int `json:"flex,omitempty"` Margin FlexComponentMarginType `json:"margin,omitempty"` Size FlexTextSizeType `json:"size,omitempty"` Align FlexComponentAlignType `json:"align,omitempty"` diff --git a/linebot/flex_test.go b/linebot/flex_test.go index ebf6199b..8973e401 100644 --- a/linebot/flex_test.go +++ b/linebot/flex_test.go @@ -334,7 +334,7 @@ func TestUnmarshalFlexMessageJSON(t *testing.T) { &TextComponent{ Type: FlexComponentTypeText, Text: "4.0", - Flex: 0, + Flex: IntPtr(0), Margin: FlexComponentMarginTypeMd, Size: FlexTextSizeTypeSm, Color: "#999999", @@ -353,14 +353,14 @@ func TestUnmarshalFlexMessageJSON(t *testing.T) { &TextComponent{ Type: FlexComponentTypeText, Text: "Place", - Flex: 1, + Flex: IntPtr(1), Size: FlexTextSizeTypeSm, Color: "#aaaaaa", }, &TextComponent{ Type: FlexComponentTypeText, Text: "Miraina Tower, 4-1-6 Shinjuku, Tokyo", - Flex: 5, + Flex: IntPtr(5), Size: FlexTextSizeTypeSm, Wrap: true, Color: "#666666", @@ -375,14 +375,14 @@ func TestUnmarshalFlexMessageJSON(t *testing.T) { &TextComponent{ Type: FlexComponentTypeText, Text: "Time", - Flex: 1, + Flex: IntPtr(1), Size: FlexTextSizeTypeSm, Color: "#aaaaaa", }, &TextComponent{ Type: FlexComponentTypeText, Text: "10:00 - 23:00", - Flex: 5, + Flex: IntPtr(5), Size: FlexTextSizeTypeSm, Wrap: true, Color: "#666666", @@ -431,6 +431,7 @@ func TestUnmarshalFlexMessageJSON(t *testing.T) { }, }, Spacing: FlexComponentSpacingTypeSm, + Flex: IntPtr(0), }, }, }, diff --git a/linebot/send_message_test.go b/linebot/send_message_test.go index 8a40bc03..3e829f32 100644 --- a/linebot/send_message_test.go +++ b/linebot/send_message_test.go @@ -384,6 +384,7 @@ func TestPushMessages(t *testing.T) { &TextComponent{ Type: FlexComponentTypeText, Text: "world", + Flex: IntPtr(0), }, &SpacerComponent{ Type: FlexComponentTypeSpacer, @@ -396,7 +397,7 @@ func TestPushMessages(t *testing.T) { ResponseCode: 200, Response: []byte(`{}`), Want: want{ - RequestBody: []byte(`{"to":"U0cc15697597f61dd8b01cea8b027050e","messages":[{"type":"flex","altText":"this is a flex message","contents":{"type":"bubble","body":{"type":"box","layout":"vertical","contents":[{"type":"text","text":"hello"},{"type":"text","text":"world"},{"type":"spacer"}]}}}]}` + "\n"), + RequestBody: []byte(`{"to":"U0cc15697597f61dd8b01cea8b027050e","messages":[{"type":"flex","altText":"this is a flex message","contents":{"type":"bubble","body":{"type":"box","layout":"vertical","contents":[{"type":"text","text":"hello"},{"type":"text","text":"world","flex":0},{"type":"spacer"}]}}}]}` + "\n"), Response: &BasicResponse{}, }, },