Skip to content
New issue

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

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

Already on GitHub? # to your account

Fixed ClusterConfig loading for boolean values #555

Merged
Merged
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
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
}
}
Loading