Skip to content

Commit

Permalink
handle type conversions when parsing attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
drlau committed Aug 10, 2020
1 parent 18747cc commit eeefcd9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 19 deletions.
59 changes: 46 additions & 13 deletions attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tfplanparse

import (
"fmt"
"strconv"
"strings"
)

Expand All @@ -14,8 +15,8 @@ const (

type AttributeChange struct {
Name string
OldValue string
NewValue string
OldValue interface{}
NewValue interface{}
UpdateType UpdateType
}

Expand Down Expand Up @@ -43,18 +44,21 @@ func NewAttributeChangeFromLine(line string) (*AttributeChange, error) {

return &AttributeChange{
Name: strings.TrimSpace(attribute[0]),
OldValue: "",
NewValue: strings.TrimSpace(dequote(attribute[1])),
OldValue: nil,
NewValue: doTypeConversion(attribute[1]),
UpdateType: NewResource,
}, nil
} else if strings.HasPrefix(line, "-") {
// destroy
attribute := strings.SplitN(removeChangeTypeCharacters(line), ATTRIBUTE_DEFINITON_DELIMITER, 2)
if len(attribute) == 1 {
// line does not have an "="
// assume delimited with a space
attribute = strings.Split(attribute[0], " ")
return &AttributeChange{
Name: strings.TrimSpace(attribute[0]),
OldValue: "",
NewValue: "",
Name: attribute[0],
OldValue: doTypeConversion(attribute[len(attribute)-1]),
NewValue: nil,
UpdateType: DestroyResource,
}, nil
}
Expand All @@ -63,16 +67,16 @@ func NewAttributeChangeFromLine(line string) (*AttributeChange, error) {
if len(values) != 2 {
return &AttributeChange{
Name: strings.TrimSpace(attribute[0]),
OldValue: strings.TrimSpace(attribute[1]),
NewValue: "",
OldValue: doTypeConversion(strings.TrimSpace(attribute[1])),
NewValue: nil,
UpdateType: DestroyResource,
}, nil
}

return &AttributeChange{
Name: strings.TrimSpace(attribute[0]),
OldValue: strings.TrimSpace(dequote(values[0])),
NewValue: "",
OldValue: doTypeConversion(values[0]),
NewValue: nil,
UpdateType: DestroyResource,
}, nil
} else if strings.HasPrefix(line, "~") {
Expand All @@ -96,8 +100,8 @@ func NewAttributeChangeFromLine(line string) (*AttributeChange, error) {

return &AttributeChange{
Name: strings.TrimSpace(attribute[0]),
OldValue: strings.TrimSpace(dequote(values[0])),
NewValue: strings.TrimSpace(dequote(values[1])),
OldValue: doTypeConversion(values[0]),
NewValue: doTypeConversion(values[1]),
UpdateType: updateType,
}, nil
} else {
Expand All @@ -115,6 +119,35 @@ func (a *AttributeChange) IsComputed() bool {
return a.OldValue == COMPUTED_VALUE || a.NewValue == COMPUTED_VALUE
}

func doTypeConversion(input string) interface{} {
// if it has quotes, assume it is a string and return it without quotes
if strings.HasPrefix(input, `"`) && strings.HasSuffix(input, `"`) {
return dequote(input)
}

if input == "{}" {
return nil
}

if input == "true" || input == "false" {
b, err := strconv.ParseBool(input)
if err != nil {
return input
}
return b
}

if i, err := strconv.Atoi(input); err == nil {
return i
}

if f, err := strconv.ParseFloat(input, 64); err == nil {
return f
}

return input
}

func removeChangeTypeCharacters(line string) string {
return strings.TrimLeft(line, "+/-~<= ")
}
Expand Down
46 changes: 43 additions & 3 deletions attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,58 @@ func TestNewAttributeChangeFromLine(t *testing.T) {
shouldError: false,
expected: &AttributeChange{
Name: "attribute",
OldValue: "",
OldValue: nil,
NewValue: "new",
UpdateType: NewResource,
},
},
"bools are parsed as bools": {
line: `+ attribute = true`,
shouldError: false,
expected: &AttributeChange{
Name: "attribute",
OldValue: nil,
NewValue: true,
UpdateType: NewResource,
},
},
"ints are parsed as ints": {
line: `+ attribute = 1`,
shouldError: false,
expected: &AttributeChange{
Name: "attribute",
OldValue: nil,
NewValue: 1,
UpdateType: NewResource,
},
},
"decimals are parsed as floats": {
line: `+ attribute = 1.23`,
shouldError: false,
expected: &AttributeChange{
Name: "attribute",
OldValue: nil,
NewValue: 1.23,
UpdateType: NewResource,
},
},
"attribute deleted": {
line: `- attribute = "deleted" -> null`,
shouldError: false,
expected: &AttributeChange{
Name: "attribute",
OldValue: "deleted",
NewValue: "",
NewValue: nil,
UpdateType: DestroyResource,
},
},
"empty map is deleted": {
line: `- attribute {}`,
shouldError: false,
expected: &AttributeChange{
Name: "attribute",
OldValue: nil,
NewValue: nil,
UpdateType: DestroyResource,
},
},
Expand Down Expand Up @@ -146,7 +186,7 @@ func TestNewAttributeChangeFromLine(t *testing.T) {
shouldError: false,
expected: &AttributeChange{
Name: "attribute",
OldValue: "",
OldValue: nil,
NewValue: "new",
UpdateType: NewResource,
},
Expand Down
6 changes: 3 additions & 3 deletions resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ func TestNewResourceChangeFromComment(t *testing.T) {
},
},
"int index": {
line: " # module.mymodule.resource.path[0] will be created",
line: " # module.mymodule.resource.path[1] will be created",
shouldError: false,
expected: &ResourceChange{
Address: "module.mymodule.resource.path[0]",
Address: "module.mymodule.resource.path[1]",
ModuleAddress: "module.mymodule",
Type: "resource",
Name: "path",
Index: 0,
Index: 1,
UpdateType: NewResource,
},
},
Expand Down

0 comments on commit eeefcd9

Please sign in to comment.