Files
mattermost/model/slack_compatibility.go
Nathan Lowe e961b4cd0d MM-13826 Webhooks: Allow "true"/"false" for bool values in payload body (#10114)
* Webhooks: Allow "true"/"false" for bool values in payload body

Some slack integrations encode bool fields as "true"/"false", which
was previously unsupported in mattermost due to how encoding/json works.
This commit adds an aliased type for bool that implements json.Unmarshaler
to maintain compatibility with Slack.

* Add missing copyright header to added files
2019-01-24 11:36:10 -04:00

31 lines
814 B
Go

// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package model
import (
"fmt"
"strings"
)
// SlackCompatibleBool is an alias for bool that implements json.Unmarshaler
type SlackCompatibleBool bool
// UnmarshalJSON implements json.Unmarshaler
//
// Slack allows bool values to be represented as strings ("true"/"false") or
// literals (true/false). To maintain compatibility, we define an Unmarshaler
// that supports both.
func (b *SlackCompatibleBool) UnmarshalJSON(data []byte) error {
value := strings.ToLower(string(data))
if value == "true" || value == `"true"` {
*b = true
} else if value == "false" || value == `"false"` {
*b = false
} else {
return fmt.Errorf("unmarshal: unable to convert %s to bool", data)
}
return nil
}