Skip to content

Commit

Permalink
refactor: remove single iteration loop
Browse files Browse the repository at this point in the history
Simplify code by taking the first element instead of
performing a single-iteration loop.
  • Loading branch information
nickajacks1 authored and daveshanley committed Oct 8, 2023
1 parent b6f5730 commit 4847c4b
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions renderer/schema_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
cryptoRand "crypto/rand"
"encoding/base64"
"fmt"
"github.com/lucasjones/reggen"
"github.com/pb33f/libopenapi/datamodel/high/base"
"golang.org/x/exp/slices"
"io"
"math/rand"
"os"
"strings"
"time"

"github.com/lucasjones/reggen"
"github.com/pb33f/libopenapi/datamodel/high/base"
"golang.org/x/exp/slices"
)

const rootType = "rootType"
Expand Down Expand Up @@ -263,29 +264,23 @@ func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, struct

// handle oneOf
oneOf := schema.OneOf
if oneOf != nil {
if len(oneOf) > 0 {
oneOfMap := make(map[string]any)
for _, oneOfSchema := range oneOf {
oneOfCompiled := oneOfSchema.Schema()
wr.DiveIntoSchema(oneOfCompiled, oneOfType, oneOfMap, depth+1)
for k, v := range oneOfMap[oneOfType].(map[string]any) {
propertyMap[k] = v
}
break // one run once for the first result.
oneOfCompiled := oneOf[0].Schema()
wr.DiveIntoSchema(oneOfCompiled, oneOfType, oneOfMap, depth+1)
for k, v := range oneOfMap[oneOfType].(map[string]any) {
propertyMap[k] = v
}
}

// handle anyOf
anyOf := schema.AnyOf
if anyOf != nil {
if len(anyOf) > 0 {
anyOfMap := make(map[string]any)
for _, anyOfSchema := range anyOf {
anyOfCompiled := anyOfSchema.Schema()
wr.DiveIntoSchema(anyOfCompiled, anyOfType, anyOfMap, depth+1)
for k, v := range anyOfMap[anyOfType].(map[string]any) {
propertyMap[k] = v
}
break // one run once for the first result only, same as oneOf
anyOfCompiled := anyOf[0].Schema()
wr.DiveIntoSchema(anyOfCompiled, anyOfType, anyOfMap, depth+1)
for k, v := range anyOfMap[anyOfType].(map[string]any) {
propertyMap[k] = v
}
}
structure[key] = propertyMap
Expand Down

0 comments on commit 4847c4b

Please sign in to comment.