Skip to content

Commit

Permalink
Check tags in embedded fields (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Dec 5, 2018
1 parent a30dbde commit 854bc42
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
11 changes: 6 additions & 5 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ 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"
"os"
"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 {
Expand Down
6 changes: 6 additions & 0 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 24 additions & 4 deletions helper_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package swgen
package swgen_test

import (
"reflect"
"testing"

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

type TestStruct1 struct {
Expand Down Expand Up @@ -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"))
}
3 changes: 2 additions & 1 deletion sample/experiment/entity.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package experiment

import (
"github.com/swaggest/swgen/sample/experiment/variation"
"time"

"github.com/swaggest/swgen/sample/experiment/variation"
)

type (
Expand Down

0 comments on commit 854bc42

Please sign in to comment.