GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — lancet/docs/en/api/packages/enum.md at main · duke-git/lancet

Expand file tree

/

enum.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
850 lines (610 loc) · 15.4 KB
/

enum.md

Copy path

File metadata and controls

850 lines (610 loc) · 15.4 KB
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

Enum

Package enum provides a simple enum implementation.

Source:

  • https://github.com/duke-git/lancet/blob/main/enum/enum.go

Usage:

import (
    "github.com/duke-git/lancet/v2/enum"
)

Index

  • NewItem
  • NewItemsFromPairs
  • Value
  • Name
  • Valid
  • MarshalJSON
  • NewRegistry
  • Add
  • Remove
  • Update
  • GetByValue
  • GetByName
  • Items
  • Contains
  • Size
  • Range
  • SortedItems
  • Filter

Documentation

NewItem

Creates a new enum item.

Signature:

func NewItem[T comparable](value T, name string) *Item[T]

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    item1 := enum.NewItem(Active, "Active")
    item2 := enum.NewItem(Inactive, "Inactive")

    fmt.Println(item1.Name(), item1.Value())
    fmt.Println(item2.Name(), item2.Value())

    // Output:
    // Active 1
    // Inactive 2
}

NewItemsFromPairs

Creates enum items from a slice of Pair structs.

Signature:

func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T]

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    items := enum.NewItemsFromPairs(
        enum.Pair[Status]{Value: Active, Name: "Active"},
        enum.Pair[Status]{Value: Inactive, Name: "Inactive"},
    )

    fmt.Println(items[0].Name(), items[0].Value())
    fmt.Println(items[1].Name(), items[1].Value())

    // Output:
    // Active 1
    // Inactive 2
}

Value

Returns the value of the enum item.

Signature:

func (it *Item[T]) Value() T

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    items := enum.NewItemsFromPairs(
        enum.Pair[Status]{Value: Active, Name: "Active"},
        enum.Pair[Status]{Value: Inactive, Name: "Inactive"},
    )

    fmt.Println(items[0].Name(), items[0].Value())
    fmt.Println(items[1].Name(), items[1].Value())

    // Output:
    // Active 1
    // Inactive 2
}

Name

Returns the name of the enum item.

Signature:

func (it *Item[T]) Name() string

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    items := enum.NewItemsFromPairs(
        enum.Pair[Status]{Value: Active, Name: "Active"},
        enum.Pair[Status]{Value: Inactive, Name: "Inactive"},
    )

    fmt.Println(items[0].Name(), items[0].Value())
    fmt.Println(items[1].Name(), items[1].Value())

    // Output:
    // Active 1
    // Inactive 2
}

Valid

Checks if the enum item is valid. If a custom check function is provided, it will be used to validate the value.

Signature:

func (it *Item[T]) Valid(checker ...func(T) bool) bool

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    item := enum.NewItem(Active, "Active")
    fmt.Println(item.Valid())

    invalidItem := enum.NewItem(Unknown, "")
    fmt.Println(invalidItem.Valid())

    // Output:
    // true
    // false
}

MarshalJSON

Implementation of json.Marshaler interface.

Signature:

func (it *Item[T]) MarshalJSON() ([]byte, error)
func (it *Item[T]) UnmarshalJSON(data []byte) error

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    item := enum.NewItem(Active, "Active")
    data, _ := item.MarshalJSON()
    fmt.Println(string(data))

    var unmarshaledItem enum.Item[Status]
    _ = unmarshaledItem.UnmarshalJSON(data)
    fmt.Println(unmarshaledItem.Name(), unmarshaledItem.Value())

    // Output:
    // {"name":"Active","value":1}
    // Active 1
}

NewRegistry

Creates a new enum registry.

Signature:

func NewRegistry[T comparable](items ...*Item[T]) *Registry[T]

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    registry := enum.NewRegistry[Status]()
    item1 := enum.NewItem(Active, "Active")
    item2 := enum.NewItem(Inactive, "Inactive")

    registry.Add(item1, item2)

    if item, found := registry.GetByValue(Active); found {
        fmt.Println("Found by value:", item.Name())
    }

    if item, found := registry.GetByName("Inactive"); found {
        fmt.Println("Found by name:", item.Value())
    }

    // Output:
    // Found by value: Active
    // Found by name: 2
}

Add

Adds enum items to the registry.

Signature:

func (r *Registry[T]) Add(items ...*Item[T])

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    registry := enum.NewRegistry[Status]()
    item1 := enum.NewItem(Active, "Active")
    item2 := enum.NewItem(Inactive, "Inactive")

    registry.Add(item1, item2)

    if item, found := registry.GetByValue(Active); found {
        fmt.Println("Found by value:", item.Name())
    }

    if item, found := registry.GetByName("Inactive"); found {
        fmt.Println("Found by name:", item.Value())
    }

    // Output:
    // Found by value: Active
    // Found by name: 2
}

Remove

Removes an enum item from the registry by its value.

Signature:

