From 7bb896f8d5c272e85345bda2ee8df7f32f5db6b7 Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Wed, 4 Oct 2023 00:57:39 +0200 Subject: [PATCH] Allow multiple definitions per Go type (#98) * Allow multiple definitions per Go type * Update deps --- go.mod | 2 +- go.sum | 4 ++-- reflect.go | 15 ++++++--------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index e34390c..9c31a10 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index ba2f1cc..7529a2b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/reflect.go b/reflect.go index adf861a..286a109 100644 --- a/reflect.go +++ b/reflect.go @@ -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. @@ -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 @@ -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 @@ -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 }