// 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 *SourcePostgresResourceModel) ToSharedSourcePostgresCreateRequest() *shared.SourcePostgresCreateRequest {
	var name string
	name = r.Name.ValueString()

	definitionID := new(string)
	if !r.DefinitionID.IsUnknown() && !r.DefinitionID.IsNull() {
		*definitionID = r.DefinitionID.ValueString()
	} else {
		definitionID = nil
	}
	var workspaceID string
	workspaceID = r.WorkspaceID.ValueString()

	var database string
	database = r.Configuration.Database.ValueString()

	var host string
	host = r.Configuration.Host.ValueString()

	jdbcURLParams := new(string)
	if !r.Configuration.JdbcURLParams.IsUnknown() && !r.Configuration.JdbcURLParams.IsNull() {
		*jdbcURLParams = r.Configuration.JdbcURLParams.ValueString()
	} else {
		jdbcURLParams = nil
	}
	password := new(string)
	if !r.Configuration.Password.IsUnknown() && !r.Configuration.Password.IsNull() {
		*password = r.Configuration.Password.ValueString()
	} else {
		password = nil
	}
	port := new(int64)
	if !r.Configuration.Port.IsUnknown() && !r.Configuration.Port.IsNull() {
		*port = r.Configuration.Port.ValueInt64()
	} else {
		port = nil
	}
	var replicationMethod *shared.SourcePostgresUpdateMethod
	if r.Configuration.ReplicationMethod != nil {
		var readChangesUsingWriteAheadLogCDC *shared.ReadChangesUsingWriteAheadLogCDC
		if r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC != nil {
			heartbeatActionQuery := new(string)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.HeartbeatActionQuery.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.HeartbeatActionQuery.IsNull() {
				*heartbeatActionQuery = r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.HeartbeatActionQuery.ValueString()
			} else {
				heartbeatActionQuery = nil
			}
			initialLoadTimeoutHours := new(int64)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InitialLoadTimeoutHours.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InitialLoadTimeoutHours.IsNull() {
				*initialLoadTimeoutHours = r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InitialLoadTimeoutHours.ValueInt64()
			} else {
				initialLoadTimeoutHours = nil
			}
			initialWaitingSeconds := new(int64)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InitialWaitingSeconds.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InitialWaitingSeconds.IsNull() {
				*initialWaitingSeconds = r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InitialWaitingSeconds.ValueInt64()
			} else {
				initialWaitingSeconds = nil
			}
			invalidCdcCursorPositionBehavior := new(shared.SourcePostgresInvalidCDCPositionBehaviorAdvanced)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InvalidCdcCursorPositionBehavior.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InvalidCdcCursorPositionBehavior.IsNull() {
				*invalidCdcCursorPositionBehavior = shared.SourcePostgresInvalidCDCPositionBehaviorAdvanced(r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InvalidCdcCursorPositionBehavior.ValueString())
			} else {
				invalidCdcCursorPositionBehavior = nil
			}
			lsnCommitBehaviour := new(shared.LSNCommitBehaviour)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.LsnCommitBehaviour.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.LsnCommitBehaviour.IsNull() {
				*lsnCommitBehaviour = shared.LSNCommitBehaviour(r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.LsnCommitBehaviour.ValueString())
			} else {
				lsnCommitBehaviour = nil
			}
			plugin := new(shared.Plugin)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.Plugin.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.Plugin.IsNull() {
				*plugin = shared.Plugin(r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.Plugin.ValueString())
			} else {
				plugin = nil
			}
			var publication string
			publication = r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.Publication.ValueString()

			queueSize := new(int64)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.QueueSize.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.QueueSize.IsNull() {
				*queueSize = r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.QueueSize.ValueInt64()
			} else {
				queueSize = nil
			}
			var replicationSlot string
			replicationSlot = r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.ReplicationSlot.ValueString()

			var additionalProperties interface{}
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.AdditionalProperties.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.AdditionalProperties.ValueString()), &additionalProperties)
			}
			readChangesUsingWriteAheadLogCDC = &shared.ReadChangesUsingWriteAheadLogCDC{
				HeartbeatActionQuery:             heartbeatActionQuery,
				InitialLoadTimeoutHours:          initialLoadTimeoutHours,
				InitialWaitingSeconds:            initialWaitingSeconds,
				InvalidCdcCursorPositionBehavior: invalidCdcCursorPositionBehavior,
				LsnCommitBehaviour:               lsnCommitBehaviour,
				Plugin:                           plugin,
				Publication:                      publication,
				QueueSize:                        queueSize,
				ReplicationSlot:                  replicationSlot,
				AdditionalProperties:             additionalProperties,
			}
		}
		if readChangesUsingWriteAheadLogCDC != nil {
			replicationMethod = &shared.SourcePostgresUpdateMethod{
				ReadChangesUsingWriteAheadLogCDC: readChangesUsingWriteAheadLogCDC,
			}
		}
		var detectChangesWithXminSystemColumn *shared.DetectChangesWithXminSystemColumn
		if r.Configuration.ReplicationMethod.DetectChangesWithXminSystemColumn != nil {
			detectChangesWithXminSystemColumn = &shared.DetectChangesWithXminSystemColumn{}
		}
		if detectChangesWithXminSystemColumn != nil {
			replicationMethod = &shared.SourcePostgresUpdateMethod{
				DetectChangesWithXminSystemColumn: detectChangesWithXminSystemColumn,
			}
		}
		var sourcePostgresScanChangesWithUserDefinedCursor *shared.SourcePostgresScanChangesWithUserDefinedCursor
		if r.Configuration.ReplicationMethod.ScanChangesWithUserDefinedCursor != nil {
			sourcePostgresScanChangesWithUserDefinedCursor = &shared.SourcePostgresScanChangesWithUserDefinedCursor{}
		}
		if sourcePostgresScanChangesWithUserDefinedCursor != nil {
			replicationMethod = &shared.SourcePostgresUpdateMethod{
				SourcePostgresScanChangesWithUserDefinedCursor: sourcePostgresScanChangesWithUserDefinedCursor,
			}
		}
	}
	var schemas []string = []string{}
	for _, schemasItem := range r.Configuration.Schemas {
		schemas = append(schemas, schemasItem.ValueString())
	}
	var sslMode *shared.SSLModes
	if r.Configuration.SslMode != nil {
		var disable *shared.Disable
		if r.Configuration.SslMode.Disable != nil {
			var additionalProperties1 interface{}
			if !r.Configuration.SslMode.Disable.AdditionalProperties.IsUnknown() && !r.Configuration.SslMode.Disable.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.SslMode.Disable.AdditionalProperties.ValueString()), &additionalProperties1)
			}
			disable = &shared.Disable{
				AdditionalProperties: additionalProperties1,
			}
		}
		if disable != nil {
			sslMode = &shared.SSLModes{
				Disable: disable,
			}
		}
		var allow *shared.Allow
		if r.Configuration.SslMode.Allow != nil {
			var additionalProperties2 interface{}
			if !r.Configuration.SslMode.Allow.AdditionalProperties.IsUnknown() && !r.Configuration.SslMode.Allow.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.SslMode.Allow.AdditionalProperties.ValueString()), &additionalProperties2)
			}
			allow = &shared.Allow{
				AdditionalProperties: additionalProperties2,
			}
		}
		if allow != nil {
			sslMode = &shared.SSLModes{
				Allow: allow,
			}
		}
		var prefer *shared.Prefer
		if r.Configuration.SslMode.Prefer != nil {
			var additionalProperties3 interface{}
			if !r.Configuration.SslMode.Prefer.AdditionalProperties.IsUnknown() && !r.Configuration.SslMode.Prefer.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.SslMode.Prefer.AdditionalProperties.ValueString()), &additionalProperties3)
			}
			prefer = &shared.Prefer{
				AdditionalProperties: additionalProperties3,
			}
		}
		if prefer != nil {
			sslMode = &shared.SSLModes{
				Prefer: prefer,
			}
		}
		var require *shared.Require
		if r.Configuration.SslMode.Require != nil {
			var additionalProperties4 interface{}
			if !r.Configuration.SslMode.Require.AdditionalProperties.IsUnknown() && !r.Configuration.SslMode.Require.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.SslMode.Require.AdditionalProperties.ValueString()), &additionalProperties4)
			}
			require = &shared.Require{
				AdditionalProperties: additionalProperties4,
			}
		}
		if require != nil {
			sslMode = &shared.SSLModes{
				Require: require,
			}
		}
		var sourcePostgresVerifyCa *shared.SourcePostgresVerifyCa
		if r.Configuration.SslMode.VerifyCa != nil {
			var caCertificate string
			caCertificate = r.Configuration.SslMode.VerifyCa.CaCertificate.ValueString()

			clientCertificate := new(string)
			if !r.Configuration.SslMode.VerifyCa.ClientCertificate.IsUnknown() && !r.Configuration.SslMode.VerifyCa.ClientCertificate.IsNull() {
				*clientCertificate = r.Configuration.SslMode.VerifyCa.ClientCertificate.ValueString()
			} else {
				clientCertificate = nil
			}
			clientKey := new(string)
			if !r.Configuration.SslMode.VerifyCa.ClientKey.IsUnknown() && !r.Configuration.SslMode.VerifyCa.ClientKey.IsNull() {
				*clientKey = r.Configuration.SslMode.VerifyCa.ClientKey.ValueString()
			} else {
				clientKey = nil
			}
			clientKeyPassword := new(string)
			if !r.Configuration.SslMode.VerifyCa.ClientKeyPassword.IsUnknown() && !r.Configuration.SslMode.VerifyCa.ClientKeyPassword.IsNull() {
				*clientKeyPassword = r.Configuration.SslMode.VerifyCa.ClientKeyPassword.ValueString()
			} else {
				clientKeyPassword = nil
			}
			var additionalProperties5 interface{}
			if !r.Configuration.SslMode.VerifyCa.AdditionalProperties.IsUnknown() && !r.Configuration.SslMode.VerifyCa.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.SslMode.VerifyCa.AdditionalProperties.ValueString()), &additionalProperties5)
			}
			sourcePostgresVerifyCa = &shared.SourcePostgresVerifyCa{
				CaCertificate:        caCertificate,
				ClientCertificate:    clientCertificate,
				ClientKey:            clientKey,
				ClientKeyPassword:    clientKeyPassword,
				AdditionalProperties: additionalProperties5,
			}
		}
		if sourcePostgresVerifyCa != nil {
			sslMode = &shared.SSLModes{
				SourcePostgresVerifyCa: sourcePostgresVerifyCa,
			}
		}
		var verifyFull *shared.VerifyFull
		if r.Configuration.SslMode.VerifyFull != nil {
			var caCertificate1 string
			caCertificate1 = r.Configuration.SslMode.VerifyFull.CaCertificate.ValueString()

			clientCertificate1 := new(string)
			if !r.Configuration.SslMode.VerifyFull.ClientCertificate.IsUnknown() && !r.Configuration.SslMode.VerifyFull.ClientCertificate.IsNull() {
				*clientCertificate1 = r.Configuration.SslMode.VerifyFull.ClientCertificate.ValueString()
			} else {
				clientCertificate1 = nil
			}
			clientKey1 := new(string)
			if !r.Configuration.SslMode.VerifyFull.ClientKey.IsUnknown() && !r.Configuration.SslMode.VerifyFull.ClientKey.IsNull() {
				*clientKey1 = r.Configuration.SslMode.VerifyFull.ClientKey.ValueString()
			} else {
				clientKey1 = nil
			}
			clientKeyPassword1 := new(string)
			if !r.Configuration.SslMode.VerifyFull.ClientKeyPassword.IsUnknown() && !r.Configuration.SslMode.VerifyFull.ClientKeyPassword.IsNull() {
				*clientKeyPassword1 = r.Configuration.SslMode.VerifyFull.ClientKeyPassword.ValueString()
			} else {
				clientKeyPassword1 = nil
			}
			var additionalProperties6 interface{}
			if !r.Configuration.SslMode.VerifyFull.AdditionalProperties.IsUnknown() && !r.Configuration.SslMode.VerifyFull.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.SslMode.VerifyFull.AdditionalProperties.ValueString()), &additionalProperties6)
			}
			verifyFull = &shared.VerifyFull{
				CaCertificate:        caCertificate1,
				ClientCertificate:    clientCertificate1,
				ClientKey:            clientKey1,
				ClientKeyPassword:    clientKeyPassword1,
				AdditionalProperties: additionalProperties6,
			}
		}
		if verifyFull != nil {
			sslMode = &shared.SSLModes{
				VerifyFull: verifyFull,
			}
		}
	}
	var tunnelMethod *shared.SourcePostgresSSHTunnelMethod
	if r.Configuration.TunnelMethod != nil {
		var sourcePostgresNoTunnel *shared.SourcePostgresNoTunnel
		if r.Configuration.TunnelMethod.NoTunnel != nil {
			sourcePostgresNoTunnel = &shared.SourcePostgresNoTunnel{}
		}
		if sourcePostgresNoTunnel != nil {
			tunnelMethod = &shared.SourcePostgresSSHTunnelMethod{
				SourcePostgresNoTunnel: sourcePostgresNoTunnel,
			}
		}
		var sourcePostgresSSHKeyAuthentication *shared.SourcePostgresSSHKeyAuthentication
		if r.Configuration.TunnelMethod.SSHKeyAuthentication != nil {
			var sshKey string
			sshKey = r.Configuration.TunnelMethod.SSHKeyAuthentication.SSHKey.ValueString()

			var tunnelHost string
			tunnelHost = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelHost.ValueString()

			tunnelPort := new(int64)
			if !r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.IsUnknown() && !r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.IsNull() {
				*tunnelPort = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.ValueInt64()
			} else {
				tunnelPort = nil
			}
			var tunnelUser string
			tunnelUser = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelUser.ValueString()

			sourcePostgresSSHKeyAuthentication = &shared.SourcePostgresSSHKeyAuthentication{
				SSHKey:     sshKey,
				TunnelHost: tunnelHost,
				TunnelPort: tunnelPort,
				TunnelUser: tunnelUser,
			}
		}
		if sourcePostgresSSHKeyAuthentication != nil {
			tunnelMethod = &shared.SourcePostgresSSHTunnelMethod{
				SourcePostgresSSHKeyAuthentication: sourcePostgresSSHKeyAuthentication,
			}
		}
		var sourcePostgresPasswordAuthentication *shared.SourcePostgresPasswordAuthentication
		if r.Configuration.TunnelMethod.PasswordAuthentication != nil {
			var tunnelHost1 string
			tunnelHost1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelHost.ValueString()

			tunnelPort1 := new(int64)
			if !r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.IsUnknown() && !r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.IsNull() {
				*tunnelPort1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.ValueInt64()
			} else {
				tunnelPort1 = nil
			}
			var tunnelUser1 string
			tunnelUser1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelUser.ValueString()

			var tunnelUserPassword string
			tunnelUserPassword = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelUserPassword.ValueString()

			sourcePostgresPasswordAuthentication = &shared.SourcePostgresPasswordAuthentication{
				TunnelHost:         tunnelHost1,
				TunnelPort:         tunnelPort1,
				TunnelUser:         tunnelUser1,
				TunnelUserPassword: tunnelUserPassword,
			}
		}
		if sourcePostgresPasswordAuthentication != nil {
			tunnelMethod = &shared.SourcePostgresSSHTunnelMethod{
				SourcePostgresPasswordAuthentication: sourcePostgresPasswordAuthentication,
			}
		}
	}
	var username string
	username = r.Configuration.Username.ValueString()

	configuration := shared.SourcePostgres{
		Database:          database,
		Host:              host,
		JdbcURLParams:     jdbcURLParams,
		Password:          password,
		Port:              port,
		ReplicationMethod: replicationMethod,
		Schemas:           schemas,
		SslMode:           sslMode,
		TunnelMethod:      tunnelMethod,
		Username:          username,
	}
	secretID := new(string)
	if !r.SecretID.IsUnknown() && !r.SecretID.IsNull() {
		*secretID = r.SecretID.ValueString()
	} else {
		secretID = nil
	}
	out := shared.SourcePostgresCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
		SecretID:      secretID,
	}
	return &out
}

