5 // MetaData allows access to meta information about TOML data that may not
6 // be inferrable via reflection. In particular, whether a key has been defined
7 // and the TOML type of a key.
9 mapping map[string]interface{}
10 types map[string]tomlType
12 decoded map[string]bool
13 context Key // Used only during decoding.
16 // IsDefined returns true if the key given exists in the TOML data. The key
17 // should be specified hierarchially. e.g.,
19 // // access the TOML key 'a.b.c'
20 // IsDefined("a", "b", "c")
22 // IsDefined will return false if an empty key given. Keys are case sensitive.
23 func (md *MetaData) IsDefined(key ...string) bool {
28 var hash map[string]interface{}
30 var hashOrVal interface{} = md.mapping
31 for _, k := range key {
32 if hash, ok = hashOrVal.(map[string]interface{}); !ok {
35 if hashOrVal, ok = hash[k]; !ok {
42 // Type returns a string representation of the type of the key specified.
44 // Type will return the empty string if given an empty key or a key that
45 // does not exist. Keys are case sensitive.
46 func (md *MetaData) Type(key ...string) string {
47 fullkey := strings.Join(key, ".")
48 if typ, ok := md.types[fullkey]; ok {
49 return typ.typeString()
54 // Key is the type of any TOML key, including key groups. Use (MetaData).Keys
55 // to get values of this type.
58 func (k Key) String() string {
59 return strings.Join(k, ".")
62 func (k Key) maybeQuotedAll() string {
65 ss = append(ss, k.maybeQuoted(i))
67 return strings.Join(ss, ".")
70 func (k Key) maybeQuoted(i int) string {
72 for _, c := range k[i] {
73 if !isBareKeyChar(c) {
79 return "\"" + strings.Replace(k[i], "\"", "\\\"", -1) + "\""
84 func (k Key) add(piece string) Key {
85 newKey := make(Key, len(k)+1)
87 newKey[len(k)] = piece
91 // Keys returns a slice of every key in the TOML data, including key groups.
92 // Each key is itself a slice, where the first element is the top of the
93 // hierarchy and the last is the most specific.
95 // The list will have the same order as the keys appeared in the TOML data.
97 // All keys returned are non-empty.
98 func (md *MetaData) Keys() []Key {
102 // Undecoded returns all keys that have not been decoded in the order in which
103 // they appear in the original TOML document.
105 // This includes keys that haven't been decoded because of a Primitive value.
106 // Once the Primitive value is decoded, the keys will be considered decoded.
108 // Also note that decoding into an empty interface will result in no decoding,
109 // and so no keys will be considered decoded.
111 // In this sense, the Undecoded keys correspond to keys in the TOML document
112 // that do not have a concrete type in your representation.
113 func (md *MetaData) Undecoded() []Key {
114 undecoded := make([]Key, 0, len(md.keys))
115 for _, key := range md.keys {
116 if !md.decoded[key.String()] {
117 undecoded = append(undecoded, key)