Blame vendor/github.com/BurntSushi/toml/decode_meta.go

Packit 63bb0d
package toml
Packit 63bb0d
Packit 63bb0d
import "strings"
Packit 63bb0d
Packit 63bb0d
// MetaData allows access to meta information about TOML data that may not
Packit 63bb0d
// be inferrable via reflection. In particular, whether a key has been defined
Packit 63bb0d
// and the TOML type of a key.
Packit 63bb0d
type MetaData struct {
Packit 63bb0d
	mapping map[string]interface{}
Packit 63bb0d
	types   map[string]tomlType
Packit 63bb0d
	keys    []Key
Packit 63bb0d
	decoded map[string]bool
Packit 63bb0d
	context Key // Used only during decoding.
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// IsDefined returns true if the key given exists in the TOML data. The key
Packit 63bb0d
// should be specified hierarchially. e.g.,
Packit 63bb0d
//
Packit 63bb0d
//	// access the TOML key 'a.b.c'
Packit 63bb0d
//	IsDefined("a", "b", "c")
Packit 63bb0d
//
Packit 63bb0d
// IsDefined will return false if an empty key given. Keys are case sensitive.
Packit 63bb0d
func (md *MetaData) IsDefined(key ...string) bool {
Packit 63bb0d
	if len(key) == 0 {
Packit 63bb0d
		return false
Packit 63bb0d
	}
Packit 63bb0d
Packit 63bb0d
	var hash map[string]interface{}
Packit 63bb0d
	var ok bool
Packit 63bb0d
	var hashOrVal interface{} = md.mapping
Packit 63bb0d
	for _, k := range key {
Packit 63bb0d
		if hash, ok = hashOrVal.(map[string]interface{}); !ok {
Packit 63bb0d
			return false
Packit 63bb0d
		}
Packit 63bb0d
		if hashOrVal, ok = hash[k]; !ok {
Packit 63bb0d
			return false
Packit 63bb0d
		}
Packit 63bb0d
	}
Packit 63bb0d
	return true
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// Type returns a string representation of the type of the key specified.
Packit 63bb0d
//
Packit 63bb0d
// Type will return the empty string if given an empty key or a key that
Packit 63bb0d
// does not exist. Keys are case sensitive.
Packit 63bb0d
func (md *MetaData) Type(key ...string) string {
Packit 63bb0d
	fullkey := strings.Join(key, ".")
Packit 63bb0d
	if typ, ok := md.types[fullkey]; ok {
Packit 63bb0d
		return typ.typeString()
Packit 63bb0d
	}
Packit 63bb0d
	return ""
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// Key is the type of any TOML key, including key groups. Use (MetaData).Keys
Packit 63bb0d
// to get values of this type.
Packit 63bb0d
type Key []string
Packit 63bb0d
Packit 63bb0d
func (k Key) String() string {
Packit 63bb0d
	return strings.Join(k, ".")
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
func (k Key) maybeQuotedAll() string {
Packit 63bb0d
	var ss []string
Packit 63bb0d
	for i := range k {
Packit 63bb0d
		ss = append(ss, k.maybeQuoted(i))
Packit 63bb0d
	}
Packit 63bb0d
	return strings.Join(ss, ".")
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
func (k Key) maybeQuoted(i int) string {
Packit 63bb0d
	quote := false
Packit 63bb0d
	for _, c := range k[i] {
Packit 63bb0d
		if !isBareKeyChar(c) {
Packit 63bb0d
			quote = true
Packit 63bb0d
			break
Packit 63bb0d
		}
Packit 63bb0d
	}
Packit 63bb0d
	if quote {
Packit 63bb0d
		return "\"" + strings.Replace(k[i], "\"", "\\\"", -1) + "\""
Packit 63bb0d
	}
Packit 63bb0d
	return k[i]
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
func (k Key) add(piece string) Key {
Packit 63bb0d
	newKey := make(Key, len(k)+1)
Packit 63bb0d
	copy(newKey, k)
Packit 63bb0d
	newKey[len(k)] = piece
Packit 63bb0d
	return newKey
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// Keys returns a slice of every key in the TOML data, including key groups.
Packit 63bb0d
// Each key is itself a slice, where the first element is the top of the
Packit 63bb0d
// hierarchy and the last is the most specific.
Packit 63bb0d
//
Packit 63bb0d
// The list will have the same order as the keys appeared in the TOML data.
Packit 63bb0d
//
Packit 63bb0d
// All keys returned are non-empty.
Packit 63bb0d
func (md *MetaData) Keys() []Key {
Packit 63bb0d
	return md.keys
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// Undecoded returns all keys that have not been decoded in the order in which
Packit 63bb0d
// they appear in the original TOML document.
Packit 63bb0d
//
Packit 63bb0d
// This includes keys that haven't been decoded because of a Primitive value.
Packit 63bb0d
// Once the Primitive value is decoded, the keys will be considered decoded.
Packit 63bb0d
//
Packit 63bb0d
// Also note that decoding into an empty interface will result in no decoding,
Packit 63bb0d
// and so no keys will be considered decoded.
Packit 63bb0d
//
Packit 63bb0d
// In this sense, the Undecoded keys correspond to keys in the TOML document
Packit 63bb0d
// that do not have a concrete type in your representation.
Packit 63bb0d
func (md *MetaData) Undecoded() []Key {
Packit 63bb0d
	undecoded := make([]Key, 0, len(md.keys))
Packit 63bb0d
	for _, key := range md.keys {
Packit 63bb0d
		if !md.decoded[key.String()] {
Packit 63bb0d
			undecoded = append(undecoded, key)
Packit 63bb0d
		}
Packit 63bb0d
	}
Packit 63bb0d
	return undecoded
Packit 63bb0d
}