README
¶
mapgen
mapgen is a Go code generation tool for creating utility functions and mappings for Go types. It automates the creation of maps, groupings, and utility functions for custom types defined in your Go code.
Features
- Automatically generate a pluralized map of constants for a specified type.
- Group constants based on
@groupannotations in comments, and generate pluralized maps for each group. - Generate utility functions:
- Convert a slice of custom types to a slice of strings or integers.
- Extract keys from a
map[Type]struct{}for both overall and group-specific maps. - Validate if a value exists in the generated map or specific groups.
- Check membership in group-specific maps with
Is<Group><Type>functions.
Installation
Install mapgen using go install:
go install github.com/c9s/mapgen@latest
Ensure that your GOPATH is properly configured and the go/bin directory is in your PATH.
Usage
Run mapgen in a Go package directory to generate code for a specific type.
Command-line options
-type: The name of the type for which the constants and utilities should be generated (required).-output: (Optional) The output file name. Defaults to<type>map.go.-stdout: Output the generated code to stdout instead of writing to a file.
Example
Using mapgen Manually
Given a type PrivateChannel with associated constants:
type PrivateChannel string
const (
// @group orders
PrivateChannelOrder PrivateChannel = "order"
PrivateChannelTrade PrivateChannel = "trade"
// @group margin
PrivateChannelMarginOrder PrivateChannel = "margin_order"
)
Run mapgen to generate utility functions and mappings:
mapgen -type PrivateChannel
The generated file privatechannelsmap.go will include:
// Code generated by go:generate; DO NOT EDIT.
package main
var AllPrivateChannels = map[PrivateChannel]struct{}{
PrivateChannelOrder: {},
PrivateChannelTrade: {},
PrivateChannelMarginOrder: {},
}
var AllOrdersPrivateChannels = map[PrivateChannel]struct{}{
PrivateChannelOrder: {},
PrivateChannelTrade: {},
}
// AllOrdersPrivateChannelsKeys converts the orders group map of PrivateChannel to a slice of PrivateChannel
func AllOrdersPrivateChannelsKeys() []PrivateChannel {
keys := make([]PrivateChannel, 0, len(AllOrdersPrivateChannels))
for k := range AllOrdersPrivateChannels {
keys = append(keys, k)
}
return keys
}
// ValidateOrdersPrivateChannels validates if a value belongs to the orders group of PrivateChannel
func ValidateOrdersPrivateChannels(ch PrivateChannel) bool {
_, ok := AllOrdersPrivateChannels[ch]
return ok
}
// IsOrdersPrivateChannel checks if the value is in the orders group of PrivateChannel
func IsOrdersPrivateChannel(ch PrivateChannel) bool {
_, exist := AllOrdersPrivateChannels[ch]
return exist
}
var AllMarginPrivateChannels = map[PrivateChannel]struct{}{
PrivateChannelMarginOrder: {},
}
// AllMarginPrivateChannelsKeys converts the margin group map of PrivateChannel to a slice of PrivateChannel
func AllMarginPrivateChannelsKeys() []PrivateChannel {
keys := make([]PrivateChannel, 0, len(AllMarginPrivateChannels))
for k := range AllMarginPrivateChannels {
keys = append(keys, k)
}
return keys
}
// ValidateMarginPrivateChannels validates if a value belongs to the margin group of PrivateChannel
func ValidateMarginPrivateChannels(ch PrivateChannel) bool {
_, ok := AllMarginPrivateChannels[ch]
return ok
}
// IsMarginPrivateChannel checks if the value is in the margin group of PrivateChannel
func IsMarginPrivateChannel(ch PrivateChannel) bool {
_, exist := AllMarginPrivateChannels[ch]
return exist
}
var AllPrivateChannelsSlice = []PrivateChannel{
PrivateChannelOrder,
PrivateChannelTrade,
PrivateChannelMarginOrder,
}
// PrivateChannelStrings converts a slice of PrivateChannel to a slice of string
func PrivateChannelStrings(slice []PrivateChannel) (out []string) {
for _, el := range slice {
out = append(out, string(el))
}
return out
}
// PrivateChannelKeys converts a map of PrivateChannel to a slice of PrivateChannel
func PrivateChannelKeys(values map[PrivateChannel]struct{}) (slice []PrivateChannel) {
for k := range values {
slice = append(slice, k)
}
return slice
}
// ValidatePrivateChannels validates a value of type PrivateChannel
func ValidatePrivateChannels(ch PrivateChannel) bool {
_, ok := AllPrivateChannels[ch]
return ok
}
Using mapgen with go:generate
To integrate mapgen with your build process, use the go:generate directive. Add the following comment above the type definition file:
//go:generate mapgen -type PrivateChannel
Then, run the following command in your project directory:
go generate
This will automatically invoke mapgen and generate the corresponding utility code for PrivateChannel. The generated file will follow the naming convention <type>map.go, e.g., privatechannelsmap.go.
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.
License
mapgen is licensed under the MIT License.
Documentation
¶
There is no documentation for this package.