This commit is contained in:
Shreyas Goenka 2024-08-20 19:09:21 +02:00
parent 65f1c750f6
commit 7db32fc211
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
3 changed files with 21 additions and 56 deletions

View File

@ -2,7 +2,6 @@ package schema
import ( import (
"encoding/json" "encoding/json"
"fmt"
"reflect" "reflect"
"testing" "testing"
@ -187,36 +186,6 @@ func TestStructOfSliceSchema(t *testing.T) {
assert.Equal(t, expected, string(jsonSchema)) assert.Equal(t, expected, string(jsonSchema))
} }
func TestMapOfPrimitivesSchema(t *testing.T) {
var elem map[string]int
schema, err := New(reflect.TypeOf(elem), nil)
assert.NoError(t, err)
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)
expected :=
`{
"type": "object",
"additionalProperties": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
}
}`
t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", expected)
assert.Equal(t, expected, string(jsonSchema))
}
func TestMapOfStructSchema(t *testing.T) { func TestMapOfStructSchema(t *testing.T) {
type Foo struct { type Foo struct {
MyInt int `json:"my_int"` MyInt int `json:"my_int"`
@ -318,28 +287,6 @@ func TestMapOfSliceSchema(t *testing.T) {
assert.Equal(t, expected, string(jsonSchema)) assert.Equal(t, expected, string(jsonSchema))
} }
func TestSliceOfPrimitivesSchema(t *testing.T) {
var elem []float32
schema, err := New(reflect.TypeOf(elem), nil)
assert.NoError(t, err)
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)
expected :=
`{
"type": "array",
"items": {
"type": "number"
}
}`
t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", expected)
assert.Equal(t, expected, string(jsonSchema))
}
func TestSliceOfSliceSchema(t *testing.T) { func TestSliceOfSliceSchema(t *testing.T) {
var elem [][]string var elem [][]string

View File

@ -159,12 +159,12 @@ func (c *constructor) walk(typ reflect.Type) error {
// This function returns all member fields of the provided type. // This function returns all member fields of the provided type.
// If the type has embedded (aka anonymous) fields, this function traverses // If the type has embedded (aka anonymous) fields, this function traverses
// those in a breadth first manner // those in a breadth first manner
func getStructFields(golangType reflect.Type) []reflect.StructField { func getStructFields(typ reflect.Type) []reflect.StructField {
fields := []reflect.StructField{} fields := []reflect.StructField{}
bfsQueue := list.New() bfsQueue := list.New()
for i := 0; i < golangType.NumField(); i++ { for i := 0; i < typ.NumField(); i++ {
bfsQueue.PushBack(golangType.Field(i)) bfsQueue.PushBack(typ.Field(i))
} }
for bfsQueue.Len() > 0 { for bfsQueue.Len() > 0 {
front := bfsQueue.Front() front := bfsQueue.Front()

View File

@ -124,3 +124,21 @@ func TestFromTypeBasic(t *testing.T) {
}) })
} }
} }
func TestGetStructFields(t *testing.T) {
type EmbeddedStruct struct {
I int
B bool
}
type MyStruct struct {
S string
*EmbeddedStruct
}
fields := getStructFields(reflect.TypeOf(MyStruct{}))
assert.Len(t, fields, 3)
assert.Equal(t, "S", fields[0].Name)
assert.Equal(t, "I", fields[1].Name)
assert.Equal(t, "B", fields[2].Name)
}