github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/datatypes/bool.go (about)

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