| 1699 | } |
| 1700 | |
| 1701 | func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error { |
| 1702 | format := strings.ToLower(v.getConfigType()) |
| 1703 | if format == "" { |
| 1704 | return errors.New("cannot decode configuration: unable to determine config type") |
| 1705 | } |
| 1706 | |
| 1707 | buf := new(bytes.Buffer) |
| 1708 | _, err := buf.ReadFrom(in) |
| 1709 | if err != nil { |
| 1710 | return fmt.Errorf("failed to read configuration from input: %w", err) |
| 1711 | } |
| 1712 | |
| 1713 | // TODO: remove this once SupportedExts is deprecated/removed |
| 1714 | if !slices.Contains(SupportedExts, format) { |
| 1715 | return UnsupportedConfigError(format) |
| 1716 | } |
| 1717 | |
| 1718 | // TODO: return [UnsupportedConfigError] if the registry does not contain the format |
| 1719 | // TODO: consider deprecating this error type |
| 1720 | decoder, err := v.decoderRegistry.Decoder(format) |
| 1721 | if err != nil { |
| 1722 | return ConfigParseError{err} |
| 1723 | } |
| 1724 | |
| 1725 | err = decoder.Decode(buf.Bytes(), c) |
| 1726 | if err != nil { |
| 1727 | return ConfigParseError{err} |
| 1728 | } |
| 1729 | |
| 1730 | insensitiviseMap(c) |
| 1731 | return nil |
| 1732 | } |
| 1733 | |
| 1734 | // Marshal a map into Writer. |
| 1735 | func (v *Viper) marshalWriter(w io.Writer, configType string) error { |