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

Function parseSliceInto

huma.go:1773–1960  ·  view source on GitHub ↗

parseSliceInto converts a slice of string values into the expected type of f and sets the result on f.

(f reflect.Value, values []string)

Source from the content-addressed store, hash-verified

1771// parseSliceInto converts a slice of string values into the expected type of f
1772// and sets the result on f.
1773func parseSliceInto(f reflect.Value, values []string) (any, error) {
1774 switch f.Type().Elem().Kind() {
1775
1776 case reflect.String:
1777 if f.Type() == stringSliceType {
1778 f.Set(reflect.ValueOf(values))
1779 } else {
1780 // Change element type to support slice of string subtypes (enums)
1781 enumValues := reflect.New(f.Type()).Elem()
1782 for _, val := range values {
1783 enumVal := reflect.New(f.Type().Elem()).Elem()
1784 enumVal.SetString(val)
1785 enumValues.Set(reflect.Append(enumValues, enumVal))
1786 }
1787 f.Set(enumValues)
1788 }
1789 return values, nil
1790
1791 case reflect.Int:
1792 vs, err := parseArrElement(values, func(s string) (int, error) {
1793 val, err := strconv.ParseInt(s, 10, strconv.IntSize)
1794 if err != nil {
1795 return 0, err
1796 }
1797 return int(val), nil
1798 })
1799 if err != nil {
1800 return nil, errors.New("invalid integer")
1801 }
1802 f.Set(reflect.ValueOf(vs))
1803 return vs, nil
1804
1805 case reflect.Int8:
1806 vs, err := parseArrElement(values, func(s string) (int8, error) {
1807 val, err := strconv.ParseInt(s, 10, 8)
1808 if err != nil {
1809 return 0, err
1810 }
1811 return int8(val), nil
1812 })
1813 if err != nil {
1814 return nil, errors.New("invalid integer")
1815 }
1816 f.Set(reflect.ValueOf(vs))
1817 return vs, nil
1818
1819 case reflect.Int16:
1820 vs, err := parseArrElement(values, func(s string) (int16, error) {
1821 val, err := strconv.ParseInt(s, 10, 16)
1822 if err != nil {
1823 return 0, err
1824 }
1825 return int16(val), nil
1826 })
1827 if err != nil {
1828 return nil, errors.New("invalid integer")
1829 }
1830 f.Set(reflect.ValueOf(vs))

Callers 1

parseIntoFunction · 0.85

Calls 1

parseArrElementFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…