refactor: remove single iteration loop

Simplify code by taking the first element instead of
performing a single-iteration loop.
This commit is contained in:
Nicholas Jackson
2023-09-22 08:04:48 -07:00
committed by quobix
parent b6f5730a7f
commit 4847c4b296

View File

@@ -7,14 +7,15 @@ import (
cryptoRand "crypto/rand" cryptoRand "crypto/rand"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"github.com/lucasjones/reggen"
"github.com/pb33f/libopenapi/datamodel/high/base"
"golang.org/x/exp/slices"
"io" "io"
"math/rand" "math/rand"
"os" "os"
"strings" "strings"
"time" "time"
"github.com/lucasjones/reggen"
"github.com/pb33f/libopenapi/datamodel/high/base"
"golang.org/x/exp/slices"
) )
const rootType = "rootType" const rootType = "rootType"
@@ -263,29 +264,23 @@ func (wr *SchemaRenderer) DiveIntoSchema(schema *base.Schema, key string, struct
// handle oneOf // handle oneOf
oneOf := schema.OneOf oneOf := schema.OneOf
if oneOf != nil { if len(oneOf) > 0 {
oneOfMap := make(map[string]any) oneOfMap := make(map[string]any)
for _, oneOfSchema := range oneOf { oneOfCompiled := oneOf[0].Schema()
oneOfCompiled := oneOfSchema.Schema() wr.DiveIntoSchema(oneOfCompiled, oneOfType, oneOfMap, depth+1)
wr.DiveIntoSchema(oneOfCompiled, oneOfType, oneOfMap, depth+1) for k, v := range oneOfMap[oneOfType].(map[string]any) {
for k, v := range oneOfMap[oneOfType].(map[string]any) { propertyMap[k] = v
propertyMap[k] = v
}
break // one run once for the first result.
} }
} }
// handle anyOf // handle anyOf
anyOf := schema.AnyOf anyOf := schema.AnyOf
if anyOf != nil { if len(anyOf) > 0 {
anyOfMap := make(map[string]any) anyOfMap := make(map[string]any)
for _, anyOfSchema := range anyOf { anyOfCompiled := anyOf[0].Schema()
anyOfCompiled := anyOfSchema.Schema() wr.DiveIntoSchema(anyOfCompiled, anyOfType, anyOfMap, depth+1)
wr.DiveIntoSchema(anyOfCompiled, anyOfType, anyOfMap, depth+1) for k, v := range anyOfMap[anyOfType].(map[string]any) {
for k, v := range anyOfMap[anyOfType].(map[string]any) { propertyMap[k] = v
propertyMap[k] = v
}
break // one run once for the first result only, same as oneOf
} }
} }
structure[key] = propertyMap structure[key] = propertyMap