From e622d8dd38086f96c49726f4d6572135ce9847f6 Mon Sep 17 00:00:00 2001 From: Dreamacro <8615343+Dreamacro@users.noreply.github.com> Date: Mon, 8 Nov 2021 13:29:37 +0800 Subject: [PATCH] Fix: parse dial interface option --- adapter/outbound/base.go | 2 +- common/structure/structure.go | 6 +++ common/structure/structure_test.go | 64 ++++++++++++++---------------- 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/adapter/outbound/base.go b/adapter/outbound/base.go index e028cd4e..15eedd1a 100644 --- a/adapter/outbound/base.go +++ b/adapter/outbound/base.go @@ -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 { diff --git a/common/structure/structure.go b/common/structure/structure.go index 75579ffc..07043abc 100644 --- a/common/structure/structure.go +++ b/common/structure/structure.go @@ -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) diff --git a/common/structure/structure_test.go b/common/structure/structure_test.go index 0feef28e..69268fa6 100644 --- a/common/structure/structure_test.go +++ b/common/structure/structure_test.go @@ -1,8 +1,9 @@ package structure import ( - "reflect" "testing" + + "github.com/stretchr/testify/assert" ) var ( @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -134,10 +117,23 @@ func TestStructure_WeakType(t *testing.T) { s := &BazSlice{} err := weakTypeDecoder.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_Nest(t *testing.T) { + rawMap := map[string]interface{}{ + "foo": 1, + } + + goal := BazOptional{ + Foo: 1, + } + + s := &struct { + BazOptional + }{} + err := decoder.Decode(rawMap, s) + assert.Nil(t, err) + assert.Equal(t, s.BazOptional, goal) }