enumbitmap

package
v1.90.1 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package enumbitmap provides functions to encode slices of enumeration strings into integer bitmap values and vice versa.

Each bit correspond to a unique enumeration value.

Example:

00000000 =   0 dec = NONE
00000001 =   1 dec = FIRST
00000010 =   2 dec = SECOND
00000100 =   4 dec = THIRD
00001000 =   8 dec = FOURTH
00010000 =  16 dec = FIFTH
00100000 =  32 dec = SIXTH
01000000 =  64 dec = SEVENTH
10000000 = 128 dec = EIGHTH
00001001 = 1 + 8 = 9 dec = FIRST + FOURTH

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BitMapToStrings

func BitMapToStrings(enum map[int]string, v int) ([]string, error)

BitMapToStrings converts a int bitmap value into a string slice.

Example
package main

import (
	"fmt"
	"log"

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

func main() {
	// create a binary map
	// each bit correspond to a different entry
	eis := map[int]string{
		0:   "first",   // 00000000
		1:   "second",  // 00000001
		2:   "third",   // 00000010
		4:   "fourth",  // 00000100
		8:   "fifth",   // 00001000
		16:  "sixth",   // 00010000
		32:  "seventh", // 00100000
		64:  "eighth",  // 01000000
		128: "ninth",   // 10000000
	}

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

	fmt.Println(s)

}
Output:

[third fifth seventh]

func StringsToBitMap

func StringsToBitMap(enum map[string]int, s []string) (int, error)

StringsToBitMap converts a string slice into a int bitmap value.

Example
package main

import (
	"fmt"
	"log"

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

func main() {
	// create a binary map
	// each entry is assigned to a different bit
	esi := map[string]int{
		"first":   0,   // 00000000
		"second":  1,   // 00000001
		"third":   2,   // 00000010
		"fourth":  4,   // 00000100
		"fifth":   8,   // 00001000
		"sixth":   16,  // 00010000
		"seventh": 32,  // 00100000
		"eighth":  64,  // 01000000
		"ninth":   128, // 10000000
	}

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

	fmt.Println(b)

}
Output:

42

Types

This section is empty.

Jump to

Keyboard shortcuts

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