github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/util/manifestparser/process.go (about)

     1  package manifestparser
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     8  )
     9  
    10  type Process struct {
    11  	DiskQuota               string                   `yaml:"disk_quota,omitempty"`
    12  	HealthCheckEndpoint     string                   `yaml:"health-check-http-endpoint,omitempty"`
    13  	HealthCheckType         constant.HealthCheckType `yaml:"health-check-type,omitempty"`
    14  	HealthCheckTimeout      int64                    `yaml:"timeout,omitempty"`
    15  	Instances               *int                     `yaml:"instances,omitempty"`
    16  	Memory                  string                   `yaml:"memory,omitempty"`
    17  	Type                    string                   `yaml:"type"`
    18  	RemainingManifestFields map[string]interface{}   `yaml:"-,inline"`
    19  }
    20  
    21  func (process *Process) SetStartCommand(command string) {
    22  	if process.RemainingManifestFields == nil {
    23  		process.RemainingManifestFields = map[string]interface{}{}
    24  	}
    25  
    26  	if command == "" {
    27  		process.RemainingManifestFields["command"] = nil
    28  	} else {
    29  		process.RemainingManifestFields["command"] = command
    30  	}
    31  }
    32  
    33  func (process *Process) UnmarshalYAML(unmarshal func(v interface{}) error) error {
    34  	// This prevents infinite recursion. The Alias type does not implement the unmarshaller interface
    35  	// so by casting application to a alias pointer, it will unmarshal in to the same memory without calling
    36  	// UnmarshalYAML on itself infinite times
    37  	type Alias Process
    38  	aliasPntr := (*Alias)(process)
    39  
    40  	err := unmarshal(aliasPntr)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	err = unmarshal(&process.RemainingManifestFields)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	value := reflect.ValueOf(*process)
    51  	removeDuplicateMapKeys(value, process.RemainingManifestFields)
    52  
    53  	return nil
    54  }
    55  
    56  func removeDuplicateMapKeys(model reflect.Value, fieldMap map[string]interface{}) {
    57  	for i := 0; i < model.NumField(); i++ {
    58  		structField := model.Type().Field(i)
    59  
    60  		yamlTag := strings.Split(structField.Tag.Get("yaml"), ",")
    61  		yamlKey := yamlTag[0]
    62  
    63  		if yamlKey == "" {
    64  			yamlKey = structField.Name
    65  		}
    66  
    67  		delete(fieldMap, yamlKey)
    68  	}
    69  }