Skip to content

Commit

Permalink
Fixed ClusterConfig loading for boolean values (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
merlimat authored Oct 21, 2024
1 parent 7c929b5 commit 3402e22
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
12 changes: 9 additions & 3 deletions cmd/coordinator/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package coordinator

import (
"errors"
"io"
"log/slog"
"strings"

"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"

"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -128,8 +130,12 @@ func loadClusterConfig(v *viper.Viper) (model.ClusterConfig, error) {
return cc, err
}

if err := v.Unmarshal(&cc); err != nil {
return cc, err
if err := v.Unmarshal(&cc, viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(
common.OptBooleanViperHook(),
mapstructure.StringToTimeDurationHookFunc(), // default hook
mapstructure.StringToSliceHookFunc(","), // default hook
))); err != nil {
return cc, errors.Wrap(err, "failed to load cluster config")
}

return cc, nil
Expand Down
43 changes: 24 additions & 19 deletions cmd/coordinator/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import (
func TestCmd(t *testing.T) {
clusterConfig := model.ClusterConfig{
Namespaces: []model.NamespaceConfig{{
Name: common.DefaultNamespace,
ReplicationFactor: 1,
InitialShardCount: 2,
Name: common.DefaultNamespace,
ReplicationFactor: 1,
InitialShardCount: 2,
NotificationsEnabled: common.Bool(false),
}},
Servers: []model.ServerAddress{{
Public: "public:1234",
Expand All @@ -47,7 +48,6 @@ func TestCmd(t *testing.T) {
assert.NoError(t, err)

name := "config.yaml"

err = os.WriteFile(name, bytes, os.ModePerm)
assert.NoError(t, err)

Expand All @@ -68,9 +68,10 @@ func TestCmd(t *testing.T) {
MetadataProviderImpl: coordinator.File,
}, model.ClusterConfig{
Namespaces: []model.NamespaceConfig{{
Name: common.DefaultNamespace,
ReplicationFactor: 1,
InitialShardCount: 2,
Name: common.DefaultNamespace,
ReplicationFactor: 1,
InitialShardCount: 2,
NotificationsEnabled: common.Bool(false),
}},
Servers: []model.ServerAddress{{
Public: "public:1234",
Expand All @@ -84,9 +85,10 @@ func TestCmd(t *testing.T) {
MetadataProviderImpl: coordinator.File,
}, model.ClusterConfig{
Namespaces: []model.NamespaceConfig{{
Name: common.DefaultNamespace,
ReplicationFactor: 1,
InitialShardCount: 2,
Name: common.DefaultNamespace,
ReplicationFactor: 1,
InitialShardCount: 2,
NotificationsEnabled: common.Bool(false),
}},
Servers: []model.ServerAddress{{
Public: "public:1234",
Expand All @@ -100,9 +102,10 @@ func TestCmd(t *testing.T) {
MetadataProviderImpl: coordinator.File,
}, model.ClusterConfig{
Namespaces: []model.NamespaceConfig{{
Name: common.DefaultNamespace,
ReplicationFactor: 1,
InitialShardCount: 2,
Name: common.DefaultNamespace,
ReplicationFactor: 1,
InitialShardCount: 2,
NotificationsEnabled: common.Bool(false),
}},
Servers: []model.ServerAddress{{
Public: "public:1234",
Expand All @@ -116,9 +119,10 @@ func TestCmd(t *testing.T) {
MetadataProviderImpl: coordinator.File,
}, model.ClusterConfig{
Namespaces: []model.NamespaceConfig{{
Name: common.DefaultNamespace,
ReplicationFactor: 1,
InitialShardCount: 2,
Name: common.DefaultNamespace,
ReplicationFactor: 1,
InitialShardCount: 2,
NotificationsEnabled: common.Bool(false),
}},
Servers: []model.ServerAddress{{
Public: "public:1234",
Expand All @@ -132,9 +136,10 @@ func TestCmd(t *testing.T) {
MetadataProviderImpl: coordinator.File,
}, model.ClusterConfig{
Namespaces: []model.NamespaceConfig{{
Name: common.DefaultNamespace,
ReplicationFactor: 1,
InitialShardCount: 2,
Name: common.DefaultNamespace,
ReplicationFactor: 1,
InitialShardCount: 2,
NotificationsEnabled: common.Bool(false),
}},
Servers: []model.ServerAddress{{
Public: "public:1234",
Expand Down
22 changes: 22 additions & 0 deletions common/opt_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package common

import (
"encoding/json"
"reflect"

"github.com/mitchellh/mapstructure"

"github.com/pkg/errors"
)
Expand All @@ -40,6 +43,10 @@ func (o *OptBooleanDefaultTrue) MarshalJSON() ([]byte, error) {
return json.Marshal(o.val)
}

func (o OptBooleanDefaultTrue) MarshalYAML() (any, error) {
return o.val, nil
}

var trueVal = true
var falseVal = false

Expand All @@ -62,3 +69,18 @@ func (o *OptBooleanDefaultTrue) UnmarshalJSON(data []byte) error {

return errors.New("invalid boolean value: " + s)
}

func OptBooleanViperHook() mapstructure.DecodeHookFuncType {
return func(_ reflect.Type, t reflect.Type, data any) (any, error) {
if t == reflect.TypeOf(OptBooleanDefaultTrue{}) {
b, ok := data.(bool)
if !ok {
return nil, errors.Errorf("invalid boolean value: '%v'", data)
}

return Bool(b), nil
}

return data, nil
}
}

0 comments on commit 3402e22

Please sign in to comment.