本文整理汇总了Golang中github.com/spf13/cast.ToStringMap函数的典型用法代码示例。如果您正苦于以下问题:Golang ToStringMap函数的具体用法?Golang ToStringMap怎么用?Golang ToStringMap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ToStringMap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Read
func Read(ymlFlashMap map[string]interface{}) (FlashMap, error) {
flashMap := newFlashMap()
ymlAreas := ymlFlashMap["areas"]
if ymlAreas == nil {
return flashMap, util.NewNewtError(
"\"areas\" mapping missing from flash map definition")
}
areaMap := cast.ToStringMap(ymlAreas)
for k, v := range areaMap {
if _, ok := flashMap.Areas[k]; ok {
return flashMap, flashAreaErr(k, "name conflict")
}
ymlArea := cast.ToStringMap(v)
area, err := parseFlashArea(k, ymlArea)
if err != nil {
return flashMap, flashAreaErr(k, err.Error())
}
flashMap.Areas[k] = area
}
flashMap.detectOverlaps()
return flashMap, nil
}
示例2: TestFlatMap
func TestFlatMap(t *testing.T) {
var v interface{}
var err error
m := map[string]interface{}{
".cat": "garfield",
".dog": "odie",
".friends.[0]": "John",
".turtle": "0",
".birdColors.cardinal": "red",
".birdColors.blueJay": "blue",
".57": int64(57),
".doesItWork": true,
}
f := NewFlatMap(m)
assert.Equal(t, f.Contains("cat"), true)
assert.Equal(t, f.Contains(".cat"), false)
assert.Equal(t, f.Contains(".birdColors.cardinal"), false)
f.Delete("birdColors")
r := map[string]interface{}{
"cat": "garfield",
"dog": "odie",
"friends": []interface{}{"John"},
"turtle": "0",
"57": int64(57),
"doesItWork": true,
}
v, err = Expand(f.Map, "")
assert.Equal(t, cast.ToStringMap(v), r)
assert.Equal(t, err, nil)
m2 := map[string]interface{}{
".friends.[0]": "Odie",
".friends.[1]": "John",
}
f2 := NewFlatMap(m2)
f.Merge(f2)
r2 := map[string]interface{}{
"cat": "garfield",
"dog": "odie",
"friends": []interface{}{"Odie", "John"},
"turtle": "0",
"57": int64(57),
"doesItWork": true,
}
v, err = Expand(f.Map, "")
// FIXME: Buggy because of array order issues
assert.Equal(t, cast.ToStringMap(v), r2)
assert.Equal(t, err, nil)
}
示例3: searchMap
// searchMap recursively searches for a value for path in source map.
// Returns nil if not found.
// Note: This assumes that the path entries and map keys are lower cased.
func (v *Viper) searchMap(source map[string]interface{}, path []string) interface{} {
if len(path) == 0 {
return source
}
next, ok := source[path[0]]
if ok {
// Fast path
if len(path) == 1 {
return next
}
// Nested case
switch next.(type) {
case map[interface{}]interface{}:
return v.searchMap(cast.ToStringMap(next), path[1:])
case map[string]interface{}:
// Type assertion is safe here since it is only reached
// if the type of `next` is the same as the type being asserted
return v.searchMap(next.(map[string]interface{}), path[1:])
default:
// got a value but nested key expected, return "nil" for not found
return nil
}
}
return nil
}
示例4: flattenAndMergeMap
// flattenAndMergeMap recursively flattens the given map into a map[string]bool
// of key paths (used as a set, easier to manipulate than a []string):
// - each path is merged into a single key string, delimited with v.keyDelim (= ".")
// - if a path is shadowed by an earlier value in the initial shadow map,
// it is skipped.
// The resulting set of paths is merged to the given shadow set at the same time.
func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interface{}, prefix string) map[string]bool {
if shadow != nil && prefix != "" && shadow[prefix] {
// prefix is shadowed => nothing more to flatten
return shadow
}
if shadow == nil {
shadow = make(map[string]bool)
}
var m2 map[string]interface{}
if prefix != "" {
prefix += v.keyDelim
}
for k, val := range m {
fullKey := prefix + k
switch val.(type) {
case map[string]interface{}:
m2 = val.(map[string]interface{})
case map[interface{}]interface{}:
m2 = cast.ToStringMap(val)
default:
// immediate value
shadow[strings.ToLower(fullKey)] = true
continue
}
// recursively merge to shadow map
shadow = v.flattenAndMergeMap(shadow, m2, fullKey)
}
return shadow
}
示例5: mergeFlatMap
// mergeFlatMap merges the given maps, excluding values of the second map
// shadowed by values from the first map.
func (v *Viper) mergeFlatMap(shadow map[string]bool, mi interface{}) map[string]bool {
// unify input map
var m map[string]interface{}
switch mi.(type) {
case map[string]string, map[string]FlagValue:
m = cast.ToStringMap(mi)
default:
return shadow
}
// scan keys
outer:
for k, _ := range m {
path := strings.Split(k, v.keyDelim)
// scan intermediate paths
var parentKey string
for i := 1; i < len(path); i++ {
parentKey = strings.Join(path[0:i], v.keyDelim)
if shadow[parentKey] {
// path is shadowed, continue
continue outer
}
}
// add key
shadow[strings.ToLower(k)] = true
}
return shadow
}
示例6: getRenderingConfig
func (p *Page) getRenderingConfig() *helpers.Blackfriday {
p.renderingConfigInit.Do(func() {
pageParam := p.GetParam("blackfriday")
siteParam := viper.GetStringMap("blackfriday")
combinedParam := siteParam
if pageParam != nil {
combinedParam = make(map[string]interface{})
for k, v := range siteParam {
combinedParam[k] = v
}
pageConfig := cast.ToStringMap(pageParam)
for key, value := range pageConfig {
combinedParam[key] = value
}
}
p.renderingConfig = helpers.NewBlackfriday()
if err := mapstructure.Decode(combinedParam, p.renderingConfig); err != nil {
jww.FATAL.Printf("Failed to get rendering config for %s:\n%s", p.BaseFileName(), err.Error())
}
})
return p.renderingConfig
}
示例7: Get
// Get can retrieve any value given the key to use
// Get returns an interface. For a specific value use one of the Get____ methods.
func (c RawConfig) Get(key string) interface{} {
path := strings.Split(key, keyDelim)
val := c.find(strings.ToLower(key))
if val == nil {
source := c.find(path[0])
if source == nil {
return nil
}
if reflect.TypeOf(source).Kind() == reflect.Map {
val = c.searchMap(cast.ToStringMap(source), path[1:])
}
}
switch val.(type) {
case bool:
return cast.ToBool(val)
case string:
return cast.ToString(val)
case int64, int32, int16, int8, int:
return cast.ToInt(val)
case float64, float32:
return cast.ToFloat64(val)
case time.Time:
return cast.ToTime(val)
case time.Duration:
return cast.ToDuration(val)
case []string:
return val
}
return val
}
示例8: TestDifferentFrontMatterVarTypes
func TestDifferentFrontMatterVarTypes(t *testing.T) {
page, _ := NewPage("test/file1.md")
_, _ = page.ReadFrom(strings.NewReader(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES))
dateval, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
if page.GetParam("a_string") != "bar" {
t.Errorf("frontmatter not handling strings correctly should be %s, got: %s", "bar", page.GetParam("a_string"))
}
if page.GetParam("an_integer") != 1 {
t.Errorf("frontmatter not handling ints correctly should be %s, got: %s", "1", page.GetParam("an_integer"))
}
if page.GetParam("a_float") != 1.3 {
t.Errorf("frontmatter not handling floats correctly should be %f, got: %s", 1.3, page.GetParam("a_float"))
}
if page.GetParam("a_bool") != false {
t.Errorf("frontmatter not handling bools correctly should be %t, got: %s", false, page.GetParam("a_bool"))
}
if page.GetParam("a_date") != dateval {
t.Errorf("frontmatter not handling dates correctly should be %s, got: %s", dateval, page.GetParam("a_date"))
}
param := page.GetParam("a_table")
if param == nil {
t.Errorf("frontmatter not handling tables correctly should be type of %v, got: type of %v", reflect.TypeOf(page.Params["a_table"]), reflect.TypeOf(param))
}
if cast.ToStringMap(param)["a_key"] != "a_value" {
t.Errorf("frontmatter not handling values inside a table correctly should be %s, got: %s", "a_value", cast.ToStringMap(page.Params["a_table"])["a_key"])
}
}
示例9: NewBlackfriday
// NewBlackfriday creates a new Blackfriday filled with site config or some sane defaults.
func NewBlackfriday() *Blackfriday {
combinedParam := map[string]interface{}{
"smartypants": true,
"angledQuotes": false,
"fractions": true,
"hrefTargetBlank": false,
"smartDashes": true,
"latexDashes": true,
"plainIDAnchors": true,
"sourceRelativeLinks": false,
"sourceRelativeLinksProjectFolder": "/docs/content",
}
siteParam := viper.GetStringMap("blackfriday")
if siteParam != nil {
siteConfig := cast.ToStringMap(siteParam)
for key, value := range siteConfig {
combinedParam[key] = value
}
}
combinedConfig := &Blackfriday{}
if err := mapstructure.Decode(combinedParam, combinedConfig); err != nil {
jww.FATAL.Printf("Failed to get site rendering config\n%s", err.Error())
}
return combinedConfig
}
示例10: TestMapMap
func TestMapMap(t *testing.T) {
var v interface{}
var processingErrors []error
var err error
m := map[string]interface{}{
"cat": "garfield",
"dog": "odie",
"friends": []interface{}{"John"},
"turtle": "0",
"57": int64(57),
"doesItWork": true,
}
r := map[string]interface{}{
"cat": "garfield",
"frog": "0",
"myOnlyFriend": "John",
}
var mappers []*Mapper
mappers = append(mappers, NewMapper("cat", "cat"))
mappers = append(mappers, NewMapper("turtle", "frog"))
mappers = append(mappers, NewMapper("friends.[0]", "myOnlyFriend"))
v, processingErrors, err = MapIt(m, mappers)
assert.Equal(t, cast.ToStringMap(v), r)
assert.Equal(t, processingErrors, []error{})
assert.Equal(t, len(processingErrors), 0)
assert.Equal(t, err, nil)
}
示例11: searchMap
func (v *Viper) searchMap(source map[string]interface{}, path []string) interface{} {
if len(path) == 0 {
return source
}
var ok bool
var next interface{}
for k, v := range source {
if strings.ToLower(k) == strings.ToLower(path[0]) {
ok = true
next = v
break
}
}
if ok {
switch next.(type) {
case map[interface{}]interface{}:
return v.searchMap(cast.ToStringMap(next), path[1:])
case map[string]interface{}:
// Type assertion is safe here since it is only reached
// if the type of `next` is the same as the type being asserted
return v.searchMap(next.(map[string]interface{}), path[1:])
default:
return next
}
} else {
return nil
}
}
示例12: updateIndex
// Updates our lookup table of insensitive materialized paths to their
// corresponding 'real' keys. E.g.
//
// Database.Connections.Hosts <- database.connections.hosts
//
// By maintaining a separate index and maintaining case in the original
// stringmaps (e.g. by lowercasing keys directly) we accomodate the passing
// of config data to structures that ~may~ be case sensitive. I.E we avoid
// destructive operations on configurationd data.
func (self *ConfigSource) updateIndex(key string, data interface{}) {
if data == nil {
return
}
// Don't change the case of the original key if it already exists.
_, index_exists := self.index[strings.ToLower(key)]
if index_exists == false {
self.index[strings.ToLower(key)] = key
}
if reflect.TypeOf(data).Kind() != reflect.Map {
return
}
for child_key, val := range cast.ToStringMap(data) {
var joined_key string
if len(key) > 0 {
joined_key = key + "." + child_key
} else {
joined_key = child_key
}
self.updateIndex(joined_key, val)
}
}
示例13: MergeAttributes
// Merges data into the our attributes configuration tier from a struct.
func (manager *Config) MergeAttributes(val interface{}) error {
merged_config := maps.Merge(
manager.attributes.ToStringMap(),
cast.ToStringMap(val),
)
manager.attributes.FromStringMap(merged_config)
return nil
}
示例14: Sub
func (v *Viper) Sub(key string) *Viper {
subv := New()
data := v.Get(key)
if reflect.TypeOf(data).Kind() == reflect.Map {
subv.config = cast.ToStringMap(data)
return subv
}
return nil
}
示例15: toCaseInsensitiveValue
// toCaseInsensitiveValue checks if the value is a map;
// if so, create a copy and lower-case the keys recursively.
func toCaseInsensitiveValue(value interface{}) interface{} {
switch v := value.(type) {
case map[interface{}]interface{}:
value = copyAndInsensitiviseMap(cast.ToStringMap(v))
case map[string]interface{}:
value = copyAndInsensitiviseMap(v)
}
return value
}