Skip to content

Commit

Permalink
refactoring: Label -> ParsedLabel
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed May 19, 2020
1 parent a397452 commit 963fe38
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion internal/terraform/lang/config_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func TestCompletableBlock_CompletionCandidatesAtPos(t *testing.T) {

cb := &completableBlock{
logger: testLogger(),
block: ParseBlock(block, []*Label{}, tc.sb),
block: ParseBlock(block, []*ParsedLabel{}, tc.sb),
}

list, err := cb.completionCandidatesAtPos(tc.pos)
Expand Down
4 changes: 2 additions & 2 deletions internal/terraform/lang/datasource_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type datasourceBlock struct {
logger *log.Logger

labelSchema LabelSchema
labels []*Label
labels []*ParsedLabel
hclBlock *hclsyntax.Block
sr schema.Reader
}
Expand All @@ -64,7 +64,7 @@ func (r *datasourceBlock) Name() string {
return fmt.Sprintf("%s.%s", firstLabel, secondLabel)
}

func (r *datasourceBlock) Labels() []*Label {
func (r *datasourceBlock) Labels() []*ParsedLabel {
if r.labels != nil {
return r.labels
}
Expand Down
4 changes: 2 additions & 2 deletions internal/terraform/lang/hcl_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

type parsedBlock struct {
hclBlock *hclsyntax.Block
labels []*Label
labels []*ParsedLabel
AttributesMap map[string]*Attribute
BlockTypesMap map[string]*BlockType

Expand Down Expand Up @@ -59,7 +59,7 @@ func (b *parsedBlock) PosInLabels(pos hcl.Pos) bool {
return false
}

func (b *parsedBlock) LabelAtPos(pos hcl.Pos) (*Label, bool) {
func (b *parsedBlock) LabelAtPos(pos hcl.Pos) (*ParsedLabel, bool) {
for i, rng := range b.hclBlock.LabelRanges {
if rng.ContainsPos(pos) {
// TODO: Guard against crashes when user sets label where we don't expect it
Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/lang/hcl_block_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (bt BlockTypes) AddBlock(name string, block *hclsyntax.Block, typeSchema *t

if block != nil {
// SDK doesn't support named blocks yet, so we expect no labels here for now
labels := make([]*Label, 0)
labels := make([]*ParsedLabel, 0)
bt[name].BlockList = append(bt[name].BlockList, ParseBlock(block, labels, typeSchema.Block))
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/lang/hcl_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// ParseBlock parses HCL configuration based on tfjson's SchemaBlock
// and keeps hold of all tfjson schema details on block or attribute level
func ParseBlock(block *hclsyntax.Block, labels []*Label, schema *tfjson.SchemaBlock) Block {
func ParseBlock(block *hclsyntax.Block, labels []*ParsedLabel, schema *tfjson.SchemaBlock) Block {
b := &parsedBlock{
hclBlock: block,
labels: labels,
Expand Down
8 changes: 4 additions & 4 deletions internal/terraform/lang/hcl_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func TestParseBlock_attributesAndBlockTypes(t *testing.T) {
t.Fatal(err)
}

b := ParseBlock(block, []*Label{}, tc.schema)
b := ParseBlock(block, []*ParsedLabel{}, tc.schema)

if diff := cmp.Diff(tc.expectedAttributes, b.Attributes(), opts...); diff != "" {
t.Fatalf("Attributes don't match.\n%s", diff)
Expand Down Expand Up @@ -471,7 +471,7 @@ func TestBlock_BlockAtPos(t *testing.T) {
t.Fatal(err)
}

b := ParseBlock(block, []*Label{}, schema)
b := ParseBlock(block, []*ParsedLabel{}, schema)
fBlock, _ := b.BlockAtPos(tc.pos)
if diff := cmp.Diff(tc.expectedBlock, fBlock, opts...); diff != "" {
t.Fatalf("Block doesn't match.\n%s", diff)
Expand Down Expand Up @@ -631,7 +631,7 @@ func TestBlock_PosInBody(t *testing.T) {
t.Fatal(err)
}

b := ParseBlock(block, []*Label{}, schema)
b := ParseBlock(block, []*ParsedLabel{}, schema)
isInBody := b.PosInBody(tc.pos)
if tc.expected != isInBody {
if tc.expected {
Expand Down Expand Up @@ -766,7 +766,7 @@ func TestBlock_PosInAttributes(t *testing.T) {
t.Fatal(err)
}

b := ParseBlock(block, []*Label{}, schema)
b := ParseBlock(block, []*ParsedLabel{}, schema)
isInAttribute := b.PosInAttribute(tc.pos)
if tc.expected != isInAttribute {
if tc.expected {
Expand Down
6 changes: 3 additions & 3 deletions internal/terraform/lang/labels.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package lang

func parseLabels(blockType string, schema LabelSchema, parsed []string) []*Label {
labels := make([]*Label, len(schema))
func parseLabels(blockType string, schema LabelSchema, parsed []string) []*ParsedLabel {
labels := make([]*ParsedLabel, len(schema))

for i, labelName := range schema {
var value string
if len(parsed)-1 >= i {
value = parsed[i]
}
labels[i] = &Label{
labels[i] = &ParsedLabel{
Name: labelName,
Value: value,
}
Expand Down
4 changes: 2 additions & 2 deletions internal/terraform/lang/provider_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type providerBlock struct {
logger *log.Logger

labelSchema LabelSchema
labels []*Label
labels []*ParsedLabel
hclBlock *hclsyntax.Block
sr schema.Reader
}
Expand All @@ -54,7 +54,7 @@ func (p *providerBlock) RawName() string {
return p.Labels()[0].Value
}

func (p *providerBlock) Labels() []*Label {
func (p *providerBlock) Labels() []*ParsedLabel {
if p.labels != nil {
return p.labels
}
Expand Down
4 changes: 2 additions & 2 deletions internal/terraform/lang/resource_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type resourceBlock struct {
logger *log.Logger

labelSchema LabelSchema
labels []*Label
labels []*ParsedLabel
hclBlock *hclsyntax.Block
sr schema.Reader
}
Expand All @@ -64,7 +64,7 @@ func (r *resourceBlock) Name() string {
return fmt.Sprintf("%s.%s", firstLabel, secondLabel)
}

func (r *resourceBlock) Labels() []*Label {
func (r *resourceBlock) Labels() []*ParsedLabel {
if r.labels != nil {
return r.labels
}
Expand Down
6 changes: 3 additions & 3 deletions internal/terraform/lang/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ type ConfigBlock interface {
CompletionCandidatesAtPos(pos hcl.Pos) (CompletionCandidates, error)
Name() string
BlockType() string
Labels() []*Label
Labels() []*ParsedLabel
}

// Block represents a decoded HCL block (by a Parser)
// which keeps track of the related schema
type Block interface {
BlockAtPos(pos hcl.Pos) (Block, bool)
LabelAtPos(pos hcl.Pos) (*Label, bool)
LabelAtPos(pos hcl.Pos) (*ParsedLabel, bool)
Range() hcl.Range
PosInLabels(pos hcl.Pos) bool
PosInBody(pos hcl.Pos) bool
Expand All @@ -40,7 +40,7 @@ type Block interface {

type LabelSchema []string

type Label struct {
type ParsedLabel struct {
Name string
Value string
}
Expand Down

0 comments on commit 963fe38

Please sign in to comment.