Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uint types support #81

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
3 changes: 3 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
35 changes: 35 additions & 0 deletions multiconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
20 changes: 19 additions & 1 deletion multiconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,6 +70,9 @@ func getDefaultServer() *Server {
DBName: "configdb",
AvailabilityRatio: 8.23,
},
Epoch: 1638551008,
Epoch32: 1638551009,
Epoch64: 1638551010,
}
}

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion testdata/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"192.168.2.3"
],
"AvailabilityRatio": 8.23
}
},
"Epoch": 1638551008,
"Epoch32": 1638551009,
"Epoch64": 1638551010
}
4 changes: 4 additions & 0 deletions testdata/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ postgres:
- 192.168.2.3
availabilityratio: 8.23

epoch: 1638551008
epoch32: 1638551009
epoch64: 1638551010