github.com/searKing/golang/go@v1.2.117/net/mux/connstate_enum.go (about)

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