github.com/kunlun-qilian/sqlx/v3@v3.0.0/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 (Bool) OpenAPISchemaType() []string {
    22  	return []string{"boolean"}
    23  }
    24  
    25  func (v Bool) MarshalText() ([]byte, error) {
    26  	switch v {
    27  	case BOOL_FALSE:
    28  		return []byte("false"), nil
    29  	case BOOL_TRUE:
    30  		return []byte("true"), nil
    31  	default:
    32  		return []byte("null"), nil
    33  	}
    34  }
    35  
    36  func (v *Bool) UnmarshalText(data []byte) (err error) {
    37  	switch string(data) {
    38  	case "false":
    39  		*v = BOOL_FALSE
    40  	case "true":
    41  		*v = BOOL_TRUE
    42  	}
    43  	return
    44  }
    45  
    46  func (v Bool) MarshalJSON() ([]byte, error) {
    47  	return v.MarshalText()
    48  }
    49  
    50  func (v *Bool) UnmarshalJSON(data []byte) (err error) {
    51  	return v.UnmarshalText(data)
    52  }