github.com/haraldrudell/parl@v0.4.176/enum/enum-item-impl.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  // Package enum provides concrete types to create enumerated collections
     7  package enum
     8  
     9  import (
    10  	"github.com/haraldrudell/parl"
    11  	"github.com/haraldrudell/parl/sets"
    12  	"golang.org/x/exp/constraints"
    13  )
    14  
    15  // EnumItemImpl is an value or flag-value item of an enumeration container.
    16  //   - K is the type of a unique key mapping one-to-one to an enumeration value
    17  //   - V is the type of the internally used value representation
    18  type EnumItemImpl[K constraints.Ordered, V any] struct {
    19  	ValueV V
    20  	// key that maps to this enumeration value
    21  	KeyK K
    22  	// sentence describing this flag
    23  	Full string
    24  }
    25  
    26  var _ sets.Element[int] = &EnumItemImpl[int, int]{}
    27  
    28  // NewEnumItemImpl returns a concrete value representing an element of an ordered collection
    29  // used to implement Integer enumeratiopns and bit-fields
    30  func NewEnumItemImpl[K constraints.Ordered, V any](value V, key K, full string) (item parl.EnumItem[K, V]) {
    31  	return &EnumItemImpl[K, V]{KeyK: key, Full: full, ValueV: value}
    32  }
    33  
    34  // Description returns a descriptive sentence for this enumeration value
    35  func (item *EnumItemImpl[K, V]) Description() (desc string) { return item.Full }
    36  
    37  // Key returns the key for this enumeration value
    38  func (item *EnumItemImpl[K, V]) Key() (key K) { return item.KeyK }
    39  
    40  // Value returns this enumeration value’s value using the restricted type
    41  func (item *EnumItemImpl[K, V]) Value() (value V) { return item.ValueV }
    42  
    43  func (item *EnumItemImpl[K, V]) String() (s string) {
    44  	return parl.Sprintf("key:%v'%s'0x%x", item.KeyK, item.Full, item.ValueV)
    45  }