A Go code generator for creating optional types with MessagePack serialization support.
Gentype generates wrapper types for various Go primitives and custom types that implement optional (some/none) semantics with full MessagePack serialization capabilities. These generated types are useful for representing values that may or may not be present, while ensuring proper encoding and decoding when using MessagePack.
- Generates optional types for built-in types (bool, int, float, string, etc.)
- Supports custom types with MessagePack extension serialization
- Provides common optional type operations:
SomeXxx(value)
- Create an optional with a valueNoneXxx()
- Create an empty optionalUnwrap()
,UnwrapOr()
,UnwrapOrElse()
- Value extractionIsSome()
,IsNone()
- Presence checking
- Full MessagePack
CustomEncoder
andCustomDecoder
implementation - Type-safe operations
go install github.com/tarantool/go-option/cmd/gentypes@latest
# OR (for go version 1.24+)
go get -tool github.com/tarantool/go-option/cmd/gentypes@latest
To generate optional types for existing types in a package:
gentypes -package ./path/to/package -ext-code 123
# OR (for go version 1.24+)
go tool gentypes -package ./path/to/package -ext-code 123
Or you can use it to generate file from go:
//go:generate go run github.com/tarantool/go-option/cmd/gentypes@latest -ext-code 123
// OR (for go version 1.24+)
//go:generate go tool gentypes -ext-code 123
Flags:
• -package
: Path to the Go package containing types to wrap (default: "."
)
• -ext-code
: MessagePack extension code to use for custom types (must be between
-128 and 127, no default value)
• -verbose
: Enable verbose output (default: false
)
Generated types follow the pattern Optional and provide methods for working with optional values:
// Create an optional with a value.
opt := SomeOptionalString("hello")
// Check if a value is present.
if opt.IsSome() {
value := opt.Unwrap()
fmt.Println(value)
}
// Use a default value if none.
value := opt.UnwrapOr("default")
// Encode to MessagePack.
err := opt.EncodeMsgpack(encoder)