Skip to content

Commit

Permalink
decoder/schema: Update legacy tests for LiteralType
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 16, 2023
1 parent ead2994 commit 6990693
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 219 deletions.
37 changes: 22 additions & 15 deletions decoder/attribute_candidates_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"list of objects",
"mylistobj",
&schema.AttributeSchema{
Constraint: schema.LiteralType{Type: cty.List(cty.Object(map[string]cty.Type{
"first": cty.String,
"second": cty.Number,
}))},
Constraint: schema.LiteralType{
Type: cty.List(cty.Object(map[string]cty.Type{
"first": cty.String,
"second": cty.Number,
})),
},
},
lang.CompleteCandidates([]lang.Candidate{
{
Expand Down Expand Up @@ -172,11 +174,13 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"object",
"myobj",
&schema.AttributeSchema{
Constraint: schema.LiteralType{Type: cty.Object(map[string]cty.Type{
"keystr": cty.String,
"keynum": cty.Number,
"keybool": cty.Bool,
})},
Constraint: schema.LiteralType{
Type: cty.Object(map[string]cty.Type{
"keystr": cty.String,
"keynum": cty.Number,
"keybool": cty.Bool,
}),
},
},
lang.CompleteCandidates([]lang.Candidate{
{
Expand Down Expand Up @@ -226,13 +230,15 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"nested object",
"myobj",
&schema.AttributeSchema{
Constraint: schema.LiteralType{Type: cty.Object(map[string]cty.Type{
"keystr": cty.String,
"another": cty.Object(map[string]cty.Type{
"nestedstr": cty.String,
"nested_number": cty.Number,
Constraint: schema.LiteralType{
Type: cty.Object(map[string]cty.Type{
"keystr": cty.String,
"another": cty.Object(map[string]cty.Type{
"nestedstr": cty.String,
"nested_number": cty.Number,
}),
}),
})},
},
},
lang.CompleteCandidates([]lang.Candidate{
{
Expand Down Expand Up @@ -272,6 +278,7 @@ func TestLegacyDecoder_CompletionAtPos_EmptyCompletionData(t *testing.T) {
"test.tf": f,
},
})
d.PrefillRequiredFields = true

ctx := context.Background()
candidates, err := d.CandidatesAtPos(ctx, "test.tf", hcl.InitialPos)
Expand Down
34 changes: 16 additions & 18 deletions decoder/expr_list_ref_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package decoder
import (
"context"

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

func (list List) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) reference.Targets {
Expand All @@ -24,22 +22,22 @@ func (list List) ReferenceTargets(ctx context.Context, targetCtx *TargetContext)
// TODO: collect parent target for the whole list
// See https://github.com/hashicorp/hcl-lang/issues/228

for i, elemExpr := range eType.Exprs {
expr := newExpression(list.pathCtx, elemExpr, list.cons.Elem)
if e, ok := expr.(ReferenceTargetsExpression); ok {
elemCtx := targetCtx.Copy()
elemCtx.ParentAddress = append(elemCtx.ParentAddress, lang.IndexStep{
Key: cty.NumberIntVal(int64(i)),
})
if elemCtx.ParentLocalAddress != nil {
elemCtx.ParentLocalAddress = append(elemCtx.ParentLocalAddress, lang.IndexStep{
Key: cty.NumberIntVal(int64(i)),
})
}

targets = append(targets, e.ReferenceTargets(ctx, elemCtx)...)
}
}
// for i, elemExpr := range eType.Exprs {
// expr := newExpression(list.pathCtx, elemExpr, list.cons.Elem)
// if e, ok := expr.(ReferenceTargetsExpression); ok {
// elemCtx := targetCtx.Copy()
// elemCtx.ParentAddress = append(elemCtx.ParentAddress, lang.IndexStep{
// Key: cty.NumberIntVal(int64(i)),
// })
// if elemCtx.ParentLocalAddress != nil {
// elemCtx.ParentLocalAddress = append(elemCtx.ParentLocalAddress, lang.IndexStep{
// Key: cty.NumberIntVal(int64(i)),
// })
// }

// targets = append(targets, e.ReferenceTargets(ctx, elemCtx)...)
// }
// }

return targets
}
40 changes: 19 additions & 21 deletions decoder/expr_tuple_ref_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package decoder
import (
"context"

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

func (tuple Tuple) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) reference.Targets {
Expand All @@ -24,25 +22,25 @@ func (tuple Tuple) ReferenceTargets(ctx context.Context, targetCtx *TargetContex
// TODO: collect parent target for the whole tuple
// See https://github.com/hashicorp/hcl-lang/issues/228

for i, elemExpr := range eType.Exprs {
if i+1 > len(tuple.cons.Elems) {
break
}

expr := newExpression(tuple.pathCtx, elemExpr, tuple.cons.Elems[i])
if e, ok := expr.(ReferenceTargetsExpression); ok {
elemCtx := targetCtx.Copy()
elemCtx.ParentAddress = append(elemCtx.ParentAddress, lang.IndexStep{
Key: cty.NumberIntVal(int64(i)),
})
if elemCtx.ParentLocalAddress != nil {
elemCtx.ParentLocalAddress = append(elemCtx.ParentLocalAddress, lang.IndexStep{
Key: cty.NumberIntVal(int64(i)),
})
}
targets = append(targets, e.ReferenceTargets(ctx, elemCtx)...)
}
}
// for i, elemExpr := range eType.Exprs {
// if i+1 > len(tuple.cons.Elems) {
// break
// }

// expr := newExpression(tuple.pathCtx, elemExpr, tuple.cons.Elems[i])
// if e, ok := expr.(ReferenceTargetsExpression); ok {
// elemCtx := targetCtx.Copy()
// elemCtx.ParentAddress = append(elemCtx.ParentAddress, lang.IndexStep{
// Key: cty.NumberIntVal(int64(i)),
// })
// if elemCtx.ParentLocalAddress != nil {
// elemCtx.ParentLocalAddress = append(elemCtx.ParentLocalAddress, lang.IndexStep{
// Key: cty.NumberIntVal(int64(i)),
// })
// }
// targets = append(targets, e.ReferenceTargets(ctx, elemCtx)...)
// }
// }

return targets
}
Loading

0 comments on commit 6990693

Please sign in to comment.