<commit_before>package deployers

import (
	"github.com/openwhisk/openwhisk-client-go/whisk"
	"github.com/openwhisk/openwhisk-wskdeploy/parsers"
)

type DeploymentReader struct {
	serviceDeployer      *ServiceDeployer
	DeploymentDescriptor *parsers.DeploymentYAML
}

func NewDeploymentReader(serviceDeployer *ServiceDeployer) *DeploymentReader {
	var dep DeploymentReader
	dep.serviceDeployer = serviceDeployer

	return &dep
}

// Wrapper parser to handle yaml dir
func (reader *DeploymentReader) HandleYaml() error {

	dep := reader.serviceDeployer

	deploymentParser := parsers.NewYAMLParser()
	deployment := deploymentParser.ParseDeployment(dep.DeploymentPath)

	reader.DeploymentDescriptor = deployment

	return nil
}

// Update entities with deployment settings
func (reader *DeploymentReader) BindAssets() error {

	reader.bindPackageInputsAndAnnotations()
	reader.bindActionInputsAndAnnotations()
	reader.bindTriggerInputsAndAnnotations()

	return nil

}

func (reader *DeploymentReader) bindPackageInputsAndAnnotations() {

	packArray := make([]parsers.Package, 0)

	if reader.DeploymentDescriptor.Application.Packages == nil {
		packArray = append(packArray, reader.DeploymentDescriptor.Application.Package)
	} else {
		for _, depPacks := range reader.DeploymentDescriptor.Application.Packages {
			packArray = append(packArray, depPacks)
		}
	}

	for _, pack := range packArray {
		serviceDeployPack := reader.serviceDeployer.Deployment.Packages[pack.Packagename]

		var keyValArr whisk.KeyValueArr
		for name, input := range pack.Inputs {
			var keyVal whisk.KeyValue

			keyVal.Key = name
			keyVal.Value = input

			keyValArr = append(keyValArr, keyVal)
		}

		serviceDeployPack.Package.Parameters = keyValArr

		keyValArr = keyValArr[:0]
		for name, input := range pack.Annotations {
			var keyVal whisk.KeyValue

			keyVal.Key = name
			keyVal.Value = input

			keyValArr = append(keyValArr, keyVal)
		}

		serviceDeployPack.Package.Annotations = keyValArr

	}
}

func (reader *DeploymentReader) bindActionInputsAndAnnotations() {

	packArray := make([]parsers.Package, 1)

	if reader.DeploymentDescriptor.Application.Packages == nil {
		packArray = append(packArray, reader.DeploymentDescriptor.Application.Package)
	} else {
		for _, depPacks := range reader.DeploymentDescriptor.Application.Packages {
			packArray = append(packArray, depPacks)
		}
	}

	for _, pack := range packArray {

		for actionName, action := range pack.Actions {

			serviceDeployPack := reader.serviceDeployer.Deployment.Packages[pack.Packagename]

			var keyValArr whisk.KeyValueArr
			for name, input := range action.Inputs {
				var keyVal whisk.KeyValue

				keyVal.Key = name
				keyVal.Value = input

				keyValArr = append(keyValArr, keyVal)
			}

			if wskAction, exists := serviceDeployPack.Actions[actionName]; exists {
				wskAction.Action.Parameters = keyValArr
			}

			keyValArr = keyValArr[:0]

			for name, input := range action.Annotations {
				var keyVal whisk.KeyValue

				keyVal.Key = name
				keyVal.Value = input

				keyValArr = append(keyValArr, keyVal)
			}

			if wskAction, exists := serviceDeployPack.Actions[actionName]; exists {
				wskAction.Action.Annotations = keyValArr
			}
		}

	}
}

