podman
121 строка · 3.1 Кб
1package toml
2
3import (
4"strings"
5)
6
7// MetaData allows access to meta information about TOML data that's not
8// accessible otherwise.
9//
10// It allows checking if a key is defined in the TOML data, whether any keys
11// were undecoded, and the TOML type of a key.
12type MetaData struct {
13context Key // Used only during decoding.
14
15keyInfo map[string]keyInfo
16mapping map[string]interface{}
17keys []Key
18decoded map[string]struct{}
19data []byte // Input file; for errors.
20}
21
22// IsDefined reports if the key exists in the TOML data.
23//
24// The key should be specified hierarchically, for example to access the TOML
25// key "a.b.c" you would use IsDefined("a", "b", "c"). Keys are case sensitive.
26//
27// Returns false for an empty key.
28func (md *MetaData) IsDefined(key ...string) bool {
29if len(key) == 0 {
30return false
31}
32
33var (
34hash map[string]interface{}
35ok bool
36hashOrVal interface{} = md.mapping
37)
38for _, k := range key {
39if hash, ok = hashOrVal.(map[string]interface{}); !ok {
40return false
41}
42if hashOrVal, ok = hash[k]; !ok {
43return false
44}
45}
46return true
47}
48
49// Type returns a string representation of the type of the key specified.
50//
51// Type will return the empty string if given an empty key or a key that does
52// not exist. Keys are case sensitive.
53func (md *MetaData) Type(key ...string) string {
54if ki, ok := md.keyInfo[Key(key).String()]; ok {
55return ki.tomlType.typeString()
56}
57return ""
58}
59
60// Keys returns a slice of every key in the TOML data, including key groups.
61//
62// Each key is itself a slice, where the first element is the top of the
63// hierarchy and the last is the most specific. The list will have the same
64// order as the keys appeared in the TOML data.
65//
66// All keys returned are non-empty.
67func (md *MetaData) Keys() []Key {
68return md.keys
69}
70
71// Undecoded returns all keys that have not been decoded in the order in which
72// they appear in the original TOML document.
73//
74// This includes keys that haven't been decoded because of a [Primitive] value.
75// Once the Primitive value is decoded, the keys will be considered decoded.
76//
77// Also note that decoding into an empty interface will result in no decoding,
78// and so no keys will be considered decoded.
79//
80// In this sense, the Undecoded keys correspond to keys in the TOML document
81// that do not have a concrete type in your representation.
82func (md *MetaData) Undecoded() []Key {
83undecoded := make([]Key, 0, len(md.keys))
84for _, key := range md.keys {
85if _, ok := md.decoded[key.String()]; !ok {
86undecoded = append(undecoded, key)
87}
88}
89return undecoded
90}
91
92// Key represents any TOML key, including key groups. Use [MetaData.Keys] to get
93// values of this type.
94type Key []string
95
96func (k Key) String() string {
97ss := make([]string, len(k))
98for i := range k {
99ss[i] = k.maybeQuoted(i)
100}
101return strings.Join(ss, ".")
102}
103
104func (k Key) maybeQuoted(i int) string {
105if k[i] == "" {
106return `""`
107}
108for _, c := range k[i] {
109if !isBareKeyChar(c, false) {
110return `"` + dblQuotedReplacer.Replace(k[i]) + `"`
111}
112}
113return k[i]
114}
115
116func (k Key) add(piece string) Key {
117newKey := make(Key, len(k)+1)
118copy(newKey, k)
119newKey[len(k)] = piece
120return newKey
121}
122