mirror of https://github.com/databricks/cli.git
added and modified tests to work with it
This commit is contained in:
parent
1a8627fc26
commit
7e00a80109
|
@ -84,6 +84,7 @@ func NewSchema(golangType reflect.Type) (*Schema, error) {
|
||||||
Properties: rootProp.Properties,
|
Properties: rootProp.Properties,
|
||||||
AdditionalProperties: rootProp.AdditionalProperties,
|
AdditionalProperties: rootProp.AdditionalProperties,
|
||||||
Items: rootProp.Items,
|
Items: rootProp.Items,
|
||||||
|
Required: rootProp.Required,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,7 +169,7 @@ func safeToSchema(golangType reflect.Type, seenTypes map[reflect.Type]struct{},
|
||||||
func addStructFields(fields []reflect.StructField, golangType reflect.Type) []reflect.StructField {
|
func addStructFields(fields []reflect.StructField, golangType reflect.Type) []reflect.StructField {
|
||||||
bfsQueue := list.New()
|
bfsQueue := list.New()
|
||||||
|
|
||||||
for i := golangType.NumField() - 1; i >= 0; i-- {
|
for i := 0; i < golangType.NumField(); i++ {
|
||||||
bfsQueue.PushBack(golangType.Field(i))
|
bfsQueue.PushBack(golangType.Field(i))
|
||||||
}
|
}
|
||||||
for bfsQueue.Len() > 0 {
|
for bfsQueue.Len() > 0 {
|
||||||
|
@ -203,7 +204,6 @@ func addStructFields(fields []reflect.StructField, golangType reflect.Type) []re
|
||||||
// helps log where the error originated from
|
// helps log where the error originated from
|
||||||
func toSchema(golangType reflect.Type, seenTypes map[reflect.Type]struct{}, debugTrace *list.List) (*Schema, error) {
|
func toSchema(golangType reflect.Type, seenTypes map[reflect.Type]struct{}, debugTrace *list.List) (*Schema, error) {
|
||||||
// *Struct and Struct generate identical json schemas
|
// *Struct and Struct generate identical json schemas
|
||||||
// TODO: add test case for pointer
|
|
||||||
if golangType.Kind() == reflect.Pointer {
|
if golangType.Kind() == reflect.Pointer {
|
||||||
return toSchema(golangType.Elem(), seenTypes, debugTrace)
|
return toSchema(golangType.Elem(), seenTypes, debugTrace)
|
||||||
}
|
}
|
||||||
|
@ -236,6 +236,7 @@ func toSchema(golangType reflect.Type, seenTypes map[reflect.Type]struct{}, debu
|
||||||
Properties: elemProps.Properties,
|
Properties: elemProps.Properties,
|
||||||
AdditionalProperties: elemProps.AdditionalProperties,
|
AdditionalProperties: elemProps.AdditionalProperties,
|
||||||
Items: elemProps.Items,
|
Items: elemProps.Items,
|
||||||
|
Required: elemProps.Required,
|
||||||
}
|
}
|
||||||
// TODO: what if there is an array of maps. Add additional properties to
|
// TODO: what if there is an array of maps. Add additional properties to
|
||||||
// TODO: what if there are maps of maps
|
// TODO: what if there are maps of maps
|
||||||
|
@ -257,22 +258,36 @@ func toSchema(golangType reflect.Type, seenTypes map[reflect.Type]struct{}, debu
|
||||||
|
|
||||||
// case struct
|
// case struct
|
||||||
properties := map[string]*Schema{}
|
properties := map[string]*Schema{}
|
||||||
|
required := []string{}
|
||||||
if golangType.Kind() == reflect.Struct {
|
if golangType.Kind() == reflect.Struct {
|
||||||
children := []reflect.StructField{}
|
children := []reflect.StructField{}
|
||||||
children = addStructFields(children, golangType)
|
children = addStructFields(children, golangType)
|
||||||
for _, child := range children {
|
for _, child := range children {
|
||||||
// compute child properties
|
// compute child properties
|
||||||
childJsonTag := child.Tag.Get("json")
|
childJsonTag := strings.Split(child.Tag.Get("json"), ",")
|
||||||
childName := strings.Split(childJsonTag, ",")[0]
|
childName := childJsonTag[0]
|
||||||
|
|
||||||
// add current field to debug trace
|
|
||||||
debugTrace.PushBack(childName)
|
|
||||||
|
|
||||||
// skip fields that are not annotated or annotated with "-"
|
// skip fields that are not annotated or annotated with "-"
|
||||||
if childName == "" || childName == "-" {
|
if childName == "" || childName == "-" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Add test for omitempty
|
||||||
|
hasOmitEmptyTag := false
|
||||||
|
for i := 1; i < len(childJsonTag); i++ {
|
||||||
|
if childJsonTag[i] == "omitempty" {
|
||||||
|
hasOmitEmptyTag = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !hasOmitEmptyTag {
|
||||||
|
required = append(required, childName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// add current field to debug trace
|
||||||
|
// TODO: Add tests testing correct traversal and adding of strings
|
||||||
|
// into debugTrace
|
||||||
|
debugTrace.PushBack(childName)
|
||||||
|
|
||||||
// recursively compute properties for this child field
|
// recursively compute properties for this child field
|
||||||
fieldProps, err := safeToSchema(child.Type, seenTypes, debugTrace)
|
fieldProps, err := safeToSchema(child.Type, seenTypes, debugTrace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -294,5 +309,6 @@ func toSchema(golangType reflect.Type, seenTypes map[reflect.Type]struct{}, debu
|
||||||
Items: items,
|
Items: items,
|
||||||
Properties: properties,
|
Properties: properties,
|
||||||
AdditionalProperties: additionalProperties,
|
AdditionalProperties: additionalProperties,
|
||||||
|
Required: required,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -155,7 +155,23 @@ func TestStructOfPrimitivesSchema(t *testing.T) {
|
||||||
"type": "number"
|
"type": "number"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"int_val",
|
||||||
|
"int8_val",
|
||||||
|
"int16_val",
|
||||||
|
"int32_val",
|
||||||
|
"int64_val",
|
||||||
|
"uint_val",
|
||||||
|
"uint8_val",
|
||||||
|
"uint16_val",
|
||||||
|
"uint32_val",
|
||||||
|
"uint64_val",
|
||||||
|
"float32_val",
|
||||||
|
"float64_val",
|
||||||
|
"string_val",
|
||||||
|
"bool_val"
|
||||||
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
@ -202,13 +218,23 @@ func TestStructOfStructsSchema(t *testing.T) {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"a",
|
||||||
|
"b"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"bar"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"foo"
|
||||||
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
@ -247,10 +273,16 @@ func TestStructOfMapsSchema(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"my_map"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"bar"
|
||||||
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
@ -289,10 +321,16 @@ func TestStructOfSliceSchema(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"my_slice"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"bar"
|
||||||
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
@ -344,7 +382,11 @@ func TestMapOfStructSchema(t *testing.T) {
|
||||||
"my_int": {
|
"my_int": {
|
||||||
"type": "number"
|
"type": "number"
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"my_int"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
|
|
||||||
|
@ -498,7 +540,10 @@ func TestSliceOfStructSchema(t *testing.T) {
|
||||||
"type": "number"
|
"type": "number"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"my_int"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
|
|
||||||
|
@ -557,7 +602,10 @@ func TestEmbeddedStructSchema(t *testing.T) {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"country"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"name": {
|
"name": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
@ -583,23 +631,39 @@ func TestEmbeddedStructSchema(t *testing.T) {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"country"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"name": {
|
"name": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"name",
|
||||||
|
"home"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"events"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"state": {
|
"state": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"plot",
|
||||||
|
"name",
|
||||||
|
"home",
|
||||||
|
"country"
|
||||||
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
@ -644,7 +708,10 @@ func TestNonAnnotatedFieldsAreSkipped(t *testing.T) {
|
||||||
"type": "number"
|
"type": "number"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"bar"
|
||||||
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
@ -675,7 +742,10 @@ func TestDashFieldsAreSkipped(t *testing.T) {
|
||||||
"type": "number"
|
"type": "number"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"bar"
|
||||||
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
@ -717,7 +787,10 @@ func TestPointerInStructSchema(t *testing.T) {
|
||||||
"type": "number"
|
"type": "number"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"ptr_val2"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"float_val": {
|
"float_val": {
|
||||||
"type": "number"
|
"type": "number"
|
||||||
|
@ -729,7 +802,10 @@ func TestPointerInStructSchema(t *testing.T) {
|
||||||
"type": "number"
|
"type": "number"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"ptr_val2"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"ptr_int": {
|
"ptr_int": {
|
||||||
"type": "number"
|
"type": "number"
|
||||||
|
@ -738,7 +814,14 @@ func TestPointerInStructSchema(t *testing.T) {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"ptr_int",
|
||||||
|
"ptr_string",
|
||||||
|
"float_val",
|
||||||
|
"ptr_bar",
|
||||||
|
"bar"
|
||||||
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
@ -785,7 +868,11 @@ func TestObjectSchema(t *testing.T) {
|
||||||
"name": {
|
"name": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"name"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"plot": {
|
"plot": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
@ -796,7 +883,11 @@ func TestObjectSchema(t *testing.T) {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"stakes"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"villian": {
|
"villian": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
@ -807,9 +898,19 @@ func TestObjectSchema(t *testing.T) {
|
||||||
"name": {
|
"name": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"name"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
"additionalProperties": false,
|
||||||
|
"required": [
|
||||||
|
"hero",
|
||||||
|
"villian",
|
||||||
|
"plot"
|
||||||
|
]
|
||||||
}`
|
}`
|
||||||
|
|
||||||
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
|
Loading…
Reference in New Issue