Skip to content

Commit

Permalink
Allow multiple definitions per Go type (#98)
Browse files Browse the repository at this point in the history
* Allow multiple definitions per Go type

* Update deps
  • Loading branch information
vearutop authored Oct 3, 2023
1 parent 2251096 commit 7bb896f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/bool64/dev v0.2.31
github.com/stretchr/testify v1.8.2
github.com/swaggest/assertjson v1.9.0
github.com/swaggest/refl v1.2.1
github.com/swaggest/refl v1.3.0
github.com/yudai/gojsondiff v1.0.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/swaggest/assertjson v1.9.0 h1:dKu0BfJkIxv/xe//mkCrK5yZbs79jL7OVf9Ija7o2xQ=
github.com/swaggest/assertjson v1.9.0/go.mod h1:b+ZKX2VRiUjxfUIal0HDN85W0nHPAYUbYH5WkkSsFsU=
github.com/swaggest/refl v1.2.1 h1:1meX9NaXjM5lmb4kk4RP3OZsXFRke9B1EHAP/pCEKO0=
github.com/swaggest/refl v1.2.1/go.mod h1:CkC6g7h1PW33KprTuYRSw8UUOslRUt4lF3oe7tTIgNU=
github.com/swaggest/refl v1.3.0 h1:PEUWIku+ZznYfsoyheF97ypSduvMApYyGkYF3nabS0I=
github.com/swaggest/refl v1.3.0/go.mod h1:3Ujvbmh1pfSbDYjC6JGG7nMgPvpG0ehQL4iNonnLNbg=
github.com/yudai/gojsondiff v1.0.0 h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCOA=
github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3IfnEUduWvb9is428/nNb5L3U01M=
Expand Down
15 changes: 6 additions & 9 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type Reflector struct {
DefaultOptions []func(*ReflectContext)
typesMap map[reflect.Type]interface{}
inlineDefinition map[refl.TypeString]bool
defNames map[reflect.Type]string
defNameTypes map[string]reflect.Type
}

// AddTypeMapping creates substitution link between types of src and dst when reflecting JSON Schema.
Expand Down Expand Up @@ -636,14 +636,11 @@ func (r *Reflector) defName(rc *ReflectContext, t reflect.Type) string {
return ""
}

if r.defNames == nil {
r.defNames = map[reflect.Type]string{}
if r.defNameTypes == nil {
r.defNameTypes = map[string]reflect.Type{}
}

defName, found := r.defNames[t]
if found {
return defName
}
var defName string

try := 1

Expand All @@ -667,7 +664,7 @@ func (r *Reflector) defName(rc *ReflectContext, t reflect.Type) string {

conflict := false

for tt, dn := range r.defNames {
for dn, tt := range r.defNameTypes {
if dn == defName && tt != t {
conflict = true

Expand All @@ -676,7 +673,7 @@ func (r *Reflector) defName(rc *ReflectContext, t reflect.Type) string {
}

if !conflict {
r.defNames[t] = defName
r.defNameTypes[defName] = t

return defName
}
Expand Down

0 comments on commit 7bb896f

Please sign in to comment.