MCPcopy
hub / github.com/pelletier/go-toml / decodeAny

Method decodeAny

unmarshaler.go:1966–2046  ·  view source on GitHub ↗

decodeAny decodes a value node into a native Go value (the representation used for interface{} targets), without going through reflect. Scalars and arrays are handled directly; inline tables still defer to the reflect-based path so that their dotted-key merge semantics remain identical.

(n *unstable.Node)

Source from the content-addressed store, hash-verified

1964// arrays are handled directly; inline tables still defer to the reflect-based
1965// path so that their dotted-key merge semantics remain identical.
1966func (d *decoder) decodeAny(n *unstable.Node) (interface{}, error) {
1967 switch n.Kind {
1968 case unstable.String:
1969 return string(n.Data), nil
1970 case unstable.Integer:
1971 i, err := parseInteger(n.Data)
1972 return i, err
1973 case unstable.Float:
1974 f, err := parseFloat(n.Data)
1975 return f, err
1976 case unstable.Bool:
1977 return n.Data[0] == 't', nil
1978 case unstable.Array:
1979 count := 0
1980 cit := n.Children()
1981 for cit.Next() {
1982 if cit.Node().Kind != unstable.Comment {
1983 count++
1984 }
1985 }
1986 slice := make([]interface{}, 0, count)
1987 it := n.Children()
1988 for it.Next() {
1989 c := it.Node()
1990 if c.Kind == unstable.Comment {
1991 continue
1992 }
1993 ev, err := d.decodeAny(c)
1994 if err != nil {
1995 return nil, err
1996 }
1997 slice = append(slice, ev)
1998 }
1999 return slice, nil
2000 case unstable.InlineTable:
2001 // Build the map natively: navigate each (possibly dotted) key with
2002 // plain Go map operations and decode each value with decodeAny. The
2003 // seen-tracker has already rejected duplicate or conflicting keys, so
2004 // intermediate parts can be created/merged without revalidation.
2005 count := 0
2006 cit := n.Children()
2007 for cit.Next() {
2008 count++
2009 }
2010 m := make(map[string]interface{}, count)
2011 it := n.Children()
2012 for it.Next() {
2013 kv := it.Node()
2014 if err := d.setAnyKey(m, kv.Key(), kv.Value()); err != nil {
2015 return nil, err
2016 }
2017 }
2018 return m, nil
2019 case unstable.DateTime:
2020 t, err := parseDateTime(n.Data)
2021 return t, err
2022 case unstable.LocalDateTime:
2023 dt, rest, err := parseLocalDateTime(n.Data)

Callers 4

fusedKeyValMethod · 0.95
setAnyKeyMethod · 0.95
descendStrMapMethod · 0.95
assignArrayMethod · 0.95

Calls 13

setAnyKeyMethod · 0.95
NewParserErrorFunction · 0.92
parseIntegerFunction · 0.85
parseFloatFunction · 0.85
parseDateTimeFunction · 0.85
parseLocalDateTimeFunction · 0.85
parseLocalDateFunction · 0.85
parseLocalTimeFunction · 0.85
ChildrenMethod · 0.80
NodeMethod · 0.80
ValueMethod · 0.80
NextMethod · 0.45

Tested by

no test coverage detected