From 8ef9e52e0a15c875c4c4424b71ac4a69de8172b5 Mon Sep 17 00:00:00 2001 From: Aishwarya Thangappa Date: Wed, 1 Jun 2016 01:48:21 -0700 Subject: [PATCH] Retrieve parent keys accessible from a child section This PR lets us retrieve a list of parent keys that are available to the child section. --- key_test.go | 7 +++++++ section.go | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/key_test.go b/key_test.go index 6675c5a..331642c 100644 --- a/key_test.go +++ b/key_test.go @@ -59,6 +59,13 @@ func Test_Key(t *testing.T) { } }) + Convey("Get parent-keys that are available to the child section", func() { + parentKeys := cfg.Section("package.sub").ParentKeys() + for _, k := range parentKeys { + So(k.Name(), ShouldEqual, "CLONE_URL") + } + }) + Convey("Get overwrite value", func() { So(cfg.Section("author").Key("E-MAIL").String(), ShouldEqual, "u@gogs.io") }) diff --git a/section.go b/section.go index ed8cbdb..28f0080 100644 --- a/section.go +++ b/section.go @@ -139,6 +139,26 @@ func (s *Section) Keys() []*Key { return keys } +// ParentKeys returns list of keys of parent section. +func (s * Section) ParentKeys() []*Key { + var parentKeys []*Key + sname := s.name + for { + if i := strings.LastIndex(sname, "."); i > -1 { + sname = sname[:i] + sec, err := s.f.GetSection(sname) + if err != nil { + continue + } + parentKeys = append(parentKeys, sec.Keys()...) + } else { + break + } + + } + return parentKeys +} + // KeyStrings returns list of key names of section. func (s *Section) KeyStrings() []string { list := make([]string, len(s.keyList))