aboutsummaryrefslogtreecommitdiffstats
path: root/parsers.go
diff options
context:
space:
mode:
Diffstat (limited to 'parsers.go')
-rw-r--r--parsers.go251
1 files changed, 251 insertions, 0 deletions
diff --git a/parsers.go b/parsers.go
new file mode 100644
index 0000000..eca2ebb
--- /dev/null
+++ b/parsers.go
@@ -0,0 +1,251 @@
+package envelope
+
+import (
+ "fmt"
+ "net"
+ "net/url"
+ "reflect"
+ "strconv"
+ "strings"
+ "time"
+)
+
+// TypeParser defines an interface for custom type parsing of values
+// from environment variables
+type TypeParser interface {
+ // Parse converts a string value from an environment variable to a value
+ // that can be set on a struct field
+ Parse(value string) (any, error)
+}
+
+// TypeParserFunc is a function type that implements TypeParser
+type TypeParserFunc func(value string) (any, error)
+
+func (f TypeParserFunc) Parse(value string) (any, error) {
+ return f(value)
+}
+
+var typeParserRegistry = map[reflect.Type]TypeParser{}
+
+// RegisterTypeParser registers a custom type parser for a specific type.
+//
+// This allows you to extend the LoadFromEnv function to support additional types
+// beyond the built-in ones. The parser will be called when LoadFromEnv encounters
+// a field of the specified type.
+//
+// You can also use this function to override the default parsers for built-in types.
+// For example, you could register a custom time.Time parser that uses a different
+// date format than the default RFC3339.
+//
+// Example:
+//
+// // Register a custom parser for UserID type
+// type UserID int64
+//
+// RegisterTypeParser(reflect.TypeOf(UserID(0)), TypeParserFunc(func(value string) (any, error) {
+// id, err := strconv.ParseInt(value, 10, 64)
+// if err != nil {
+// return nil, err
+// }
+// return UserID(id), nil
+// }))
+//
+// // Override the default time.Time parser to use a custom format
+// RegisterTypeParser(reflect.TypeOf(time.Time{}), TypeParserFunc(func(value string) (any, error) {
+// return time.Parse("2006-01-02", value)
+// }))
+func RegisterTypeParser(t reflect.Type, parser TypeParser) {
+ typeParserRegistry[t] = parser
+}
+
+func init() {
+ // String parser
+ RegisterTypeParser(reflect.TypeOf(""), TypeParserFunc(func(value string) (any, error) {
+ return value, nil
+ }))
+
+ // Int parsers
+ RegisterTypeParser(reflect.TypeOf(int(0)), TypeParserFunc(func(value string) (any, error) {
+ i, err := strconv.ParseInt(value, 10, 0)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as an integer: %w", value, err)
+ }
+ return int(i), nil
+ }))
+
+ RegisterTypeParser(reflect.TypeOf(int8(0)), TypeParserFunc(func(value string) (any, error) {
+ i, err := strconv.ParseInt(value, 10, 8)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as an int8: %w", value, err)
+ }
+ return int8(i), nil
+ }))
+
+ RegisterTypeParser(reflect.TypeOf(int16(0)), TypeParserFunc(func(value string) (any, error) {
+ i, err := strconv.ParseInt(value, 10, 16)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as an int16: %w", value, err)
+ }
+ return int16(i), nil
+ }))
+
+ RegisterTypeParser(reflect.TypeOf(int32(0)), TypeParserFunc(func(value string) (any, error) {
+ i, err := strconv.ParseInt(value, 10, 32)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as an int32: %w", value, err)
+ }
+ return int32(i), nil
+ }))
+
+ RegisterTypeParser(reflect.TypeOf(int64(0)), TypeParserFunc(func(value string) (any, error) {
+ i, err := strconv.ParseInt(value, 10, 64)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as an int64: %w", value, err)
+ }
+ return i, nil
+ }))
+
+ // Uint parsers
+ RegisterTypeParser(reflect.TypeOf(uint(0)), TypeParserFunc(func(value string) (any, error) {
+ i, err := strconv.ParseUint(value, 10, 0)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as a uint: %w", value, err)
+ }
+ return uint(i), nil
+ }))
+
+ RegisterTypeParser(reflect.TypeOf(uint8(0)), TypeParserFunc(func(value string) (any, error) {
+ i, err := strconv.ParseUint(value, 10, 8)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as a uint8: %w", value, err)
+ }
+ return uint8(i), nil
+ }))
+
+ RegisterTypeParser(reflect.TypeOf(uint16(0)), TypeParserFunc(func(value string) (any, error) {
+ i, err := strconv.ParseUint(value, 10, 16)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as a uint16: %w", value, err)
+ }
+ return uint16(i), nil
+ }))
+
+ RegisterTypeParser(reflect.TypeOf(uint32(0)), TypeParserFunc(func(value string) (any, error) {
+ i, err := strconv.ParseUint(value, 10, 32)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as a uint32: %w", value, err)
+ }
+ return uint32(i), nil
+ }))
+
+ RegisterTypeParser(reflect.TypeOf(uint64(0)), TypeParserFunc(func(value string) (any, error) {
+ i, err := strconv.ParseUint(value, 10, 64)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as a uint64: %w", value, err)
+ }
+ return uint64(i), nil
+ }))
+
+ // Bool parser
+ RegisterTypeParser(reflect.TypeOf(bool(false)), TypeParserFunc(func(value string) (any, error) {
+ lower := strings.ToLower(value)
+ if lower == "on" || lower == "yes" || lower == "y" {
+ return true, nil
+ } else if lower == "off" || lower == "no" || lower == "n" {
+ return false, nil
+ }
+ b, err := strconv.ParseBool(value)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as a bool: %w", value, err)
+ }
+ return b, nil
+ }))
+
+ // Float parsers
+ RegisterTypeParser(reflect.TypeOf(float32(0)), TypeParserFunc(func(value string) (any, error) {
+ f, err := strconv.ParseFloat(value, 32)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as a float32: %w", value, err)
+ }
+ return float32(f), nil
+ }))
+
+ RegisterTypeParser(reflect.TypeOf(float64(0)), TypeParserFunc(func(value string) (any, error) {
+ f, err := strconv.ParseFloat(value, 64)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as a float64: %w", value, err)
+ }
+ return f, nil
+ }))
+
+ // Time.Time parser
+ RegisterTypeParser(reflect.TypeOf(time.Time{}), TypeParserFunc(func(value string) (any, error) {
+ t, err := time.Parse(time.RFC3339, value)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as time (RFC3339 format expected): %w", value, err)
+ }
+ return t, nil
+ }))
+
+ // time.Duration parser
+ RegisterTypeParser(reflect.TypeOf(time.Duration(0)), TypeParserFunc(func(value string) (any, error) {
+ d, err := time.ParseDuration(value)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as duration: %w", value, err)
+ }
+ return d, nil
+ }))
+
+ // net.IP parser
+ RegisterTypeParser(reflect.TypeOf(net.IP{}), TypeParserFunc(func(value string) (any, error) {
+ ip := net.ParseIP(value)
+ if ip == nil {
+ return nil, fmt.Errorf("unable to parse %s as IP address", value)
+ }
+ return ip, nil
+ }))
+
+ // net.IPNet parser (CIDR notation)
+ RegisterTypeParser(reflect.TypeOf(net.IPNet{}), TypeParserFunc(func(value string) (any, error) {
+ _, ipNet, err := net.ParseCIDR(value)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as CIDR: %w", value, err)
+ }
+ return *ipNet, nil
+ }))
+
+ // url.URL parser
+ RegisterTypeParser(reflect.TypeOf(url.URL{}), TypeParserFunc(func(value string) (any, error) {
+ u, err := url.Parse(value)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse %s as URL: %w", value, err)
+ }
+ return *u, nil
+ }))
+
+ // []string parser (comma separated)
+ RegisterTypeParser(reflect.TypeOf([]string{}), TypeParserFunc(func(value string) (any, error) {
+ if value == "" {
+ return []string{}, nil
+ }
+ return strings.Split(value, ","), nil
+ }))
+
+ // []int parser (comma separated)
+ RegisterTypeParser(reflect.TypeOf([]int{}), TypeParserFunc(func(value string) (any, error) {
+ if value == "" {
+ return []int{}, nil
+ }
+ parts := strings.Split(value, ",")
+ result := make([]int, 0, len(parts))
+ for _, part := range parts {
+ trimmed := strings.TrimSpace(part)
+ i, err := strconv.Atoi(trimmed)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse '%s' as int in comma-separated list: %w", trimmed, err)
+ }
+ result = append(result, i)
+ }
+ return result, nil
+ }))
+}