-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a command to automatically generate type info for output in…
… metadata file (#2602)
- Loading branch information
Showing
7 changed files
with
461 additions
and
9 deletions.
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package parser | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"google.golang.org/protobuf/types/known/structpb" | ||
|
||
tfjson "github.com/hashicorp/terraform-json" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
func ParseOutputTypesFromState(stateData []byte) (map[string]*structpb.Value, error) { | ||
|
||
var state tfjson.State | ||
|
||
// Unmarshal the state data into tfjson.State | ||
err := json.Unmarshal(stateData, &state) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal state data: %w", err) | ||
} | ||
|
||
outputTypeMap := make(map[string]*structpb.Value) | ||
for name, output := range state.Values.Outputs { | ||
pbValue, err := convertOutputTypeToStructpb(output) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to convert output %q to structpb.Value: %w", name, err) | ||
} | ||
outputTypeMap[name] = pbValue | ||
} | ||
|
||
return outputTypeMap, nil | ||
} | ||
|
||
func convertOutputTypeToStructpb(output *tfjson.StateOutput) (*structpb.Value, error) { | ||
// Handle nil values explicitly | ||
if output.Value == nil { | ||
return structpb.NewNullValue(), nil | ||
} | ||
|
||
// Handle cases where output.Type is NilType | ||
if output.Type == cty.NilType { | ||
return structpb.NewNullValue(), nil | ||
} | ||
|
||
// Marshal the output value to JSON | ||
var buf bytes.Buffer | ||
enc := json.NewEncoder(&buf) | ||
enc.SetEscapeHTML(false) | ||
err := enc.Encode(output.Type) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal output type to JSON: %w", err) | ||
} | ||
|
||
// Unmarshal the JSON into a structpb.Value | ||
pbValue := &structpb.Value{} | ||
err = pbValue.UnmarshalJSON(buf.Bytes()) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal JSON into structpb.Value: %w", err) | ||
} | ||
|
||
return pbValue, nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package parser | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
) | ||
|
||
func TestParseOutputTypesFromState_WithSimpleTypes(t *testing.T) { | ||
t.Parallel() | ||
stateData := []byte(` | ||
{ | ||
"format_version": "1.0", | ||
"terraform_version": "1.2.0", | ||
"values": { | ||
"outputs": { | ||
"boolean_output": { | ||
"type": "bool", | ||
"value": true | ||
}, | ||
"number_output": { | ||
"type": "number", | ||
"value": 42 | ||
}, | ||
"string_output": { | ||
"type": "string", | ||
"value": "foo" | ||
} | ||
} | ||
} | ||
} | ||
`) | ||
want := map[string]*structpb.Value{ | ||
"boolean_output": structpb.NewStringValue("bool"), | ||
"number_output": structpb.NewStringValue("number"), | ||
"string_output": structpb.NewStringValue("string"), | ||
} | ||
got, err := ParseOutputTypesFromState(stateData) | ||
if err != nil { | ||
t.Errorf("ParseOutputTypesFromState() error = %v", err) | ||
return | ||
} | ||
if diff := cmp.Diff(got, want, cmp.Comparer(compareStructpbValues)); diff != "" { | ||
t.Errorf("ParseOutputTypesFromState() mismatch (-got +want):\n%s", diff) | ||
} | ||
} | ||
|
||
func TestParseOutputTypesFromState_WithComplexTypes(t *testing.T) { | ||
t.Parallel() | ||
stateData := []byte(` | ||
{ | ||
"format_version": "1.0", | ||
"terraform_version": "1.2.0", | ||
"values": { | ||
"outputs": { | ||
"interpolated_deep": { | ||
"type": [ | ||
"object", | ||
{ | ||
"foo": "string", | ||
"map": [ | ||
"object", | ||
{ | ||
"bar": "string", | ||
"id": "string" | ||
} | ||
], | ||
"number": "number" | ||
} | ||
], | ||
"value": { | ||
"foo": "bar", | ||
"map": { | ||
"bar": "baz", | ||
"id": "424881806176056736" | ||
}, | ||
"number": 42 | ||
} | ||
}, | ||
"list_output": { | ||
"type": [ | ||
"tuple", | ||
[ | ||
"string", | ||
"string" | ||
] | ||
], | ||
"value": [ | ||
"foo", | ||
"bar" | ||
] | ||
}, | ||
"map_output": { | ||
"type": [ | ||
"object", | ||
{ | ||
"foo": "string", | ||
"number": "number" | ||
} | ||
], | ||
"value": { | ||
"foo": "bar", | ||
"number": 42 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`) | ||
want := map[string]*structpb.Value{ | ||
"interpolated_deep": structpb.NewListValue(&structpb.ListValue{Values: []*structpb.Value{ | ||
structpb.NewStringValue("object"), | ||
structpb.NewStructValue(&structpb.Struct{Fields: map[string]*structpb.Value{ | ||
"foo": structpb.NewStringValue("string"), | ||
"map": structpb.NewListValue(&structpb.ListValue{Values: []*structpb.Value{structpb.NewStringValue("object"), structpb.NewStructValue(&structpb.Struct{Fields: map[string]*structpb.Value{"bar": structpb.NewStringValue("string"), "id": structpb.NewStringValue("string")}})}}), | ||
"number": structpb.NewStringValue("number"), | ||
}}), | ||
}}), | ||
"list_output": structpb.NewListValue(&structpb.ListValue{Values: []*structpb.Value{ | ||
structpb.NewStringValue("tuple"), | ||
structpb.NewListValue(&structpb.ListValue{Values: []*structpb.Value{structpb.NewStringValue("string"), structpb.NewStringValue("string")}}), | ||
}}), | ||
"map_output": structpb.NewListValue(&structpb.ListValue{Values: []*structpb.Value{ | ||
structpb.NewStringValue("object"), | ||
structpb.NewStructValue(&structpb.Struct{Fields: map[string]*structpb.Value{ | ||
"foo": structpb.NewStringValue("string"), | ||
"number": structpb.NewStringValue("number"), | ||
}}), | ||
}}), | ||
} | ||
got, err := ParseOutputTypesFromState(stateData) | ||
if err != nil { | ||
t.Errorf("ParseOutputTypesFromState() error = %v", err) | ||
return | ||
} | ||
if diff := cmp.Diff(got, want, cmp.Comparer(compareStructpbValues)); diff != "" { | ||
t.Errorf("ParseOutputTypesFromState() mismatch (-got +want):\n%s", diff) | ||
} | ||
} | ||
|
||
func TestParseOutputTypesFromState_WithoutTypes(t *testing.T) { | ||
t.Parallel() | ||
stateData := []byte(` | ||
{ | ||
"format_version": "1.0", | ||
"terraform_version": "1.2.0", | ||
"values": { | ||
"outputs": { | ||
"no_type_output": { | ||
"value": "some_value" | ||
} | ||
} | ||
} | ||
} | ||
`) | ||
want := map[string]*structpb.Value{ | ||
"no_type_output": structpb.NewNullValue(), // Expecting null value when type is missing | ||
} | ||
|
||
got, err := ParseOutputTypesFromState(stateData) | ||
if err != nil { | ||
t.Errorf("ParseOutputTypesFromState() error = %v", err) | ||
return | ||
} | ||
if diff := cmp.Diff(got, want, cmp.Comparer(compareStructpbValues)); diff != "" { | ||
t.Errorf("ParseOutputTypesFromState() mismatch (-got +want):\n%s", diff) | ||
} | ||
} | ||
|
||
// compareStructpbValues is a custom comparer for structpb.Value | ||
func compareStructpbValues(x, y *structpb.Value) bool { | ||
// Marshal to JSON and compare the JSON strings | ||
xJSON, _ := x.MarshalJSON() | ||
yJSON, _ := y.MarshalJSON() | ||
return string(xJSON) == string(yJSON) | ||
} |
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
Oops, something went wrong.