Skip to content

Commit

Permalink
Fix the bug involving multi-level reference resolution
Browse files Browse the repository at this point in the history
Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
  • Loading branch information
ulucinar committed Jun 1, 2022
1 parent 31b0a79 commit d55cf71
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/pipeline/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
type pavedWithManifest struct {
manifestPath string
paved *fieldpath.Paved
refsResolved bool
}

// ResourceExample represents the scraped example HCL configuration
Expand Down Expand Up @@ -99,7 +100,7 @@ func NewExampleGenerator(rootDir string, resourceMeta map[string]*Resource) *Exa
// their respective API groups.
func (eg *ExampleGenerator) StoreExamples() error {
for n, pm := range eg.resources {
if err := eg.resolveReferences(pm.paved.UnstructuredContent()); err != nil {
if err := eg.resolveReferencesOfPaved(pm); err != nil {
return errors.Wrapf(err, "cannot resolve references for resource: %s", n)
}
u := pm.paved.UnstructuredContent()
Expand Down Expand Up @@ -141,6 +142,14 @@ func commentOut(buff []byte) []byte {
return []byte(strings.Join(commentedOutLines, "\n"))
}

func (eg *ExampleGenerator) resolveReferencesOfPaved(pm *pavedWithManifest) error {
if pm.refsResolved {
return nil
}
pm.refsResolved = true
return errors.Wrap(eg.resolveReferences(pm.paved.UnstructuredContent()), "failed to resolve references of paved")
}

func (eg *ExampleGenerator) resolveReferences(params map[string]interface{}) error { // nolint:gocyclo
for k, v := range params {
switch t := v.(type) {
Expand Down Expand Up @@ -174,6 +183,9 @@ func (eg *ExampleGenerator) resolveReferences(params map[string]interface{}) err
if pm == nil || pm.paved == nil {
continue
}
if err := eg.resolveReferencesOfPaved(pm); err != nil {
return errors.Wrapf(err, "cannot recursively resolve references for %q", path[0])
}
pathStr := strings.Join(append([]string{"spec", "forProvider"}, path[2:]...), ".")
s, err := pm.paved.GetString(pathStr)
if fieldpath.IsNotFound(err) {
Expand Down

0 comments on commit d55cf71

Please sign in to comment.