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


Golang Map.Get方法代碼示例

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


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

示例1: 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

示例2: envVarOrEmptyMap

func envVarOrEmptyMap(yamlMap generic.Map, errs *ManifestErrors) *map[string]string {
	key := "env"
	switch envVars := yamlMap.Get(key).(type) {
	case nil:
		aMap := make(map[string]string, 0)
		return &aMap
	case map[string]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]string, envVars.Count())
		generic.Each(envVars, func(key, value interface{}) {
			result[key.(string)] = value.(string)
		})
		return &result
	default:
		*errs = append(*errs, errors.New(fmt.Sprintf("Expected %s to be a set of key => value.", key)))
		return nil
	}
}
開發者ID:normalnorman,項目名稱:cli,代碼行數:26,代碼來源: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: setBytesVal

func setBytesVal(appMap, yamlMap generic.Map, key string, errs *ManifestErrors) {
	value, err := formatters.ToMegabytes(yamlMap.Get(key).(string))
	if err != nil {
		*errs = append(*errs, errors.New(fmt.Sprintf("Unexpected value for %s :\n%s", key, err.Error())))
		return
	}
	appMap.Set(key, value)
}
開發者ID:nsnt,項目名稱:cli,代碼行數:8,代碼來源:manifest.go

示例5: setStringVal

func setStringVal(appMap generic.Map, yamlMap generic.Map, key string, errs *ManifestErrors) {
	val := yamlMap.Get(key)
	stringVal, ok := val.(string)
	if !ok {
		*errs = append(*errs, errors.New(fmt.Sprintf("%s must be a string value", key)))
		return
	}
	appMap.Set(key, stringVal)
}
開發者ID:nsnt,項目名稱:cli,代碼行數:9,代碼來源:manifest.go

示例6: formatDescription

func formatDescription(metadata generic.Map, keys []string) string {
	parts := []string{}
	for _, key := range keys {
		value := metadata.Get(key)
		if value != nil {
			parts = append(parts, fmt.Sprintf("%s: %s", key, formatDescriptionPart(value)))
		}
	}
	return strings.Join(parts, ", ")
}
開發者ID:normalnorman,項目名稱:cli,代碼行數:10,代碼來源:app_events.go

示例7: stringVal

func stringVal(yamlMap generic.Map, key string, errs *[]error) *string {
	val := yamlMap.Get(key)
	if val == nil {
		return nil
	}
	result, ok := val.(string)
	if !ok {
		*errs = append(*errs, errors.NewWithFmt("%s must be a string value", key))
		return nil
	}
	return &result
}
開發者ID:julz,項目名稱:cli,代碼行數:12,代碼來源:manifest.go

示例8: bytesVal

func bytesVal(yamlMap generic.Map, key string, errs *ManifestErrors) *uint64 {
	yamlVal := yamlMap.Get(key)
	if yamlVal == nil {
		return nil
	}
	value, err := formatters.ToMegabytes(yamlVal.(string))
	if err != nil {
		*errs = append(*errs, errors.New(fmt.Sprintf("Unexpected value for %s :\n%s", key, err.Error())))
		return nil
	}
	return &value
}
開發者ID:normalnorman,項目名稱:cli,代碼行數:12,代碼來源:manifest.go

示例9: stringVal

func stringVal(yamlMap generic.Map, key string, errs *ManifestErrors) *string {
	val := yamlMap.Get(key)
	if val == nil {
		return nil
	}
	result, ok := val.(string)
	if !ok {
		*errs = append(*errs, errors.New(fmt.Sprintf("%s must be a string value", key)))
		return nil
	}
	return &result
}
開發者ID:normalnorman,項目名稱:cli,代碼行數:12,代碼來源:manifest.go

示例10: 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

示例11: intVal

func intVal(yamlMap generic.Map, key string, errs *ManifestErrors) *int {
	var (
		intVal int
		err    error
	)

	switch val := yamlMap.Get(key).(type) {
	case string:
		intVal, err = strconv.Atoi(val)
	case int:
		intVal = val
	case int64:
		intVal = int(val)
	case nil:
		return nil
	default:
		err = errors.New(fmt.Sprintf("Expected %s to be a number, but it was a %T.", key, val))
	}

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

	return &intVal
}
開發者ID:popeye17,項目名稱:cli,代碼行數:26,代碼來源:manifest.go

示例12: setStringOrNullVal

func setStringOrNullVal(appMap, yamlMap generic.Map, key string, errs *ManifestErrors) {
	switch val := yamlMap.Get(key).(type) {
	case string:
		appMap.Set(key, val)
	case nil:
		appMap.Set(key, "")
	default:
		*errs = append(*errs, errors.New(fmt.Sprintf("%s must be a string or null value", key)))
	}
}
開發者ID:nsnt,項目名稱:cli,代碼行數:10,代碼來源:manifest.go

示例13: 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

示例14: envVarOrEmptyMap

func envVarOrEmptyMap(yamlMap generic.Map, errs *ManifestErrors) *map[string]string {
	key := "env"
	switch envVars := yamlMap.Get(key).(type) {
	case nil:
		aMap := make(map[string]string, 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]string, envVars.Count())
		generic.Each(envVars, func(key, value interface{}) {

			switch value.(type) {
			case string:
				result[key.(string)] = value.(string)
			case int64, int, int32:
				result[key.(string)] = fmt.Sprintf("%d", value)
			case float32, float64:
				result[key.(string)] = fmt.Sprintf("%f", value)
			default:
				*errs = append(*errs, errors.New(fmt.Sprintf("Expected environment variable %s to have a string value, but it was a %T.", key, value)))
			}

		})
		return &result
	default:
		*errs = append(*errs, errors.New(fmt.Sprintf("Expected %s to be a set of key => value, but it was a %T.", key, envVars)))
		return nil
	}
}
開發者ID:juggernaut,項目名稱:cli,代碼行數:40,代碼來源:manifest.go

示例15: setBoolVal

func setBoolVal(appMap, yamlMap generic.Map, key string, errs *ManifestErrors) {
	switch val := yamlMap.Get(key).(type) {
	case bool:
		appMap.Set(key, val)
	case string:
		boolVal := val == "true"
		appMap.Set(key, boolVal)
	default:
		*errs = append(*errs, errors.New(fmt.Sprintf("Expected %s to be a boolean.", key)))
	}

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


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