mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-08 20:47:43 +00:00
refactor: remove single iteration loop
Simplify code by taking the first element instead of performing a single-iteration loop.
This commit is contained in:
@@ -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,30 +264,24 @@ 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
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user