Skip to content

Commit

Permalink
decoder: Implement reference origins for Object
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 10, 2023
1 parent ba575c4 commit 1f9dfc1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
5 changes: 0 additions & 5 deletions decoder/expr_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ type Object struct {
pathCtx *PathContext
}

func (obj Object) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
// TODO
return nil
}

func (obj Object) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) reference.Targets {
// TODO
return nil
Expand Down
42 changes: 42 additions & 0 deletions decoder/expr_object_ref_origins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package decoder

import (
"context"

"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl/v2/hclsyntax"
)

func (obj Object) ReferenceOrigins(ctx context.Context, allowSelfRefs bool) reference.Origins {
eType, ok := obj.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return reference.Origins{}
}

if len(eType.Items) == 0 || len(obj.cons.Attributes) == 0 {
return reference.Origins{}
}

origins := make(reference.Origins, 0)

for _, item := range eType.Items {
attrName, _, ok := rawObjectKey(item.KeyExpr)
if !ok {
continue
}

aSchema, ok := obj.cons.Attributes[attrName]
if !ok {
// skip unknown attribute
continue
}

expr := newExpression(obj.pathCtx, item.ValueExpr, aSchema.Constraint)

if elemExpr, ok := expr.(ReferenceOriginsExpression); ok {
origins = append(origins, elemExpr.ReferenceOrigins(ctx, allowSelfRefs)...)
}
}

return origins
}

0 comments on commit 1f9dfc1

Please sign in to comment.