From 854bc424b3613bcec90a59a0edf40a1785432787 Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Wed, 5 Dec 2018 16:18:11 +0100 Subject: [PATCH] Check tags in embedded fields (#12) --- generator_test.go | 11 ++++++----- helper.go | 6 ++++++ helper_test.go | 28 ++++++++++++++++++++++++---- sample/experiment/entity.go | 3 ++- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/generator_test.go b/generator_test.go index 449d7f7..08b232c 100644 --- a/generator_test.go +++ b/generator_test.go @@ -2,11 +2,6 @@ package swgen import ( "encoding/json" - "github.com/stretchr/testify/assert" - "github.com/swaggest/swgen/sample" - "github.com/swaggest/swgen/sample/experiment" - "github.com/yudai/gojsondiff" - "github.com/yudai/gojsondiff/formatter" "io/ioutil" "net/http" "net/http/httptest" @@ -14,6 +9,12 @@ import ( "path" "testing" "time" + + "github.com/stretchr/testify/assert" + "github.com/swaggest/swgen/sample" + "github.com/swaggest/swgen/sample/experiment" + "github.com/yudai/gojsondiff" + "github.com/yudai/gojsondiff/formatter" ) type TestSampleStruct struct { diff --git a/helper.go b/helper.go index 991017e..93ff022 100644 --- a/helper.go +++ b/helper.go @@ -45,6 +45,12 @@ func ObjectHasXFields(i interface{}, tagname string) bool { if tag := field.Tag.Get(tagname); tag != "" && tag != "-" { return true } + if field.Anonymous { + if ObjectHasXFields(reflect.New(field.Type).Interface(), tagname) { + return true + } + } + } return false } diff --git a/helper_test.go b/helper_test.go index 450b7e9..08562c8 100644 --- a/helper_test.go +++ b/helper_test.go @@ -1,8 +1,11 @@ -package swgen +package swgen_test import ( "reflect" "testing" + + "github.com/stretchr/testify/assert" + "github.com/swaggest/swgen" ) type TestStruct1 struct { @@ -35,15 +38,32 @@ func TestReflectTypeHash(t *testing.T) { t.Error("Different reflect.Type on instances of the same named struct") } - if ReflectTypeHash(reflect.TypeOf(ts1a)) == ReflectTypeHash(reflect.TypeOf(ts2)) { - t.Error("Same reflect.Type on instances of different named structs:", ReflectTypeHash(reflect.TypeOf(ts1a))) + if swgen.ReflectTypeHash(reflect.TypeOf(ts1a)) == swgen.ReflectTypeHash(reflect.TypeOf(ts2)) { + t.Error("Same reflect.Type on instances of different named structs:", swgen.ReflectTypeHash(reflect.TypeOf(ts1a))) } if reflect.TypeOf(anon1a) != reflect.TypeOf(anon1b) { t.Error("Different reflect.Type on instances of the same anonymous struct") } - if ReflectTypeHash(reflect.TypeOf(anon1a)) != ReflectTypeHash(reflect.TypeOf(anon2)) { + if swgen.ReflectTypeHash(reflect.TypeOf(anon1a)) != swgen.ReflectTypeHash(reflect.TypeOf(anon2)) { t.Error("Different reflect.Type on instances of the different anonymous structs with same fields") } } + +type ( + structWithEmbedded struct { + B int `path:"b"` + embedded + } + + embedded struct { + A int `json:"a"` + } +) + +func TestObjectHasXFields(t *testing.T) { + assert.True(t, swgen.ObjectHasXFields(new(structWithEmbedded), "json")) + assert.True(t, swgen.ObjectHasXFields(new(structWithEmbedded), "path")) + assert.False(t, swgen.ObjectHasXFields(new(structWithEmbedded), "query")) +} diff --git a/sample/experiment/entity.go b/sample/experiment/entity.go index cf3c2be..0975b2c 100644 --- a/sample/experiment/entity.go +++ b/sample/experiment/entity.go @@ -1,8 +1,9 @@ package experiment import ( - "github.com/swaggest/swgen/sample/experiment/variation" "time" + + "github.com/swaggest/swgen/sample/experiment/variation" ) type (