當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Map.Has方法代碼示例

本文整理匯總了Golang中generic.Map.Has方法的典型用法代碼示例。如果您正苦於以下問題:Golang Map.Has方法的具體用法?Golang Map.Has怎麽用?Golang Map.Has使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在generic.Map的用法示例。


在下文中一共展示了Map.Has方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: sliceOrEmptyVal

func sliceOrEmptyVal(yamlMap generic.Map, key string, errs *ManifestErrors) *[]string {
	if !yamlMap.Has(key) {
		return new([]string)
	}

	var (
		stringSlice []string
		err         error
	)

	errMsg := fmt.Sprintf("Expected %s to be a list of strings.", key)

	switch input := yamlMap.Get(key).(type) {
	case []interface{}:
		for _, value := range input {
			stringValue, ok := value.(string)
			if !ok {
				err = errors.New(errMsg)
				break
			}
			stringSlice = append(stringSlice, stringValue)
		}
	default:
		err = errors.New(errMsg)
	}

	if err != nil {
		*errs = append(*errs, err)
		return nil
	}

	return &stringSlice
}
開發者ID:normalnorman,項目名稱:cli,代碼行數:33,代碼來源:manifest.go

示例2: mapToAppSet

func mapToAppSet(basePath string, data generic.Map) (appSet []models.AppParams, errs ManifestErrors) {
	if data.Has("applications") {
		appMaps, ok := data.Get("applications").([]interface{})
		if !ok {
			errs = append(errs, errors.New("Expected applications to be a list"))
			return
		}

		// we delete applications so that we may merge top level app params into each app
		data.Delete("applications")

		for _, appData := range appMaps {
			if !generic.IsMappable(appData) {
				errs = append(errs, errors.New("Expected application to be a dictionary"))
				continue
			}

			appMap := generic.DeepMerge(data, generic.NewMap(appData))

			appParams, appErrs := mapToAppParams(basePath, appMap)
			if !appErrs.Empty() {
				errs = append(errs, appErrs)
				continue
			}

			appSet = append(appSet, appParams)
		}
	}

	return
}
開發者ID:normalnorman,項目名稱:cli,代碼行數:31,代碼來源:manifest.go

示例3: getAppMaps

func (m Manifest) getAppMaps(data generic.Map) (apps []generic.Map, errs []error) {
	globalProperties := data.Except([]interface{}{"applications"})

	if data.Has("applications") {
		appMaps, ok := data.Get("applications").([]interface{})
		if !ok {
			errs = append(errs, errors.New("Expected applications to be a list"))
			return
		}

		for _, appData := range appMaps {
			if !generic.IsMappable(appData) {
				errs = append(errs, errors.New(fmt.Sprintf("Expected application to be a list of key/value pairs\nError occurred in manifest near:\n'%s'", appData)))
				continue
			}

			appMap := generic.DeepMerge(globalProperties, generic.NewMap(appData))
			apps = append(apps, appMap)
		}
	} else {
		apps = append(apps, globalProperties)
	}

	return
}
開發者ID:juggernaut,項目名稱:cli,代碼行數:25,代碼來源:manifest.go

示例4: stringOrNullVal

func stringOrNullVal(yamlMap generic.Map, key string, errs *ManifestErrors) *string {
	if !yamlMap.Has(key) {
		return nil
	}
	switch val := yamlMap.Get(key).(type) {
	case string:
		return &val
	case nil:
		empty := ""
		return &empty
	default:
		*errs = append(*errs, errors.New(fmt.Sprintf("%s must be a string or null value", key)))
		return nil
	}
}
開發者ID:normalnorman,項目名稱:cli,代碼行數:15,代碼來源:manifest.go

示例5: mapToAppParams

func mapToAppParams(yamlMap generic.Map) (appParams cf.AppParams, errs ManifestErrors) {
	appParams = cf.NewEmptyAppParams()

	errs = checkForNulls(yamlMap)
	if !errs.Empty() {
		return
	}

	for key, handler := range manifestKeys {
		if yamlMap.Has(key) {
			handler(appParams, yamlMap, key, &errs)
		}
	}

	return
}
開發者ID:nsnt,項目名稱:cli,代碼行數:16,代碼來源:manifest.go

示例6: stringValOrDefault

