github.com/kongr45gpen/mattermost-server@v5.11.1+incompatible/model/slack_compatibility.go (about)

     1  // Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  )
    10  
    11  // SlackCompatibleBool is an alias for bool that implements json.Unmarshaler
    12  type SlackCompatibleBool bool
    13  
    14  // UnmarshalJSON implements json.Unmarshaler
    15  //
    16  // Slack allows bool values to be represented as strings ("true"/"false") or
    17  // literals (true/false). To maintain compatibility, we define an Unmarshaler
    18  // that supports both.
    19  func (b *SlackCompatibleBool) UnmarshalJSON(data []byte) error {
    20  	value := strings.ToLower(string(data))
    21  	if value == "true" || value == `"true"` {
    22  		*b = true
    23  	} else if value == "false" || value == `"false"` {
    24  		*b = false
    25  	} else {
    26  		return fmt.Errorf("unmarshal: unable to convert %s to bool", data)
    27  	}
    28  
    29  	return nil
    30  }