New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically run go-cty.convert after applying defaults #560
Closed
liamcervante
wants to merge
1
commit into
hashicorp:main
from
liamcervante:liamcervante/automated-conversion
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -68,7 +68,10 @@ func TestDefaults_Apply(t *testing.T) { | |||||
}, | ||||||
}, | ||||||
value: cty.UnknownVal(cty.Map(cty.String)), | ||||||
want: cty.UnknownVal(cty.Map(cty.String)), | ||||||
want: cty.UnknownVal(cty.Object(map[string]cty.Type{ | ||||||
"a": cty.String, | ||||||
"b": cty.Bool, | ||||||
})), | ||||||
}, | ||||||
// Defaults do not override attributes which are present in the given | ||||||
// value. | ||||||
|
@@ -85,7 +88,7 @@ func TestDefaults_Apply(t *testing.T) { | |||||
}), | ||||||
want: cty.ObjectVal(map[string]cty.Value{ | ||||||
"a": cty.StringVal("foo"), | ||||||
"b": cty.StringVal("false"), | ||||||
"b": cty.False, | ||||||
}), | ||||||
}, | ||||||
// Defaults will replace explicit nulls. | ||||||
|
@@ -188,7 +191,7 @@ func TestDefaults_Apply(t *testing.T) { | |||||
"a": cty.StringVal("bar"), | ||||||
}), | ||||||
}), | ||||||
want: cty.ObjectVal(map[string]cty.Value{ | ||||||
want: cty.MapVal(map[string]cty.Value{ | ||||||
"f": cty.ObjectVal(map[string]cty.Value{ | ||||||
"a": cty.StringVal("foo"), | ||||||
"b": cty.True, | ||||||
|
@@ -221,7 +224,7 @@ func TestDefaults_Apply(t *testing.T) { | |||||
"a": cty.StringVal("bar"), | ||||||
}), | ||||||
}), | ||||||
want: cty.TupleVal([]cty.Value{ | ||||||
want: cty.ListVal([]cty.Value{ | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
"a": cty.StringVal("foo"), | ||||||
"b": cty.True, | ||||||
|
@@ -307,7 +310,7 @@ func TestDefaults_Apply(t *testing.T) { | |||||
"d": cty.NumberIntVal(7), | ||||||
}), | ||||||
}), | ||||||
want: cty.TupleVal([]cty.Value{ | ||||||
want: cty.SetVal([]cty.Value{ | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
"c": cty.ObjectVal(map[string]cty.Value{ | ||||||
"a": cty.StringVal("foo"), | ||||||
|
@@ -317,7 +320,11 @@ func TestDefaults_Apply(t *testing.T) { | |||||
}), | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
// No default value for "c" specified, so none applied. The | ||||||
// convert stage will fill in a null. | ||||||
// convert stage will have filled in a null. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A little more distinct from the previous logic:
Suggested change
|
||||||
"c": cty.NullVal(cty.Object(map[string]cty.Type{ | ||||||
"a": cty.String, | ||||||
"b": cty.Bool, | ||||||
})), | ||||||
"d": cty.NumberIntVal(7), | ||||||
}), | ||||||
}), | ||||||
|
@@ -339,6 +346,7 @@ func TestDefaults_Apply(t *testing.T) { | |||||
"c": { | ||||||
Type: simpleObject, | ||||||
DefaultValues: map[string]cty.Value{ | ||||||
"a": cty.StringVal("bar"), | ||||||
"b": cty.True, | ||||||
}, | ||||||
}, | ||||||
|
@@ -357,7 +365,7 @@ func TestDefaults_Apply(t *testing.T) { | |||||
"d": cty.NumberIntVal(7), | ||||||
}), | ||||||
}), | ||||||
want: cty.TupleVal([]cty.Value{ | ||||||
want: cty.SetVal([]cty.Value{ | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
"c": cty.ObjectVal(map[string]cty.Value{ | ||||||
"a": cty.StringVal("foo"), | ||||||
|
@@ -369,6 +377,7 @@ func TestDefaults_Apply(t *testing.T) { | |||||
"c": cty.ObjectVal(map[string]cty.Value{ | ||||||
// Default value for "b" is applied to the empty object | ||||||
// specified as the default for "c" | ||||||
"a": cty.StringVal("bar"), | ||||||
"b": cty.True, | ||||||
}), | ||||||
"d": cty.NumberIntVal(7), | ||||||
|
@@ -413,7 +422,7 @@ func TestDefaults_Apply(t *testing.T) { | |||||
"d": cty.NumberIntVal(7), | ||||||
}), | ||||||
}), | ||||||
want: cty.TupleVal([]cty.Value{ | ||||||
want: cty.SetVal([]cty.Value{ | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
"c": cty.ObjectVal(map[string]cty.Value{ | ||||||
"a": cty.StringVal("foo"), | ||||||
|
@@ -433,6 +442,98 @@ func TestDefaults_Apply(t *testing.T) { | |||||
}), | ||||||
}), | ||||||
}, | ||||||
"my_test_case": { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A guess at a slightly better test case name:
Suggested change
|
||||||
defaults: &Defaults{ | ||||||
Type: cty.Set(cty.Object(map[string]cty.Type{ | ||||||
"name": cty.String, | ||||||
"schedules": cty.Set(cty.ObjectWithOptionalAttrs( | ||||||
map[string]cty.Type{ | ||||||
"name": cty.String, | ||||||
"cold_storage_after": cty.Number, | ||||||
}, []string{"cold_storage_after"})), | ||||||
})), | ||||||
DefaultValues: nil, | ||||||
Children: map[string]*Defaults{ | ||||||
"": { | ||||||
Type: cty.Object(map[string]cty.Type{ | ||||||
"name": cty.String, | ||||||
"schedules": cty.Set(cty.ObjectWithOptionalAttrs( | ||||||
map[string]cty.Type{ | ||||||
"name": cty.String, | ||||||
"cold_storage_after": cty.Number, | ||||||
}, []string{"cold_storage_after"})), | ||||||
}), | ||||||
Children: map[string]*Defaults{ | ||||||
"schedules": { | ||||||
Type: cty.Set(cty.ObjectWithOptionalAttrs( | ||||||
map[string]cty.Type{ | ||||||
"name": cty.String, | ||||||
"cold_storage_after": cty.Number, | ||||||
}, []string{"cold_storage_after"})), | ||||||
Children: map[string]*Defaults{ | ||||||
"": { | ||||||
Type: cty.ObjectWithOptionalAttrs( | ||||||
map[string]cty.Type{ | ||||||
"name": cty.String, | ||||||
"cold_storage_after": cty.Number, | ||||||
}, []string{"cold_storage_after"}), | ||||||
DefaultValues: map[string]cty.Value{ | ||||||
"cold_storage_after": cty.NumberIntVal(10), | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
}, | ||||||
value: cty.SetVal([]cty.Value{ | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
"name": cty.StringVal("test1"), | ||||||
"schedules": cty.SetVal([]cty.Value{ | ||||||
cty.MapVal(map[string]cty.Value{ | ||||||
"name": cty.StringVal("daily"), | ||||||
}), | ||||||
}), | ||||||
}), | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
"name": cty.StringVal("test2"), | ||||||
"schedules": cty.SetVal([]cty.Value{ | ||||||
cty.MapVal(map[string]cty.Value{ | ||||||
"name": cty.StringVal("daily"), | ||||||
}), | ||||||
cty.MapVal(map[string]cty.Value{ | ||||||
"name": cty.StringVal("weekly"), | ||||||
"cold_storage_after": cty.StringVal("0"), | ||||||
}), | ||||||
}), | ||||||
}), | ||||||
}), | ||||||
want: cty.SetVal([]cty.Value{ | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
"name": cty.StringVal("test1"), | ||||||
"schedules": cty.SetVal([]cty.Value{ | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
"name": cty.StringVal("daily"), | ||||||
"cold_storage_after": cty.NumberIntVal(10), | ||||||
}), | ||||||
}), | ||||||
}), | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
"name": cty.StringVal("test2"), | ||||||
"schedules": cty.SetVal([]cty.Value{ | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
"name": cty.StringVal("daily"), | ||||||
"cold_storage_after": cty.NumberIntVal(10), | ||||||
}), | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
"name": cty.StringVal("weekly"), | ||||||
"cold_storage_after": cty.NumberIntVal(0), | ||||||
}), | ||||||
}), | ||||||
}), | ||||||
}), | ||||||
}, | ||||||
"set of nested objects, nulls in default sub-object overridden": { | ||||||
defaults: &Defaults{ | ||||||
Type: cty.Set(nestedObject), | ||||||
|
@@ -472,7 +573,7 @@ func TestDefaults_Apply(t *testing.T) { | |||||
"d": cty.NumberIntVal(7), | ||||||
}), | ||||||
}), | ||||||
want: cty.TupleVal([]cty.Value{ | ||||||
want: cty.SetVal([]cty.Value{ | ||||||
cty.ObjectVal(map[string]cty.Value{ | ||||||
"c": cty.ObjectVal(map[string]cty.Value{ | ||||||
"a": cty.StringVal("foo"), | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function and its collaborator
traverseChildType
are so similar totraverse
andtraverseChild
that I think we should consider merging them. Could we extendtraverse
andtraverseChild
to return(cty.Value, cty.Type)
so that the recursion logic is shared?