mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* 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
31 lines
814 B
Go
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
|
|
}
|