createStructDefinition creates a JSON schema definition for a struct type
(t reflect.Type)
| 15 | |
| 16 | // createStructDefinition creates a JSON schema definition for a struct type |
| 17 | func createStructDefinition(t reflect.Type) map[string]any { |
| 18 | structDef := make(map[string]any) |
| 19 | structDef["type"] = "object" |
| 20 | properties := make(map[string]any) |
| 21 | required := make([]string, 0) |
| 22 | |
| 23 | for i := 0; i < t.NumField(); i++ { |
| 24 | field := t.Field(i) |
| 25 | if !field.IsExported() { |
| 26 | continue |
| 27 | } |
| 28 | |
| 29 | // Parse JSON tag |
| 30 | fieldInfo, shouldInclude := util.ParseJSONTag(field) |
| 31 | if !shouldInclude { |
| 32 | continue // Skip this field |
| 33 | } |
| 34 | |
| 35 | // If field has "string" option, force schema type to string |
| 36 | var fieldSchema map[string]any |
| 37 | if fieldInfo.AsString { |
| 38 | fieldSchema = map[string]any{"type": "string"} |
| 39 | } else { |
| 40 | fieldSchema = generateShallowJSONSchema(field.Type, nil) |
| 41 | } |
| 42 | |
| 43 | // Add description from "desc" tag if present |
| 44 | if desc := field.Tag.Get("desc"); desc != "" { |
| 45 | fieldSchema["description"] = desc |
| 46 | } |
| 47 | |
| 48 | // Add enum values from "enum" tag if present (only for string types) |
| 49 | if enumTag := field.Tag.Get("enum"); enumTag != "" && fieldSchema["type"] == "string" { |
| 50 | enumValues := make([]any, 0) |
| 51 | for _, val := range strings.Split(enumTag, ",") { |
| 52 | trimmed := strings.TrimSpace(val) |
| 53 | if trimmed != "" { |
| 54 | enumValues = append(enumValues, trimmed) |
| 55 | } |
| 56 | } |
| 57 | if len(enumValues) > 0 { |
| 58 | fieldSchema["enum"] = enumValues |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Add units from "units" tag if present |
| 63 | if units := field.Tag.Get("units"); units != "" { |
| 64 | fieldSchema["units"] = units |
| 65 | } |
| 66 | |
| 67 | // Add min/max constraints for numeric types |
| 68 | if fieldSchema["type"] == "number" || fieldSchema["type"] == "integer" { |
| 69 | if minTag := field.Tag.Get("min"); minTag != "" { |
| 70 | if minVal, err := strconv.ParseFloat(minTag, 64); err == nil { |
| 71 | fieldSchema["minimum"] = minVal |
| 72 | } |
| 73 | } |
| 74 | if maxTag := field.Tag.Get("max"); maxTag != "" { |
no test coverage detected