mirror of
https://github.com/abice/go-enum.git
synced 2026-10-28 16:46:59 +08:00
172 lines
3.7 KiB
Go
172 lines
3.7 KiB
Go
// Code generated by go-enum DO NOT EDIT.
|
|
// Version: example
|
|
// Revision: example
|
|
// Build Date: example
|
|
// Built By: example
|
|
|
|
//go:build example
|
|
// +build example
|
|
|
|
package example
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
const (
|
|
// JobStatePending is a JobState of type Pending.
|
|
JobStatePending JobState = iota
|
|
// JobStateProcessing is a JobState of type Processing.
|
|
JobStateProcessing
|
|
// JobStateCompleted is a JobState of type Completed.
|
|
JobStateCompleted
|
|
// JobStateFailed is a JobState of type Failed.
|
|
JobStateFailed
|
|
)
|
|
|
|
var ErrInvalidJobState = errors.New("not a valid JobState")
|
|
|
|
const _JobStateName = "pendingprocessingcompletedfailed"
|
|
|
|
var _JobStateMap = map[JobState]string{
|
|
JobStatePending: _JobStateName[0:7],
|
|
JobStateProcessing: _JobStateName[7:17],
|
|
JobStateCompleted: _JobStateName[17:26],
|
|
JobStateFailed: _JobStateName[26:32],
|
|
}
|
|
|
|
// String implements the Stringer interface.
|
|
func (x JobState) String() string {
|
|
if str, ok := _JobStateMap[x]; ok {
|
|
return str
|
|
}
|
|
return fmt.Sprintf("JobState(%d)", x)
|
|
}
|
|
|
|
// IsValid provides a quick way to determine if the typed value is
|
|
// part of the allowed enumerated values
|
|
func (x JobState) IsValid() bool {
|
|
_, ok := _JobStateMap[x]
|
|
return ok
|
|
}
|
|
|
|
var _JobStateValue = map[string]JobState{
|
|
_JobStateName[0:7]: JobStatePending,
|
|
_JobStateName[7:17]: JobStateProcessing,
|
|
_JobStateName[17:26]: JobStateCompleted,
|
|
_JobStateName[26:32]: JobStateFailed,
|
|
}
|
|
|
|
// ParseJobState attempts to convert a string to a JobState.
|
|
func ParseJobState(name string) (JobState, error) {
|
|
if x, ok := _JobStateValue[name]; ok {
|
|
return x, nil
|
|
}
|
|
return JobState(0), fmt.Errorf("%s is %w", name, ErrInvalidJobState)
|
|
}
|
|
|
|
var errJobStateNilPtr = errors.New("value pointer is nil") // one per type for package clashes
|
|
|
|
// Scan implements the Scanner interface.
|
|
func (x *JobState) Scan(value interface{}) (err error) {
|
|
if value == nil {
|
|
*x = JobState(0)
|
|
return
|
|
}
|
|
|
|
// A wider range of scannable types.
|
|
// driver.Value values at the top of the list for expediency
|
|
switch v := value.(type) {
|
|
case int64:
|
|
*x = JobState(v)
|
|
case string:
|
|
*x, err = ParseJobState(v)
|
|
case []byte:
|
|
*x, err = ParseJobState(string(v))
|
|
case JobState:
|
|
*x = v
|
|
case int:
|
|
*x = JobState(v)
|
|
case *JobState:
|
|
if v == nil {
|
|
return errJobStateNilPtr
|
|
}
|
|
*x = *v
|
|
case uint:
|
|
*x = JobState(v)
|
|
case uint64:
|
|
*x = JobState(v)
|
|
case *int:
|
|
if v == nil {
|
|
return errJobStateNilPtr
|
|
}
|
|
*x = JobState(*v)
|
|
case *int64:
|
|
if v == nil {
|
|
return errJobStateNilPtr
|
|
}
|
|
*x = JobState(*v)
|
|
case float64: // json marshals everything as a float64 if it's a number
|
|
*x = JobState(v)
|
|
case *float64: // json marshals everything as a float64 if it's a number
|
|
if v == nil {
|
|
return errJobStateNilPtr
|
|
}
|
|
*x = JobState(*v)
|
|
case *uint:
|
|
if v == nil {
|
|
return errJobStateNilPtr
|
|
}
|
|
*x = JobState(*v)
|
|
case *uint64:
|
|
if v == nil {
|
|
return errJobStateNilPtr
|
|
}
|
|
*x = JobState(*v)
|
|
case *string:
|
|
if v == nil {
|
|
return errJobStateNilPtr
|
|
}
|
|
*x, err = ParseJobState(*v)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Value implements the driver Valuer interface.
|
|
func (x JobState) Value() (driver.Value, error) {
|
|
return x.String(), nil
|
|
}
|
|
|
|
type NullJobState struct {
|
|
JobState JobState
|
|
Valid bool
|
|
}
|
|
|
|
func NewNullJobState(val interface{}) (x NullJobState) {
|
|
x.Scan(val) // yes, we ignore this error, it will just be an invalid value.
|
|
return
|
|
}
|
|
|
|
// Scan implements the Scanner interface.
|
|
func (x *NullJobState) Scan(value interface{}) (err error) {
|
|
if value == nil {
|
|
x.JobState, x.Valid = JobState(0), false
|
|
return
|
|
}
|
|
|
|
err = x.JobState.Scan(value)
|
|
x.Valid = (err == nil)
|
|
return
|
|
}
|
|
|
|
// Value implements the driver Valuer interface.
|
|
func (x NullJobState) Value() (driver.Value, error) {
|
|
if !x.Valid {
|
|
return nil, nil
|
|
}
|
|
return x.JobState.String(), nil
|
|
}
|