Files
go-enum/_example/string_sqluint_enum.go

314 lines
6.8 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 (
// MediaTypeMp4 is a MediaType of type mp4.
MediaTypeMp4 MediaType = "mp4"
// MediaTypeMp3 is a MediaType of type mp3.
MediaTypeMp3 MediaType = "mp3"
// MediaTypeOgg is a MediaType of type ogg.
MediaTypeOgg MediaType = "ogg"
// MediaTypeFlac is a MediaType of type flac.
MediaTypeFlac MediaType = "flac"
)
var ErrInvalidMediaType = fmt.Errorf("not a valid MediaType, try [%s]", strings.Join(_MediaTypeNames, ", "))
var _MediaTypeNames = []string{
string(MediaTypeMp4),
string(MediaTypeMp3),
string(MediaTypeOgg),
string(MediaTypeFlac),
}
// MediaTypeNames returns a list of possible string values of MediaType.
func MediaTypeNames() []string {
tmp := make([]string, len(_MediaTypeNames))
copy(tmp, _MediaTypeNames)
return tmp
}
// String implements the Stringer interface.
func (x MediaType) String() string {
return string(x)
}
// IsValid returns whether the string contained is a valid MediaType.
func (x MediaType) IsValid() bool {
_, err := ParseMediaType(string(x))
return err == nil
}
var _MediaTypeValue = map[string]MediaType{
"mp4": MediaTypeMp4,
"mp3": MediaTypeMp3,
"ogg": MediaTypeOgg,
"flac": MediaTypeFlac,
}
// ParseMediaType attempts to convert a string to a MediaType.
func ParseMediaType(name string) (MediaType, error) {
if x, ok := _MediaTypeValue[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 := _MediaTypeValue[strings.ToLower(name)]; ok {
return x, nil
}
return MediaType(""), fmt.Errorf("%s is %w", name, ErrInvalidMediaType)
}
// MustParseMediaType converts a string to a MediaType, and panics if is not valid.
func MustParseMediaType(name string) MediaType {
val, err := ParseMediaType(name)
if err != nil {
panic(err)
}
return val
}
func (x MediaType) Ptr() *MediaType {
return &x
}
// MarshalText implements the text marshaller method.
func (x MediaType) MarshalText() ([]byte, error) {
return []byte(string(x)), nil
}
// UnmarshalText implements the text unmarshaller method.
func (x *MediaType) UnmarshalText(text []byte) error {
tmp, err := ParseMediaType(string(text))
if err != nil {
return err
}
*x = tmp
return nil
}
var ErrMediaTypeNilPtr = errors.New("value pointer is nil") // one per type for package clashes
var _MediaTypeSqlValues = map[MediaType]uint64{
MediaTypeMp4: 1,
MediaTypeMp3: 2,
MediaTypeOgg: 3,
MediaTypeFlac: 4,
}
var _MediaTypeSqlScan = map[uint64]MediaType{
1: MediaTypeMp4,
2: MediaTypeMp3,
3: MediaTypeOgg,
4: MediaTypeFlac,
}
func (x *MediaType) parseSqlInt(v int64) error {
val, ok := _MediaTypeSqlScan[uint64(v)]
if !ok {
return ErrInvalidMediaType
}
*x = val
return nil
}
func (x *MediaType) parseSqlUInt(v uint64) error {
val, ok := _MediaTypeSqlScan[v]
if !ok {
return ErrInvalidMediaType
}
*x = val
return nil
}
func (x *MediaType) parseSqlFloat64(v float64) error {
val, ok := _MediaTypeSqlScan[uint64(v)]
if !ok {
return ErrInvalidMediaType
}
*x = val
return nil
}
func (x *MediaType) parseSqlString(v string) error {
// Assume enum first
parsed, err := ParseMediaType(v)
if err != nil {
// Assume integer
parsedVal, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return err
}
return x.parseSqlUInt(parsedVal)
}
*x = parsed
return nil
}
// Scan implements the Scanner interface.
func (x *MediaType) Scan(value interface{}) (err error) {
if value == nil {
*x = MediaType("")
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 MediaType:
*x = v
case int:
err = x.parseSqlInt(int64(v))
case *MediaType:
if v == nil {
return ErrMediaTypeNilPtr
}
*x = *v
case uint:
err = x.parseSqlUInt(uint64(v))
case uint64:
err = x.parseSqlUInt(v)
case *int:
if v == nil {
return ErrMediaTypeNilPtr
}
err = x.parseSqlInt(int64(*v))
case *int64:
if v == nil {
return ErrMediaTypeNilPtr
}
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 ErrMediaTypeNilPtr
}
err = x.parseSqlFloat64(*v)
case *uint:
if v == nil {
return ErrMediaTypeNilPtr
}
err = x.parseSqlUInt(uint64(*v))
case *uint64:
if v == nil {
return ErrMediaTypeNilPtr
}
err = x.parseSqlUInt(*v)
case *string:
if v == nil {
return ErrMediaTypeNilPtr
}
err = x.parseSqlString(*v)
default:
return errors.New("invalid type for MediaType")
}
return
}
// Value implements the driver Valuer interface.
func (x MediaType) Value() (driver.Value, error) {
valid, err := ParseMediaType(string(x))
if err != nil {
return nil, err
}
val, ok := _MediaTypeSqlValues[valid]
if !ok {
return nil, fmt.Errorf("%s is %w", string(x), ErrInvalidMediaType)
}
return int64(val), nil
}
// Set implements the Golang flag.Value interface func.
func (x *MediaType) Set(val string) error {
v, err := ParseMediaType(val)
*x = v
return err
}
// Get implements the Golang flag.Getter interface func.
func (x *MediaType) Get() interface{} {
return *x
}
// Type implements the github.com/spf13/pFlag Value interface.
func (x *MediaType) Type() string {
return "MediaType"
}
type NullMediaType struct {
MediaType MediaType
Valid bool
Set bool
}
func NewNullMediaType(val interface{}) (x NullMediaType) {
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 *NullMediaType) Scan(value interface{}) (err error) {
if value == nil {
x.MediaType, x.Valid = MediaType(""), false
return
}
err = x.MediaType.Scan(value)
x.Valid = (err == nil)
return
}
// Value implements the driver Valuer interface.
func (x NullMediaType) Value() (driver.Value, error) {
if !x.Valid {
return nil, nil
}
// return underlying enum's value after null is checked
return x.MediaType.Value()
}
// MarshalJSON correctly serializes a NullMediaType to JSON.
func (n NullMediaType) MarshalJSON() ([]byte, error) {
const nullStr = "null"
if n.Valid {
return json.Marshal(n.MediaType)
}
return []byte(nullStr), nil
}
// UnmarshalJSON correctly deserializes a NullMediaType from JSON.
func (n *NullMediaType) 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
}