-
Notifications
You must be signed in to change notification settings - Fork 0
/
setters.go
43 lines (38 loc) · 946 Bytes
/
setters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Created by Yakka (https://theyakka.com)
//
// Copyright (c) 2020 Yakka LLC.
// All rights reserved.
// See the LICENSE file for licensing details and requirements.
package ystore
import (
"github.com/spf13/cast"
"strings"
)
func (ds *Store) Set(key string, value interface{}) {
splitKey := strings.Split(key, DataKeySeparator)
if len(splitKey) == 1 {
ds.data[key] = value
return
}
mapValue := value
for i := len(splitKey) - 1; i > 0; i-- {
mapValue = map[string]interface{}{
splitKey[i]: mapValue,
}
}
ds.data[splitKey[0]] = mapValue
}
func (ds *Store) SetAsString(key string, value interface{}) {
splitKey := strings.Split(key, DataKeySeparator)
if len(splitKey) == 1 {
ds.data[key] = cast.ToString(value)
return
}
var mapValue map[string]interface{}
for i := len(splitKey) - 1; i > 0; i-- {
mapValue = map[string]interface{}{
splitKey[i]: cast.ToString(value),
}
}
ds.data[splitKey[0]] = mapValue
}