Skip to content

Commit

Permalink
decoder: Implement reference targets for LiteralType
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 20, 2023
1 parent 7bf62de commit 1909c29
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 8 deletions.
8 changes: 0 additions & 8 deletions decoder/expr_literal_type.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package decoder

import (
"context"

"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
)
Expand All @@ -14,8 +11,3 @@ type LiteralType struct {

pathCtx *PathContext
}

func (lt LiteralType) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) reference.Targets {
// TODO
return nil
}
114 changes: 114 additions & 0 deletions decoder/expr_literal_type_ref_targets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package decoder

import (
"context"

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

func (lt LiteralType) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) reference.Targets {
typ := lt.cons.Type

// Primitive types are collected separately on attribute level
if typ.IsPrimitiveType() {
return reference.Targets{}
}

if typ.IsListType() {
expr, ok := lt.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return nil
}

list := List{
cons: schema.List{
Elem: schema.LiteralType{
Type: typ.ElementType(),
},
},
expr: expr,
pathCtx: lt.pathCtx,
}
return list.ReferenceTargets(ctx, targetCtx)
}

if typ.IsSetType() {
expr, ok := lt.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return nil
}

set := Set{
cons: schema.Set{
Elem: schema.LiteralType{
Type: typ.ElementType(),
},
},
expr: expr,
pathCtx: lt.pathCtx,
}
return set.ReferenceTargets(ctx, targetCtx)
}

if typ.IsTupleType() {
expr, ok := lt.expr.(*hclsyntax.TupleConsExpr)
if !ok {
return nil
}

elemTypes := typ.TupleElementTypes()
cons := schema.Tuple{
Elems: make([]schema.Constraint, len(elemTypes)),
}
for i, elemType := range elemTypes {
cons.Elems[i] = schema.LiteralType{
Type: elemType,
}
}
tuple := Tuple{
cons: cons,
expr: expr,
pathCtx: lt.pathCtx,
}

return tuple.ReferenceTargets(ctx, targetCtx)
}

if typ.IsMapType() {
expr, ok := lt.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return nil
}

m := Map{
cons: schema.Map{
Elem: schema.LiteralType{
Type: typ.ElementType(),
},
},
expr: expr,
pathCtx: lt.pathCtx,
}
return m.ReferenceTargets(ctx, targetCtx)
}

if typ.IsObjectType() {
expr, ok := lt.expr.(*hclsyntax.ObjectConsExpr)
if !ok {
return nil
}

obj := Object{
cons: schema.Object{
Attributes: ctyObjectToObjectAttributes(typ),
},
expr: expr,
pathCtx: lt.pathCtx,
}
return obj.ReferenceTargets(ctx, targetCtx)
}

return reference.Targets{}
}

0 comments on commit 1909c29

Please sign in to comment.