2023-01-13 03:52:25 +00:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2023-01-13 18:08:21 +00:00
|
|
|
"container/list"
|
2023-01-13 03:52:25 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-01-17 12:59:56 +00:00
|
|
|
// defines schema for a json object
|
|
|
|
type Schema struct {
|
|
|
|
// Type of the object
|
2023-01-18 15:58:09 +00:00
|
|
|
Type JavascriptType `json:"type,omitempty"`
|
2023-01-17 12:59:56 +00:00
|
|
|
|
2023-01-18 15:02:38 +00:00
|
|
|
// Description of the object. This is rendered as inline documentation in the
|
2023-01-19 10:34:55 +00:00
|
|
|
// IDE. This is manually injected here using schema.Docs
|
2023-01-18 15:02:38 +00:00
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
|
2023-01-19 14:25:17 +00:00
|
|
|
// Schemas for the fields of an struct. The keys are the first json tag.
|
2023-01-19 10:34:55 +00:00
|
|
|
// The values are the schema for the type of the field
|
2023-01-17 12:59:56 +00:00
|
|
|
Properties map[string]*Schema `json:"properties,omitempty"`
|
|
|
|
|
2023-01-19 14:25:17 +00:00
|
|
|
// The schema for all values of an array
|
2023-01-17 12:59:56 +00:00
|
|
|
Items *Schema `json:"items,omitempty"`
|
|
|
|
|
2023-01-19 14:25:17 +00:00
|
|
|
// The schema for any properties not mentioned in the Schema.Properties field.
|
2023-01-19 17:58:31 +00:00
|
|
|
// this validates maps[string]any in bundle configuration
|
2023-01-17 16:53:58 +00:00
|
|
|
// OR
|
2023-01-19 14:25:17 +00:00
|
|
|
// A boolean type with value false. Setting false here validates that all
|
2023-01-19 17:58:31 +00:00
|
|
|
// properties in the config have been defined in the json schema as properties
|
2023-01-17 16:53:58 +00:00
|
|
|
//
|
|
|
|
// Its type during runtime will either be *Schema or bool
|
2023-01-19 14:25:17 +00:00
|
|
|
AdditionalProperties any `json:"additionalProperties,omitempty"`
|
2023-01-17 12:59:56 +00:00
|
|
|
|
2023-01-19 14:25:17 +00:00
|
|
|
// Required properties for the object. Any fields missing the "omitempty"
|
2023-01-19 17:58:31 +00:00
|
|
|
// json tag will be included
|
2023-01-17 12:59:56 +00:00
|
|
|
Required []string `json:"required,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-01-20 15:24:39 +00:00
|
|
|
// This function translates golang types into json schema. Here is the mapping
|
|
|
|
// between json schema types and golang types
|
|
|
|
//
|
|
|
|
// - GolangType -> Javascript type / Json Schema2
|
|
|
|
//
|
|
|
|
// - bool -> boolean
|
|
|
|
//
|
|
|
|
// - string -> string
|
|
|
|
//
|
|
|
|
// - int (all variants) -> number
|
|
|
|
//
|
|
|
|
// - float (all variants) -> number
|
|
|
|
//
|
|
|
|
// - map[string]MyStruct -> { type: object, additionalProperties: {}}
|
|
|
|
// for details visit: https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties
|
|
|
|
//
|
|
|
|
// - []MyStruct -> {type: array, items: {}}
|
|
|
|
// for details visit: https://json-schema.org/understanding-json-schema/reference/array.html#items
|
|
|
|
//
|
|
|
|
// - []MyStruct -> {type: object, properties: {}, additionalProperties: false}
|
|
|
|
// for details visit: https://json-schema.org/understanding-json-schema/reference/object.html#properties
|
2023-01-18 15:02:38 +00:00
|
|
|
func NewSchema(golangType reflect.Type, docs *Docs) (*Schema, error) {
|
2023-01-19 17:23:37 +00:00
|
|
|
tracker := newTracker()
|
|
|
|
schema, err := safeToSchema(golangType, docs, "", tracker)
|
2023-01-13 03:52:25 +00:00
|
|
|
if err != nil {
|
2023-01-19 17:23:37 +00:00
|
|
|
return nil, tracker.errWithTrace(err.Error())
|
2023-01-13 03:52:25 +00:00
|
|
|
}
|
2023-01-18 15:02:38 +00:00
|
|
|
return schema, nil
|
2023-01-13 03:52:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 19:03:24 +00:00
|
|
|
type JavascriptType string
|
2023-01-13 03:52:25 +00:00
|
|
|
|
|
|
|
const (
|
2023-01-13 19:03:24 +00:00
|
|
|
Invalid JavascriptType = "invalid"
|
|
|
|
Boolean JavascriptType = "boolean"
|
|
|
|
String JavascriptType = "string"
|
|
|
|
Number JavascriptType = "number"
|
|
|
|
Object JavascriptType = "object"
|
|
|
|
Array JavascriptType = "array"
|
2023-01-13 03:52:25 +00:00
|
|
|
)
|
|
|
|
|
2023-01-13 18:40:09 +00:00
|
|
|
func javascriptType(golangType reflect.Type) (JavascriptType, error) {
|
2023-01-13 15:45:01 +00:00
|
|
|
switch golangType.Kind() {
|
2023-01-13 03:52:25 +00:00
|
|
|
case reflect.Bool:
|
|
|
|
return Boolean, nil
|
|
|
|
case reflect.String:
|
|
|
|
return String, nil
|
2023-01-13 15:45:01 +00:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
|
|
|
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
|
|
|
reflect.Float32, reflect.Float64:
|
|
|
|
|
2023-01-13 03:52:25 +00:00
|
|
|
return Number, nil
|
|
|
|
case reflect.Struct:
|
|
|
|
return Object, nil
|
|
|
|
case reflect.Map:
|
2023-01-13 15:45:01 +00:00
|
|
|
if golangType.Key().Kind() != reflect.String {
|
|
|
|
return Invalid, fmt.Errorf("only strings map keys are valid. key type: %v", golangType.Key().Kind())
|
2023-01-13 03:52:25 +00:00
|
|
|
}
|
|
|
|
return Object, nil
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
return Array, nil
|
|
|
|
default:
|
2023-01-13 15:45:01 +00:00
|
|
|
return Invalid, fmt.Errorf("unhandled golang type: %s", golangType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:58:31 +00:00
|
|
|
// A wrapper over toSchema function to:
|
|
|
|
// 1. Detect cycles in the bundle config struct.
|
|
|
|
// 2. Update tracker
|
|
|
|
//
|
|
|
|
// params:
|
|
|
|
//
|
|
|
|
// - golangType: Golang type to generate json schema for
|
|
|
|
//
|
|
|
|
// - docs: Contains documentation to be injected into the generated json schema
|
|
|
|
//
|
|
|
|
// - traceId: An identifier for the current type, to trace recursive traversal.
|
|
|
|
// Its value is the first json tag in case of struct fields and "" in other cases
|
|
|
|
// like array, map or no json tags
|
|
|
|
//
|
|
|
|
// - tracker: Keeps track of types / traceIds seen during recursive traversal
|
2023-01-19 17:23:37 +00:00
|
|
|
func safeToSchema(golangType reflect.Type, docs *Docs, traceId string, tracker *tracker) (*Schema, error) {
|
2023-01-17 16:53:58 +00:00
|
|
|
// WE ERROR OUT IF THERE ARE CYCLES IN THE JSON SCHEMA
|
|
|
|
// There are mechanisms to deal with cycles though recursive identifiers in json
|
|
|
|
// schema. However if we use them, we would need to make sure we are able to detect
|
2023-01-19 17:58:31 +00:00
|
|
|
// cycles where two properties (directly or indirectly) pointing to each other
|
2023-01-17 16:53:58 +00:00
|
|
|
//
|
|
|
|
// see: https://json-schema.org/understanding-json-schema/structuring.html#recursion
|
|
|
|
// for details
|
2023-01-19 17:23:37 +00:00
|
|
|
if tracker.hasCycle(golangType) {
|
2023-01-13 19:03:24 +00:00
|
|
|
return nil, fmt.Errorf("cycle detected")
|
2023-01-13 18:08:21 +00:00
|
|
|
}
|
2023-01-18 16:36:50 +00:00
|
|
|
|
2023-01-20 15:26:13 +00:00
|
|
|
tracker.push(golangType, traceId)
|
2023-01-19 17:23:37 +00:00
|
|
|
props, err := toSchema(golangType, docs, tracker)
|
2023-01-13 18:08:21 +00:00
|
|
|
if err != nil {
|
2023-01-13 19:03:24 +00:00
|
|
|
return nil, err
|
2023-01-13 18:08:21 +00:00
|
|
|
}
|
2023-01-20 15:26:13 +00:00
|
|
|
tracker.pop(golangType)
|
2023-01-13 18:08:21 +00:00
|
|
|
return props, nil
|
|
|
|
}
|
|
|
|
|
2023-01-19 14:25:17 +00:00
|
|
|
// 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
|
2023-01-18 16:45:53 +00:00
|
|
|
func getStructFields(golangType reflect.Type) []reflect.StructField {
|
|
|
|
fields := []reflect.StructField{}
|
2023-01-13 18:08:21 +00:00
|
|
|
bfsQueue := list.New()
|
|
|
|
|
2023-01-18 11:54:51 +00:00
|
|
|
for i := 0; i < golangType.NumField(); i++ {
|
2023-01-13 18:08:21 +00:00
|
|
|
bfsQueue.PushBack(golangType.Field(i))
|
|
|
|
}
|
|
|
|
for bfsQueue.Len() > 0 {
|
|
|
|
front := bfsQueue.Front()
|
|
|
|
field := front.Value.(reflect.StructField)
|
|
|
|
bfsQueue.Remove(front)
|
|
|
|
|
|
|
|
if !field.Anonymous {
|
|
|
|
fields = append(fields, field)
|
|
|
|
continue
|
|
|
|
}
|
2023-01-13 18:25:42 +00:00
|
|
|
|
|
|
|
fieldType := field.Type
|
|
|
|
if fieldType.Kind() == reflect.Pointer {
|
|
|
|
fieldType = fieldType.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < fieldType.NumField(); i++ {
|
|
|
|
bfsQueue.PushBack(fieldType.Field(i))
|
2023-01-13 18:08:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fields
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:23:37 +00:00
|
|
|
func toSchema(golangType reflect.Type, docs *Docs, tracker *tracker) (*Schema, error) {
|
2023-01-13 03:52:25 +00:00
|
|
|
// *Struct and Struct generate identical json schemas
|
2023-01-13 15:45:01 +00:00
|
|
|
if golangType.Kind() == reflect.Pointer {
|
2023-01-19 17:23:37 +00:00
|
|
|
return safeToSchema(golangType.Elem(), docs, "", tracker)
|
2023-01-13 19:03:24 +00:00
|
|
|
}
|
|
|
|
if golangType.Kind() == reflect.Interface {
|
2023-01-18 15:58:09 +00:00
|
|
|
return &Schema{}, nil
|
2023-01-13 03:52:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 15:45:01 +00:00
|
|
|
rootJavascriptType, err := javascriptType(golangType)
|
2023-01-13 03:52:25 +00:00
|
|
|
if err != nil {
|
2023-01-13 18:08:21 +00:00
|
|
|
return nil, err
|
2023-01-13 03:52:25 +00:00
|
|
|
}
|
2023-01-19 17:31:55 +00:00
|
|
|
schema := &Schema{Type: rootJavascriptType}
|
2023-01-13 03:52:25 +00:00
|
|
|
|
2023-01-18 15:02:38 +00:00
|
|
|
if docs != nil {
|
2023-01-19 17:31:55 +00:00
|
|
|
schema.Description = docs.Documentation
|
2023-01-18 15:02:38 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 18:08:21 +00:00
|
|
|
// case array/slice
|
2023-01-13 15:45:01 +00:00
|
|
|
if golangType.Kind() == reflect.Array || golangType.Kind() == reflect.Slice {
|
2023-01-13 17:01:53 +00:00
|
|
|
elemGolangType := golangType.Elem()
|
|
|
|
elemJavascriptType, err := javascriptType(elemGolangType)
|
2023-01-13 03:52:25 +00:00
|
|
|
if err != nil {
|
2023-01-13 18:08:21 +00:00
|
|
|
return nil, err
|
2023-01-13 17:01:53 +00:00
|
|
|
}
|
2023-01-19 17:23:37 +00:00
|
|
|
elemProps, err := safeToSchema(elemGolangType, docs, "", tracker)
|
2023-01-13 17:01:53 +00:00
|
|
|
if err != nil {
|
2023-01-13 18:08:21 +00:00
|
|
|
return nil, err
|
2023-01-13 17:01:53 +00:00
|
|
|
}
|
2023-01-19 17:31:55 +00:00
|
|
|
schema.Items = &Schema{
|
2023-01-17 18:31:18 +00:00
|
|
|
Type: elemJavascriptType,
|
|
|
|
Properties: elemProps.Properties,
|
|
|
|
AdditionalProperties: elemProps.AdditionalProperties,
|
|
|
|
Items: elemProps.Items,
|
2023-01-18 11:54:51 +00:00
|
|
|
Required: elemProps.Required,
|
2023-01-13 03:52:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 18:08:21 +00:00
|
|
|
// case map
|
|
|
|
if golangType.Kind() == reflect.Map {
|
|
|
|
if golangType.Key().Kind() != reflect.String {
|
2023-01-13 19:03:24 +00:00
|
|
|
return nil, fmt.Errorf("only string keyed maps allowed")
|
2023-01-13 18:08:21 +00:00
|
|
|
}
|
2023-01-19 17:31:55 +00:00
|
|
|
schema.AdditionalProperties, err = safeToSchema(golangType.Elem(), docs, "", tracker)
|
2023-01-13 18:08:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 11:09:19 +00:00
|
|
|
|
2023-01-13 18:08:21 +00:00
|
|
|
// case struct
|
2023-01-13 15:45:01 +00:00
|
|
|
if golangType.Kind() == reflect.Struct {
|
2023-01-18 16:45:53 +00:00
|
|
|
children := getStructFields(golangType)
|
2023-01-19 17:31:55 +00:00
|
|
|
properties := map[string]*Schema{}
|
|
|
|
required := []string{}
|
2023-01-13 18:08:21 +00:00
|
|
|
for _, child := range children {
|
2023-01-18 15:58:09 +00:00
|
|
|
// get child json tags
|
2023-01-18 11:54:51 +00:00
|
|
|
childJsonTag := strings.Split(child.Tag.Get("json"), ",")
|
|
|
|
childName := childJsonTag[0]
|
2023-01-13 19:03:24 +00:00
|
|
|
|
2023-01-18 15:58:09 +00:00
|
|
|
// skip children that have no json tags, the first json tag is ""
|
|
|
|
// or the first json tag is "-"
|
2023-01-17 10:58:32 +00:00
|
|
|
if childName == "" || childName == "-" {
|
2023-01-13 15:45:01 +00:00
|
|
|
continue
|
2023-01-13 11:09:19 +00:00
|
|
|
}
|
2023-01-13 15:45:01 +00:00
|
|
|
|
2023-01-18 15:02:38 +00:00
|
|
|
// get docs for the child if they exist
|
|
|
|
var childDocs *Docs
|
|
|
|
if docs != nil {
|
|
|
|
if val, ok := docs.Children[childName]; ok {
|
|
|
|
childDocs = &val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 15:58:09 +00:00
|
|
|
// compute if the child is a required field. Determined by the
|
2023-01-19 17:58:31 +00:00
|
|
|
// presence of "omitempty" in the json tags
|
2023-01-18 11:54:51 +00:00
|
|
|
hasOmitEmptyTag := false
|
|
|
|
for i := 1; i < len(childJsonTag); i++ {
|
|
|
|
if childJsonTag[i] == "omitempty" {
|
|
|
|
hasOmitEmptyTag = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasOmitEmptyTag {
|
|
|
|
required = append(required, childName)
|
|
|
|
}
|
|
|
|
|
2023-01-18 15:58:09 +00:00
|
|
|
// compute Schema.Properties for the child recursively
|
2023-01-19 17:23:37 +00:00
|
|
|
fieldProps, err := safeToSchema(child.Type, childDocs, childName, tracker)
|
2023-01-13 03:52:25 +00:00
|
|
|
if err != nil {
|
2023-01-13 19:03:24 +00:00
|
|
|
return nil, err
|
2023-01-13 03:52:25 +00:00
|
|
|
}
|
2023-01-16 20:53:20 +00:00
|
|
|
properties[childName] = fieldProps
|
2023-01-13 03:52:25 +00:00
|
|
|
}
|
2023-01-18 15:58:09 +00:00
|
|
|
|
2023-01-19 17:31:55 +00:00
|
|
|
schema.AdditionalProperties = false
|
|
|
|
schema.Properties = properties
|
|
|
|
schema.Required = required
|
2023-01-13 03:52:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 17:31:55 +00:00
|
|
|
return schema, nil
|
2023-01-13 03:52:25 +00:00
|
|
|
}
|