added test for pointers in structs

This commit is contained in:
Shreyas Goenka 2023-01-18 11:02:25 +01:00
parent 4dfe3cea9a
commit 1a8627fc26
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
2 changed files with 63 additions and 3 deletions

View File

@ -12,7 +12,6 @@ import (
// TODO: Add example documentation // TODO: Add example documentation
// TODO: Do final checks for more validation that can be added to json schema // TODO: Do final checks for more validation that can be added to json schema
// TODO: Run all tests to see code coverage and add tests for missing assertions // TODO: Run all tests to see code coverage and add tests for missing assertions
// TODO: test all permutations of types [primitives, maps, arrays, objects]
// defines schema for a json object // defines schema for a json object
type Schema struct { type Schema struct {
@ -100,7 +99,6 @@ const (
) )
// TODO: document that only string keys allowed in maps // TODO: document that only string keys allowed in maps
// TODO: document mapping between schema and json
func javascriptType(golangType reflect.Type) (JavascriptType, error) { func javascriptType(golangType reflect.Type) (JavascriptType, error) {
switch golangType.Kind() { switch golangType.Kind() {
case reflect.Bool: case reflect.Bool:
@ -183,7 +181,6 @@ func addStructFields(fields []reflect.StructField, golangType reflect.Type) []re
continue continue
} }
// TODO: add test case for pointer too
fieldType := field.Type fieldType := field.Type
if fieldType.Kind() == reflect.Pointer { if fieldType.Kind() == reflect.Pointer {
fieldType = fieldType.Elem() fieldType = fieldType.Elem()

View File

@ -684,6 +684,69 @@ func TestDashFieldsAreSkipped(t *testing.T) {
assert.Equal(t, expectedSchema, string(jsonSchema)) assert.Equal(t, expectedSchema, string(jsonSchema))
} }
func TestPointerInStructSchema(t *testing.T) {
type Bar struct {
PtrVal2 *int `json:"ptr_val2"`
}
type Foo struct {
PtrInt *int `json:"ptr_int"`
PtrString *string `json:"ptr_string"`
FloatVal float32 `json:"float_val"`
PtrBar *Bar `json:"ptr_bar"`
Bar *Bar `json:"bar"`
}
elem := Foo{}
schema, err := NewSchema(reflect.TypeOf(elem))
require.NoError(t, err)
jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)
expectedSchema :=
`{
"type": "object",
"properties": {
"bar": {
"type": "object",
"properties": {
"ptr_val2": {
"type": "number"
}
},
"additionalProperties": false
},
"float_val": {
"type": "number"
},
"ptr_bar": {
"type": "object",
"properties": {
"ptr_val2": {
"type": "number"
}
},
"additionalProperties": false
},
"ptr_int": {
"type": "number"
},
"ptr_string": {
"type": "string"
}
},
"additionalProperties": false
}`
t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", expectedSchema)
assert.Equal(t, expectedSchema, string(jsonSchema))
}
// TODO: last test to do once all the todos are done // TODO: last test to do once all the todos are done
func TestObjectSchema(t *testing.T) { func TestObjectSchema(t *testing.T) {
type Person struct { type Person struct {