Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-goenka committed Aug 20, 2024
1 parent 65f1c75 commit 7db32fc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 56 deletions.
53 changes: 0 additions & 53 deletions bundle/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package schema

import (
"encoding/json"
"fmt"
"reflect"
"testing"

Expand Down Expand Up @@ -187,36 +186,6 @@ func TestStructOfSliceSchema(t *testing.T) {
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) {
type Foo struct {
MyInt int `json:"my_int"`
Expand Down Expand Up @@ -318,28 +287,6 @@ func TestMapOfSliceSchema(t *testing.T) {
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) {
var elem [][]string

Expand Down
6 changes: 3 additions & 3 deletions libs/jsonschema/from_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ func (c *constructor) walk(typ reflect.Type) error {
// This function returns all member fields of the provided type.
// If the type has embedded (aka anonymous) fields, this function traverses
// those in a breadth first manner
func getStructFields(golangType reflect.Type) []reflect.StructField {
func getStructFields(typ reflect.Type) []reflect.StructField {
fields := []reflect.StructField{}
bfsQueue := list.New()

for i := 0; i < golangType.NumField(); i++ {
bfsQueue.PushBack(golangType.Field(i))
for i := 0; i < typ.NumField(); i++ {
bfsQueue.PushBack(typ.Field(i))
}
for bfsQueue.Len() > 0 {
front := bfsQueue.Front()
Expand Down
18 changes: 18 additions & 0 deletions libs/jsonschema/from_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 7db32fc

Please sign in to comment.