MCPcopy Create free account
hub / github.com/astercloud/aster / FromStruct

Method FromStruct

pkg/structured/schema.go:30–43  ·  view source on GitHub ↗

FromStruct 从 Go struct 生成 JSON Schema 支持的 struct tags: - json: JSON 字段名 - required: 是否必需 ("true"/"false") - description: 字段描述 - enum: 枚举值(逗号分隔) - minimum: 最小值(数字类型) - maximum: 最大值(数字类型) - pattern: 正则模式(字符串类型)

(v any)

Source from the content-addressed store, hash-verified

28// - maximum: 最大值(数字类型)
29// - pattern: 正则模式(字符串类型)
30func (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
46func (g *SchemaGenerator) structToSchema(typ reflect.Type) (map[string]any, error) {

Callers

nothing calls this directly

Calls 2

structToSchemaMethod · 0.95
KindMethod · 0.65

Tested by

no test coverage detected