diff --git a/env_test.go b/env_test.go index af235a9..7e49cd5 100644 --- a/env_test.go +++ b/env_test.go @@ -96,6 +96,9 @@ func setEnvVars(t *testing.T, structName, prefix string) { "POSTGRES_DBNAME": "configdb", "POSTGRES_AVAILABILITYRATIO": "8.23", "POSTGRES_FOO": "8.23,9.12,11,90", + "EPOCH": "1638551008", + "EPOCH32": "1638551009", + "EPOCH64": "1638551010", } case "CamelCaseServer": env = map[string]string{ diff --git a/flag_test.go b/flag_test.go index 9a88a91..68bb669 100644 --- a/flag_test.go +++ b/flag_test.go @@ -206,6 +206,9 @@ func getFlags(t *testing.T, structName, prefix string) []string { "-postgres-hosts": "192.168.2.1,192.168.2.2,192.168.2.3", "-postgres-dbname": "configdb", "-postgres-availabilityratio": "8.23", + "-epoch": "1638551008", + "-epoch32": "1638551009", + "-epoch64": "1638551010", } case "FlattenedServer": flags = map[string]string{ diff --git a/multiconfig.go b/multiconfig.go index 0070344..7723ea7 100644 --- a/multiconfig.go +++ b/multiconfig.go @@ -218,7 +218,42 @@ func fieldSet(field *structs.Field, v string) error { return fmt.Errorf("multiconfig: field '%s' of type int64 is unsupported: %s (%T)", field.Name(), field.Kind(), t) } + case reflect.Uint: + u, err := strconv.ParseUint(v, 10, 0) + if err != nil { + return err + } + + if err := field.Set(uint(u)); err != nil { + return err + } + case reflect.Uint16: + u, err := strconv.ParseUint(v, 10, 16) + if err != nil { + return err + } + if err := field.Set(uint16(u)); err != nil { + return err + } + case reflect.Uint32: + u, err := strconv.ParseUint(v, 10, 32) + if err != nil { + return err + } + + if err := field.Set(uint32(u)); err != nil { + return err + } + case reflect.Uint64: + u, err := strconv.ParseUint(v, 10, 64) + if err != nil { + return err + } + + if err := field.Set(u); err != nil { + return err + } default: return fmt.Errorf("multiconfig: field '%s' has unsupported type: %s", field.Name(), field.Kind()) } diff --git a/multiconfig_test.go b/multiconfig_test.go index 3ff44ed..1cda0f5 100644 --- a/multiconfig_test.go +++ b/multiconfig_test.go @@ -16,12 +16,15 @@ type ( Postgres Postgres unexported string Interval time.Duration + Epoch uint `default:"1638551008"` + Epoch32 uint32 `default:"1638551009"` + Epoch64 uint64 `default:"1638551010"` } // Postgres holds Postgresql database related configuration Postgres struct { Enabled bool - Port int `required:"true" customRequired:"yes"` + Port uint16 `required:"true" customRequired:"yes"` Hosts []string `required:"true"` DBName string `default:"configdb"` AvailabilityRatio float64 @@ -67,6 +70,9 @@ func getDefaultServer() *Server { DBName: "configdb", AvailabilityRatio: 8.23, }, + Epoch: 1638551008, + Epoch32: 1638551009, + Epoch64: 1638551010, } } @@ -155,6 +161,18 @@ func testStruct(t *testing.T, s *Server, d *Server) { } testPostgres(t, s.Postgres, d.Postgres) + + if s.Epoch != d.Epoch { + t.Errorf("Epoch value is wrong: %v, want: %v", s.Epoch, d.Epoch) + } + + if s.Epoch32 != d.Epoch32 { + t.Errorf("Epoch32 value is wrong: %v, want: %v", s.Epoch32, d.Epoch32) + } + + if s.Epoch64 != d.Epoch64 { + t.Errorf("Epoch64 value is wrong: %v, want: %v", s.Epoch64, d.Epoch64) + } } func testFlattenedStruct(t *testing.T, s *FlattenedServer, d *Server) { diff --git a/testdata/config.json b/testdata/config.json index 6c2e924..56af004 100644 --- a/testdata/config.json +++ b/testdata/config.json @@ -20,5 +20,8 @@ "192.168.2.3" ], "AvailabilityRatio": 8.23 - } + }, + "Epoch": 1638551008, + "Epoch32": 1638551009, + "Epoch64": 1638551010 } diff --git a/testdata/config.toml b/testdata/config.toml index bfc01a4..a285aa8 100644 --- a/testdata/config.toml +++ b/testdata/config.toml @@ -10,3 +10,7 @@ Enabled = true Port = 5432 Hosts = ["192.168.2.1", "192.168.2.2", "192.168.2.3"] AvailabilityRatio = 8.23 + +Epoch = 1638551008 +Epoch32 = 1638551009 +Epoch64 = 1638551010 diff --git a/testdata/config.yaml b/testdata/config.yaml index 8354703..64e62c9 100644 --- a/testdata/config.yaml +++ b/testdata/config.yaml @@ -26,3 +26,6 @@ postgres: - 192.168.2.3 availabilityratio: 8.23 +epoch: 1638551008 +epoch32: 1638551009 +epoch64: 1638551010