// Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.

package provider

import (
	"encoding/json"
	tfTypes "github.com/airbytehq/terraform-provider-airbyte/internal/provider/types"
	"github.com/airbytehq/terraform-provider-airbyte/internal/sdk/models/shared"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func (r *ConnectionResourceModel) ToSharedConnectionCreateRequest() *shared.ConnectionCreateRequest {
	name := new(string)
	if !r.Name.IsUnknown() && !r.Name.IsNull() {
		*name = r.Name.ValueString()
	} else {
		name = nil
	}
	var sourceID string
	sourceID = r.SourceID.ValueString()

	var destinationID string
	destinationID = r.DestinationID.ValueString()

	var configurations *shared.StreamConfigurations
	if r.Configurations != nil {
		var streams []shared.StreamConfiguration = []shared.StreamConfiguration{}
		for _, streamsItem := range r.Configurations.Streams {
			var name1 string
			name1 = streamsItem.Name.ValueString()

			namespace := new(string)
			if !streamsItem.Namespace.IsUnknown() && !streamsItem.Namespace.IsNull() {
				*namespace = streamsItem.Namespace.ValueString()
			} else {
				namespace = nil
			}
			syncMode := new(shared.ConnectionSyncModeEnum)
			if !streamsItem.SyncMode.IsUnknown() && !streamsItem.SyncMode.IsNull() {
				*syncMode = shared.ConnectionSyncModeEnum(streamsItem.SyncMode.ValueString())
			} else {
				syncMode = nil
			}
			var cursorField []string = []string{}
			for _, cursorFieldItem := range streamsItem.CursorField {
				cursorField = append(cursorField, cursorFieldItem.ValueString())
			}
			var primaryKey [][]string = [][]string{}
			for _, primaryKeyItem := range streamsItem.PrimaryKey {
				var primaryKeyTmp []string = []string{}
				for _, item := range primaryKeyItem {
					primaryKeyTmp = append(primaryKeyTmp, item.ValueString())
				}
				primaryKey = append(primaryKey, primaryKeyTmp)
			}
			includeFiles := new(bool)
			if !streamsItem.IncludeFiles.IsUnknown() && !streamsItem.IncludeFiles.IsNull() {
				*includeFiles = streamsItem.IncludeFiles.ValueBool()
			} else {
				includeFiles = nil
			}
			destinationObjectName := new(string)
			if !streamsItem.DestinationObjectName.IsUnknown() && !streamsItem.DestinationObjectName.IsNull() {
				*destinationObjectName = streamsItem.DestinationObjectName.ValueString()
			} else {
				destinationObjectName = nil
			}
			var selectedFields []shared.SelectedFieldInfo = []shared.SelectedFieldInfo{}
			for _, selectedFieldsItem := range streamsItem.SelectedFields {
				var fieldPath []string = []string{}
				for _, fieldPathItem := range selectedFieldsItem.FieldPath {
					fieldPath = append(fieldPath, fieldPathItem.ValueString())
				}
				selectedFields = append(selectedFields, shared.SelectedFieldInfo{
					FieldPath: fieldPath,
				})
			}
			var mappers []shared.ConfiguredStreamMapper = []shared.ConfiguredStreamMapper{}
			for _, mappersItem := range streamsItem.Mappers {
				id := new(string)
				if !mappersItem.ID.IsUnknown() && !mappersItem.ID.IsNull() {
					*id = mappersItem.ID.ValueString()
				} else {
					id = nil
				}
				typeVar := shared.StreamMapperType(mappersItem.Type.ValueString())
				var mapperConfiguration shared.MapperConfiguration
				var hashing *shared.Hashing
				if mappersItem.MapperConfiguration.Hashing != nil {
					var fieldNameSuffix string
					fieldNameSuffix = mappersItem.MapperConfiguration.Hashing.FieldNameSuffix.ValueString()

					method := shared.HashingMethod(mappersItem.MapperConfiguration.Hashing.Method.ValueString())
					var targetField string
					targetField = mappersItem.MapperConfiguration.Hashing.TargetField.ValueString()

					hashing = &shared.Hashing{
						FieldNameSuffix: fieldNameSuffix,
						Method:          method,
						TargetField:     targetField,
					}
				}
				if hashing != nil {
					mapperConfiguration = shared.MapperConfiguration{
						Hashing: hashing,
					}
				}
				var fieldRenaming *shared.FieldRenaming
				if mappersItem.MapperConfiguration.FieldRenaming != nil {
					var newFieldName string
					newFieldName = mappersItem.MapperConfiguration.FieldRenaming.NewFieldName.ValueString()

					var originalFieldName string
					originalFieldName = mappersItem.MapperConfiguration.FieldRenaming.OriginalFieldName.ValueString()

					fieldRenaming = &shared.FieldRenaming{
						NewFieldName:      newFieldName,
						OriginalFieldName: originalFieldName,
					}
				}
				if fieldRenaming != nil {
					mapperConfiguration = shared.MapperConfiguration{
						FieldRenaming: fieldRenaming,
					}
				}
				var rowFiltering *shared.RowFiltering
				if mappersItem.MapperConfiguration.RowFiltering != nil {
					var conditions interface{}
					_ = json.Unmarshal([]byte(mappersItem.MapperConfiguration.RowFiltering.Conditions.ValueString()), &conditions)
					rowFiltering = &shared.RowFiltering{
						Conditions: conditions,
					}
				}
				if rowFiltering != nil {
					mapperConfiguration = shared.MapperConfiguration{
						RowFiltering: rowFiltering,
					}
				}
				var encryption *shared.Encryption
				if mappersItem.MapperConfiguration.Encryption != nil {
					var encryptionRSA *shared.EncryptionRSA
					if mappersItem.MapperConfiguration.Encryption.Rsa != nil {
						algorithm := shared.EncryptionMapperAlgorithm(mappersItem.MapperConfiguration.Encryption.Rsa.Algorithm.ValueString())
						var fieldNameSuffix1 string
						fieldNameSuffix1 = mappersItem.MapperConfiguration.Encryption.Rsa.FieldNameSuffix.ValueString()

						var publicKey string
						publicKey = mappersItem.MapperConfiguration.Encryption.Rsa.PublicKey.ValueString()

						var targetField1 string
						targetField1 = mappersItem.MapperConfiguration.Encryption.Rsa.TargetField.ValueString()

						encryptionRSA = &shared.EncryptionRSA{
							Algorithm:       algorithm,
							FieldNameSuffix: fieldNameSuffix1,
							PublicKey:       publicKey,
							TargetField:     targetField1,
						}
					}
					if encryptionRSA != nil {
						encryption = &shared.Encryption{
							EncryptionRSA: encryptionRSA,
						}
					}
					var encryptionAES *shared.EncryptionAES
					if mappersItem.MapperConfiguration.Encryption.Aes != nil {
						algorithm1 := shared.EncryptionMapperAlgorithm(mappersItem.MapperConfiguration.Encryption.Aes.Algorithm.ValueString())
						var fieldNameSuffix2 string
						fieldNameSuffix2 = mappersItem.MapperConfiguration.Encryption.Aes.FieldNameSuffix.ValueString()

						var key string
						key = mappersItem.MapperConfiguration.Encryption.Aes.Key.ValueString()

						mode := shared.Mode(mappersItem.MapperConfiguration.Encryption.Aes.Mode.ValueString())
						padding := shared.Padding(mappersItem.MapperConfiguration.Encryption.Aes.Padding.ValueString())
						var targetField2 string
						targetField2 = mappersItem.MapperConfiguration.Encryption.Aes.TargetField.ValueString()

						encryptionAES = &shared.EncryptionAES{
							Algorithm:       algorithm1,
							FieldNameSuffix: fieldNameSuffix2,
							Key:             key,
							Mode:            mode,
							Padding:         padding,
							TargetField:     targetField2,
						}
					}
					if encryptionAES != nil {
						encryption = &shared.Encryption{
							EncryptionAES: encryptionAES,
						}
					}
				}
				if encryption != nil {
					mapperConfiguration = shared.MapperConfiguration{
						Encryption: encryption,
					}
				}
				mappers = append(mappers, shared.ConfiguredStreamMapper{
					ID:                  id,
					Type:                typeVar,
					MapperConfiguration: mapperConfiguration,
				})
			}
			streams = append(streams, shared.StreamConfiguration{
				Name:                  name1,
				Namespace:             namespace,
				SyncMode:              syncMode,
				CursorField:           cursorField,
				PrimaryKey:            primaryKey,
				IncludeFiles:          includeFiles,
				DestinationObjectName: destinationObjectName,
				SelectedFields:        selectedFields,
				Mappers:               mappers,
			})
		}
		configurations = &shared.StreamConfigurations{
			Streams: streams,
		}
	}
	var schedule *shared.AirbyteAPIConnectionSchedule
	if r.Schedule != nil {
		scheduleType := shared.ScheduleTypeEnum(r.Schedule.ScheduleType.ValueString())
		cronExpression := new(string)
		if !r.Schedule.CronExpression.IsUnknown() && !r.Schedule.CronExpression.IsNull() {
			*cronExpression = r.Schedule.CronExpression.ValueString()
		} else {
			cronExpression = nil
		}
		schedule = &shared.AirbyteAPIConnectionSchedule{
			ScheduleType:   scheduleType,
			CronExpression: cronExpression,
		}
	}
	dataResidency := new(string)
	if !r.DataResidency.IsUnknown() && !r.DataResidency.IsNull() {
		*dataResidency = r.DataResidency.ValueString()
	} else {
		dataResidency = nil
	}
	namespaceDefinition := new(shared.NamespaceDefinitionEnum)
	if !r.NamespaceDefinition.IsUnknown() && !r.NamespaceDefinition.IsNull() {
		*namespaceDefinition = shared.NamespaceDefinitionEnum(r.NamespaceDefinition.ValueString())
	} else {
		namespaceDefinition = nil
	}
	namespaceFormat := new(string)
	if !r.NamespaceFormat.IsUnknown() && !r.NamespaceFormat.IsNull() {
		*namespaceFormat = r.NamespaceFormat.ValueString()
	} else {
		namespaceFormat = nil
	}
	prefix := new(string)
	if !r.Prefix.IsUnknown() && !r.Prefix.IsNull() {
		*prefix = r.Prefix.ValueString()
	} else {
		prefix = nil
	}
	nonBreakingSchemaUpdatesBehavior := new(shared.NonBreakingSchemaUpdatesBehaviorEnum)
	if !r.NonBreakingSchemaUpdatesBehavior.IsUnknown() && !r.NonBreakingSchemaUpdatesBehavior.IsNull() {
		*nonBreakingSchemaUpdatesBehavior = shared.NonBreakingSchemaUpdatesBehaviorEnum(r.NonBreakingSchemaUpdatesBehavior.ValueString())
	} else {
		nonBreakingSchemaUpdatesBehavior = nil
	}
	status := new(shared.ConnectionStatusEnum)
	if !r.Status.IsUnknown() && !r.Status.IsNull() {
		*status = shared.ConnectionStatusEnum(r.Status.ValueString())
	} else {
		status = nil
	}
	var tags []shared.Tag = []shared.Tag{}
	for _, tagsItem := range r.Tags {
		var tagID string
		tagID = tagsItem.TagID.ValueString()

		var workspaceID string
		workspaceID = tagsItem.WorkspaceID.ValueString()

		var name2 string
		name2 = tagsItem.Name.ValueString()

		var color string
		color = tagsItem.Color.ValueString()

		tags = append(tags, shared.Tag{
			TagID:       tagID,
			WorkspaceID: workspaceID,
			Name:        name2,
			Color:       color,
		})
	}
	out := shared.ConnectionCreateRequest{
		Name:                             name,
		SourceID:                         sourceID,
		DestinationID:                    destinationID,
		Configurations:                   configurations,
		Schedule:                         schedule,
		DataResidency:                    dataResidency,
		NamespaceDefinition:              namespaceDefinition,
		NamespaceFormat:                  namespaceFormat,
		Prefix:                           prefix,
		NonBreakingSchemaUpdatesBehavior: nonBreakingSchemaUpdatesBehavior,
		Status:                           status,
		Tags:                             tags,
	}
	return &out
}

func (r *ConnectionResourceModel) RefreshFromSharedConnectionResponse(resp *shared.ConnectionResponse) {
	if resp != nil {
		if r.Configurations == nil {
			r.Configurations = &tfTypes.StreamConfigurations{}
		}
		r.Configurations.Streams = []tfTypes.StreamConfiguration{}
		if len(r.Configurations.Streams) > len(resp.Configurations.Streams) {
			r.Configurations.Streams = r.Configurations.Streams[:len(resp.Configurations.Streams)]
		}
		for streamsCount, streamsItem := range resp.Configurations.Streams {
			var streams1 tfTypes.StreamConfiguration
			streams1.CursorField = make([]types.String, 0, len(streamsItem.CursorField))
			for _, v := range streamsItem.CursorField {
				streams1.CursorField = append(streams1.CursorField, types.StringValue(v))
			}
			streams1.DestinationObjectName = types.StringPointerValue(streamsItem.DestinationObjectName)
			streams1.IncludeFiles = types.BoolPointerValue(streamsItem.IncludeFiles)
			streams1.Mappers = []tfTypes.ConfiguredStreamMapper{}
			for mappersCount, mappersItem := range streamsItem.Mappers {
				var mappers1 tfTypes.ConfiguredStreamMapper
				mappers1.ID = types.StringPointerValue(mappersItem.ID)
				if mappersItem.MapperConfiguration.Encryption != nil {
					mappers1.MapperConfiguration.Encryption = &tfTypes.Encryption{}
					if mappersItem.MapperConfiguration.Encryption.EncryptionAES != nil {
						mappers1.MapperConfiguration.Encryption.Aes = &tfTypes.EncryptionAES{}
						mappers1.MapperConfiguration.Encryption.Aes.Algorithm = types.StringValue(string(mappersItem.MapperConfiguration.Encryption.EncryptionAES.Algorithm))
						mappers1.MapperConfiguration.Encryption.Aes.FieldNameSuffix = types.StringValue(mappersItem.MapperConfiguration.Encryption.EncryptionAES.FieldNameSuffix)
						mappers1.MapperConfiguration.Encryption.Aes.Key = types.StringValue(mappersItem.MapperConfiguration.Encryption.EncryptionAES.Key)
						mappers1.MapperConfiguration.Encryption.Aes.Mode = types.StringValue(string(mappersItem.MapperConfiguration.Encryption.EncryptionAES.Mode))
						mappers1.MapperConfiguration.Encryption.Aes.Padding = types.StringValue(string(mappersItem.MapperConfiguration.Encryption.EncryptionAES.Padding))
						mappers1.MapperConfiguration.Encryption.Aes.TargetField = types.StringValue(mappersItem.MapperConfiguration.Encryption.EncryptionAES.TargetField)
					}
					if mappersItem.MapperConfiguration.Encryption.EncryptionRSA != nil {
						mappers1.MapperConfiguration.Encryption.Rsa = &tfTypes.EncryptionRSA{}
						mappers1.MapperConfiguration.Encryption.Rsa.Algorithm = types.StringValue(string(mappersItem.MapperConfiguration.Encryption.EncryptionRSA.Algorithm))
						mappers1.MapperConfiguration.Encryption.Rsa.FieldNameSuffix = types.StringValue(mappersItem.MapperConfiguration.Encryption.EncryptionRSA.FieldNameSuffix)
						mappers1.MapperConfiguration.Encryption.Rsa.PublicKey = types.StringValue(mappersItem.MapperConfiguration.Encryption.EncryptionRSA.PublicKey)
						mappers1.MapperConfiguration.Encryption.Rsa.TargetField = types.StringValue(mappersItem.MapperConfiguration.Encryption.EncryptionRSA.TargetField)
					}
				}
				if mappersItem.MapperConfiguration.FieldRenaming != nil {
					mappers1.MapperConfiguration.FieldRenaming = &tfTypes.FieldRenaming{}
					mappers1.MapperConfiguration.FieldRenaming.NewFieldName = types.StringValue(mappersItem.MapperConfiguration.FieldRenaming.NewFieldName)
					mappers1.MapperConfiguration.FieldRenaming.OriginalFieldName = types.StringValue(mappersItem.MapperConfiguration.FieldRenaming.OriginalFieldName)
				}
				if mappersItem.MapperConfiguration.Hashing != nil {
					mappers1.MapperConfiguration.Hashing = &tfTypes.Hashing{}
					mappers1.MapperConfiguration.Hashing.FieldNameSuffix = types.StringValue(mappersItem.MapperConfiguration.Hashing.FieldNameSuffix)
					mappers1.MapperConfiguration.Hashing.Method = types.StringValue(string(mappersItem.MapperConfiguration.Hashing.Method))
					mappers1.MapperConfiguration.Hashing.TargetField = types.StringValue(mappersItem.MapperConfiguration.Hashing.TargetField)
				}
				if mappersItem.MapperConfiguration.RowFiltering != nil {
					mappers1.MapperConfiguration.RowFiltering = &tfTypes.RowFiltering{}
					conditionsResult, _ := json.Marshal(mappersItem.MapperConfiguration.RowFiltering.Conditions)
					mappers1.MapperConfiguration.RowFiltering.Conditions = types.StringValue(string(conditionsResult))
				}
				mappers1.Type = types.StringValue(string(mappersItem.Type))
				if mappersCount+1 > len(streams1.Mappers) {
					streams1.Mappers = append(streams1.Mappers, mappers1)
				} else {
					streams1.Mappers[mappersCount].ID = mappers1.ID
					streams1.Mappers[mappersCount].MapperConfiguration = mappers1.MapperConfiguration
					streams1.Mappers[mappersCount].Type = mappers1.Type
				}
			}
			streams1.Name = types.StringValue(streamsItem.Name)
			streams1.Namespace = types.StringPointerValue(streamsItem.Namespace)
			streams1.PrimaryKey = nil
			for _, primaryKeyItem := range streamsItem.PrimaryKey {
				var primaryKey1 []types.String
				primaryKey1 = make([]types.String, 0, len(primaryKeyItem))
				for _, v := range primaryKeyItem {
					primaryKey1 = append(primaryKey1, types.StringValue(v))
				}
				streams1.PrimaryKey = append(streams1.PrimaryKey, primaryKey1)
			}
			streams1.SelectedFields = []tfTypes.SelectedFieldInfo{}
			for selectedFieldsCount, selectedFieldsItem := range streamsItem.SelectedFields {
				var selectedFields1 tfTypes.SelectedFieldInfo
				selectedFields1.FieldPath = make([]types.String, 0, len(selectedFieldsItem.FieldPath))
				for _, v := range selectedFieldsItem.FieldPath {
					selectedFields1.FieldPath = append(selectedFields1.FieldPath, types.StringValue(v))
				}
				if selectedFieldsCount+1 > len(streams1.SelectedFields) {
					streams1.SelectedFields = append(streams1.SelectedFields, selectedFields1)
				} else {
					streams1.SelectedFields[selectedFieldsCount].FieldPath = selectedFields1.FieldPath
				}
			}
			if streamsItem.SyncMode != nil {
				streams1.SyncMode = types.StringValue(string(*streamsItem.SyncMode))
			} else {
				streams1.SyncMode = types.StringNull()
			}
			if streamsCount+1 > len(r.Configurations.Streams) {
				r.Configurations.Streams = append(r.Configurations.Streams, streams1)
			} else {
				r.Configurations.Streams[streamsCount].CursorField = streams1.CursorField
				r.Configurations.Streams[streamsCount].DestinationObjectName = streams1.DestinationObjectName
				r.Configurations.Streams[streamsCount].IncludeFiles = streams1.IncludeFiles
				r.Configurations.Streams[streamsCount].Mappers = streams1.Mappers
				r.Configurations.Streams[streamsCount].Name = streams1.Name
				r.Configurations.Streams[streamsCount].Namespace = streams1.Namespace
				r.Configurations.Streams[streamsCount].PrimaryKey = streams1.PrimaryKey
				r.Configurations.Streams[streamsCount].SelectedFields = streams1.SelectedFields
				r.Configurations.Streams[streamsCount].SyncMode = streams1.SyncMode
			}
		}
		r.ConnectionID = types.StringValue(resp.ConnectionID)
		r.CreatedAt = types.Int64Value(resp.CreatedAt)
		r.DestinationID = types.StringValue(resp.DestinationID)
		r.Name = types.StringValue(resp.Name)
		if resp.NamespaceDefinition != nil {
			r.NamespaceDefinition = types.StringValue(string(*resp.NamespaceDefinition))
		} else {
			r.NamespaceDefinition = types.StringNull()
		}
		r.NamespaceFormat = types.StringPointerValue(resp.NamespaceFormat)
		if resp.NonBreakingSchemaUpdatesBehavior != nil {
			r.NonBreakingSchemaUpdatesBehavior = types.StringValue(string(*resp.NonBreakingSchemaUpdatesBehavior))
		} else {
			r.NonBreakingSchemaUpdatesBehavior = types.StringNull()
		}
		r.Prefix = types.StringPointerValue(resp.Prefix)
		if r.Schedule == nil {
			r.Schedule = &tfTypes.AirbyteAPIConnectionSchedule{}
		}
		r.Schedule.BasicTiming = types.StringPointerValue(resp.Schedule.BasicTiming)
		r.Schedule.CronExpression = types.StringPointerValue(resp.Schedule.CronExpression)
		r.Schedule.ScheduleType = types.StringValue(string(resp.Schedule.ScheduleType))
		r.SourceID = types.StringValue(resp.SourceID)
		r.Status = types.StringValue(string(resp.Status))
		r.Tags = []tfTypes.Tag{}
		if len(r.Tags) > len(resp.Tags) {
			r.Tags = r.Tags[:len(resp.Tags)]
		}
		for tagsCount, tagsItem := range resp.Tags {
			var tags1 tfTypes.Tag
			tags1.Color = types.StringValue(tagsItem.Color)
			tags1.Name = types.StringValue(tagsItem.Name)
			tags1.TagID = types.StringValue(tagsItem.TagID)
			tags1.WorkspaceID = types.StringValue(tagsItem.WorkspaceID)
			if tagsCount+1 > len(r.Tags) {
				r.Tags = append(r.Tags, tags1)
			} else {
				r.Tags[tagsCount].Color = tags1.Color
				r.Tags[tagsCount].Name = tags1.Name
				r.Tags[tagsCount].TagID = tags1.TagID
				r.Tags[tagsCount].WorkspaceID = tags1.WorkspaceID
			}
		}
		r.WorkspaceID = types.StringValue(resp.WorkspaceID)
	}
}

func (r *ConnectionResourceModel) ToSharedConnectionPatchRequest() *shared.ConnectionPatchRequest {
	name := new(string)
	if !r.Name.IsUnknown() && !r.Name.IsNull() {
		*name = r.Name.ValueString()
	} else {
		name = nil
	}
	var configurations *shared.StreamConfigurations
	if r.Configurations != nil {
		var streams []shared.StreamConfiguration = []shared.StreamConfiguration{}
		for _, streamsItem := range r.Configurations.Streams {
			var name1 string
			name1 = streamsItem.Name.ValueString()

			namespace := new(string)
			if !streamsItem.Namespace.IsUnknown() && !streamsItem.Namespace.IsNull() {
				*namespace = streamsItem.Namespace.ValueString()
			} else {
				namespace = nil
			}
			syncMode := new(shared.ConnectionSyncModeEnum)
			if !streamsItem.SyncMode.IsUnknown() && !streamsItem.SyncMode.IsNull() {
				*syncMode = shared.ConnectionSyncModeEnum(streamsItem.SyncMode.ValueString())
			} else {
				syncMode = nil
			}
			var cursorField []string = []string{}
			for _, cursorFieldItem := range streamsItem.CursorField {
				cursorField = append(cursorField, cursorFieldItem.ValueString())
			}
			var primaryKey [][]string = [][]string{}
			for _, primaryKeyItem := range streamsItem.PrimaryKey {
				var primaryKeyTmp []string = []string{}
				for _, item := range primaryKeyItem {
					primaryKeyTmp = append(primaryKeyTmp, item.ValueString())
				}
				primaryKey = append(primaryKey, primaryKeyTmp)
			}
			includeFiles := new(bool)
			if !streamsItem.IncludeFiles.IsUnknown() && !streamsItem.IncludeFiles.IsNull() {
				*includeFiles = streamsItem.IncludeFiles.ValueBool()
			} else {
				includeFiles = nil
			}
			destinationObjectName := new(string)
			if !streamsItem.DestinationObjectName.IsUnknown() && !streamsItem.DestinationObjectName.IsNull() {
				*destinationObjectName = streamsItem.DestinationObjectName.ValueString()
			} else {
				destinationObjectName = nil
			}
			var selectedFields []shared.SelectedFieldInfo = []shared.SelectedFieldInfo{}
			for _, selectedFieldsItem := range streamsItem.SelectedFields {
				var fieldPath []string = []string{}
				for _, fieldPathItem := range selectedFieldsItem.FieldPath {
					fieldPath = append(fieldPath, fieldPathItem.ValueString())
				}
				selectedFields = append(selectedFields, shared.SelectedFieldInfo{
					FieldPath: fieldPath,
				})
			}
			var mappers []shared.ConfiguredStreamMapper = []shared.ConfiguredStreamMapper{}
			for _, mappersItem := range streamsItem.Mappers {
				id := new(string)
				if !mappersItem.ID.IsUnknown() && !mappersItem.ID.IsNull() {
					*id = mappersItem.ID.ValueString()
				} else {
					id = nil
				}
				typeVar := shared.StreamMapperType(mappersItem.Type.ValueString())
				var mapperConfiguration shared.MapperConfiguration
				var hashing *shared.Hashing
				if mappersItem.MapperConfiguration.Hashing != nil {
					var fieldNameSuffix string
					fieldNameSuffix = mappersItem.MapperConfiguration.Hashing.FieldNameSuffix.ValueString()

					method := shared.HashingMethod(mappersItem.MapperConfiguration.Hashing.Method.ValueString())
					var targetField string
					targetField = mappersItem.MapperConfiguration.Hashing.TargetField.ValueString()

					hashing = &shared.Hashing{
						FieldNameSuffix: fieldNameSuffix,
						Method:          method,
						TargetField:     targetField,
					}
				}
				if hashing != nil {
					mapperConfiguration = shared.MapperConfiguration{
						Hashing: hashing,
					}
				}
				var fieldRenaming *shared.FieldRenaming
				if mappersItem.MapperConfiguration.FieldRenaming != nil {
					var newFieldName string
					newFieldName = mappersItem.MapperConfiguration.FieldRenaming.NewFieldName.ValueString()

					var originalFieldName string
					originalFieldName = mappersItem.MapperConfiguration.FieldRenaming.OriginalFieldName.ValueString()

					fieldRenaming = &shared.FieldRenaming{
						NewFieldName:      newFieldName,
						OriginalFieldName: originalFieldName,
					}
				}
				if fieldRenaming != nil {
					mapperConfiguration = shared.MapperConfiguration{
						FieldRenaming: fieldRenaming,
					}
				}
				var rowFiltering *shared.RowFiltering
				if mappersItem.MapperConfiguration.RowFiltering != nil {
					var conditions interface{}
					_ = json.Unmarshal([]byte(mappersItem.MapperConfiguration.RowFiltering.Conditions.ValueString()), &conditions)
					rowFiltering = &shared.RowFiltering{
						Conditions: conditions,
					}
				}
				if rowFiltering != nil {
					mapperConfiguration = shared.MapperConfiguration{
						RowFiltering: rowFiltering,
					}
				}
				var encryption *shared.Encryption
				if mappersItem.MapperConfiguration.Encryption != nil {
					var encryptionRSA *shared.EncryptionRSA
					if mappersItem.MapperConfiguration.Encryption.Rsa != nil {
						algorithm := shared.EncryptionMapperAlgorithm(mappersItem.MapperConfiguration.Encryption.Rsa.Algorithm.ValueString())
						var fieldNameSuffix1 string
						fieldNameSuffix1 = mappersItem.MapperConfiguration.Encryption.Rsa.FieldNameSuffix.ValueString()

						var publicKey string
						publicKey = mappersItem.MapperConfiguration.Encryption.Rsa.PublicKey.ValueString()

						var targetField1 string
						targetField1 = mappersItem.MapperConfiguration.Encryption.Rsa.TargetField.ValueString()

						encryptionRSA = &shared.EncryptionRSA{
							Algorithm:       algorithm,
							FieldNameSuffix: fieldNameSuffix1,
							PublicKey:       publicKey,
							TargetField:     targetField1,
						}
					}
					if encryptionRSA != nil {
						encryption = &shared.Encryption{
							EncryptionRSA: encryptionRSA,
						}
					}
					var encryptionAES *shared.EncryptionAES
					if mappersItem.MapperConfiguration.Encryption.Aes != nil {
						algorithm1 := shared.EncryptionMapperAlgorithm(mappersItem.MapperConfiguration.Encryption.Aes.Algorithm.ValueString())
						var fieldNameSuffix2 string
						fieldNameSuffix2 = mappersItem.MapperConfiguration.Encryption.Aes.FieldNameSuffix.ValueString()

						var key string
						key = mappersItem.MapperConfiguration.Encryption.Aes.Key.ValueString()

						mode := shared.Mode(mappersItem.MapperConfiguration.Encryption.Aes.Mode.ValueString())
						padding := shared.Padding(mappersItem.MapperConfiguration.Encryption.Aes.Padding.ValueString())
						var targetField2 string
						targetField2 = mappersItem.MapperConfiguration.Encryption.Aes.TargetField.ValueString()

						encryptionAES = &shared.EncryptionAES{
							Algorithm:       algorithm1,
							FieldNameSuffix: fieldNameSuffix2,
							Key:             key,
							Mode:            mode,
							Padding:         padding,
							TargetField:     targetField2,
						}
					}
					if encryptionAES != nil {
						encryption = &shared.Encryption{
							EncryptionAES: encryptionAES,
						}
					}
				}
				if encryption != nil {
					mapperConfiguration = shared.MapperConfiguration{
						Encryption: encryption,
					}
				}
				mappers = append(mappers, shared.ConfiguredStreamMapper{
					ID:                  id,
					Type:                typeVar,
					MapperConfiguration: mapperConfiguration,
				})
			}
			streams = append(streams, shared.StreamConfiguration{
				Name:                  name1,
				Namespace:             namespace,
				SyncMode:              syncMode,
				CursorField:           cursorField,
				PrimaryKey:            primaryKey,
				IncludeFiles:          includeFiles,
				DestinationObjectName: destinationObjectName,
				SelectedFields:        selectedFields,
				Mappers:               mappers,
			})
		}
		configurations = &shared.StreamConfigurations{
			Streams: streams,
		}
	}
	var schedule *shared.AirbyteAPIConnectionSchedule
	if r.Schedule != nil {
		scheduleType := shared.ScheduleTypeEnum(r.Schedule.ScheduleType.ValueString())
		cronExpression := new(string)
		if !r.Schedule.CronExpression.IsUnknown() && !r.Schedule.CronExpression.IsNull() {
			*cronExpression = r.Schedule.CronExpression.ValueString()
		} else {
			cronExpression = nil
		}
		schedule = &shared.AirbyteAPIConnectionSchedule{
			ScheduleType:   scheduleType,
			CronExpression: cronExpression,
		}
	}
	dataResidency := new(string)
	if !r.DataResidency.IsUnknown() && !r.DataResidency.IsNull() {
		*dataResidency = r.DataResidency.ValueString()
	} else {
		dataResidency = nil
	}
	namespaceDefinition := new(shared.NamespaceDefinitionEnumNoDefault)
	if !r.NamespaceDefinition.IsUnknown() && !r.NamespaceDefinition.IsNull() {
		*namespaceDefinition = shared.NamespaceDefinitionEnumNoDefault(r.NamespaceDefinition.ValueString())
	} else {
		namespaceDefinition = nil
	}
	namespaceFormat := new(string)
	if !r.NamespaceFormat.IsUnknown() && !r.NamespaceFormat.IsNull() {
		*namespaceFormat = r.NamespaceFormat.ValueString()
	} else {
		namespaceFormat = nil
	}
	prefix := new(string)
	if !r.Prefix.IsUnknown() && !r.Prefix.IsNull() {
		*prefix = r.Prefix.ValueString()
	} else {
		prefix = nil
	}
	nonBreakingSchemaUpdatesBehavior := new(shared.NonBreakingSchemaUpdatesBehaviorEnumNoDefault)
	if !r.NonBreakingSchemaUpdatesBehavior.IsUnknown() && !r.NonBreakingSchemaUpdatesBehavior.IsNull() {
		*nonBreakingSchemaUpdatesBehavior = shared.NonBreakingSchemaUpdatesBehaviorEnumNoDefault(r.NonBreakingSchemaUpdatesBehavior.ValueString())
	} else {
		nonBreakingSchemaUpdatesBehavior = nil
	}
	status := new(shared.ConnectionStatusEnum)
	if !r.Status.IsUnknown() && !r.Status.IsNull() {
		*status = shared.ConnectionStatusEnum(r.Status.ValueString())
	} else {
		status = nil
	}
	var tags []shared.Tag = []shared.Tag{}
	for _, tagsItem := range r.Tags {
		var tagID string
		tagID = tagsItem.TagID.ValueString()

		var workspaceID string
		workspaceID = tagsItem.WorkspaceID.ValueString()

		var name2 string
		name2 = tagsItem.Name.ValueString()

		var color string
		color = tagsItem.Color.ValueString()

		tags = append(tags, shared.Tag{
			TagID:       tagID,
			WorkspaceID: workspaceID,
			Name:        name2,
			Color:       color,
		})
	}
	out := shared.ConnectionPatchRequest{
		Name:                             name,
		Configurations:                   configurations,
		Schedule:                         schedule,
		DataResidency:                    dataResidency,
		NamespaceDefinition:              namespaceDefinition,
		NamespaceFormat:                  namespaceFormat,
		Prefix:                           prefix,
		NonBreakingSchemaUpdatesBehavior: nonBreakingSchemaUpdatesBehavior,
		Status:                           status,
		Tags:                             tags,
	}
	return &out
}
