mirror of https://github.com/databricks/cli.git
added ignoring - fields and tests for ignoring dashed and non annotated fields
This commit is contained in:
parent
2a66a2fc74
commit
aa81d83970
|
@ -8,7 +8,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: Add tests for the error cases, forcefully triggering them
|
// TODO: Add tests for the error cases, forcefully triggering them
|
||||||
// TODO: Add ignore for -
|
|
||||||
// TODO: Add required validation for omitempty
|
// TODO: Add required validation for omitempty
|
||||||
// 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
|
||||||
|
@ -34,7 +33,7 @@ type Item struct {
|
||||||
// NOTE about loops in golangType: Right now we error out if there is a loop
|
// NOTE about loops in golangType: Right now we error out if there is a loop
|
||||||
// in the types traversed when generating the json schema. This can be solved
|
// in the types traversed when generating the json schema. This can be solved
|
||||||
// using $refs but there is complexity around making sure we do not create json
|
// using $refs but there is complexity around making sure we do not create json
|
||||||
// schema's where properties indirectly refer to each other, which would be an
|
// schemas where properties indirectly refer to each other, which would be an
|
||||||
// invalid json schema. See https://json-schema.org/understanding-json-schema/structuring.html#recursion
|
// invalid json schema. See https://json-schema.org/understanding-json-schema/structuring.html#recursion
|
||||||
// for more details
|
// for more details
|
||||||
func NewSchema(golangType reflect.Type) (*Schema, error) {
|
func NewSchema(golangType reflect.Type) (*Schema, error) {
|
||||||
|
@ -219,8 +218,8 @@ func toProperty(golangType reflect.Type, seenTypes map[reflect.Type]struct{}, de
|
||||||
// add current field to debug trace
|
// add current field to debug trace
|
||||||
debugTrace.PushBack(childName)
|
debugTrace.PushBack(childName)
|
||||||
|
|
||||||
// skip non json annotated fields
|
// skip fields that are not annotated or annotated with "-"
|
||||||
if childName == "" {
|
if childName == "" || childName == "-" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: add tests to assert that these are valid json schemas. Maybe validate some
|
// TODO: add tests to assert that these are valid json schemas. Maybe validate some
|
||||||
|
@ -354,6 +355,66 @@ func TestErrorWithTrace(t *testing.T) {
|
||||||
assert.ErrorContains(t, err, "[ERROR] with depth = 4. traversal trace: root -> resources -> pipelines -> datasets")
|
assert.ErrorContains(t, err, "[ERROR] with depth = 4. traversal trace: root -> resources -> pipelines -> datasets")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNonAnnotatedFieldsAreSkipped(t *testing.T) {
|
||||||
|
type MyStruct struct {
|
||||||
|
Foo string
|
||||||
|
Bar int `json:"bar"`
|
||||||
|
}
|
||||||
|
|
||||||
|
elem := MyStruct{}
|
||||||
|
|
||||||
|
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": "number"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
t.Log("[DEBUG] expected: ", expectedSchema)
|
||||||
|
|
||||||
|
assert.Equal(t, expectedSchema, string(jsonSchema))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDashFieldsAreSkipped(t *testing.T) {
|
||||||
|
type MyStruct struct {
|
||||||
|
Foo string `json:"-"`
|
||||||
|
Bar int `json:"bar"`
|
||||||
|
}
|
||||||
|
|
||||||
|
elem := MyStruct{}
|
||||||
|
|
||||||
|
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": "number"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
|
||||||
|
t.Log("[DEBUG] actual: ", string(jsonSchema))
|
||||||
|
t.Log("[DEBUG] expected: ", expectedSchema)
|
||||||
|
|
||||||
|
assert.Equal(t, expectedSchema, string(jsonSchema))
|
||||||
|
}
|
||||||
|
|
||||||
// // Only for testing bundle, will be removed
|
// // Only for testing bundle, will be removed
|
||||||
// func TestBundleSchema(t *testing.T) {
|
// func TestBundleSchema(t *testing.T) {
|
||||||
// elem := config.Root{}
|
// elem := config.Root{}
|
||||||
|
|
Loading…
Reference in New Issue