FromStruct 从 Go struct 生成 JSON Schema 支持的 struct tags: - json: JSON 字段名 - required: 是否必需 ("true"/"false") - description: 字段描述 - enum: 枚举值(逗号分隔) - minimum: 最小值(数字类型) - maximum: 最大值(数字类型) - pattern: 正则模式(字符串类型)
(v any)
| 28 | // - maximum: 最大值(数字类型) |
| 29 | // - pattern: 正则模式(字符串类型) |
| 30 | func (g *SchemaGenerator) FromStruct(v any) (map[string]any, error) { |
| 31 | typ := reflect.TypeOf(v) |
| 32 | |
| 33 | // 如果是指针,获取实际类型 |
| 34 | if typ.Kind() == reflect.Ptr { |
| 35 | typ = typ.Elem() |
| 36 | } |
| 37 | |
| 38 | if typ.Kind() != reflect.Struct { |
| 39 | return nil, fmt.Errorf("expected struct type, got %v", typ.Kind()) |
| 40 | } |
| 41 | |
| 42 | return g.structToSchema(typ) |
| 43 | } |
| 44 | |
| 45 | // structToSchema 将 struct 类型转换为 JSON Schema |
| 46 | func (g *SchemaGenerator) structToSchema(typ reflect.Type) (map[string]any, error) { |
nothing calls this directly
no test coverage detected