diff --git a/VERSION b/VERSION index 66d62a8..c432e90 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v1.4.1 +v1.4.2 diff --git a/field.go b/field.go index 4114e7c..2c58654 100644 --- a/field.go +++ b/field.go @@ -20,6 +20,7 @@ import ( "sort" "strings" + "github.com/gogo/protobuf/gogoproto" "github.com/gravitational/trace" ) @@ -158,7 +159,7 @@ func BuildFields(m MessageBuildContext) ([]*Field, error) { } if f != nil { - fields = append(fields, f) + fields = append(fields, f...) } } @@ -173,7 +174,7 @@ func BuildFields(m MessageBuildContext) ([]*Field, error) { } // BuildField builds Field structure -func BuildField(c *FieldBuildContext) (*Field, error) { +func BuildField(c *FieldBuildContext) ([]*Field, error) { var err error if c.IsExcluded() { @@ -208,6 +209,11 @@ func BuildField(c *FieldBuildContext) (*Field, error) { if err != nil { return nil, trace.Wrap(err) } + + // If this is an embedded field, return message's fields instead of creating another field + if gogoproto.IsEmbed(c.field.FieldDescriptorProto) { + return f.Message.Fields, nil + } } if c.IsRepeated() { @@ -233,7 +239,7 @@ func BuildField(c *FieldBuildContext) (*Field, error) { f.OneOfType = c.GetOneOfTypeName() } - return f, nil + return []*Field{f}, nil } // BuildPlaceholderField represents no-field-message single field placeholder @@ -270,11 +276,16 @@ func (f *Field) setMapValues(c *FieldBuildContext) error { var err error // gogoprotobuf returns incorrect elem type for maps. It always contains "*", we have to override. - typ, f.MapValueField, err = f.getMapValueField(c) + typ, fs, err := f.getMapValueField(c) if err != nil { return trace.Wrap(err) } + if len(fs) == 0 { + return trace.BadParameter("expected at least one field") + } + f.MapValueField = fs[0] + // Otherwise, that would contain artificial protobuf Map_Entry type information f.GoType = typ f.IsNullable = strings.Contains(typ, "*") @@ -324,7 +335,7 @@ func (f *Field) getMessage(c *FieldBuildContext) (*Message, error) { } // getMapValueField returns map value field for a field -func (f *Field) getMapValueField(c *FieldBuildContext) (string, *Field, error) { +func (f *Field) getMapValueField(c *FieldBuildContext) (string, []*Field, error) { // For some reason, gogoprotobuf incorrectly treats nullable status when map value is a message. // We have to override it. typ, d, err := c.GetMapValueFieldDescriptorAndType() diff --git a/field_build_context.go b/field_build_context.go index ef50a78..756431d 100644 --- a/field_build_context.go +++ b/field_build_context.go @@ -95,6 +95,11 @@ type FieldBuildContext struct { func NewFieldBuildContext(m MessageBuildContext, field *FieldDescriptorProtoExt, index int) (*FieldBuildContext, error) { typeName := m.GetName() + "." + field.GetName() path := m.GetPath() + "." + field.GetName() + // If the field is an embedded field, path should be + // message name, instead of full path to message name. + if gogoproto.IsEmbed(field.FieldDescriptorProto) { + path = m.GetName() + } var t string diff --git a/test/copy_from_terraform_test.go b/test/copy_from_terraform_test.go index 5df5335..10529e5 100644 --- a/test/copy_from_terraform_test.go +++ b/test/copy_from_terraform_test.go @@ -162,3 +162,13 @@ func TestCopyFromOneOfObjectNoBranch(t *testing.T) { require.Equal(t, nil, target.OneOf) } + +func TestCopyFromEmbeddedField(t *testing.T) { + obj := copyFromTerraformObject(t) + + target := Test{} + require.False(t, CopyTestFromTerraform(context.Background(), obj, &target).HasError()) + + require.Equal(t, "embdtest1", target.EmbeddedString) + require.Equal(t, "embdtest2", target.EmbeddedNestedField.EmbeddedNestedString) +} diff --git a/test/copy_to_terraform_test.go b/test/copy_to_terraform_test.go index cb869f4..781345e 100644 --- a/test/copy_to_terraform_test.go +++ b/test/copy_to_terraform_test.go @@ -318,3 +318,18 @@ func TestCopyToOneOfNoBranch(t *testing.T) { require.True(t, o.Attrs["branch2"].(types.Object).Null) require.True(t, o.Attrs["branch3"].(types.String).Null) } + +func TestCopyToEmbeddedField(t *testing.T) { + o := copyToTerraformObject(t) + testObj := createTestObj() + + diags := CopyTestToTerraform(context.Background(), testObj, &o) + require.False(t, diags.HasError()) + + require.Equal(t, "embdtest1", o.Attrs["embedded_string"].(types.String).Value) + require.False(t, o.Attrs["embedded_string"].(types.String).Unknown) + require.False(t, o.Attrs["embedded_string"].(types.String).Null) + + require.Equal(t, "embdtest2", o.Attrs["embedded_nested_field"].(types.Object).Attrs["embedded_nested_string"].(types.String).Value) + +} diff --git a/test/fixtures.go b/test/fixtures.go index 05afb0a..d75eb0a 100644 --- a/test/fixtures.go +++ b/test/fixtures.go @@ -92,6 +92,13 @@ func createTestObj() Test { }, Map: map[string]string{"key1": "value1", "key2": "value2"}, + + EmbeddedField: EmbeddedField{ + EmbeddedString: "embdtest1", + EmbeddedNestedField: &EmbeddedNestedField{ + EmbeddedNestedString: "embdtest2", + }, + }, } } @@ -409,6 +416,12 @@ func copyFromTerraformObject(t *testing.T) types.Object { "branch3": types.String{Null: true}, "empty_message_branch": types.Object{Null: true}, "string_branch": types.String{Null: true}, + "embedded_string": types.String{Value: "embdtest1"}, + "embedded_nested_field": types.Object{ + Attrs: map[string]attr.Value{ + "embedded_nested_string": types.String{Value: "embdtest2"}, + }, + }, }, AttrTypes: obj.AttrTypes, } diff --git a/test/test.pb.go b/test/test.pb.go index 757de91..b747ee8 100644 --- a/test/test.pb.go +++ b/test/test.pb.go @@ -127,9 +127,11 @@ type Test struct { // *Test_EmptyMessageBranch // *Test_StringBranch OneOfWithEmptyMessage isTest_OneOfWithEmptyMessage `protobuf_oneof:"OneOfWithEmptyMessage"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + // EmbeddedField encapsulates fields which can be shared among various types + EmbeddedField `protobuf:"bytes,38,opt,name=EmbeddedField,proto3,embedded=EmbeddedField" json:""` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *Test) Reset() { *m = Test{} } @@ -413,6 +415,73 @@ func (m *Branch2) XXX_DiscardUnknown() { var xxx_messageInfo_Branch2 proto.InternalMessageInfo +// EmbeddedField encapsulates fields which can be shared among various types +type EmbeddedField struct { + // EmbeddedString string field + EmbeddedString string `protobuf:"bytes,1,opt,name=EmbeddedString,proto3" json:"embedded_string"` + // Nested EmbeddedNestedField field + EmbeddedNestedField *EmbeddedNestedField `protobuf:"bytes,2,opt,name=EmbeddedNestedField,proto3" json:"EmbeddedNestedField,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *EmbeddedField) Reset() { *m = EmbeddedField{} } +func (m *EmbeddedField) String() string { return proto.CompactTextString(m) } +func (*EmbeddedField) ProtoMessage() {} +func (*EmbeddedField) Descriptor() ([]byte, []int) { + return fileDescriptor_c161fcfdc0c3ff1e, []int{6} +} +func (m *EmbeddedField) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_EmbeddedField.Unmarshal(m, b) +} +func (m *EmbeddedField) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_EmbeddedField.Marshal(b, m, deterministic) +} +func (m *EmbeddedField) XXX_Merge(src proto.Message) { + xxx_messageInfo_EmbeddedField.Merge(m, src) +} +func (m *EmbeddedField) XXX_Size() int { + return xxx_messageInfo_EmbeddedField.Size(m) +} +func (m *EmbeddedField) XXX_DiscardUnknown() { + xxx_messageInfo_EmbeddedField.DiscardUnknown(m) +} + +var xxx_messageInfo_EmbeddedField proto.InternalMessageInfo + +type EmbeddedNestedField struct { + // EmbeddedNestedString string field + EmbeddedNestedString string `protobuf:"bytes,1,opt,name=EmbeddedNestedString,proto3" json:"embedded_nested_string"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *EmbeddedNestedField) Reset() { *m = EmbeddedNestedField{} } +func (m *EmbeddedNestedField) String() string { return proto.CompactTextString(m) } +func (*EmbeddedNestedField) ProtoMessage() {} +func (*EmbeddedNestedField) Descriptor() ([]byte, []int) { + return fileDescriptor_c161fcfdc0c3ff1e, []int{7} +} +func (m *EmbeddedNestedField) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_EmbeddedNestedField.Unmarshal(m, b) +} +func (m *EmbeddedNestedField) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_EmbeddedNestedField.Marshal(b, m, deterministic) +} +func (m *EmbeddedNestedField) XXX_Merge(src proto.Message) { + xxx_messageInfo_EmbeddedNestedField.Merge(m, src) +} +func (m *EmbeddedNestedField) XXX_Size() int { + return xxx_messageInfo_EmbeddedNestedField.Size(m) +} +func (m *EmbeddedNestedField) XXX_DiscardUnknown() { + xxx_messageInfo_EmbeddedNestedField.DiscardUnknown(m) +} + +var xxx_messageInfo_EmbeddedNestedField proto.InternalMessageInfo + func init() { proto.RegisterEnum("test.Mode", Mode_name, Mode_value) proto.RegisterType((*Test)(nil), "test.Test") @@ -426,72 +495,80 @@ func init() { proto.RegisterType((*OtherNested)(nil), "test.OtherNested") proto.RegisterType((*Branch1)(nil), "test.Branch1") proto.RegisterType((*Branch2)(nil), "test.Branch2") + proto.RegisterType((*EmbeddedField)(nil), "test.EmbeddedField") + proto.RegisterType((*EmbeddedNestedField)(nil), "test.EmbeddedNestedField") } func init() { proto.RegisterFile("test.proto", fileDescriptor_c161fcfdc0c3ff1e) } var fileDescriptor_c161fcfdc0c3ff1e = []byte{ - // 978 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x56, 0x4f, 0x73, 0x1b, 0xc5, - 0x13, 0xd5, 0x68, 0xd7, 0xfa, 0xd3, 0x96, 0x65, 0xb9, 0x23, 0xdb, 0x93, 0xcd, 0x2f, 0xd2, 0x46, - 0x3f, 0xa7, 0xb2, 0xe4, 0x20, 0x63, 0x39, 0xe5, 0xa2, 0x52, 0xc0, 0x61, 0x89, 0x5d, 0x21, 0x20, - 0x09, 0xc6, 0x31, 0x39, 0xaf, 0xac, 0x8d, 0xac, 0x20, 0x6b, 0x55, 0xda, 0x11, 0x85, 0xbf, 0x05, - 0x47, 0xf8, 0x46, 0x3e, 0x72, 0xa4, 0x38, 0x98, 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ipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x56, 0x4d, 0x73, 0x1b, 0x45, + 0x10, 0xd5, 0x68, 0x65, 0x5b, 0x6a, 0xcb, 0xb2, 0xdc, 0x96, 0x9d, 0x89, 0x42, 0xb4, 0x1b, 0xe1, + 0x90, 0x25, 0x07, 0x19, 0xcb, 0x29, 0x17, 0x15, 0x3e, 0x0e, 0x4b, 0x6c, 0x42, 0x82, 0x24, 0x18, + 0xc7, 0xe4, 0x48, 0xad, 0xac, 0x89, 0xac, 0x20, 0x6b, 0x55, 0xda, 0x11, 0x85, 0xff, 0x05, 0x47, + 0xf8, 0x47, 0x3e, 0xe6, 0x48, 0x71, 0x10, 0x85, 0x8f, 0xfe, 0x09, 0x9c, 0xa8, 0x9d, 0xd1, 0x7e, + 0x6a, 0x1d, 0x48, 0x6e, 0xd3, 0xaf, 0xdf, 0x7b, 0xf3, 0xd5, 0x3d, 0xbb, 0x00, 0x82, 0xbb, 0xa2, + 0x31, 0x9e, 0x38, 0xc2, 0xc1, 0x9c, 0x37, 0xae, 0x56, 0xfa, 0x4e, 0xdf, 0x91, 0xc0, 0xae, 0x37, + 0x52, 0xb9, 0xaa, 0xde, 0x77, 0x9c, 0xfe, 0x90, 0xef, 0xca, 0xa8, 0x3b, 0x7d, 0xb5, 0x2b, 0x06, + 0xe7, 0xdc, 0x15, 0xf6, 0xf9, 0x58, 0x11, 0xea, 0x57, 0x65, 0xc8, 0xbd, 0xe0, 0xae, 0xc0, 0x2a, + 0x68, 0xc7, 0x62, 0x42, 0x89, 0x41, 0xcc, 0x82, 0x95, 0xbf, 0x9e, 0xe9, 0x39, 0x57, 0x4c, 0xf6, + 0x98, 0x07, 0x62, 0x05, 0x96, 0xbe, 0x19, 0x89, 0xfd, 0x26, 0xcd, 0x1a, 0xc4, 0x5c, 0x62, 0x2a, + 0x98, 0xa3, 0x07, 0x8f, 0xa8, 0x66, 0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d, + 0xef, 0xf6, 0x04, 0x2a, 0x69, 0xd3, 0xa4, 0x78, 0x3c, 0x88, 0xdf, 0x46, 0xca, 0x59, 0x84, 0xb6, + 0x75, 0x1d, 0x56, 0x23, 0x99, 0xc5, 0xe3, 0xad, 0xdf, 0x09, 0x1a, 0x37, 0x25, 0xa9, 0x07, 0xad, + 0x1a, 0xfe, 0xf9, 0x91, 0xc8, 0x9f, 0x5f, 0xfd, 0x77, 0x92, 0xe8, 0x09, 0xfc, 0x0c, 0x4a, 0x3e, + 0xa0, 0x9a, 0x67, 0xfe, 0x23, 0xb9, 0x79, 0x3d, 0xd3, 0xd7, 0xf9, 0x3c, 0xf3, 0xa3, 0x2b, 0x53, + 0x2c, 0x41, 0xc5, 0xe7, 0xb0, 0xe9, 0x23, 0x6a, 0xc1, 0xaa, 0xcf, 0xd4, 0x46, 0x6f, 0xc7, 0xfb, + 0x2c, 0x42, 0x60, 0x69, 0xaa, 0x3a, 0x4f, 0x35, 0xc3, 0x36, 0x54, 0xe2, 0x70, 0x6c, 0x99, 0xd5, + 0xeb, 0x99, 0xbe, 0x1d, 0x2c, 0x73, 0x24, 0x09, 0xfe, 0x6a, 0x53, 0x75, 0x0f, 0x77, 0xd4, 0xab, + 0x89, 0xab, 0xb0, 0x72, 0xd2, 0x7e, 0xde, 0xee, 0xbc, 0x6c, 0x97, 0x33, 0xb8, 0x0c, 0xd9, 0x4e, + 0xbb, 0x4c, 0x70, 0x05, 0xb4, 0xce, 0xd1, 0x51, 0x39, 0x6b, 0x15, 0x2f, 0xff, 0xae, 0x65, 0x2e, + 0xaf, 0x6a, 0x99, 0x37, 0x57, 0xb5, 0x4c, 0x77, 0x59, 0x7e, 0xc1, 0xf7, 0xff, 0x0d, 0x00, 0x00, + 0xff, 0xff, 0x33, 0x06, 0xbb, 0x5b, 0xbd, 0x0b, 0x00, 0x00, } diff --git a/test/test.proto b/test/test.proto index 7853931..b8c6dcd 100644 --- a/test/test.proto +++ b/test/test.proto @@ -154,6 +154,13 @@ message Test { // StringBranch is the oneof branch triggered by string value string StringBranch = 37; } + + // EmbeddedField encapsulates fields which can be shared among various types + EmbeddedField EmbeddedField = 38 [ + (gogoproto.nullable) = false, + (gogoproto.jsontag) = "", + (gogoproto.embed) = true + ]; } // EmptyMessageBranch message for empty oneof branch @@ -190,4 +197,18 @@ message Branch1 { message Branch2 { // Int32 int field int32 Int32 = 1; -} \ No newline at end of file +} + +// EmbeddedField encapsulates fields which can be shared among various types +message EmbeddedField { + // EmbeddedString string field + string EmbeddedString = 1 [(gogoproto.jsontag) = "embedded_string"]; + // Nested EmbeddedNestedField field + EmbeddedNestedField EmbeddedNestedField = 2; +} + +message EmbeddedNestedField { + // EmbeddedNestedString string field + string EmbeddedNestedString = 1 [(gogoproto.jsontag) = "embedded_nested_string"]; + } + diff --git a/test/test_terraform.go b/test/test_terraform.go index 2daea67..6cee8ee 100644 --- a/test/test_terraform.go +++ b/test/test_terraform.go @@ -112,6 +112,20 @@ func GenSchemaTest(ctx context.Context) (github_com_hashicorp_terraform_plugin_f Optional: true, Type: DurationType{}, }, + "embedded_nested_field": { + Attributes: github_com_hashicorp_terraform_plugin_framework_tfsdk.SingleNestedAttributes(map[string]github_com_hashicorp_terraform_plugin_framework_tfsdk.Attribute{"embedded_nested_string": { + Description: "EmbeddedNestedString string field", + Optional: true, + Type: github_com_hashicorp_terraform_plugin_framework_types.StringType, + }}), + Description: "Nested EmbeddedNestedField field", + Optional: true, + }, + "embedded_string": { + Description: "EmbeddedString string field", + Optional: true, + Type: github_com_hashicorp_terraform_plugin_framework_types.StringType, + }, "empty_message_branch": { Attributes: github_com_hashicorp_terraform_plugin_framework_tfsdk.SingleNestedAttributes(map[string]github_com_hashicorp_terraform_plugin_framework_tfsdk.Attribute{"active": { Computed: true, @@ -717,6 +731,58 @@ func CopyTestFromTerraform(_ context.Context, tf github_com_hashicorp_terraform_ } } } + { + a, ok := tf.Attrs["embedded_nested_field"] + if !ok { + diags.Append(attrReadMissingDiag{"Test.EmbeddedNestedField"}) + } else { + v, ok := a.(github_com_hashicorp_terraform_plugin_framework_types.Object) + if !ok { + diags.Append(attrReadConversionFailureDiag{"Test.EmbeddedNestedField", "github.com/hashicorp/terraform-plugin-framework/types.Object"}) + } else { + obj.EmbeddedNestedField = nil + if !v.Null && !v.Unknown { + tf := v + obj.EmbeddedNestedField = &EmbeddedNestedField{} + obj := obj.EmbeddedNestedField + { + a, ok := tf.Attrs["embedded_nested_string"] + if !ok { + diags.Append(attrReadMissingDiag{"Test.EmbeddedNestedField.EmbeddedNestedString"}) + } else { + v, ok := a.(github_com_hashicorp_terraform_plugin_framework_types.String) + if !ok { + diags.Append(attrReadConversionFailureDiag{"Test.EmbeddedNestedField.EmbeddedNestedString", "github.com/hashicorp/terraform-plugin-framework/types.String"}) + } else { + var t string + if !v.Null && !v.Unknown { + t = string(v.Value) + } + obj.EmbeddedNestedString = t + } + } + } + } + } + } + } + { + a, ok := tf.Attrs["embedded_string"] + if !ok { + diags.Append(attrReadMissingDiag{"Test.EmbeddedString"}) + } else { + v, ok := a.(github_com_hashicorp_terraform_plugin_framework_types.String) + if !ok { + diags.Append(attrReadConversionFailureDiag{"Test.EmbeddedString", "github.com/hashicorp/terraform-plugin-framework/types.String"}) + } else { + var t string + if !v.Null && !v.Unknown { + t = string(v.Value) + } + obj.EmbeddedString = t + } + } + } { a, ok := tf.Attrs["empty_message_branch"] if !ok { @@ -2550,6 +2616,82 @@ func CopyTestToTerraform(ctx context.Context, obj Test, tf *github_com_hashicorp tf.Attrs["duration_standard_missing"] = v } } + { + a, ok := tf.AttrTypes["embedded_nested_field"] + if !ok { + diags.Append(attrWriteMissingDiag{"Test.EmbeddedNestedField"}) + } else { + o, ok := a.(github_com_hashicorp_terraform_plugin_framework_types.ObjectType) + if !ok { + diags.Append(attrWriteConversionFailureDiag{"Test.EmbeddedNestedField", "github.com/hashicorp/terraform-plugin-framework/types.ObjectType"}) + } else { + v, ok := tf.Attrs["embedded_nested_field"].(github_com_hashicorp_terraform_plugin_framework_types.Object) + if !ok { + v = github_com_hashicorp_terraform_plugin_framework_types.Object{ + + AttrTypes: o.AttrTypes, + Attrs: make(map[string]github_com_hashicorp_terraform_plugin_framework_attr.Value, len(o.AttrTypes)), + } + } else { + if v.Attrs == nil { + v.Attrs = make(map[string]github_com_hashicorp_terraform_plugin_framework_attr.Value, len(tf.AttrTypes)) + } + } + if obj.EmbeddedNestedField == nil { + v.Null = true + } else { + obj := obj.EmbeddedNestedField + tf := &v + { + t, ok := tf.AttrTypes["embedded_nested_string"] + if !ok { + diags.Append(attrWriteMissingDiag{"Test.EmbeddedNestedField.EmbeddedNestedString"}) + } else { + v, ok := tf.Attrs["embedded_nested_string"].(github_com_hashicorp_terraform_plugin_framework_types.String) + if !ok { + i, err := t.ValueFromTerraform(ctx, github_com_hashicorp_terraform_plugin_go_tftypes.NewValue(t.TerraformType(ctx), nil)) + if err != nil { + diags.Append(attrWriteGeneralError{"Test.EmbeddedNestedField.EmbeddedNestedString", err}) + } + v, ok = i.(github_com_hashicorp_terraform_plugin_framework_types.String) + if !ok { + diags.Append(attrWriteConversionFailureDiag{"Test.EmbeddedNestedField.EmbeddedNestedString", "github.com/hashicorp/terraform-plugin-framework/types.String"}) + } + v.Null = string(obj.EmbeddedNestedString) == "" + } + v.Value = string(obj.EmbeddedNestedString) + v.Unknown = false + tf.Attrs["embedded_nested_string"] = v + } + } + } + v.Unknown = false + tf.Attrs["embedded_nested_field"] = v + } + } + } + { + t, ok := tf.AttrTypes["embedded_string"] + if !ok { + diags.Append(attrWriteMissingDiag{"Test.EmbeddedString"}) + } else { + v, ok := tf.Attrs["embedded_string"].(github_com_hashicorp_terraform_plugin_framework_types.String) + if !ok { + i, err := t.ValueFromTerraform(ctx, github_com_hashicorp_terraform_plugin_go_tftypes.NewValue(t.TerraformType(ctx), nil)) + if err != nil { + diags.Append(attrWriteGeneralError{"Test.EmbeddedString", err}) + } + v, ok = i.(github_com_hashicorp_terraform_plugin_framework_types.String) + if !ok { + diags.Append(attrWriteConversionFailureDiag{"Test.EmbeddedString", "github.com/hashicorp/terraform-plugin-framework/types.String"}) + } + v.Null = string(obj.EmbeddedString) == "" + } + v.Value = string(obj.EmbeddedString) + v.Unknown = false + tf.Attrs["embedded_string"] = v + } + } { a, ok := tf.AttrTypes["empty_message_branch"] if !ok {