some final cleanup

This commit is contained in:
Shreyas Goenka 2023-01-19 11:34:55 +01:00
parent e4935642ca
commit e2f4f4d5f1
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
2 changed files with 18 additions and 21 deletions

View File

@ -13,38 +13,37 @@ type Schema struct {
Type JavascriptType `json:"type,omitempty"` Type JavascriptType `json:"type,omitempty"`
// Description of the object. This is rendered as inline documentation in the // Description of the object. This is rendered as inline documentation in the
// IDE // IDE. This is manually injected here using schema.Docs
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
// keys are named properties of the object // schemas for the fields of an struct. The keys are the first json tag.
// values are json schema for the values of the named properties // The values are the schema for the type of the field
Properties map[string]*Schema `json:"properties,omitempty"` Properties map[string]*Schema `json:"properties,omitempty"`
// the schema for all values of the array // the schema for all values of an array
Items *Schema `json:"items,omitempty"` Items *Schema `json:"items,omitempty"`
// the schema for any properties not mentioned in the Schema.Properties field. // the schema for any properties not mentioned in the Schema.Properties field.
// we leverage this to validate Maps in bundle configuration // this validates Maps in bundle configuration
// OR // OR
// a boolean type with value false // a boolean type with value false. Setting false here validates that all
// properties in the yaml file have been defined in the json schema
// //
// Its type during runtime will either be *Schema or bool // Its type during runtime will either be *Schema or bool
AdditionalProperties interface{} `json:"additionalProperties,omitempty"` AdditionalProperties interface{} `json:"additionalProperties,omitempty"`
// required properties for the object. Any propertites listed here should // required properties for the object. Any fields missing the "omitempty"
// also be listed in Schema.Properties // tag will be included
Required []string `json:"required,omitempty"` Required []string `json:"required,omitempty"`
} }
// This function translates golang types into json schema. Here is the mapping // This function translates golang types into json schema. Here is the mapping
// between json schema types and golang types // between json schema types and golang types
// - GolangType -> Javascript type / Json Schema2 // - GolangType -> Javascript type / Json Schema2
// Javascript Primitives:
// - bool -> boolean // - bool -> boolean
// - string -> string // - string -> string
// - int (all variants) -> number // - int (all variants) -> number
// - float (all variants) -> number // - float (all variants) -> number
// Json Schema2 Fields:
// - map[string]MyStruct -> { // - map[string]MyStruct -> {
// type: object // type: object
// additionalProperties: {} // additionalProperties: {}
@ -150,12 +149,9 @@ func safeToSchema(golangType reflect.Type, docs *Docs, debugTraceId string, seen
} }
// Adds the member fields of golangType to the passed slice. Needed because // Adds the member fields of golangType to the passed slice. Needed because
// golangType can contain embedded fields (aka anonymous) // golangType can contain embedded fields (aka anonymous fields)
// The function traverses the embedded fields in a breadth first manner // The function traverses the embedded fields in a breadth first mannerå
// params:
// fields: slice to which member fields of golangType will be added to
func getStructFields(golangType reflect.Type) []reflect.StructField { func getStructFields(golangType reflect.Type) []reflect.StructField {
fields := []reflect.StructField{} fields := []reflect.StructField{}
bfsQueue := list.New() bfsQueue := list.New()
@ -187,6 +183,7 @@ func getStructFields(golangType reflect.Type) []reflect.StructField {
// params: // params:
// golangType: golang type for which json schema properties to generate // golangType: golang type for which json schema properties to generate
// docs: Struct containing documentation to be injected into the json schema generated
// seenTypes : set of golang types already seen in path during recursion. // seenTypes : set of golang types already seen in path during recursion.
// Used to identify cycles. // Used to identify cycles.
// debugTrace: linked list of golang types encounted. In case of errors this // debugTrace: linked list of golang types encounted. In case of errors this

View File

@ -18,10 +18,10 @@ func TestIntSchema(t *testing.T) {
"type": "number" "type": "number"
}` }`
Int, err := NewSchema(reflect.TypeOf(elemInt), nil) schema, err := NewSchema(reflect.TypeOf(elemInt), nil)
require.NoError(t, err) require.NoError(t, err)
jsonSchema, err := json.MarshalIndent(Int, " ", " ") jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err) assert.NoError(t, err)
t.Log("[DEBUG] actual: ", string(jsonSchema)) t.Log("[DEBUG] actual: ", string(jsonSchema))
@ -37,10 +37,10 @@ func TestBooleanSchema(t *testing.T) {
"type": "boolean" "type": "boolean"
}` }`
Int, err := NewSchema(reflect.TypeOf(elem), nil) schema, err := NewSchema(reflect.TypeOf(elem), nil)
require.NoError(t, err) require.NoError(t, err)
jsonSchema, err := json.MarshalIndent(Int, " ", " ") jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err) assert.NoError(t, err)
t.Log("[DEBUG] actual: ", string(jsonSchema)) t.Log("[DEBUG] actual: ", string(jsonSchema))
@ -56,10 +56,10 @@ func TestStringSchema(t *testing.T) {
"type": "string" "type": "string"
}` }`
Int, err := NewSchema(reflect.TypeOf(elem), nil) schema, err := NewSchema(reflect.TypeOf(elem), nil)
require.NoError(t, err) require.NoError(t, err)
jsonSchema, err := json.MarshalIndent(Int, " ", " ") jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err) assert.NoError(t, err)
t.Log("[DEBUG] actual: ", string(jsonSchema)) t.Log("[DEBUG] actual: ", string(jsonSchema))