Skip to content

Commit

Permalink
chore: fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
omissis committed Dec 23, 2024
1 parent bc8e96c commit 84c8d2b
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions pkg/generator/schema_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,36 +91,9 @@ func (g *schemaGenerator) generateReferencedType(t *schemas.Type) (codegen.Type,
return codegen.EmptyInterfaceType{}, nil
}

fileName := t.Ref

var scope, defName string

if i := strings.IndexRune(t.Ref, '#'); i != -1 {
var prefix string

fileName, scope = t.Ref[0:i], t.Ref[i+1:]
lowercaseScope := strings.ToLower(scope)

for _, currentPrefix := range []string{
"/$defs/", // Draft-handrews-json-schema-validation-02.
"/definitions/", // Legacy.
} {
if strings.HasPrefix(lowercaseScope, currentPrefix) {
prefix = currentPrefix

break
}
}

if len(prefix) == 0 {
return nil, fmt.Errorf(
"%w: value must point to definition within file: '%s'",
errCannotGenerateReferencedType,
t.Ref,
)
}

defName = scope[len(prefix):]
defName, fileName, err := g.extractRefNames(t)
if err != nil {
return nil, err
}

schema := g.schema
Expand Down Expand Up @@ -230,6 +203,42 @@ func (g *schemaGenerator) generateReferencedType(t *schemas.Type) (codegen.Type,
}, nil
}

func (g *schemaGenerator) extractRefNames(t *schemas.Type) (string, string, error) {
scope := ""
defName := ""
fileName := t.Ref

if i := strings.IndexRune(t.Ref, '#'); i != -1 {
var prefix string

fileName, scope = t.Ref[0:i], t.Ref[i+1:]
lowercaseScope := strings.ToLower(scope)

for _, currentPrefix := range []string{
"/$defs/", // Draft-handrews-json-schema-validation-02.
"/definitions/", // Legacy.
} {
if strings.HasPrefix(lowercaseScope, currentPrefix) {
prefix = currentPrefix

break
}
}

if len(prefix) == 0 {
return "", "", fmt.Errorf(
"%w: value must point to definition within file: '%s'",
errCannotGenerateReferencedType,
t.Ref,
)
}

defName = scope[len(prefix):]
}

return defName, fileName, nil
}

func (g *schemaGenerator) generateDeclaredType(t *schemas.Type, scope nameScope) (codegen.Type, error) {
if decl, ok := g.output.declsBySchema[t]; ok {
if t.Dereferenced {
Expand Down Expand Up @@ -1168,13 +1177,11 @@ func (g *schemaGenerator) extractPointedType(typ codegen.Type) (*codegen.NamedTy
if ntyp, ok := rtyp.Type.(*codegen.NamedType); ok {
return ntyp, nil
}

}

if ntyp, ok := typ.(*codegen.NamedType); ok {
return ntyp, nil
}

return nil, fmt.Errorf("%w: got %T", errExpectedNamedType, typ)

}

0 comments on commit 84c8d2b

Please sign in to comment.