Skip to content

Commit 58f54e6

Browse files
committed
Make plan.Section() simpler - no reflection
1 parent e55b435 commit 58f54e6

File tree

2 files changed

+9
-37
lines changed

2 files changed

+9
-37
lines changed

internals/plan/extensions_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,15 @@ func (s *S) TestPlanExtensions(c *C) {
381381
if _, ok := planTest.extensions[xField]; ok {
382382
// Verify "x-field" data.
383383
var x *xSection
384-
err = p.Section(xField, &x)
384+
x = p.Section(xField).(*xSection)
385385
c.Assert(err, IsNil)
386386
c.Assert(x.Entries, DeepEquals, planTest.combinedPlanResult.x.Entries)
387387
}
388388

389389
if _, ok := planTest.extensions[yField]; ok {
390390
// Verify "y-field" data.
391391
var y *ySection
392-
err = p.Section(yField, &y)
392+
y = p.Section(yField).(*ySection)
393393
c.Assert(err, IsNil)
394394
c.Assert(y.Entries, DeepEquals, planTest.combinedPlanResult.y.Entries)
395395
}
@@ -451,19 +451,10 @@ func (x xExtension) CombineSections(sections ...plan.LayerSection) (plan.LayerSe
451451

452452
func (x xExtension) ValidatePlan(p *plan.Plan) error {
453453
var xs *xSection
454-
err := p.Section(xField, &xs)
455-
if err != nil {
456-
return err
457-
}
454+
xs = p.Section(xField).(*xSection)
458455
if xs != nil {
459456
var ys *ySection
460-
err = p.Section(yField, &ys)
461-
if err != nil {
462-
return err
463-
}
464-
if ys == nil {
465-
return fmt.Errorf("cannot validate %v field without %v field", xField, yField)
466-
}
457+
ys = p.Section(yField).(*ySection)
467458

468459
// Test dependency: Make sure every Y field in X refer to an existing Y entry.
469460
for xEntryField, xEntryValue := range xs.Entries {

internals/plan/plan.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,30 +95,11 @@ type Plan struct {
9595
Sections map[string]LayerSection `yaml:",inline"`
9696
}
9797

98-
// Section retrieves a section from the plan.
99-
func (p *Plan) Section(field string, out interface{}) error {
100-
if _, found := layerExtensions[field]; !found {
101-
return fmt.Errorf("cannot find registered extension for field %q", field)
102-
}
103-
104-
outVal := reflect.ValueOf(out)
105-
if outVal.Kind() != reflect.Ptr || outVal.IsNil() {
106-
return fmt.Errorf("cannot read non pointer to section type %q", outVal.Kind())
107-
}
108-
109-
section, exists := p.Sections[field]
110-
if !exists {
111-
return fmt.Errorf("internal error: section %q is nil", field)
112-
}
113-
114-
sectionVal := reflect.ValueOf(section)
115-
sectionType := sectionVal.Type()
116-
outValPtrType := outVal.Elem().Type()
117-
if !sectionType.AssignableTo(outValPtrType) {
118-
return fmt.Errorf("cannot assign value of type %s to out argument of type %s", sectionType, outValPtrType)
119-
}
120-
outVal.Elem().Set(sectionVal)
121-
return nil
98+
// Section retrieves a section from the plan. If Section is called
99+
// before the plan is loaded, or with an unregistered field, this method
100+
// will return nil.
101+
func (p *Plan) Section(field string) LayerSection {
102+
return p.Sections[field]
122103
}
123104

124105
// MarshalYAML implements an override for top level omitempty tags handling.

0 commit comments

Comments
 (0)