Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
candiduslynx committed Oct 7, 2023
1 parent 9b9a825 commit 09ed1ca
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 45 deletions.
69 changes: 26 additions & 43 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,23 +577,10 @@ func (r *Reflector) addDefinition(definitions Definitions, t reflect.Type, s *Sc
if name == "" {
return
}
s.sourceType = t

typeName, pkgPath := t.Name(), t.PkgPath()
def, ok := r.findDef(definitions, name, typeName, pkgPath)
if !ok || def != nil { // def != nil means the same type, so we're OK to overwrite
definitions[name] = s
return
}

idx := 0
defName := name
for ok && def == nil { // we skip all names where we found entry but have different type
idx++
defName = name + "_" + strconv.Itoa(idx)
def, ok = r.findDef(definitions, defName, typeName, pkgPath)
}
// we save both type & pkg info to match against reflected type later
s.sourceType = fullyQualifiedTypeName(t)

_, defName := r.findDef(definitions, t)
// either def != nil & we're overwriting, or it's a new one
definitions[defName] = s
}
Expand All @@ -608,43 +595,39 @@ func (r *Reflector) refDefinition(definitions Definitions, t reflect.Type) *Sche
return nil
}

typeName, pkgPath := t.Name(), t.PkgPath()
def, ok := r.findDef(definitions, name, typeName, pkgPath)
if !ok {
// no entry even for a bare name
def, defName := r.findDef(definitions, t)
if def == nil {
// no entry present in definitions
return nil
}

idx := 0
defName := name
for def == nil { // this will result in 0 iterations if def is already found
idx++
defName = name + "_" + strconv.Itoa(idx)
def, ok = r.findDef(definitions, defName, typeName, pkgPath)
if !ok {
return nil
}
}

// here we have def != nil
return &Schema{
Ref: "#/$defs/" + defName,
Ref: "#/$defs/" + defName,
sourceType: fullyQualifiedTypeName(t),
}
}

// findDef returns the matching definition + whether the def with the defName passed was found
func (r *Reflector) findDef(definitions Definitions, defName, typeName, pkgPath string) (*Schema, bool) {
def, ok := definitions[defName]
if !ok {
return nil, false
// findDef returns the matching definition for the passed reflect.Type
// It will add suffix like `_idx` if necessary & return the corresponding key as well.
// If no applicable entry is available, it will return nil & the key available to use.
func (r *Reflector) findDef(definitions Definitions, t reflect.Type) (*Schema, string) {
name := r.typeName(t)
if name == "" {
return nil, ""
}
sourceType := fullyQualifiedTypeName(t)

// now we need to check that the def is indeed created from the same type
if def.sourceType.PkgPath() != pkgPath || def.sourceType.Name() != typeName {
return nil, true
defName := name
for idx := 1; ; idx++ {
def, ok := definitions[defName]
if !ok {
return nil, defName
}
if def.sourceType == sourceType {
return def, defName
}
defName = name + "_" + strconv.Itoa(idx)
}

return def, true
}

func (r *Reflector) lookupID(t reflect.Type) ID {
Expand Down
3 changes: 1 addition & 2 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jsonschema

import (
"encoding/json"
"reflect"

orderedmap "github.com/wk8/go-ordered-map/v2"
)
Expand Down Expand Up @@ -82,7 +81,7 @@ type Schema struct {
boolean *bool

// sourceType is used to define whether the looked-up definition points to the proper type or not
sourceType reflect.Type
sourceType string
}

var (
Expand Down

0 comments on commit 09ed1ca

Please sign in to comment.