SYS-REQ-002, SYS-REQ-071, SYS-REQ-072, SYS-REQ-073, SYS-REQ-074 GetString returns the value retrieved by `Get`, cast to a string if possible, trying to properly handle escape and utf8 symbols If key data type do not match, it will return an error.
(data []byte, keys ...string)
| 1985 | // GetString returns the value retrieved by `Get`, cast to a string if possible, trying to properly handle escape and utf8 symbols |
| 1986 | // If key data type do not match, it will return an error. |
| 1987 | func GetString(data []byte, keys ...string) (val string, err error) { |
| 1988 | v, t, _, e := Get(data, keys...) |
| 1989 | |
| 1990 | if e != nil { |
| 1991 | return "", e |
| 1992 | } |
| 1993 | |
| 1994 | if t != String { |
| 1995 | if t == Null { |
| 1996 | return "", NullValueError |
| 1997 | } |
| 1998 | return "", fmt.Errorf("Value is not a string: %s", string(v)) |
| 1999 | } |
| 2000 | |
| 2001 | // If no escapes return raw content |
| 2002 | if bytes.IndexByte(v, '\\') == -1 { |
| 2003 | return string(v), nil |
| 2004 | } |
| 2005 | |
| 2006 | return ParseString(v) |
| 2007 | } |
| 2008 | |
| 2009 | // GetFloat returns the value retrieved by `Get`, cast to a float64 if possible. |
| 2010 | // The offset is the same as in `Get`. |