From 1909c29ade5ab6320593ce3eefc3d14281baf40b Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Wed, 15 Mar 2023 17:28:06 +0000 Subject: [PATCH] decoder: Implement reference targets for LiteralType --- decoder/expr_literal_type.go | 8 -- decoder/expr_literal_type_ref_targets.go | 114 +++++++++++++++++++++++ 2 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 decoder/expr_literal_type_ref_targets.go diff --git a/decoder/expr_literal_type.go b/decoder/expr_literal_type.go index 829f91b0..57279383 100644 --- a/decoder/expr_literal_type.go +++ b/decoder/expr_literal_type.go @@ -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" ) @@ -14,8 +11,3 @@ type LiteralType struct { pathCtx *PathContext } - -func (lt LiteralType) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) reference.Targets { - // TODO - return nil -} diff --git a/decoder/expr_literal_type_ref_targets.go b/decoder/expr_literal_type_ref_targets.go new file mode 100644 index 00000000..6934a593 --- /dev/null +++ b/decoder/expr_literal_type_ref_targets.go @@ -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{} +}