github.com/kyleu/dbaudit@v0.0.2-0.20240321155047-ff2f2c940496/app/lib/types/enum.go (about)

     1  // Package types - Content managed by Project Forge, see [projectforge.md] for details.
     2  package types
     3  
     4  import (
     5  	"fmt"
     6  )
     7  
     8  const KeyEnum = "enum"
     9  
    10  type Enum struct {
    11  	Ref string `json:"ref"`
    12  }
    13  
    14  var _ Type = (*Enum)(nil)
    15  
    16  func (x *Enum) Key() string {
    17  	return KeyEnum
    18  }
    19  
    20  func (x *Enum) Sortable() bool {
    21  	return true
    22  }
    23  
    24  func (x *Enum) Scalar() bool {
    25  	return true
    26  }
    27  
    28  func (x *Enum) String() string {
    29  	return fmt.Sprintf("%s(%s)", x.Key(), x.Ref)
    30  }
    31  
    32  func (x *Enum) From(v any) any {
    33  	switch t := v.(type) {
    34  	case string:
    35  		return t
    36  	default:
    37  		return invalidInput(x.Key(), t)
    38  	}
    39  }
    40  
    41  func (x *Enum) Default(_ string) any {
    42  	return ""
    43  }
    44  
    45  func NewEnum(ref string) *Wrapped {
    46  	return Wrap(&Enum{Ref: ref})
    47  }