Documentation
¶
Overview ¶
Package types provides a type system for converting string values to Go types during request parsing.
It includes built-in extractors for common types and allows registration of custom extractors for domain-specific types.
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultRegistry = NewRegistry()
DefaultRegistry is the global registry instance
Functions ¶
Types ¶
type Extractor ¶
type Extractor struct {
// TypeName is the full type name (e.g., "decimal.Decimal", "time.Time")
TypeName string
// Import is the import path needed for this type (e.g., "github.com/shopspring/decimal")
// Leave empty for built-in types
Import string
// ParseFunc generates the code to parse a string value into this type.
// Parameters:
// - varName: the variable containing the string value
// - fieldName: the struct field name to assign to
// - isPointer: whether the field is a pointer type
// Returns: Go code as a string
ParseFunc func(varName, fieldName string, isPointer bool) string
// RequiresError indicates if the parsing can fail and needs error handling
RequiresError bool
}
Extractor defines how to convert a string value to a specific Go type.
Example:
&Extractor{
TypeName: "decimal.Decimal",
Import: "github.com/shopspring/decimal",
ParseFunc: func(varName, fieldName string, isPointer bool) string {
return fmt.Sprintf(`
if d, err := decimal.NewFromString(%s); err == nil {
payload.%s = d
} else {
return fmt.Errorf("invalid %s: %%w", err)
}
`, varName, fieldName, fieldName)
},
}
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds all registered type extractors
func NewRegistry ¶
func NewRegistry() *Registry
NewRegistry creates a new type registry with built-in types registered
Click to show internal directories.
Click to hide internal directories.