Skip to content

Commit

Permalink
issue #30: fix decode crash because of maptsructure bug; debug config…
Browse files Browse the repository at this point in the history
… decode logging
  • Loading branch information
skipor committed Feb 9, 2017
1 parent 6bf9a28 commit 61e95bc
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func newDecoderConfig(result interface{}) *mapstructure.DecoderConfig {
}

var hooks = []mapstructure.DecodeHookFunc{
DebugHook,
mapstructure.StringToTimeDurationHookFunc(),
StringToURLHook,
StringToIPHook,
Expand Down
28 changes: 27 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package config

import (
"testing"

"time"

"github.com/facebookgo/stack"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -211,3 +211,29 @@ func TestDeltaUpdate(t *testing.T) {
assert.Equal(t, "val3", l2.Val2.Val1)
assert.Equal(t, "val5", l2.Val2.Val2)
}

func TestNextSquash(t *testing.T) {
// TODO(skipor): fix mapstructure #70
t.Skip("Skipped until fix https://github.com/mitchellh/mapstructure/issues/70")
data := &struct {
Level1 struct {
Level2 struct {
Foo string
} `config:",squash"`
} `config:",squash"`
}{}

defer func() {
r := recover()
if r == nil {
return
}
t.Fatalf("panic: %s\n %s", r, stack.Callers(3))
}()

err := Decode(M{
"foo": "baz",
}, &data)
require.NoError(t, err)
assert.Equal(t, "baz", data.Level1.Level2.Foo)
}
25 changes: 25 additions & 0 deletions config/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (

"github.com/asaskevich/govalidator"
"github.com/c2h5oh/datasize"
"github.com/facebookgo/stack"
"github.com/facebookgo/stackerr"

"github.com/yandex/pandora/plugin"
"github.com/yandex/pandora/tag"
)

const PluginNameKey = "type"
Expand Down Expand Up @@ -106,6 +108,25 @@ func PluginFactoryHook(f reflect.Type, t reflect.Type, data interface{}) (p inte
return plugin.NewFactory(t, name, fillConf)
}

// DebugHook used to debug config decode.
func DebugHook(f reflect.Type, t reflect.Type, data interface{}) (p interface{}, err error) {
p, err = data, nil
if !tag.Debug {
return
}
callers := stack.Callers(2)
var decodeCallers int
for _, caller := range callers {
if caller.Name == "(*Decoder).decode" {
decodeCallers++
}
}

offset := strings.Repeat(" ", decodeCallers)
fmt.Printf("%s %s from %s %v\n", offset, t, f, data)
return
}

func parseConf(t reflect.Type, data interface{}) (name string, fillConf func(conf interface{}) error, err error) {
confData, err := toStringKeyMap(data)
if err != nil {
Expand Down Expand Up @@ -133,6 +154,10 @@ func parseConf(t reflect.Type, data interface{}) (name string, fillConf func(con
}
name = names[0]
fillConf = func(conf interface{}) error {
if tag.Debug {
fmt.Printf("Decoding %s %s plugin\n"+"%s from %v",
t, name, reflect.TypeOf(conf).Elem(), confData)
}
err := DecodeAndValidate(confData, conf)
if err != nil {
err = fmt.Errorf("%s %s plugin\n"+
Expand Down
3 changes: 2 additions & 1 deletion gun/phttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type Client interface {
}

type ClientConfig struct {
Transport TransportConfig `config:",squash"`
// TODO: squash after fix https://github.com/mitchellh/mapstructure/issues/70
Transport TransportConfig // `config:",squash"`
Dialer DialerConfig `config:"dial"`
}

Expand Down
24 changes: 24 additions & 0 deletions gun/phttp/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2017 Yandex LLC. All rights reserved.
// Use of this source code is governed by a MPL 2.0
// license that can be found in the LICENSE file.
// Author: Vladimir Skipor <skipor@yandex-team.ru>

package phttp

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/yandex/pandora/config"
)

var _ = Describe("Base", func() {
It("GunClientConfig decode", func() {
conf := NewDefaultHTTPGunClientConfig()
data := map[interface{}]interface{}{
"target": "test-trbo01e.haze.yandex.net:3000",
}
err := config.DecodeAndValidate(data, &conf)
Expect(err).To(BeNil())
})
})
10 changes: 10 additions & 0 deletions tag/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2017 Yandex LLC. All rights reserved.
// Use of this source code is governed by a MPL 2.0
// license that can be found in the LICENSE file.
// Author: Vladimir Skipor <skipor@yandex-team.ru>

// +build debug

package tag

const Debug = true
10 changes: 10 additions & 0 deletions tag/no_degug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) 2017 Yandex LLC. All rights reserved.
// Use of this source code is governed by a MPL 2.0
// license that can be found in the LICENSE file.
// Author: Vladimir Skipor <skipor@yandex-team.ru>

// +build !debug

package tag

const Debug = false

0 comments on commit 61e95bc

Please sign in to comment.