github.com/searKing/golang/go@v1.2.117/format/radix/radix_enum.go (about)

     1  // Code generated by "go-enum -type Radix"; DO NOT EDIT.
     2  
     3  package radix
     4  
     5  import (
     6  	"database/sql"
     7  	"database/sql/driver"
     8  	"encoding"
     9  	"encoding/json"
    10  	"fmt"
    11  	"strconv"
    12  )
    13  
    14  func _() {
    15  	// An "invalid array index" compiler error signifies that the constant values have changed.
    16  	// Re-run the stringer command to generate them again.
    17  	var x [1]struct{}
    18  	_ = x[Binary-0]
    19  	_ = x[Octonary-1]
    20  	_ = x[Decimal-2]
    21  	_ = x[Hexadecimal-3]
    22  }
    23  
    24  const _Radix_name = "BinaryOctonaryDecimalHexadecimal"
    25  
    26  var _Radix_index = [...]uint8{0, 6, 14, 21, 32}
    27  
    28  func _() {
    29  	var _nil_Radix_value = func() (val Radix) { return }()
    30  
    31  	// An "cannot convert Radix literal (type Radix) to type fmt.Stringer" compiler error signifies that the base type have changed.
    32  	// Re-run the go-enum command to generate them again.
    33  	var _ fmt.Stringer = _nil_Radix_value
    34  }
    35  
    36  func (i Radix) String() string {
    37  	if i < 0 || i >= Radix(len(_Radix_index)-1) {
    38  		return "Radix(" + strconv.FormatInt(int64(i), 10) + ")"
    39  	}
    40  	return _Radix_name[_Radix_index[i]:_Radix_index[i+1]]
    41  }
    42  
    43  var _Radix_values = []Radix{0, 1, 2, 3}
    44  
    45  var _Radix_name_to_values = map[string]Radix{
    46  	_Radix_name[0:6]:   0,
    47  	_Radix_name[6:14]:  1,
    48  	_Radix_name[14:21]: 2,
    49  	_Radix_name[21:32]: 3,
    50  }
    51  
    52  // ParseRadixString retrieves an enum value from the enum constants string name.
    53  // Throws an error if the param is not part of the enum.
    54  func ParseRadixString(s string) (Radix, error) {
    55  	if val, ok := _Radix_name_to_values[s]; ok {
    56  		return val, nil
    57  	}
    58  	return 0, fmt.Errorf("%[1]s does not belong to Radix values", s)
    59  }
    60  
    61  // RadixValues returns all values of the enum
    62  func RadixValues() []Radix {
    63  	return _Radix_values
    64  }
    65  
    66  // IsARadix returns "true" if the value is listed in the enum definition. "false" otherwise
    67  func (i Radix) Registered() bool {
    68  	for _, v := range _Radix_values {
    69  		if i == v {
    70  			return true
    71  		}
    72  	}
    73  	return false
    74  }
    75  
    76  func _() {
    77  	var _nil_Radix_value = func() (val Radix) { return }()
    78  
    79  	// An "cannot convert Radix literal (type Radix) to type encoding.BinaryMarshaler" compiler error signifies that the base type have changed.
    80  	// Re-run the go-enum command to generate them again.
    81  	var _ encoding.BinaryMarshaler = &_nil_Radix_value
    82  
    83  	// An "cannot convert Radix literal (type Radix) to type encoding.BinaryUnmarshaler" compiler error signifies that the base type have changed.
    84  	// Re-run the go-enum command to generate them again.
    85  	var _ encoding.BinaryUnmarshaler = &_nil_Radix_value
    86  }
    87  
    88  // MarshalBinary implements the encoding.BinaryMarshaler interface for Radix
    89  func (i Radix) MarshalBinary() (data []byte, err error) {
    90  	return []byte(i.String()), nil
    91  }
    92  
    93  // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface for Radix
    94  func (i *Radix) UnmarshalBinary(data []byte) error {
    95  	var err error
    96  	*i, err = ParseRadixString(string(data))
    97  	return err
    98  }
    99  
   100  func _() {
   101  	var _nil_Radix_value = func() (val Radix) { return }()
   102  
   103  	// An "cannot convert Radix literal (type Radix) to type json.Marshaler" compiler error signifies that the base type have changed.
   104  	// Re-run the go-enum command to generate them again.
   105  	var _ json.Marshaler = _nil_Radix_value
   106  
   107  	// An "cannot convert Radix literal (type Radix) to type encoding.Unmarshaler" compiler error signifies that the base type have changed.
   108  	// Re-run the go-enum command to generate them again.
   109  	var _ json.Unmarshaler = &_nil_Radix_value
   110  }
   111  
   112  // MarshalJSON implements the json.Marshaler interface for Radix
   113  func (i Radix) MarshalJSON() ([]byte, error) {
   114  	return json.Marshal(i.String())
   115  }
   116  
   117  // UnmarshalJSON implements the json.Unmarshaler interface for Radix
   118  func (i *Radix) UnmarshalJSON(data []byte) error {
   119  	var s string
   120  	if err := json.Unmarshal(data, &s); err != nil {
   121  		return fmt.Errorf("Radix should be a string, got %[1]s", data)
   122  	}
   123  
   124  	var err error
   125  	*i, err = ParseRadixString(s)
   126  	return err
   127  }
   128  
   129  func _() {
   130  	var _nil_Radix_value = func() (val Radix) { return }()
   131  
   132  	// An "cannot convert Radix literal (type Radix) to type encoding.TextMarshaler" compiler error signifies that the base type have changed.
   133  	// Re-run the go-enum command to generate them again.
   134  	var _ encoding.TextMarshaler = _nil_Radix_value
   135  
   136  	// An "cannot convert Radix literal (type Radix) to type encoding.TextUnmarshaler" compiler error signifies that the base type have changed.
   137  	// Re-run the go-enum command to generate them again.
   138  	var _ encoding.TextUnmarshaler = &_nil_Radix_value
   139  }
   140  
   141  // MarshalText implements the encoding.TextMarshaler interface for Radix
   142  func (i Radix) MarshalText() ([]byte, error) {
   143  	return []byte(i.String()), nil
   144  }
   145  
   146  // UnmarshalText implements the encoding.TextUnmarshaler interface for Radix
   147  func (i *Radix) UnmarshalText(text []byte) error {
   148  	var err error
   149  	*i, err = ParseRadixString(string(text))
   150  	return err
   151  }
   152  
   153  //func _() {
   154  //	var _nil_Radix_value = func() (val Radix) { return }()
   155  //
   156  //	// An "cannot convert Radix literal (type Radix) to type yaml.Marshaler" compiler error signifies that the base type have changed.
   157  //	// Re-run the go-enum command to generate them again.
   158  //	var _ yaml.Marshaler = _nil_Radix_value
   159  //
   160  //	// An "cannot convert Radix literal (type Radix) to type yaml.Unmarshaler" compiler error signifies that the base type have changed.
   161  //	// Re-run the go-enum command to generate them again.
   162  //	var _ yaml.Unmarshaler = &_nil_Radix_value
   163  //}
   164  
   165  // MarshalYAML implements a YAML Marshaler for Radix
   166  func (i Radix) MarshalYAML() (any, error) {
   167  	return i.String(), nil
   168  }
   169  
   170  // UnmarshalYAML implements a YAML Unmarshaler for Radix
   171  func (i *Radix) UnmarshalYAML(unmarshal func(any) error) error {
   172  	var s string
   173  	if err := unmarshal(&s); err != nil {
   174  		return err
   175  	}
   176  
   177  	var err error
   178  	*i, err = ParseRadixString(s)
   179  	return err
   180  }
   181  
   182  func _() {
   183  	var _nil_Radix_value = func() (val Radix) { return }()
   184  
   185  	// An "cannot convert Radix literal (type Radix) to type driver.Valuer" compiler error signifies that the base type have changed.
   186  	// Re-run the go-enum command to generate them again.
   187  	var _ driver.Valuer = _nil_Radix_value
   188  
   189  	// An "cannot convert Radix literal (type Radix) to type sql.Scanner" compiler error signifies that the base type have changed.
   190  	// Re-run the go-enum command to generate them again.
   191  	var _ sql.Scanner = &_nil_Radix_value
   192  }
   193  
   194  func (i Radix) Value() (driver.Value, error) {
   195  	return i.String(), nil
   196  }
   197  
   198  func (i *Radix) Scan(value any) error {
   199  	if value == nil {
   200  		return nil
   201  	}
   202  
   203  	str, ok := value.(string)
   204  	if !ok {
   205  		bytes, ok := value.([]byte)
   206  		if !ok {
   207  			return fmt.Errorf("value is not a byte slice")
   208  		}
   209  
   210  		str = string(bytes[:])
   211  	}
   212  
   213  	val, err := ParseRadixString(str)
   214  	if err != nil {
   215  		return err
   216  	}
   217  
   218  	*i = val
   219  	return nil
   220  }
   221  
   222  // RadixSliceContains reports whether sunEnums is within enums.
   223  func RadixSliceContains(enums []Radix, sunEnums ...Radix) bool {
   224  	var seenEnums = map[Radix]bool{}
   225  	for _, e := range sunEnums {
   226  		seenEnums[e] = false
   227  	}
   228  
   229  	for _, v := range enums {
   230  		if _, has := seenEnums[v]; has {
   231  			seenEnums[v] = true
   232  		}
   233  	}
   234  
   235  	for _, seen := range seenEnums {
   236  		if !seen {
   237  			return false
   238  		}
   239  	}
   240  
   241  	return true
   242  }
   243  
   244  // RadixSliceContainsAny reports whether any sunEnum is within enums.
   245  func RadixSliceContainsAny(enums []Radix, sunEnums ...Radix) bool {
   246  	var seenEnums = map[Radix]struct{}{}
   247  	for _, e := range sunEnums {
   248  		seenEnums[e] = struct{}{}
   249  	}
   250  
   251  	for _, v := range enums {
   252  		if _, has := seenEnums[v]; has {
   253  			return true
   254  		}
   255  	}
   256  
   257  	return false
   258  }