github.com/eden-framework/sqlx@v0.0.2/datatypes/bool.go (about)

     1  package datatypes
     2  
     3  import (
     4  	"encoding/json"
     5  )
     6  
     7  // openapi:type boolean
     8  type Bool int
     9  
    10  const (
    11  	BOOL_UNKNOWN Bool = iota
    12  	BOOL_TRUE         // true
    13  	BOOL_FALSE        // false
    14  )
    15  
    16  var _ interface {
    17  	json.Unmarshaler
    18  	json.Marshaler
    19  } = (*Bool)(nil)
    20  
    21  func (v Bool) MarshalText() ([]byte, error) {
    22  	switch v {
    23  	case BOOL_FALSE:
    24  		return []byte("false"), nil
    25  	case BOOL_TRUE:
    26  		return []byte("true"), nil
    27  	default:
    28  		return []byte("null"), nil
    29  	}
    30  }
    31  
    32  func (v *Bool) UnmarshalText(data []byte) (err error) {
    33  	switch string(data) {
    34  	case "false":
    35  		*v = BOOL_FALSE
    36  	case "true":
    37  		*v = BOOL_TRUE
    38  	}
    39  	return
    40  }
    41  
    42  func (v Bool) MarshalJSON() ([]byte, error) {
    43  	return v.MarshalText()
    44  }
    45  
    46  func (v *Bool) UnmarshalJSON(data []byte) (err error) {
    47  	return v.UnmarshalText(data)
    48  }
    49  
    50  func (v Bool) True() bool {
    51  	return v == BOOL_TRUE
    52  }
    53  
    54  func (v Bool) False() bool {
    55  	return v == BOOL_FALSE
    56  }