func (r *Registry[T]) Remove(value T) bool

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    registry := enum.NewRegistry[Status]()
    item1 := enum.NewItem(Active, "Active")

    registry.Add(item1)
    fmt.Println("Size before removal:", registry.Size())

    removed := registry.Remove(Active)
    fmt.Println("Removed:", removed)
    fmt.Println("Size after removal:", registry.Size())

    // Output:
    // Size before removal: 1
    // Removed: true
    // Size after removal: 0
}

Update

Updates the name of an enum item in the registry by its value.

Signature:

func (r *Registry[T]) Update(value T, newName string) bool

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    registry := enum.NewRegistry[Status]()
    item1 := enum.NewItem(Active, "Active")

    registry.Add(item1)
    updated := registry.Update(Active, "Activated")
    fmt.Println("Updated:", updated)

    if item, found := registry.GetByValue(Active); found {
        fmt.Println("New name:", item.Name())
    }

    // Output:
    // Updated: true
    // New name: Activated
}

GetByValue

Retrieves an enum item by its value.

Signature:

func (r *Registry[T]) GetByValue(value T) (*Item[T], bool)

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    registry := enum.NewRegistry[Status]()
    item := enum.NewItem(Active, "Active")

    registry.Add(item)

    if item, found := registry.GetByValue(Active); found {
        fmt.Println("Found name by value:", item.Name())
    }

    // Output:
    // Found name by value: Active
}

GetByName

Retrieves an enum item by its name.

Signature:

func (r *Registry[T]) GetByName(name string) (*Item[T], bool)

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    registry := enum.NewRegistry[Status]()
    item := enum.NewItem(Active, "Active")

    registry.Add(item)

    if item, found := registry.GetByName("Active"); found {
        fmt.Println("Found value by name:", item.Value())
    }

    // Output:
    // Found value by name: 1
}

Items

Returns a slice of all enum items in the registry.

Signature:

func (r *Registry[T]) Items() []*Item[T]

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    registry := enum.NewRegistry[Status]()
    item1 := enum.NewItem(Active, "Active")
    item2 := enum.NewItem(Inactive, "Inactive")

    registry.Add(item1, item2)

    for _, item := range registry.Items() {
        fmt.Println(item.Name(), item.Value())
    }

    // Output:
    // Active 1
    // Inactive 2
}

Contains

Checks if an enum item with the given value exists in the registry.

Signature:

func (r *Registry[T]) Contains(value T) bool

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    registry := enum.NewRegistry[Status]()
    item := enum.NewItem(Active, "Active")
    registry.Add(item)

    fmt.Println(registry.Contains(Active))
    fmt.Println(registry.Contains(Inactive))

    // Output:
    // true
    // false
}

Size

Returns the number of enum items in the registry.

Signature:

func (r *Registry[T]) Size() int

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    registry := enum.NewRegistry[Status]()
    fmt.Println("Initial size:", registry.Size())

    item1 := enum.NewItem(Active, "Active")
    item2 := enum.NewItem(Inactive, "Inactive")
    registry.Add(item1, item2)

    fmt.Println("Size after adding items:", registry.Size())

    registry.Remove(Active)
    fmt.Println("Size after removing an item:", registry.Size())

    // Output:
    // Initial size: 0
    // Size after adding items: 2
    // Size after removing an item: 1
}

Range

Iterates over all enum items in the registry and applies the given function.

Signature:

func (r *Registry[T]) Range(fn func(*Item[T]) bool)

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    registry := enum.NewRegistry[Status]()
    item1 := enum.NewItem(Active, "Active")
    item2 := enum.NewItem(Inactive, "Inactive")
    registry.Add(item1, item2)

    registry.Range(func(item *enum.Item[Status]) bool {
        fmt.Println(item.Name(), item.Value())
        return true // continue iteration
    })

    // Output:
    // Active 1
    // Inactive 2
}

SortedItems

Returns a slice of all enum items sorted by the given less function.

Signature:

func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T]

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    registry := enum.NewRegistry[Status]()
    item1 := enum.NewItem(Inactive, "Inactive")
    item2 := enum.NewItem(Active, "Active")
    registry.Add(item1, item2)

    for _, item := range registry.SortedItems(func(i1, i2 *enum.Item[Status]) bool {
        return i1.Value() < i2.Value()
    }) {
        fmt.Println(item.Name(), item.Value())
    }

    // Output:
    // Active 1
    // Inactive 2
}

Filter

Returns a slice of enum items that satisfy the given predicate function.

Signature:

func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T]

Example:Run

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/enum"
)

type Status int

const (
    Unknown Status = iota
    Active
    Inactive
)

func main() {
    registry := enum.NewRegistry[Status]()
    item1 := enum.NewItem(Active, "Active")
    item2 := enum.NewItem(Inactive, "Inactive")
    registry.Add(item1, item2)

    activeItems := registry.Filter(func(item *enum.Item[Status]) bool {
        return item.Value() == Active
    })

    for _, item := range activeItems {
        fmt.Println(item.Name(), item.Value())
    }

    // Output:
    // Active 1
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.