2021-06-07 11:20:34 +00:00
|
|
|
package toml
|
|
|
|
|
2022-02-28 12:32:27 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
2021-06-07 11:20:34 +00:00
|
|
|
|
2022-02-28 12:32:27 +00:00
|
|
|
// MetaData allows access to meta information about TOML data that's not
|
|
|
|
// accessible otherwise.
|
|
|
|
//
|
|
|
|
// It allows checking if a key is defined in the TOML data, whether any keys
|
|
|
|
// were undecoded, and the TOML type of a key.
|
2021-06-07 11:20:34 +00:00
|
|
|
type MetaData struct {
|
2022-02-28 12:32:27 +00:00
|
|
|
context Key // Used only during decoding.
|
|
|
|
|
2023-04-25 10:28:07 +00:00
|
|
|
keyInfo map[string]keyInfo
|
2021-06-07 11:20:34 +00:00
|
|
|
mapping map[string]interface{}
|
|
|
|
keys []Key
|
2022-02-28 12:32:27 +00:00
|
|
|
decoded map[string]struct{}
|
2023-04-25 10:28:07 +00:00
|
|
|
data []byte // Input file; for errors.
|
2021-06-07 11:20:34 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 12:32:27 +00:00
|
|
|
// IsDefined reports if the key exists in the TOML data.
|
2021-06-07 11:20:34 +00:00
|
|
|
//
|
2022-02-28 12:32:27 +00:00
|
|
|
// The key should be specified hierarchically, for example to access the TOML
|
|
|
|
// key "a.b.c" you would use IsDefined("a", "b", "c"). Keys are case sensitive.
|
2021-06-07 11:20:34 +00:00
|
|
|
//
|
2022-02-28 12:32:27 +00:00
|
|
|
// Returns false for an empty key.
|
2021-06-07 11:20:34 +00:00
|
|
|
func (md *MetaData) IsDefined(key ...string) bool {
|
|
|
|
if len(key) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:32:27 +00:00
|
|
|
var (
|
|
|
|
hash map[string]interface{}
|
|
|
|
ok bool
|
|
|
|
hashOrVal interface{} = md.mapping
|
|
|
|
)
|
2021-06-07 11:20:34 +00:00
|
|
|
for _, k := range key {
|
|
|
|
if hash, ok = hashOrVal.(map[string]interface{}); !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if hashOrVal, ok = hash[k]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns a string representation of the type of the key specified.
|
|
|
|
//
|
2022-02-28 12:32:27 +00:00
|
|
|
// Type will return the empty string if given an empty key or a key that does
|
|
|
|
// not exist. Keys are case sensitive.
|
2021-06-07 11:20:34 +00:00
|
|
|
func (md *MetaData) Type(key ...string) string {
|
2023-04-25 10:28:07 +00:00
|
|
|
if ki, ok := md.keyInfo[Key(key).String()]; ok {
|
|
|
|
return ki.tomlType.typeString()
|
2021-06-07 11:20:34 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keys returns a slice of every key in the TOML data, including key groups.
|
|
|
|
//
|
2022-02-28 12:32:27 +00:00
|
|
|
// Each key is itself a slice, where the first element is the top of the
|
|
|
|
// hierarchy and the last is the most specific. The list will have the same
|
|
|
|
// order as the keys appeared in the TOML data.
|
2021-06-07 11:20:34 +00:00
|
|
|
//
|
|
|
|
// All keys returned are non-empty.
|
|
|
|
func (md *MetaData) Keys() []Key {
|
|
|
|
return md.keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Undecoded returns all keys that have not been decoded in the order in which
|
|
|
|
// they appear in the original TOML document.
|
|
|
|
//
|
2023-04-25 10:28:07 +00:00
|
|
|
// This includes keys that haven't been decoded because of a [Primitive] value.
|
2021-06-07 11:20:34 +00:00
|
|
|
// Once the Primitive value is decoded, the keys will be considered decoded.
|
|
|
|
//
|
|
|
|
// Also note that decoding into an empty interface will result in no decoding,
|
|
|
|
// and so no keys will be considered decoded.
|
|
|
|
//
|
|
|
|
// In this sense, the Undecoded keys correspond to keys in the TOML document
|
|
|
|
// that do not have a concrete type in your representation.
|
|
|
|
func (md *MetaData) Undecoded() []Key {
|
|
|
|
undecoded := make([]Key, 0, len(md.keys))
|
|
|
|
for _, key := range md.keys {
|
2022-02-28 12:32:27 +00:00
|
|
|
if _, ok := md.decoded[key.String()]; !ok {
|
2021-06-07 11:20:34 +00:00
|
|
|
undecoded = append(undecoded, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undecoded
|
|
|
|
}
|
2022-02-28 12:32:27 +00:00
|
|
|
|
2023-04-25 10:28:07 +00:00
|
|
|
// Key represents any TOML key, including key groups. Use [MetaData.Keys] to get
|
2022-02-28 12:32:27 +00:00
|
|
|
// values of this type.
|
|
|
|
type Key []string
|
|
|
|
|
|
|
|
func (k Key) String() string {
|
|
|
|
ss := make([]string, len(k))
|
|
|
|
for i := range k {
|
|
|
|
ss[i] = k.maybeQuoted(i)
|
|
|
|
}
|
|
|
|
return strings.Join(ss, ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k Key) maybeQuoted(i int) string {
|
|
|
|
if k[i] == "" {
|
|
|
|
return `""`
|
|
|
|
}
|
|
|
|
for _, c := range k[i] {
|
|
|
|
if !isBareKeyChar(c) {
|
|
|
|
return `"` + dblQuotedReplacer.Replace(k[i]) + `"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return k[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k Key) add(piece string) Key {
|
|
|
|
newKey := make(Key, len(k)+1)
|
|
|
|
copy(newKey, k)
|
|
|
|
newKey[len(k)] = piece
|
|
|
|
return newKey
|
|
|
|
}
|