Skip to content

Commit

Permalink
Make plan.Section() simpler - no reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
flotter committed Aug 20, 2024
1 parent e075f17 commit 662fd6a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
17 changes: 4 additions & 13 deletions internals/plan/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,15 @@ 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)
}

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)
}
Expand Down Expand Up @@ -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 {
Expand Down
29 changes: 5 additions & 24 deletions internals/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 662fd6a

Please sign in to comment.