func (r *SourcePostgresResourceModel) RefreshFromSharedSourceResponse(resp *shared.SourceResponse) {
	if resp != nil {
		r.CreatedAt = types.Int64Value(resp.CreatedAt)
		r.DefinitionID = types.StringValue(resp.DefinitionID)
		r.Name = types.StringValue(resp.Name)
		if resp.ResourceAllocation == nil {
			r.ResourceAllocation = nil
		} else {
			r.ResourceAllocation = &tfTypes.ScopedResourceRequirements{}
			if resp.ResourceAllocation.Default == nil {
				r.ResourceAllocation.Default = nil
			} else {
				r.ResourceAllocation.Default = &tfTypes.ResourceRequirements{}
				r.ResourceAllocation.Default.CPULimit = types.StringPointerValue(resp.ResourceAllocation.Default.CPULimit)
				r.ResourceAllocation.Default.CPURequest = types.StringPointerValue(resp.ResourceAllocation.Default.CPURequest)
				r.ResourceAllocation.Default.EphemeralStorageLimit = types.StringPointerValue(resp.ResourceAllocation.Default.EphemeralStorageLimit)
				r.ResourceAllocation.Default.EphemeralStorageRequest = types.StringPointerValue(resp.ResourceAllocation.Default.EphemeralStorageRequest)
				r.ResourceAllocation.Default.MemoryLimit = types.StringPointerValue(resp.ResourceAllocation.Default.MemoryLimit)
				r.ResourceAllocation.Default.MemoryRequest = types.StringPointerValue(resp.ResourceAllocation.Default.MemoryRequest)
			}
			r.ResourceAllocation.JobSpecific = []tfTypes.JobTypeResourceLimit{}
			if len(r.ResourceAllocation.JobSpecific) > len(resp.ResourceAllocation.JobSpecific) {
				r.ResourceAllocation.JobSpecific = r.ResourceAllocation.JobSpecific[:len(resp.ResourceAllocation.JobSpecific)]
			}
			for jobSpecificCount, jobSpecificItem := range resp.ResourceAllocation.JobSpecific {
				var jobSpecific1 tfTypes.JobTypeResourceLimit
				jobSpecific1.JobType = types.StringValue(string(jobSpecificItem.JobType))
				jobSpecific1.ResourceRequirements.CPULimit = types.StringPointerValue(jobSpecificItem.ResourceRequirements.CPULimit)
				jobSpecific1.ResourceRequirements.CPURequest = types.StringPointerValue(jobSpecificItem.ResourceRequirements.CPURequest)
				jobSpecific1.ResourceRequirements.EphemeralStorageLimit = types.StringPointerValue(jobSpecificItem.ResourceRequirements.EphemeralStorageLimit)
				jobSpecific1.ResourceRequirements.EphemeralStorageRequest = types.StringPointerValue(jobSpecificItem.ResourceRequirements.EphemeralStorageRequest)
				jobSpecific1.ResourceRequirements.MemoryLimit = types.StringPointerValue(jobSpecificItem.ResourceRequirements.MemoryLimit)
				jobSpecific1.ResourceRequirements.MemoryRequest = types.StringPointerValue(jobSpecificItem.ResourceRequirements.MemoryRequest)
				if jobSpecificCount+1 > len(r.ResourceAllocation.JobSpecific) {
					r.ResourceAllocation.JobSpecific = append(r.ResourceAllocation.JobSpecific, jobSpecific1)
				} else {
					r.ResourceAllocation.JobSpecific[jobSpecificCount].JobType = jobSpecific1.JobType
					r.ResourceAllocation.JobSpecific[jobSpecificCount].ResourceRequirements = jobSpecific1.ResourceRequirements
				}
			}
		}
		r.SourceID = types.StringValue(resp.SourceID)
		r.SourceType = types.StringValue(resp.SourceType)
		r.WorkspaceID = types.StringValue(resp.WorkspaceID)
	}
}

