Some databases do not have enums and we ofter just store strings. This package provides functionality to encode and decode strings into Enum values. This package is designed for situations where you need to represent strings as unique integer values, often used for enumeration-like functionality. It goes with improved performance and memory usage.
This package has zero dependencies.
To install the enum
package, simply run:
go get github.com/kaatinga/enum
Here's a quick guide on how to use the package:
import "github.com/kaatinga/enum"
To encode a string into an Enum
value, use the Encode
function. It will convert a string of characters into an
integer value:
s := "Hello"
encoded, err := enum.Encode(s)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Encoded:", encoded)
}
To decode an Enum
back into its original string representation, you can use the String
method of the Enum
type:
s := "Hello" // Replace with your actual encoded value
encoded, err := enum.Encode(s)
decoded := encoded.String()
fmt.Println("Decoded:", decoded)
The package supports encoding and decoding using the following characters:
- Digits: 0-9
- Uppercase Letters: A-Z
- Lowercase Letters: a-z
- Underscore: _
- Space: (whitespace)
The package enforces a maximum string length for encoding, supporting strings with lengths between 1 and 10 characters.
If the input string length does not meet this criterion, the errInvalidLength
error is returned.
In cases where an error occurs during encoding, the Encode
function returns an error, allowing you to handle it
gracefully.
This package is provided under the MIT License. Feel free to use and modify it as needed.
Contributions to the package are welcome. If you encounter any issues or have ideas for improvements, please open an issue or a pull request on the GitHub repository.