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

Function parseInto

huma.go:1685–1769  ·  view source on GitHub ↗

parseInto converts the string value into the expected type using the parameter field information p and sets the result on f.

(ctx Context, f reflect.Value, value string, preSplit []string, p paramFieldInfo)

Source from the content-addressed store, hash-verified

1683// parseInto converts the string value into the expected type using the
1684// parameter field information p and sets the result on f.
1685func parseInto(ctx Context, f reflect.Value, value string, preSplit []string, p paramFieldInfo) (any, error) {
1686 // built-in types
1687 switch p.Type.Kind() {
1688 case reflect.String:
1689 f.SetString(value)
1690 return value, nil
1691 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
1692 v, err := strconv.ParseInt(value, 10, 64)
1693 if err != nil {
1694 return nil, errors.New("invalid integer")
1695 }
1696 f.SetInt(v)
1697 return v, nil
1698 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
1699 v, err := strconv.ParseUint(value, 10, 64)
1700 if err != nil {
1701 return nil, errors.New("invalid integer")
1702 }
1703 f.SetUint(v)
1704 return v, nil
1705 case reflect.Float32, reflect.Float64:
1706 v, err := strconv.ParseFloat(value, 64)
1707 if err != nil {
1708 return nil, errors.New("invalid float")
1709 }
1710 f.SetFloat(v)
1711 return v, nil
1712 case reflect.Bool:
1713 v, err := strconv.ParseBool(value)
1714 if err != nil {
1715 return nil, errors.New("invalid boolean")
1716 }
1717 f.SetBool(v)
1718 return v, nil
1719 case reflect.Slice:
1720 var values []string
1721 if preSplit != nil {
1722 values = preSplit
1723 } else {
1724 if p.Explode {
1725 u := ctx.URL()
1726 values = (&u).Query()[p.Name]
1727 } else {
1728 values = strings.Split(value, ",")
1729 }
1730 }
1731 pv, err := parseSliceInto(f, values)
1732 if err != nil {
1733 if errors.Is(err, errUnparsable) {
1734 break
1735 }
1736 return nil, err
1737 }
1738 return pv, nil
1739 }
1740
1741 // special types
1742 switch f.Type() {

Callers 1

RegisterFunction · 0.85

Calls 6

parseSliceIntoFunction · 0.85
URLMethod · 0.65
QueryMethod · 0.65
ErrorMethod · 0.65
UnmarshalTextMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…