added recursive limit

This commit is contained in:
Shreyas Goenka 2023-01-13 12:09:19 +01:00
parent d46dbbce6a
commit 987dd29357
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
1 changed files with 44 additions and 18 deletions

View File

@ -6,17 +6,21 @@ import (
"strings" "strings"
) )
// TODO: should omit empty denote non required fields in the json schema const MaxHistoryOccurances = 3
// TODO: should omit empty denote non required fields in the json schema?
type Schema struct { type Schema struct {
Type JsType `json:"type"` Type JsType `json:"type"`
Properities map[string]*Property `json:"properities,omitempty"` Properities map[string]*Property `json:"properities,omitempty"`
AdditionalProperities *Property `json:"additionalProperities,omitempty"`
} }
type Property struct { type Property struct {
// TODO: Add a enum for json types // TODO: Add a enum for json types
Type JsType `json:"type"` Type JsType `json:"type"`
Items *Item `json:"item,omitempty"` Items *Item `json:"item,omitempty"`
Properities map[string]*Property `json:"properities,omitempty"` Properities map[string]*Property `json:"properities,omitempty"`
AdditionalProperities *Property `json:"additionalProperities,omitempty"`
} }
type Item struct { type Item struct {
@ -29,8 +33,9 @@ func NewSchema(goType reflect.Type) (*Schema, error) {
return nil, err return nil, err
} }
return &Schema{ return &Schema{
Type: rootProp.Type, Type: rootProp.Type,
Properities: rootProp.Properities, Properities: rootProp.Properities,
AdditionalProperities: rootProp.AdditionalProperities,
}, nil }, nil
} }
@ -45,7 +50,6 @@ const (
Array = "array" Array = "array"
) )
// TODO: add support for pointers
func jsType(goType reflect.Type) (JsType, error) { func jsType(goType reflect.Type) (JsType, error) {
switch goType.Kind() { switch goType.Kind() {
case reflect.Bool: case reflect.Bool:
@ -71,8 +75,8 @@ func jsType(goType reflect.Type) (JsType, error) {
// TODO: handle case of self referential pointers in structs // TODO: handle case of self referential pointers in structs
// TODO: add support for lower case field name if json tag is missing // TODO: add doc string explaining numHistoryOccurances
func properity(goType reflect.Type) (*Property, error) { func properity(goType reflect.Type, numHistoryOccurances map[string]int) (*Property, error) {
// *Struct and Struct generate identical json schemas // *Struct and Struct generate identical json schemas
if goType.Kind() == reflect.Pointer { if goType.Kind() == reflect.Pointer {
return properity(goType.Elem()) return properity(goType.Elem())
@ -100,26 +104,48 @@ func properity(goType reflect.Type) (*Property, error) {
} }
properities := map[string]*Property{} properities := map[string]*Property{}
var additionalProperities *Property
// TODO: for reflect.Map case for prop computation
if goType.Kind() == reflect.Struct { if goType.Kind() == reflect.Struct {
for i := 0; i < goType.NumField(); i++ { for i := 0; i < goType.NumField(); i++ {
field := goType.Field(i) field := goType.Field(i)
// compute child properties
fieldJsonTag := field.Tag.Get("json")
fieldName := strings.Split(fieldJsonTag, ",")[0]
// stopgap infinite recursion
numHistoryOccurances[fieldName] += 1
if numHistoryOccurances[fieldName] > MaxHistoryOccurances {
return nil
}
fieldProps, err := properity(field.Type) fieldProps, err := properity(field.Type)
numHistoryOccurances[fieldName] -= 1
// TODO: make sure this error floats up with context // TODO: make sure this error floats up with context
if err != nil { if err != nil {
return nil, err return nil, err
} }
fieldJsonTags := field.Tag.Get("json")
// TODO: test if this split is needed
fieldName := strings.Split(fieldJsonTags, ",")[0]
properities[fieldName] = fieldProps if fieldJsonTag != "" {
properities[fieldName] = fieldProps
} else if additionalProperities == nil {
// TODO: add error disallowing self referenincing without json tags
additionalProperities = fieldProps
} else {
// TODO: float error up with context
return nil, fmt.Errorf("only one non json annotated field allowed")
}
} }
} }
return &Property{ return &Property{
Type: rootJsType, Type: rootJsType,
Items: items, Items: items,
Properities: properities, Properities: properities,
AdditionalProperities: additionalProperities,
}, nil }, nil
} }