From 58f54e6a6babf2694fd330e651d796d45b84b73f Mon Sep 17 00:00:00 2001 From: Fred Lotter Date: Mon, 19 Aug 2024 23:44:10 +0200 Subject: [PATCH] Make plan.Section() simpler - no reflection --- internals/plan/extensions_test.go | 17 ++++------------- internals/plan/plan.go | 29 +++++------------------------ 2 files changed, 9 insertions(+), 37 deletions(-) diff --git a/internals/plan/extensions_test.go b/internals/plan/extensions_test.go index e2ae1482..8a4f04b2 100644 --- a/internals/plan/extensions_test.go +++ b/internals/plan/extensions_test.go @@ -381,7 +381,7 @@ func (s *S) TestPlanExtensions(c *C) { if _, ok := planTest.extensions[xField]; ok { // Verify "x-field" data. var x *xSection - err = p.Section(xField, &x) + x = p.Section(xField).(*xSection) c.Assert(err, IsNil) c.Assert(x.Entries, DeepEquals, planTest.combinedPlanResult.x.Entries) } @@ -389,7 +389,7 @@ func (s *S) TestPlanExtensions(c *C) { if _, ok := planTest.extensions[yField]; ok { // Verify "y-field" data. var y *ySection - err = p.Section(yField, &y) + y = p.Section(yField).(*ySection) c.Assert(err, IsNil) c.Assert(y.Entries, DeepEquals, planTest.combinedPlanResult.y.Entries) } @@ -451,19 +451,10 @@ func (x xExtension) CombineSections(sections ...plan.LayerSection) (plan.LayerSe func (x xExtension) ValidatePlan(p *plan.Plan) error { var xs *xSection - err := p.Section(xField, &xs) - if err != nil { - return err - } + xs = p.Section(xField).(*xSection) if xs != nil { var ys *ySection - err = p.Section(yField, &ys) - if err != nil { - return err - } - if ys == nil { - return fmt.Errorf("cannot validate %v field without %v field", xField, yField) - } + ys = p.Section(yField).(*ySection) // Test dependency: Make sure every Y field in X refer to an existing Y entry. for xEntryField, xEntryValue := range xs.Entries { diff --git a/internals/plan/plan.go b/internals/plan/plan.go index e35d88ea..fae05339 100644 --- a/internals/plan/plan.go +++ b/internals/plan/plan.go @@ -95,30 +95,11 @@ type Plan struct { Sections map[string]LayerSection `yaml:",inline"` } -// Section retrieves a section from the plan. -func (p *Plan) Section(field string, out interface{}) error { - if _, found := layerExtensions[field]; !found { - return fmt.Errorf("cannot find registered extension for field %q", field) - } - - outVal := reflect.ValueOf(out) - if outVal.Kind() != reflect.Ptr || outVal.IsNil() { - return fmt.Errorf("cannot read non pointer to section type %q", outVal.Kind()) - } - - section, exists := p.Sections[field] - if !exists { - return fmt.Errorf("internal error: section %q is nil", field) - } - - sectionVal := reflect.ValueOf(section) - sectionType := sectionVal.Type() - outValPtrType := outVal.Elem().Type() - if !sectionType.AssignableTo(outValPtrType) { - return fmt.Errorf("cannot assign value of type %s to out argument of type %s", sectionType, outValPtrType) - } - outVal.Elem().Set(sectionVal) - return nil +// Section retrieves a section from the plan. If Section is called +// before the plan is loaded, or with an unregistered field, this method +// will return nil. +func (p *Plan) Section(field string) LayerSection { + return p.Sections[field] } // MarshalYAML implements an override for top level omitempty tags handling.