gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/BurntSushi/toml/decode_meta.go (about)

     1  package toml
     2  
     3  import "strings"
     4  
     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.
     8  type MetaData struct {
     9  	mapping map[string]interface{}
    10  	types   map[string]tomlType
    11  	keys    []Key
    12  	decoded map[string]bool
    13  	context Key // Used only during decoding.
    14  }
    15  
    16  // IsDefined returns true if the key given exists in the TOML data. The key
    17  // should be specified hierarchially. e.g.,
    18  //
    19  //	// access the TOML key 'a.b.c'
    20  //	IsDefined("a", "b", "c")
    21  //
    22  // IsDefined will return false if an empty key given. Keys are case sensitive.
    23  func (md *MetaData) IsDefined(key ...string) bool {
    24  	if len(key) == 0 {
    25  		return false
    26  	}
    27  
    28  	var hash map[string]interface{}
    29  	var ok bool
    30  	var hashOrVal interface{} = md.mapping
    31  	for _, k := range key {
    32  		if hash, ok = hashOrVal.(map[string]interface{}); !ok {
    33  			return false
    34  		}
    35  		if hashOrVal, ok = hash[k]; !ok {
    36  			return false
    37  		}
    38  	}
    39  	return true
    40  }
    41  
    42  // Type returns a string representation of the type of the key specified.
    43  //
    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()
    50  	}
    51  	return ""
    52  }
    53  
    54  // Key is the type of any TOML key, including key groups. Use (MetaData).Keys
    55  // to get values of this type.
    56  type Key []string
    57  
    58  func (k Key) String() string {
    59  	return strings.Join(k, ".")
    60  }
    61  
    62  func (k Key) maybeQuotedAll() string {
    63  	var ss []string
    64  	for i := range k {
    65  		ss = append(ss, k.maybeQuoted(i))
    66  	}
    67  	return strings.Join(ss, ".")
    68  }
    69  
    70  func (k Key) maybeQuoted(i int) string {
    71  	quote := false
    72  	for _, c := range k[i] {
    73  		if !isBareKeyChar(c) {
    74  			quote = true
    75  			break
    76  		}
    77  	}
    78  	if quote {
    79  		return "\"" + strings.Replace(k[i], "\"", "\\\"", -1) + "\""
    80  	}
    81  	return k[i]
    82  }
    83  
    84  func (k Key) add(piece string) Key {
    85  	newKey := make(Key, len(k)+1)
    86  	copy(newKey, k)
    87  	newKey[len(k)] = piece
    88  	return newKey
    89  }
    90  
    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.
    94  //
    95  // The list will have the same order as the keys appeared in the TOML data.
    96  //
    97  // All keys returned are non-empty.
    98  func (md *MetaData) Keys() []Key {
    99  	return md.keys
   100  }
   101  
   102  // Undecoded returns all keys that have not been decoded in the order in which
   103  // they appear in the original TOML document.
   104  //
   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.
   107  //
   108  // Also note that decoding into an empty interface will result in no decoding,
   109  // and so no keys will be considered decoded.
   110  //
   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)
   118  		}
   119  	}
   120  	return undecoded
   121  }