enumcache

package
v1.98.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package enumcache provides a collection of thread-safe methods for storing and retrieving enumeration sets with integer IDs and string names. It includes support for binary maps (github.com/Vonage/gosrvlib/pkg/enumbitmap), where each bit represents a different name in the enumeration set.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EnumCache

type EnumCache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

EnumCache handles name and id value mapping.

func New

func New() *EnumCache

New returns a new empty EnumCache.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/enumcache"
)

func main() {
	// create a new cache
	ec := enumcache.New()

	// add an entry
	ec.Set(1, "alpha")

	// get the numerical ID associated to a string
	id, err := ec.ID("alpha")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(id)

	// get the string name associated to a numerical ID
	name, err := ec.Name(1)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(name)

}
Output:

1
alpha

func (*EnumCache) DecodeBinaryMap

func (ec *EnumCache) DecodeBinaryMap(v int) ([]string, error)

DecodeBinaryMap decodes a int binary map into a list of string names. The EnumCache must contain the mapping between the bit values and the names.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/enumcache"
)

func main() {
	// create a new cache
	ec := enumcache.New()

	ec.Set(0, "first")    // 00000000
	ec.Set(1, "second")   // 00000001
	ec.Set(2, "third")    // 00000010
	ec.Set(4, "fourth")   // 00000100
	ec.Set(8, "fifth")    // 00001000
	ec.Set(16, "sixth")   // 00010000
	ec.Set(32, "seventh") // 00100000
	ec.Set(64, "eighth")  // 01000000
	ec.Set(128, "ninth")  // 10000000

	// convert binary code to a slice of strings
	s, err := ec.DecodeBinaryMap(0b00101010) // 42
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(s)

}
Output:

[third fifth seventh]

func (*EnumCache) EncodeBinaryMap

func (ec *EnumCache) EncodeBinaryMap(s []string) (int, error)

EncodeBinaryMap encode a list of string names into a int binary map. The EnumCache must contain the mapping between the bit values and the names.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/enumcache"
)

func main() {
	// create a new cache
	ec := enumcache.New()

	ec.Set(0, "first")    // 00000000
	ec.Set(1, "second")   // 00000001
	ec.Set(2, "third")    // 00000010
	ec.Set(4, "fourth")   // 00000100
	ec.Set(8, "fifth")    // 00001000
	ec.Set(16, "sixth")   // 00010000
	ec.Set(32, "seventh") // 00100000
	ec.Set(64, "eighth")  // 01000000
	ec.Set(128, "ninth")  // 10000000

	// convert a slice of string to the equivalent binary code
	v, err := ec.EncodeBinaryMap([]string{"third", "fifth", "seventh"})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(v)

}
Output:

42

func (*EnumCache) ID

func (ec *EnumCache) ID(name string) (int, error)

ID returns the numerical ID associated to the given name.

func (*EnumCache) Name

func (ec *EnumCache) Name(id int) (string, error)

Name returns the name associated with the given numerical ID.

func (*EnumCache) Set

func (ec *EnumCache) Set(id int, name string)

Set a single id-name key-value.

func (*EnumCache) SetAllIDByName

func (ec *EnumCache) SetAllIDByName(enum IDByName)

SetAllIDByName sets all the specified enumeration ID values indexed by Name.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/enumcache"
)

func main() {
	// create a new cache
	ec := enumcache.New()

	// define cache entries indexed by string
	e := enumcache.IDByName{
		"first":  11,
		"second": 23,
		"third":  31,
	}

	// populate the cache with the specified entries
	ec.SetAllIDByName(e)

	// get the numerical ID associated to a string
	id, err := ec.ID("second")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(id)

	// get the string name associated to a numerical ID
	name, err := ec.Name(23)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(name)

}
Output:

23
second

func (*EnumCache) SetAllNameByID

func (ec *EnumCache) SetAllNameByID(enum NameByID)

SetAllNameByID sets all the specified enumeration Name values indexed by ID.

Example
package main

import (
	"fmt"
	"log"

	"github.com/Vonage/gosrvlib/pkg/enumcache"
)

func main() {
	// create a new cache
	ec := enumcache.New()

	// define cache entries indexed by numerical ID
	e := enumcache.NameByID{
		11: "first",
		23: "second",
		31: "third",
	}

	// populate the cache with the specified entries
	ec.SetAllNameByID(e)

	// get the numerical ID associated to a string
	id, err := ec.ID("second")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(id)

	// get the string name associated to a numerical ID
	name, err := ec.Name(23)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(name)

}
Output:

23
second

func (*EnumCache) SortIDs

func (ec *EnumCache) SortIDs() []int

SortIDs returns a list of sorted IDs.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/enumcache"
)

func main() {
	// create a new cache
	ec := enumcache.New()

	// define cache entries indexed by numerical ID
	e := enumcache.NameByID{
		55: "delta",
		33: "charlie",
		22: "bravo",
		66: "foxtrot",
		44: "echo",
		11: "alpha",
	}

	// populate the cache with the specified entries
	ec.SetAllNameByID(e)

	// get the sorted list of IDs
	sorted := ec.SortIDs()

	fmt.Println(sorted)

}
Output:

[11 22 33 44 55 66]

func (*EnumCache) SortNames

func (ec *EnumCache) SortNames() []string

SortNames returns a list of sorted names.

Example
package main

import (
	"fmt"

	"github.com/Vonage/gosrvlib/pkg/enumcache"
)

func main() {
	// create a new cache
	ec := enumcache.New()

	// define cache entries indexed by numerical ID
	e := enumcache.NameByID{
		1:  "delta",
		2:  "charlie",
		4:  "bravo",
		8:  "foxtrot",
		16: "echo",
		32: "alpha",
	}

	// populate the cache with the specified entries
	ec.SetAllNameByID(e)

	// get the sorted list of names
	sorted := ec.SortNames()

	fmt.Println(sorted)

}
Output:

[alpha bravo charlie delta echo foxtrot]

type IDByName

type IDByName map[string]int

IDByName type maps strings to integers IDs.

type NameByID

type NameByID map[int]string

NameByID maps integers to string names.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL