From 6b22fd1202f06425944f79a59f9c36f87249819c Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com> Date: Fri, 3 Dec 2021 17:17:27 +0000 Subject: [PATCH 1/3] Add uint types support Signed-off-by: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com> --- multiconfig.go | 35 +++++++++++++++++++++++++++++++++++ multiconfig_test.go | 20 +++++++++++++++++++- testdata/config.json | 5 ++++- testdata/config.toml | 4 ++++ testdata/config.yaml | 3 +++ 5 files changed, 65 insertions(+), 2 deletions(-) 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..7ce6683 100644 --- a/multiconfig_test.go +++ b/multiconfig_test.go @@ -16,12 +16,15 @@ type ( Postgres Postgres unexported string Interval time.Duration + Epoch uint + Epoch32 uint32 + Epoch64 uint64 } // 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..fd391e7 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 = 1638551008 +Epoch64 = 1638551008 diff --git a/testdata/config.yaml b/testdata/config.yaml index 8354703..720fa0c 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 \ No newline at end of file From 9d7811bb7bede9767a9e4fb711ba2dcf1a8981c4 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com> Date: Tue, 7 Dec 2021 09:46:24 +0000 Subject: [PATCH 2/3] Fix: unit tests Signed-off-by: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com> --- env_test.go | 3 +++ flag_test.go | 3 +++ multiconfig_test.go | 6 +++--- testdata/config.toml | 4 ++-- 4 files changed, 11 insertions(+), 5 deletions(-) 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_test.go b/multiconfig_test.go index 7ce6683..1cda0f5 100644 --- a/multiconfig_test.go +++ b/multiconfig_test.go @@ -16,9 +16,9 @@ type ( Postgres Postgres unexported string Interval time.Duration - Epoch uint - Epoch32 uint32 - Epoch64 uint64 + Epoch uint `default:"1638551008"` + Epoch32 uint32 `default:"1638551009"` + Epoch64 uint64 `default:"1638551010"` } // Postgres holds Postgresql database related configuration diff --git a/testdata/config.toml b/testdata/config.toml index fd391e7..a285aa8 100644 --- a/testdata/config.toml +++ b/testdata/config.toml @@ -12,5 +12,5 @@ Hosts = ["192.168.2.1", "192.168.2.2", "192.168.2.3"] AvailabilityRatio = 8.23 Epoch = 1638551008 -Epoch32 = 1638551008 -Epoch64 = 1638551008 +Epoch32 = 1638551009 +Epoch64 = 1638551010 From 0fb39967580979bed95446475040de2af1a7f889 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com> Date: Tue, 7 Dec 2021 09:48:52 +0000 Subject: [PATCH 3/3] fixup! Fix: unit tests Signed-off-by: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com> --- testdata/config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testdata/config.yaml b/testdata/config.yaml index 720fa0c..64e62c9 100644 --- a/testdata/config.yaml +++ b/testdata/config.yaml @@ -28,4 +28,4 @@ postgres: epoch: 1638551008 epoch32: 1638551009 -epoch64: 1638551010 \ No newline at end of file +epoch64: 1638551010