Skip to content

Commit

Permalink
Fix: parse dial interface option
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Nov 8, 2021
1 parent d40e5e4 commit e622d8d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion adapter/outbound/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (b *Base) DialOptions(opts ...dialer.Option) []dialer.Option {
}

type BasicOption struct {
Interface string `proxy:"interface-name"`
Interface string `proxy:"interface-name,omitempty" group:"interface-name,omitempty"`
}

type BaseOption struct {
Expand Down
6 changes: 6 additions & 0 deletions common/structure/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func (d *Decoder) Decode(src map[string]interface{}, dst interface{}) error {
v := reflect.ValueOf(dst).Elem()
for idx := 0; idx < v.NumField(); idx++ {
field := t.Field(idx)
if field.Anonymous {
if err := d.decodeStruct(field.Name, src, v.Field(idx)); err != nil {
return err
}
continue
}

tag := field.Tag.Get(d.option.TagName)
str := strings.SplitN(tag, ",", 2)
Expand Down
60 changes: 28 additions & 32 deletions common/structure/structure_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package structure

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

var (
Expand Down Expand Up @@ -39,12 +40,8 @@ func TestStructure_Basic(t *testing.T) {

s := &Baz{}
err := decoder.Decode(rawMap, s)
if err != nil {
t.Fatal(err.Error())
}
if !reflect.DeepEqual(s, goal) {
t.Fatalf("bad: %#v", s)
}
assert.Nil(t, err)
assert.Equal(t, goal, s)
}

func TestStructure_Slice(t *testing.T) {
Expand All @@ -60,12 +57,8 @@ func TestStructure_Slice(t *testing.T) {

s := &BazSlice{}
err := decoder.Decode(rawMap, s)
if err != nil {
t.Fatal(err.Error())
}
if !reflect.DeepEqual(s, goal) {
t.Fatalf("bad: %#v", s)
}
assert.Nil(t, err)
assert.Equal(t, goal, s)
}

func TestStructure_Optional(t *testing.T) {
Expand All @@ -79,12 +72,8 @@ func TestStructure_Optional(t *testing.T) {

s := &BazOptional{}
err := decoder.Decode(rawMap, s)
if err != nil {
t.Fatal(err.Error())
}
if !reflect.DeepEqual(s, goal) {
t.Fatalf("bad: %#v", s)
}
assert.Nil(t, err)
assert.Equal(t, goal, s)
}

func TestStructure_MissingKey(t *testing.T) {
Expand All @@ -94,18 +83,14 @@ func TestStructure_MissingKey(t *testing.T) {

s := &Baz{}
err := decoder.Decode(rawMap, s)
if err == nil {
t.Fatalf("should throw error: %#v", s)
}
assert.NotNilf(t, err, "should throw error: %#v", s)
}

func TestStructure_ParamError(t *testing.T) {
rawMap := map[string]interface{}{}
s := Baz{}
err := decoder.Decode(rawMap, s)
if err == nil {
t.Fatalf("should throw error: %#v", s)
}
assert.NotNilf(t, err, "should throw error: %#v", s)
}

func TestStructure_SliceTypeError(t *testing.T) {
Expand All @@ -116,9 +101,7 @@ func TestStructure_SliceTypeError(t *testing.T) {

s := &BazSlice{}
err := decoder.Decode(rawMap, s)
if err == nil {
t.Fatalf("should throw error: %#v", s)
}
assert.NotNilf(t, err, "should throw error: %#v", s)
}

func TestStructure_WeakType(t *testing.T) {
Expand All @@ -134,10 +117,23 @@ func TestStructure_WeakType(t *testing.T) {

s := &BazSlice{}
err := weakTypeDecoder.Decode(rawMap, s)
if err != nil {
t.Fatal(err.Error())
assert.Nil(t, err)
assert.Equal(t, goal, s)
}

func TestStructure_Nest(t *testing.T) {
rawMap := map[string]interface{}{
"foo": 1,
}
if !reflect.DeepEqual(s, goal) {
t.Fatalf("bad: %#v", s)

goal := BazOptional{
Foo: 1,
}

s := &struct {
BazOptional
}{}
err := decoder.Decode(rawMap, s)
assert.Nil(t, err)
assert.Equal(t, s.BazOptional, goal)
}

0 comments on commit e622d8d

Please sign in to comment.