func stringValOrDefault(yamlMap generic.Map, key string, errs *[]error) *string {
	if !yamlMap.Has(key) {
		return nil
	}
	empty := ""
	switch val := yamlMap.Get(key).(type) {
	case string:
		if val == "default" {
			return &empty
		} else {
			return &val
		}
	case nil:
		return &empty
	default:
		*errs = append(*errs, errors.NewWithFmt("%s must be a string or null value", key))
		return nil
	}
}
開發者ID:nota-ja,項目名稱:cli,代碼行數:19,代碼來源:manifest.go

示例7: servicesOrEmptyVal

func servicesOrEmptyVal(yamlMap generic.Map, key string, errs *ManifestErrors) (*[]string, *[]map[string]string) {
	if !yamlMap.Has(key) {
		return new([]string), new([]map[string]string)
	}

	var (
		stringSlice []string
		mapSlice    []map[string]string
		err         error
	)

	errMsg := fmt.Sprintf("Expected %s to be a list of strings.", key)

	switch input := yamlMap.Get(key).(type) {
	case []map[interface{}]interface{}:
		for _, mapValue := range input {
			stringMap := map[string]string{}
			for key, value := range mapValue {
				stringMap[key.(string)] = value.(string)
			}
			mapSlice = append(mapSlice, stringMap)
		}
	case []interface{}:
		for _, value := range input {
			stringValue, ok := value.(string)
			if !ok {
				err = errors.New(errMsg)
				break
			}
			stringSlice = append(stringSlice, stringValue)
		}
	default:
		err = errors.New(errMsg)
	}

	if err != nil {
		*errs = append(*errs, err)
		return nil, nil
	}

	return &stringSlice, &mapSlice
}
開發者ID:juggernaut,項目名稱:cli,代碼行數:42,代碼來源:manifest.go

示例8: setEnvVarOrEmptyMap

func setEnvVarOrEmptyMap(appMap, yamlMap generic.Map, key string, errs *ManifestErrors) {
	if !yamlMap.Has(key) {
		appMap.Set(key, generic.NewMap())
		return
	}

	envVars := yamlMap.Get(key)

	if !generic.IsMappable(envVars) {
		*errs = append(*errs, errors.New(fmt.Sprintf("Expected %s to be a set of key => value.", key)))
		return
	}

	merrs := validateEnvVars(envVars)
	if merrs != nil {
		*errs = append(*errs, merrs)
		return
	}

	appMap.Set(key, generic.NewMap(envVars))
}
開發者ID:nsnt,項目名稱:cli,代碼行數:21,代碼來源:manifest.go

示例9: mapToAppParams

func mapToAppParams(yamlMap generic.Map) (appParams cf.AppParams, errs ManifestErrors) {
	appParams = cf.NewEmptyAppParams()

	errs = checkForNulls(yamlMap)
	if !errs.Empty() {
		return
	}

	for _, key := range []string{"buildpack", "command", "disk_quota", "domain", "host", "name", "path", "stack", "no-route"} {
		if yamlMap.Has(key) {
			setStringVal(appParams, key, yamlMap.Get(key), &errs)
		}
	}

	if yamlMap.Has("memory") {
		memory, err := formatters.ToMegabytes(yamlMap.Get("memory").(string))
		if err != nil {
			errs = append(errs, errors.New(fmt.Sprintf("Unexpected value for app memory:\n%s", err.Error())))
			return
		}
		appParams.Set("memory", memory)
	}

	if yamlMap.Has("timeout") {
		setIntVal(appParams, "health_check_timeout", yamlMap.Get("timeout"), &errs)
	}

	if yamlMap.Has("instances") {
		setIntVal(appParams, "instances", yamlMap.Get("instances"), &errs)
	}

	if yamlMap.Has("services") {
		setStringSlice(appParams, "services", yamlMap.Get("services"), &errs)
	} else {
		appParams.Set("services", []string{})
	}

	if yamlMap.Has("env") {
		setEnvVar(appParams, yamlMap.Get("env"), &errs)
	} else {
		appParams.Set("env", generic.NewMap())
	}

	return
}
開發者ID:pmuellr,項目名稱:cli,代碼行數:45,代碼來源:manifest.go


注:本文中的generic.Map.Has方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。