mirror of
https://github.com/abice/go-enum.git
synced 2026-10-29 00:56:10 +08:00
314 lines
7.2 KiB
Go
314 lines
7.2 KiB
Go
// Code generated by go-enum DO NOT EDIT.
|
|
// Version: example
|
|
// Revision: example
|
|
// Build Date: example
|
|
// Built By: example
|
|
|
|
package example
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
// MediaTypeIntMp4 is a MediaTypeInt of type mp4.
|
|
MediaTypeIntMp4 MediaTypeInt = "mp4"
|
|
// MediaTypeIntMp3 is a MediaTypeInt of type mp3.
|
|
MediaTypeIntMp3 MediaTypeInt = "mp3"
|
|
// MediaTypeIntOgg is a MediaTypeInt of type ogg.
|
|
MediaTypeIntOgg MediaTypeInt = "ogg"
|
|
// MediaTypeIntFlac is a MediaTypeInt of type flac.
|
|
MediaTypeIntFlac MediaTypeInt = "flac"
|
|
)
|
|
|
|
var ErrInvalidMediaTypeInt = fmt.Errorf("not a valid MediaTypeInt, try [%s]", strings.Join(_MediaTypeIntNames, ", "))
|
|
|
|
var _MediaTypeIntNames = []string{
|
|
string(MediaTypeIntMp4),
|
|
string(MediaTypeIntMp3),
|
|
string(MediaTypeIntOgg),
|
|
string(MediaTypeIntFlac),
|
|
}
|
|
|
|
// MediaTypeIntNames returns a list of possible string values of MediaTypeInt.
|
|
func MediaTypeIntNames() []string {
|
|
tmp := make([]string, len(_MediaTypeIntNames))
|
|
copy(tmp, _MediaTypeIntNames)
|
|
return tmp
|
|
}
|
|
|
|
// String implements the Stringer interface.
|
|
func (x MediaTypeInt) String() string {
|
|
return string(x)
|
|
}
|
|
|
|
// IsValid returns whether the string contained is a valid MediaTypeInt.
|
|
func (x MediaTypeInt) IsValid() bool {
|
|
_, err := ParseMediaTypeInt(string(x))
|
|
return err == nil
|
|
}
|
|
|
|
var _MediaTypeIntValue = map[string]MediaTypeInt{
|
|
"mp4": MediaTypeIntMp4,
|
|
"mp3": MediaTypeIntMp3,
|
|
"ogg": MediaTypeIntOgg,
|
|
"flac": MediaTypeIntFlac,
|
|
}
|
|
|
|
// ParseMediaTypeInt attempts to convert a string to a MediaTypeInt.
|
|
func ParseMediaTypeInt(name string) (MediaTypeInt, error) {
|
|
if x, ok := _MediaTypeIntValue[name]; ok {
|
|
return x, nil
|
|
}
|
|
// Case insensitive parse, do a separate lookup to prevent unnecessary cost of lowercasing a string if we don't need to.
|
|
if x, ok := _MediaTypeIntValue[strings.ToLower(name)]; ok {
|
|
return x, nil
|
|
}
|
|
|
|
return MediaTypeInt(""), fmt.Errorf("%s is %w", name, ErrInvalidMediaTypeInt)
|
|
}
|
|
|
|
// MustParseMediaTypeInt converts a string to a MediaTypeInt, and panics if is not valid.
|
|
func MustParseMediaTypeInt(name string) MediaTypeInt {
|
|
val, err := ParseMediaTypeInt(name)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return val
|
|
}
|
|
|
|
func (x MediaTypeInt) Ptr() *MediaTypeInt {
|
|
return &x
|
|
}
|
|
|
|
// MarshalText implements the text marshaller method.
|
|
func (x MediaTypeInt) MarshalText() ([]byte, error) {
|
|
return []byte(string(x)), nil
|
|
}
|
|
|
|
// UnmarshalText implements the text unmarshaller method.
|
|
func (x *MediaTypeInt) UnmarshalText(text []byte) error {
|
|
tmp, err := ParseMediaTypeInt(string(text))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*x = tmp
|
|
return nil
|
|
}
|
|
|
|
var ErrMediaTypeIntNilPtr = errors.New("value pointer is nil") // one per type for package clashes
|
|
|
|
var _MediaTypeIntSqlValues = map[MediaTypeInt]int64{
|
|
MediaTypeIntMp4: 1,
|
|
MediaTypeIntMp3: 2,
|
|
MediaTypeIntOgg: 3,
|
|
MediaTypeIntFlac: 4,
|
|
}
|
|
|
|
var _MediaTypeIntSqlScan = map[int64]MediaTypeInt{
|
|
1: MediaTypeIntMp4,
|
|
2: MediaTypeIntMp3,
|
|
3: MediaTypeIntOgg,
|
|
4: MediaTypeIntFlac,
|
|
}
|
|
|
|
func (x *MediaTypeInt) parseSqlInt(v int64) error {
|
|
val, ok := _MediaTypeIntSqlScan[v]
|
|
if !ok {
|
|
return ErrInvalidMediaTypeInt
|
|
}
|
|
*x = val
|
|
return nil
|
|
}
|
|
|
|
func (x *MediaTypeInt) parseSqlUInt(v uint64) error {
|
|
val, ok := _MediaTypeIntSqlScan[int64(v)]
|
|
if !ok {
|
|
return ErrInvalidMediaTypeInt
|
|
}
|
|
*x = val
|
|
return nil
|
|
}
|
|
|
|
func (x *MediaTypeInt) parseSqlFloat64(v float64) error {
|
|
val, ok := _MediaTypeIntSqlScan[int64(v)]
|
|
if !ok {
|
|
return ErrInvalidMediaTypeInt
|
|
}
|
|
*x = val
|
|
return nil
|
|
}
|
|
|
|
func (x *MediaTypeInt) parseSqlString(v string) error {
|
|
// Assume enum first
|
|
parsed, err := ParseMediaTypeInt(v)
|
|
if err != nil {
|
|
// Assume integer
|
|
parsedVal, err := strconv.ParseInt(v, 10, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return x.parseSqlInt(parsedVal)
|
|
}
|
|
*x = parsed
|
|
return nil
|
|
}
|
|
|
|
// Scan implements the Scanner interface.
|
|
func (x *MediaTypeInt) Scan(value interface{}) (err error) {
|
|
if value == nil {
|
|
*x = MediaTypeInt("")
|
|
return
|
|
}
|
|
|
|
// A wider range of scannable types.
|
|
// driver.Value values at the top of the list for expediency
|
|
switch v := value.(type) {
|
|
case int64:
|
|
err = x.parseSqlInt(v)
|
|
case string:
|
|
err = x.parseSqlString(v)
|
|
case []byte:
|
|
err = x.parseSqlString(string(v))
|
|
case MediaTypeInt:
|
|
*x = v
|
|
case int:
|
|
err = x.parseSqlInt(int64(v))
|
|
case *MediaTypeInt:
|
|
if v == nil {
|
|
return ErrMediaTypeIntNilPtr
|
|
}
|
|
*x = *v
|
|
case uint:
|
|
err = x.parseSqlUInt(uint64(v))
|
|
case uint64:
|
|
err = x.parseSqlUInt(v)
|
|
case *int:
|
|
if v == nil {
|
|
return ErrMediaTypeIntNilPtr
|
|
}
|
|
err = x.parseSqlInt(int64(*v))
|
|
case *int64:
|
|
if v == nil {
|
|
return ErrMediaTypeIntNilPtr
|
|
}
|
|
err = x.parseSqlInt(*v)
|
|
case float64: // json marshals everything as a float64 if it's a number
|
|
err = x.parseSqlFloat64(v)
|
|
case *float64: // json marshals everything as a float64 if it's a number
|
|
if v == nil {
|
|
return ErrMediaTypeIntNilPtr
|
|
}
|
|
err = x.parseSqlFloat64(*v)
|
|
case *uint:
|
|
if v == nil {
|
|
return ErrMediaTypeIntNilPtr
|
|
}
|
|
err = x.parseSqlUInt(uint64(*v))
|
|
case *uint64:
|
|
if v == nil {
|
|
return ErrMediaTypeIntNilPtr
|
|
}
|
|
err = x.parseSqlUInt(*v)
|
|
case *string:
|
|
if v == nil {
|
|
return ErrMediaTypeIntNilPtr
|
|
}
|
|
err = x.parseSqlString(*v)
|
|
|
|
default:
|
|
return errors.New("invalid type for MediaTypeInt")
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Value implements the driver Valuer interface.
|
|
func (x MediaTypeInt) Value() (driver.Value, error) {
|
|
valid, err := ParseMediaTypeInt(string(x))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
val, ok := _MediaTypeIntSqlValues[valid]
|
|
if !ok {
|
|
return nil, fmt.Errorf("%s is %w", string(x), ErrInvalidMediaTypeInt)
|
|
}
|
|
return val, nil
|
|
}
|
|
|
|
// Set implements the Golang flag.Value interface func.
|
|
func (x *MediaTypeInt) Set(val string) error {
|
|
v, err := ParseMediaTypeInt(val)
|
|
*x = v
|
|
return err
|
|
}
|
|
|
|
// Get implements the Golang flag.Getter interface func.
|
|
func (x *MediaTypeInt) Get() interface{} {
|
|
return *x
|
|
}
|
|
|
|
// Type implements the github.com/spf13/pFlag Value interface.
|
|
func (x *MediaTypeInt) Type() string {
|
|
return "MediaTypeInt"
|
|
}
|
|
|
|
type NullMediaTypeInt struct {
|
|
MediaTypeInt MediaTypeInt
|
|
Valid bool
|
|
Set bool
|
|
}
|
|
|
|
func NewNullMediaTypeInt(val interface{}) (x NullMediaTypeInt) {
|
|
err := x.Scan(val) // yes, we ignore this error, it will just be an invalid value.
|
|
_ = err // make any errcheck linters happy
|
|
return
|
|
}
|
|
|
|
// Scan implements the Scanner interface.
|
|
func (x *NullMediaTypeInt) Scan(value interface{}) (err error) {
|
|
if value == nil {
|
|
x.MediaTypeInt, x.Valid = MediaTypeInt(""), false
|
|
return
|
|
}
|
|
|
|
err = x.MediaTypeInt.Scan(value)
|
|
x.Valid = (err == nil)
|
|
return
|
|
}
|
|
|
|
// Value implements the driver Valuer interface.
|
|
func (x NullMediaTypeInt) Value() (driver.Value, error) {
|
|
if !x.Valid {
|
|
return nil, nil
|
|
}
|
|
// return underlying enum's value after null is checked
|
|
return x.MediaTypeInt.Value()
|
|
}
|
|
|
|
// MarshalJSON correctly serializes a NullMediaTypeInt to JSON.
|
|
func (n NullMediaTypeInt) MarshalJSON() ([]byte, error) {
|
|
const nullStr = "null"
|
|
if n.Valid {
|
|
return json.Marshal(n.MediaTypeInt)
|
|
}
|
|
return []byte(nullStr), nil
|
|
}
|
|
|
|
// UnmarshalJSON correctly deserializes a NullMediaTypeInt from JSON.
|
|
func (n *NullMediaTypeInt) UnmarshalJSON(b []byte) error {
|
|
n.Set = true
|
|
var x interface{}
|
|
err := json.Unmarshal(b, &x)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = n.Scan(x)
|
|
return err
|
|
}
|