func (reader *DeploymentReader) bindTriggerInputsAndAnnotations() {

	packArray := make([]parsers.Package, 1)
<commit_msg>WIP, change error messages<commit_after>package deployers

import (
	"fmt"

	"github.com/openwhisk/openwhisk-client-go/whisk"
	"github.com/openwhisk/openwhisk-wskdeploy/parsers"
)

type DeploymentReader struct {
	serviceDeployer      *ServiceDeployer
	DeploymentDescriptor *parsers.DeploymentYAML
}

func NewDeploymentReader(serviceDeployer *ServiceDeployer) *DeploymentReader {
	var dep DeploymentReader
	dep.serviceDeployer = serviceDeployer

	return &dep
}

// Wrapper parser to handle yaml dir
func (reader *DeploymentReader) HandleYaml() error {

	dep := reader.serviceDeployer

	deploymentParser := parsers.NewYAMLParser()
	deployment := deploymentParser.ParseDeployment(dep.DeploymentPath)

	reader.DeploymentDescriptor = deployment

	return nil
}

// Update entities with deployment settings
func (reader *DeploymentReader) BindAssets() error {

	reader.bindPackageInputsAndAnnotations()
	reader.bindActionInputsAndAnnotations()
	reader.bindTriggerInputsAndAnnotations()

	return nil

}

func (reader *DeploymentReader) bindPackageInputsAndAnnotations() {

	packArray := make([]parsers.Package, 0)

	if reader.DeploymentDescriptor.Application.Packages == nil {
		packArray = append(packArray, reader.DeploymentDescriptor.Application.Package)
	} else {
		for _, depPacks := range reader.DeploymentDescriptor.Application.Packages {
			packArray = append(packArray, depPacks)
		}
	}

	for _, pack := range packArray {
		serviceDeployPack, exist := reader.serviceDeployer.Deployment.Packages[pack.Packagename]

		if exist {
			var keyValArr whisk.KeyValueArr
			for name, input := range pack.Inputs {
				var keyVal whisk.KeyValue

				keyVal.Key = name
				keyVal.Value = input

				keyValArr = append(keyValArr, keyVal)
			}

			serviceDeployPack.Package.Parameters = keyValArr

			keyValArr = keyValArr[:0]
			for name, input := range pack.Annotations {
				var keyVal whisk.KeyValue

				keyVal.Key = name
				keyVal.Value = input

				keyValArr = append(keyValArr, keyVal)
			}

			serviceDeployPack.Package.Annotations = keyValArr
		} else {
			fmt.Println("Warning: deployment file attempts to bind parameters to undeclared package: " + pack.Packagename)
		}

	}
}

func (reader *DeploymentReader) bindActionInputsAndAnnotations() {

	packArray := make([]parsers.Package, 1)

	if reader.DeploymentDescriptor.Application.Packages == nil {
		packArray = append(packArray, reader.DeploymentDescriptor.Application.Package)
	} else {
		for _, depPacks := range reader.DeploymentDescriptor.Application.Packages {
			packArray = append(packArray, depPacks)
		}
	}

	for _, pack := range packArray {

		for actionName, action := range pack.Actions {

			serviceDeployPack, exists := reader.serviceDeployer.Deployment.Packages[pack.Packagename]

			if exists {
				var keyValArr whisk.KeyValueArr
				for name, input := range action.Inputs {
					var keyVal whisk.KeyValue

					keyVal.Key = name
					keyVal.Value = input

					keyValArr = append(keyValArr, keyVal)
				}

				if wskAction, exists := serviceDeployPack.Actions[actionName]; exists {
					wskAction.Action.Parameters = keyValArr
				}

				keyValArr = keyValArr[:0]

				for name, input := range action.Annotations {
					var keyVal whisk.KeyValue

					keyVal.Key = name
					keyVal.Value = input

					keyValArr = append(keyValArr, keyVal)
				}

				if wskAction, exists := serviceDeployPack.Actions[actionName]; exists {
					wskAction.Action.Annotations = keyValArr
				}
			} else {
				fmt.Println("Warning: deployment file sets action bindings undeclared package: " + pack.Packagename)
			}
		}

	}
}

func (reader *DeploymentReader) bindTriggerInputsAndAnnotations() {

	packArray := make([]parsers.Package, 1)

{'fraction_non_alphanumeric': 0.09238434163701068, 'fraction_numerical': 0.0014234875444839859, 'mean_word_length': 4.3106575963718825, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7364788', 'n_tokens_mistral': 2457, 'n_tokens_neox': 2156, 'n_words': 458}