MCPcopy
hub / github.com/danielgtaylor/huma / schemaFromType

Function schemaFromType

schema.go:764–989  ·  view source on GitHub ↗
(r Registry, t reflect.Type)

Source from the content-addressed store, hash-verified

762}
763
764func schemaFromType(r Registry, t reflect.Type) *Schema {
765 isPointer := t.Kind() == reflect.Pointer
766
767 s := Schema{}
768 t = deref(t)
769
770 ptrT := reflect.PointerTo(t)
771 if t.Implements(schemaProviderType) || ptrT.Implements(schemaProviderType) {
772 // Special case: type provides its own schema. Do not try to generate.
773 custom := reflect.New(t).Interface().(SchemaProvider).Schema(r)
774 custom.PrecomputeMessages()
775 return custom
776 }
777
778 // Handle special cases for known stdlib types.
779 switch t {
780 case timeType:
781 return &Schema{Type: TypeString, Nullable: isPointer, Format: "date-time"}
782 case urlType:
783 return &Schema{Type: TypeString, Nullable: isPointer, Format: "uri"}
784 case ipType:
785 return &Schema{Type: TypeString, Nullable: isPointer, Format: "ipv4"}
786 case ipAddrType:
787 return &Schema{Type: TypeString, Nullable: isPointer, Format: "ip"}
788 case rawMessageType:
789 return &Schema{}
790 }
791
792 if t.Implements(textUnmarshalerType) || ptrT.Implements(textUnmarshalerType) {
793 // Special case: types that implement encoding.TextUnmarshaler are able to
794 // be loaded from plain text, and so should be treated as strings.
795 // This behavior can be overridden by implementing `huma.SchemaProvider`
796 // and returning a custom schema.
797 return &Schema{Type: TypeString, Nullable: isPointer}
798 }
799
800 minZero := 0.0
801 switch t.Kind() {
802 case reflect.Bool:
803 s.Type = TypeBoolean
804 case reflect.Int:
805 s.Type = TypeInteger
806 if bits.UintSize == 32 {
807 s.Format = "int32"
808 } else {
809 s.Format = "int64"
810 }
811 case reflect.Int8, reflect.Int16, reflect.Int32:
812 s.Type = TypeInteger
813 s.Format = "int32"
814 case reflect.Int64:
815 s.Type = TypeInteger
816 s.Format = "int64"
817 case reflect.Uint:
818 s.Type = TypeInteger
819 if bits.UintSize == 32 {
820 s.Format = "int32"
821 } else {

Callers 1

SchemaFromTypeFunction · 0.85

Calls 8

PrecomputeMessagesMethod · 0.95
getFieldsFunction · 0.85
boolTagFunction · 0.85
SchemaFromFieldFunction · 0.85
LenMethod · 0.80
derefFunction · 0.70
SchemaMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…