func envVarOrEmptyMap(yamlMap generic.Map, errs *[]error) *map[string]interface{} { key := "env" switch envVars := yamlMap.Get(key).(type) { case nil: aMap := make(map[string]interface{}, 0) return &aMap case map[string]interface{}: yamlMap.Set(key, generic.NewMap(yamlMap.Get(key))) return envVarOrEmptyMap(yamlMap, errs) case map[interface{}]interface{}: yamlMap.Set(key, generic.NewMap(yamlMap.Get(key))) return envVarOrEmptyMap(yamlMap, errs) case generic.Map: merrs := validateEnvVars(envVars) if merrs != nil { *errs = append(*errs, merrs...) return nil } result := make(map[string]interface{}, envVars.Count()) generic.Each(envVars, func(key, value interface{}) { result[key.(string)] = value }) return &result default: *errs = append(*errs, fmt.Errorf(T("Expected {{.Name}} to be a set of key => value, but it was a {{.Type}}.", map[string]interface{}{"Name": key, "Type": envVars}))) return nil } }
func (resource EventResourceNewV2) ToFields() models.EventFields { metadata := generic.NewMap(resource.Entity.Metadata) if metadata.Has("request") { metadata = generic.NewMap(metadata.Get("request")) } return models.EventFields{ GUID: resource.Metadata.GUID, Name: resource.Entity.Type, Timestamp: resource.Entity.Timestamp, Description: formatDescription(metadata, knownMetadataKeys), Actor: resource.Entity.Actor, ActorName: resource.Entity.ActorName, } }
func (m Manifest) getAppMaps(data generic.Map) ([]generic.Map, error) { globalProperties := data.Except([]interface{}{"applications"}) var apps []generic.Map var errs []error if data.Has("applications") { appMaps, ok := data.Get("applications").([]interface{}) if !ok { return []generic.Map{}, errors.New(T("Expected applications to be a list")) } for _, appData := range appMaps { if !generic.IsMappable(appData) { errs = append(errs, fmt.Errorf(T("Expected application to be a list of key/value pairs\nError occurred in manifest near:\n'{{.YmlSnippet}}'", map[string]interface{}{"YmlSnippet": appData}))) continue } appMap := generic.DeepMerge(globalProperties, generic.NewMap(appData)) apps = append(apps, appMap) } } else { apps = append(apps, globalProperties) } if len(errs) > 0 { message := "" for i := range errs { message = message + fmt.Sprintf("%s\n", errs[i].Error()) } return []generic.Map{}, errors.New(message) } return apps, nil }
func (m Manifest) Applications() ([]models.AppParams, error) { rawData, err := expandProperties(m.Data, generator.NewWordGenerator()) if err != nil { return []models.AppParams{}, err } data := generic.NewMap(rawData) appMaps, err := m.getAppMaps(data) if err != nil { return []models.AppParams{}, err } var apps []models.AppParams var mapToAppErrs []error for _, appMap := range appMaps { app, err := mapToAppParams(filepath.Dir(m.Path), appMap) if err != nil { mapToAppErrs = append(mapToAppErrs, err) continue } apps = append(apps, app) } if len(mapToAppErrs) > 0 { message := "" for i := range mapToAppErrs { message = message + fmt.Sprintf("%s\n", mapToAppErrs[i].Error()) } return []models.AppParams{}, errors.New(message) } return apps, nil }
func parseManifest(file io.Reader) (yamlMap generic.Map, err error) { manifest, err := ioutil.ReadAll(file) if err != nil { return } mmap := make(map[interface{}]interface{}) err = yaml.Unmarshal(manifest, &mmap) if err != nil { return } if !generic.IsMappable(mmap) || len(mmap) == 0 { err = errors.New(T("Invalid manifest. Expected a map")) return } yamlMap = generic.NewMap(mmap) return }
. "github.com/onsi/gomega" . "github.com/cloudfoundry/cli/testhelpers/matchers" ) func NewManifest(path string, data generic.Map) (m *manifest.Manifest) { return &manifest.Manifest{Path: path, Data: data} } var _ = Describe("Manifests", func() { It("merges global properties into each app's properties", func() { m := NewManifest("/some/path/manifest.yml", generic.NewMap(map[interface{}]interface{}{ "instances": "3", "memory": "512M", "applications": []interface{}{ map[interface{}]interface{}{ "name": "bitcoin-miner", "no-route": true, }, }, })) apps, err := m.Applications() Expect(err).NotTo(HaveOccurred()) Expect(*apps[0].InstanceCount).To(Equal(3)) Expect(*apps[0].Memory).To(Equal(int64(512))) Expect(apps[0].NoRoute).To(BeTrue()) }) Context("when there is no applications block", func() { It("returns a single application with the global properties", func() {
func NewEmptyManifest() (m *Manifest) { return &Manifest{Data: generic.NewMap()} }
func expandProperties(input interface{}, babbler generator.WordGenerator) (interface{}, error) { var errs []error var output interface{} switch input := input.(type) { case string: match := propertyRegex.FindStringSubmatch(input) if match != nil { if match[0] == "${random-word}" { output = strings.Replace(input, "${random-word}", strings.ToLower(babbler.Babble()), -1) } else { err := fmt.Errorf(T("Property '{{.PropertyName}}' found in manifest. This feature is no longer supported. Please remove it and try again.", map[string]interface{}{"PropertyName": match[0]})) errs = append(errs, err) } } else { output = input } case []interface{}: outputSlice := make([]interface{}, len(input)) for index, item := range input { itemOutput, itemErr := expandProperties(item, babbler) if itemErr != nil { errs = append(errs, itemErr) break } outputSlice[index] = itemOutput } output = outputSlice case map[interface{}]interface{}: outputMap := make(map[interface{}]interface{}) for key, value := range input { itemOutput, itemErr := expandProperties(value, babbler) if itemErr != nil { errs = append(errs, itemErr) break } outputMap[key] = itemOutput } output = outputMap case generic.Map: outputMap := generic.NewMap() generic.Each(input, func(key, value interface{}) { itemOutput, itemErr := expandProperties(value, babbler) if itemErr != nil { errs = append(errs, itemErr) return } outputMap.Set(key, itemOutput) }) output = outputMap default: output = input } if len(errs) > 0 { message := "" for _, err := range errs { message = message + fmt.Sprintf("%s\n", err.Error()) } return nil, errors.New(message) } return output, nil }