Documentation
¶
Overview ¶
Package strenum provides strongly typed string enums using phantom-type generics. It supports raw ("value") and qualified ("Enum.value") wire formats, JSON/Text/SQL, and a registry.
Example ¶
package main
import (
"fmt"
"github.com/JasonAcar/strenum"
)
func main() {
// Qualified / "strong" example
// In real code you'd declare the tag type and spec at package scope.
// They're local here to keep the example self-contained for pkg.go.dev.
type PTQTag struct{}
type ProjectType = strenum.Enum[PTQTag]
var ProjectTypeSpec = strenum.NewStrongEnum[PTQTag]("ProjectType", "go", "node")
v := ProjectTypeSpec.Must("go")
fmt.Println(v)
}
Output: ProjectType.go
Index ¶
- Constants
- func RegistryKeys() []reflect.Type
- func RegistrySpecNames() []string
- func TagFields(t reflect.Type) []string
- type Enum
- func (e Enum[T]) IsValid() bool
- func (e Enum[T]) MarshalJSON() ([]byte, error)
- func (e Enum[T]) MarshalText() ([]byte, error)
- func (e Enum[T]) Qualified() string
- func (e Enum[T]) Raw() string
- func (e *Enum[T]) Scan(src any) error
- func (e Enum[T]) String() string
- func (e *Enum[T]) UnmarshalJSON(b []byte) error
- func (e *Enum[T]) UnmarshalText(b []byte) error
- func (e Enum[T]) Value() (driver.Value, error)
- type RegistryEntry
- type Spec
Examples ¶
Constants ¶
View Source
const UnknownType = ""
Variables ¶
This section is empty.
Functions ¶
func RegistryKeys ¶
func RegistrySpecNames ¶
func RegistrySpecNames() []string
Types ¶
type Enum ¶
type Enum[T any] struct { // contains filtered or unexported fields }
func (Enum[T]) MarshalJSON ¶
func (Enum[T]) MarshalText ¶
func (*Enum[T]) UnmarshalJSON ¶
Example (Qualified) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/JasonAcar/strenum"
)
func main() {
// Qualified / "strong" example
// In real code you'd declare the tag type and spec at package scope.
// They're local here to keep the example self-contained for pkg.go.dev.
type PTQTag struct{}
type ProjectType = strenum.Enum[PTQTag]
var ProjectTypeSpec = strenum.NewStrongEnum[PTQTag]("ProjectType", "go", "node")
_ = ProjectTypeSpec // to avoid unused error
type S struct {
T ProjectType `json:"t"`
}
var s S
_ = json.Unmarshal([]byte(`{"t": "ProjectType.go"}`), &s)
fmt.Println(s.T)
}
Output: ProjectType.go
Example (Raw) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/JasonAcar/strenum"
)
func main() {
// raw example
// In real code you'd declare the tag type and spec at package scope.
// They're local here to keep the example self-contained for pkg.go.dev.
type MSRawTag struct{}
type MessageStatus = strenum.Enum[MSRawTag]
var MessageStatusSpec = strenum.NewEnum[MSRawTag]("MessageStatus", "queued", "pending", "delivered", "failed")
_ = MessageStatusSpec // to avoid unused error
type M struct {
Status MessageStatus `json:"status"`
}
var m M
_ = json.Unmarshal([]byte(`{"status": "queued"}`), &m)
fmt.Println(m.Status)
}
Output: queued
func (*Enum[T]) UnmarshalText ¶
type RegistryEntry ¶
type RegistryEntry struct {
Tag reflect.Type
TagShort string // e.g. "ProjectTypeTag"
TagFull string // e.g. "github.com/me/strenum/generic.ProjectTypeTag"
SpecName string // e.g. "ProjectType"
Values []string
Qualified bool // if allTypedValues is present and values over-the-wire are "Enum.value"
}
func RegistryEntries ¶
func RegistryEntries() []RegistryEntry
func (RegistryEntry) String ¶
func (e RegistryEntry) String() string
type Spec ¶
func (*Spec[T]) AllStrings ¶
func (*Spec[T]) Parse ¶
Example (Qualified) ¶
package main
import (
"fmt"
"github.com/JasonAcar/strenum"
)
func main() {
// Qualified / "strong" example
// In real code you'd declare the tag type and spec at package scope.
// They're local here to keep the example self-contained for pkg.go.dev.
type PTQTag struct{}
type ProjectType = strenum.Enum[PTQTag]
var ProjectTypeSpec = strenum.NewStrongEnum[PTQTag]("ProjectType", "go", "node")
v, _ := ProjectTypeSpec.Parse("ProjectType.go")
fmt.Println(v)
}
Output: ProjectType.go
Example (Raw) ¶
package main
import (
"fmt"
"github.com/JasonAcar/strenum"
)
func main() {
// raw example
// In real code you'd declare the tag type and spec at package scope.
// They're local here to keep the example self-contained for pkg.go.dev.
type MSRawTag struct{}
type MessageStatus = strenum.Enum[MSRawTag]
var MessageStatusSpec = strenum.NewEnum[MSRawTag]("MessageStatus", "queued", "pending", "delivered", "failed")
v, _ := MessageStatusSpec.Parse("delivered")
fmt.Println(v)
}
Output: delivered
Click to show internal directories.
Click to hide internal directories.