func (r *SourcePostgresResourceModel) ToSharedSourcePostgresPutRequest() *shared.SourcePostgresPutRequest {
	var name string
	name = r.Name.ValueString()

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

	var database string
	database = r.Configuration.Database.ValueString()

	var host string
	host = r.Configuration.Host.ValueString()

	jdbcURLParams := new(string)
	if !r.Configuration.JdbcURLParams.IsUnknown() && !r.Configuration.JdbcURLParams.IsNull() {
		*jdbcURLParams = r.Configuration.JdbcURLParams.ValueString()
	} else {
		jdbcURLParams = nil
	}
	password := new(string)
	if !r.Configuration.Password.IsUnknown() && !r.Configuration.Password.IsNull() {
		*password = r.Configuration.Password.ValueString()
	} else {
		password = nil
	}
	port := new(int64)
	if !r.Configuration.Port.IsUnknown() && !r.Configuration.Port.IsNull() {
		*port = r.Configuration.Port.ValueInt64()
	} else {
		port = nil
	}
	var replicationMethod *shared.SourcePostgresUpdateUpdateMethod
	if r.Configuration.ReplicationMethod != nil {
		var sourcePostgresUpdateReadChangesUsingWriteAheadLogCDC *shared.SourcePostgresUpdateReadChangesUsingWriteAheadLogCDC
		if r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC != nil {
			heartbeatActionQuery := new(string)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.HeartbeatActionQuery.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.HeartbeatActionQuery.IsNull() {
				*heartbeatActionQuery = r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.HeartbeatActionQuery.ValueString()
			} else {
				heartbeatActionQuery = nil
			}
			initialLoadTimeoutHours := new(int64)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InitialLoadTimeoutHours.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InitialLoadTimeoutHours.IsNull() {
				*initialLoadTimeoutHours = r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InitialLoadTimeoutHours.ValueInt64()
			} else {
				initialLoadTimeoutHours = nil
			}
			initialWaitingSeconds := new(int64)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InitialWaitingSeconds.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InitialWaitingSeconds.IsNull() {
				*initialWaitingSeconds = r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InitialWaitingSeconds.ValueInt64()
			} else {
				initialWaitingSeconds = nil
			}
			invalidCdcCursorPositionBehavior := new(shared.SourcePostgresUpdateInvalidCDCPositionBehaviorAdvanced)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InvalidCdcCursorPositionBehavior.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InvalidCdcCursorPositionBehavior.IsNull() {
				*invalidCdcCursorPositionBehavior = shared.SourcePostgresUpdateInvalidCDCPositionBehaviorAdvanced(r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.InvalidCdcCursorPositionBehavior.ValueString())
			} else {
				invalidCdcCursorPositionBehavior = nil
			}
			lsnCommitBehaviour := new(shared.SourcePostgresUpdateLSNCommitBehaviour)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.LsnCommitBehaviour.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.LsnCommitBehaviour.IsNull() {
				*lsnCommitBehaviour = shared.SourcePostgresUpdateLSNCommitBehaviour(r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.LsnCommitBehaviour.ValueString())
			} else {
				lsnCommitBehaviour = nil
			}
			plugin := new(shared.SourcePostgresUpdatePlugin)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.Plugin.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.Plugin.IsNull() {
				*plugin = shared.SourcePostgresUpdatePlugin(r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.Plugin.ValueString())
			} else {
				plugin = nil
			}
			var publication string
			publication = r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.Publication.ValueString()

			queueSize := new(int64)
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.QueueSize.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.QueueSize.IsNull() {
				*queueSize = r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.QueueSize.ValueInt64()
			} else {
				queueSize = nil
			}
			var replicationSlot string
			replicationSlot = r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.ReplicationSlot.ValueString()

			var additionalProperties interface{}
			if !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.AdditionalProperties.IsUnknown() && !r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.ReplicationMethod.ReadChangesUsingWriteAheadLogCDC.AdditionalProperties.ValueString()), &additionalProperties)
			}
			sourcePostgresUpdateReadChangesUsingWriteAheadLogCDC = &shared.SourcePostgresUpdateReadChangesUsingWriteAheadLogCDC{
				HeartbeatActionQuery:             heartbeatActionQuery,
				InitialLoadTimeoutHours:          initialLoadTimeoutHours,
				InitialWaitingSeconds:            initialWaitingSeconds,
				InvalidCdcCursorPositionBehavior: invalidCdcCursorPositionBehavior,
				LsnCommitBehaviour:               lsnCommitBehaviour,
				Plugin:                           plugin,
				Publication:                      publication,
				QueueSize:                        queueSize,
				ReplicationSlot:                  replicationSlot,
				AdditionalProperties:             additionalProperties,
			}
		}
		if sourcePostgresUpdateReadChangesUsingWriteAheadLogCDC != nil {
			replicationMethod = &shared.SourcePostgresUpdateUpdateMethod{
				SourcePostgresUpdateReadChangesUsingWriteAheadLogCDC: sourcePostgresUpdateReadChangesUsingWriteAheadLogCDC,
			}
		}
		var sourcePostgresUpdateDetectChangesWithXminSystemColumn *shared.SourcePostgresUpdateDetectChangesWithXminSystemColumn
		if r.Configuration.ReplicationMethod.DetectChangesWithXminSystemColumn != nil {
			sourcePostgresUpdateDetectChangesWithXminSystemColumn = &shared.SourcePostgresUpdateDetectChangesWithXminSystemColumn{}
		}
		if sourcePostgresUpdateDetectChangesWithXminSystemColumn != nil {
			replicationMethod = &shared.SourcePostgresUpdateUpdateMethod{
				SourcePostgresUpdateDetectChangesWithXminSystemColumn: sourcePostgresUpdateDetectChangesWithXminSystemColumn,
			}
		}
		var sourcePostgresUpdateScanChangesWithUserDefinedCursor *shared.SourcePostgresUpdateScanChangesWithUserDefinedCursor
		if r.Configuration.ReplicationMethod.ScanChangesWithUserDefinedCursor != nil {
			sourcePostgresUpdateScanChangesWithUserDefinedCursor = &shared.SourcePostgresUpdateScanChangesWithUserDefinedCursor{}
		}
		if sourcePostgresUpdateScanChangesWithUserDefinedCursor != nil {
			replicationMethod = &shared.SourcePostgresUpdateUpdateMethod{
				SourcePostgresUpdateScanChangesWithUserDefinedCursor: sourcePostgresUpdateScanChangesWithUserDefinedCursor,
			}
		}
	}
	var schemas []string = []string{}
	for _, schemasItem := range r.Configuration.Schemas {
		schemas = append(schemas, schemasItem.ValueString())
	}
	var sslMode *shared.SourcePostgresUpdateSSLModes
	if r.Configuration.SslMode != nil {
		var sourcePostgresUpdateDisable *shared.SourcePostgresUpdateDisable
		if r.Configuration.SslMode.Disable != nil {
			var additionalProperties1 interface{}
			if !r.Configuration.SslMode.Disable.AdditionalProperties.IsUnknown() && !r.Configuration.SslMode.Disable.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.SslMode.Disable.AdditionalProperties.ValueString()), &additionalProperties1)
			}
			sourcePostgresUpdateDisable = &shared.SourcePostgresUpdateDisable{
				AdditionalProperties: additionalProperties1,
			}
		}
		if sourcePostgresUpdateDisable != nil {
			sslMode = &shared.SourcePostgresUpdateSSLModes{
				SourcePostgresUpdateDisable: sourcePostgresUpdateDisable,
			}
		}
		var sourcePostgresUpdateAllow *shared.SourcePostgresUpdateAllow
		if r.Configuration.SslMode.Allow != nil {
			var additionalProperties2 interface{}
			if !r.Configuration.SslMode.Allow.AdditionalProperties.IsUnknown() && !r.Configuration.SslMode.Allow.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.SslMode.Allow.AdditionalProperties.ValueString()), &additionalProperties2)
			}
			sourcePostgresUpdateAllow = &shared.SourcePostgresUpdateAllow{
				AdditionalProperties: additionalProperties2,
			}
		}
		if sourcePostgresUpdateAllow != nil {
			sslMode = &shared.SourcePostgresUpdateSSLModes{
				SourcePostgresUpdateAllow: sourcePostgresUpdateAllow,
			}
		}
		var sourcePostgresUpdatePrefer *shared.SourcePostgresUpdatePrefer
		if r.Configuration.SslMode.Prefer != nil {
			var additionalProperties3 interface{}
			if !r.Configuration.SslMode.Prefer.AdditionalProperties.IsUnknown() && !r.Configuration.SslMode.Prefer.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.SslMode.Prefer.AdditionalProperties.ValueString()), &additionalProperties3)
			}
			sourcePostgresUpdatePrefer = &shared.SourcePostgresUpdatePrefer{
				AdditionalProperties: additionalProperties3,
			}
		}
		if sourcePostgresUpdatePrefer != nil {
			sslMode = &shared.SourcePostgresUpdateSSLModes{
				SourcePostgresUpdatePrefer: sourcePostgresUpdatePrefer,
			}
		}
		var sourcePostgresUpdateRequire *shared.SourcePostgresUpdateRequire
		if r.Configuration.SslMode.Require != nil {
			var additionalProperties4 interface{}
			if !r.Configuration.SslMode.Require.AdditionalProperties.IsUnknown() && !r.Configuration.SslMode.Require.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.SslMode.Require.AdditionalProperties.ValueString()), &additionalProperties4)
			}
			sourcePostgresUpdateRequire = &shared.SourcePostgresUpdateRequire{
				AdditionalProperties: additionalProperties4,
			}
		}
		if sourcePostgresUpdateRequire != nil {
			sslMode = &shared.SourcePostgresUpdateSSLModes{
				SourcePostgresUpdateRequire: sourcePostgresUpdateRequire,
			}
		}
		var sourcePostgresUpdateVerifyCa *shared.SourcePostgresUpdateVerifyCa
		if r.Configuration.SslMode.VerifyCa != nil {
			var caCertificate string
			caCertificate = r.Configuration.SslMode.VerifyCa.CaCertificate.ValueString()

			clientCertificate := new(string)
			if !r.Configuration.SslMode.VerifyCa.ClientCertificate.IsUnknown() && !r.Configuration.SslMode.VerifyCa.ClientCertificate.IsNull() {
				*clientCertificate = r.Configuration.SslMode.VerifyCa.ClientCertificate.ValueString()
			} else {
				clientCertificate = nil
			}
			clientKey := new(string)
			if !r.Configuration.SslMode.VerifyCa.ClientKey.IsUnknown() && !r.Configuration.SslMode.VerifyCa.ClientKey.IsNull() {
				*clientKey = r.Configuration.SslMode.VerifyCa.ClientKey.ValueString()
			} else {
				clientKey = nil
			}
			clientKeyPassword := new(string)
			if !r.Configuration.SslMode.VerifyCa.ClientKeyPassword.IsUnknown() && !r.Configuration.SslMode.VerifyCa.ClientKeyPassword.IsNull() {
				*clientKeyPassword = r.Configuration.SslMode.VerifyCa.ClientKeyPassword.ValueString()
			} else {
				clientKeyPassword = nil
			}
			var additionalProperties5 interface{}
			if !r.Configuration.SslMode.VerifyCa.AdditionalProperties.IsUnknown() && !r.Configuration.SslMode.VerifyCa.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.SslMode.VerifyCa.AdditionalProperties.ValueString()), &additionalProperties5)
			}
			sourcePostgresUpdateVerifyCa = &shared.SourcePostgresUpdateVerifyCa{
				CaCertificate:        caCertificate,
				ClientCertificate:    clientCertificate,
				ClientKey:            clientKey,
				ClientKeyPassword:    clientKeyPassword,
				AdditionalProperties: additionalProperties5,
			}
		}
		if sourcePostgresUpdateVerifyCa != nil {
			sslMode = &shared.SourcePostgresUpdateSSLModes{
				SourcePostgresUpdateVerifyCa: sourcePostgresUpdateVerifyCa,
			}
		}
		var sourcePostgresUpdateVerifyFull *shared.SourcePostgresUpdateVerifyFull
		if r.Configuration.SslMode.VerifyFull != nil {
			var caCertificate1 string
			caCertificate1 = r.Configuration.SslMode.VerifyFull.CaCertificate.ValueString()

			clientCertificate1 := new(string)
			if !r.Configuration.SslMode.VerifyFull.ClientCertificate.IsUnknown() && !r.Configuration.SslMode.VerifyFull.ClientCertificate.IsNull() {
				*clientCertificate1 = r.Configuration.SslMode.VerifyFull.ClientCertificate.ValueString()
			} else {
				clientCertificate1 = nil
			}
			clientKey1 := new(string)
			if !r.Configuration.SslMode.VerifyFull.ClientKey.IsUnknown() && !r.Configuration.SslMode.VerifyFull.ClientKey.IsNull() {
				*clientKey1 = r.Configuration.SslMode.VerifyFull.ClientKey.ValueString()
			} else {
				clientKey1 = nil
			}
			clientKeyPassword1 := new(string)
			if !r.Configuration.SslMode.VerifyFull.ClientKeyPassword.IsUnknown() && !r.Configuration.SslMode.VerifyFull.ClientKeyPassword.IsNull() {
				*clientKeyPassword1 = r.Configuration.SslMode.VerifyFull.ClientKeyPassword.ValueString()
			} else {
				clientKeyPassword1 = nil
			}
			var additionalProperties6 interface{}
			if !r.Configuration.SslMode.VerifyFull.AdditionalProperties.IsUnknown() && !r.Configuration.SslMode.VerifyFull.AdditionalProperties.IsNull() {
				_ = json.Unmarshal([]byte(r.Configuration.SslMode.VerifyFull.AdditionalProperties.ValueString()), &additionalProperties6)
			}
			sourcePostgresUpdateVerifyFull = &shared.SourcePostgresUpdateVerifyFull{
				CaCertificate:        caCertificate1,
				ClientCertificate:    clientCertificate1,
				ClientKey:            clientKey1,
				ClientKeyPassword:    clientKeyPassword1,
				AdditionalProperties: additionalProperties6,
			}
		}
		if sourcePostgresUpdateVerifyFull != nil {
			sslMode = &shared.SourcePostgresUpdateSSLModes{
				SourcePostgresUpdateVerifyFull: sourcePostgresUpdateVerifyFull,
			}
		}
	}
	var tunnelMethod *shared.SourcePostgresUpdateSSHTunnelMethod
	if r.Configuration.TunnelMethod != nil {
		var sourcePostgresUpdateNoTunnel *shared.SourcePostgresUpdateNoTunnel
		if r.Configuration.TunnelMethod.NoTunnel != nil {
			sourcePostgresUpdateNoTunnel = &shared.SourcePostgresUpdateNoTunnel{}
		}
		if sourcePostgresUpdateNoTunnel != nil {
			tunnelMethod = &shared.SourcePostgresUpdateSSHTunnelMethod{
				SourcePostgresUpdateNoTunnel: sourcePostgresUpdateNoTunnel,
			}
		}
		var sourcePostgresUpdateSSHKeyAuthentication *shared.SourcePostgresUpdateSSHKeyAuthentication
		if r.Configuration.TunnelMethod.SSHKeyAuthentication != nil {
			var sshKey string
			sshKey = r.Configuration.TunnelMethod.SSHKeyAuthentication.SSHKey.ValueString()

			var tunnelHost string
			tunnelHost = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelHost.ValueString()

			tunnelPort := new(int64)
			if !r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.IsUnknown() && !r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.IsNull() {
				*tunnelPort = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.ValueInt64()
			} else {
				tunnelPort = nil
			}
			var tunnelUser string
			tunnelUser = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelUser.ValueString()

			sourcePostgresUpdateSSHKeyAuthentication = &shared.SourcePostgresUpdateSSHKeyAuthentication{
				SSHKey:     sshKey,
				TunnelHost: tunnelHost,
				TunnelPort: tunnelPort,
				TunnelUser: tunnelUser,
			}
		}
		if sourcePostgresUpdateSSHKeyAuthentication != nil {
			tunnelMethod = &shared.SourcePostgresUpdateSSHTunnelMethod{
				SourcePostgresUpdateSSHKeyAuthentication: sourcePostgresUpdateSSHKeyAuthentication,
			}
		}
		var sourcePostgresUpdatePasswordAuthentication *shared.SourcePostgresUpdatePasswordAuthentication
		if r.Configuration.TunnelMethod.PasswordAuthentication != nil {
			var tunnelHost1 string
			tunnelHost1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelHost.ValueString()

			tunnelPort1 := new(int64)
			if !r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.IsUnknown() && !r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.IsNull() {
				*tunnelPort1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.ValueInt64()
			} else {
				tunnelPort1 = nil
			}
			var tunnelUser1 string
			tunnelUser1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelUser.ValueString()

			var tunnelUserPassword string
			tunnelUserPassword = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelUserPassword.ValueString()

			sourcePostgresUpdatePasswordAuthentication = &shared.SourcePostgresUpdatePasswordAuthentication{
				TunnelHost:         tunnelHost1,
				TunnelPort:         tunnelPort1,
				TunnelUser:         tunnelUser1,
				TunnelUserPassword: tunnelUserPassword,
			}
		}
		if sourcePostgresUpdatePasswordAuthentication != nil {
			tunnelMethod = &shared.SourcePostgresUpdateSSHTunnelMethod{
				SourcePostgresUpdatePasswordAuthentication: sourcePostgresUpdatePasswordAuthentication,
			}
		}
	}
	var username string
	username = r.Configuration.Username.ValueString()

	configuration := shared.SourcePostgresUpdate{
		Database:          database,
		Host:              host,
		JdbcURLParams:     jdbcURLParams,
		Password:          password,
		Port:              port,
		ReplicationMethod: replicationMethod,
		Schemas:           schemas,
		SslMode:           sslMode,
		TunnelMethod:      tunnelMethod,
		Username:          username,
	}
	out := shared.SourcePostgresPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
