Exemple #1
0
// SubKeys returns the sub-keys for the given prefix.
//
// Given:
// ------
// db.us.host www.host.com
// db.us.port 1234
// db.gb.host www.host.com
// db.gb.port 1234
//
// Then:
// -----
// SubKeys("db") => ["us", "gb"]
// SubKeys("db", "us") => ["host", "port"]
// SubKeys("db", "us", "port") => []
//
func (c *Config) SubKeys(stems ...string) []string {
	result := make([]string, 0, 1)
	prefix := join(stems...) + "."
	added := set.NewS()

	for _, e := range c.entries {

		// Found Prefix
		if strings.HasPrefix(e.key, prefix) {

			// Remove Prefix and Split Suffix
			suffix := strings.TrimPrefix(e.key, prefix)
			pieces := strings.Split(suffix, ".")

			// Add Sub-Key
			if len(pieces) > 0 {
				subKey := pieces[0]

				if !added.Contains(subKey) {
					result = append(result, subKey)
					added.Insert(subKey)
				}
			}
		}
	}

	return result
}
Exemple #2
0
// New returns a new Config object constructed using the supplied filename
func New(filename string) *Config {
	c := &Config{
		defines:  make(map[string]string),
		includes: set.NewS(),
	}

	c.fromFile(filename)
	return c
}