|
| 1 | +package cmap |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/pkg/errors" |
| 8 | + "net/url" |
| 9 | + "reflect" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +//type Map map[string]interface{} |
| 14 | +type CMap map[string][]string |
| 15 | + |
| 16 | +// Get gets the first value associated with the given key. |
| 17 | +// If there are no values associated with the key, Get returns |
| 18 | +// the empty string. To access multiple values, use the map |
| 19 | +// directly. |
| 20 | +func (v CMap) Get(key string) string { |
| 21 | + if v == nil { |
| 22 | + return "" |
| 23 | + } |
| 24 | + vs := v[key] |
| 25 | + if len(vs) == 0 { |
| 26 | + return "" |
| 27 | + } |
| 28 | + return vs[0] |
| 29 | +} |
| 30 | + |
| 31 | +// return Get value and Del key |
| 32 | +func (v CMap) Pop(key string) string { |
| 33 | + s := v.Get(key) |
| 34 | + v.Del(key) |
| 35 | + return s |
| 36 | +} |
| 37 | + |
| 38 | +// Set sets the key to value. It replaces any existing |
| 39 | +// values. |
| 40 | +func (v CMap) Set(key, value string) CMap { |
| 41 | + v[key] = []string{value} |
| 42 | + return v |
| 43 | +} |
| 44 | + |
| 45 | +// Add adds the value to key. It appends to any existing |
| 46 | +// values associated with key. |
| 47 | +func (v CMap) Add(key, value string) CMap { |
| 48 | + v[key] = append(v[key], value) |
| 49 | + return v |
| 50 | +} |
| 51 | + |
| 52 | +// Del deletes the values associated with key. |
| 53 | +func (v CMap) Del(key string) CMap { |
| 54 | + delete(v, key) |
| 55 | + return v |
| 56 | +} |
| 57 | + |
| 58 | +// Obtain get all values associated with the given key. |
| 59 | +func (v CMap) Obtain(key string) []string { |
| 60 | + if v == nil { |
| 61 | + return []string{} |
| 62 | + } |
| 63 | + return v[key] |
| 64 | +} |
| 65 | + |
| 66 | +// Append set the key to value if it doesn't exists. append if it exists. |
| 67 | +func (v CMap) Append(key, value string) CMap { |
| 68 | + vs := v.Get(key) |
| 69 | + if vs == "" || len(strings.Trim(vs, " ")) == 0 { |
| 70 | + v.Set(key, value) |
| 71 | + return v |
| 72 | + } |
| 73 | + return v.Set(key, vs+value) |
| 74 | +} |
| 75 | + |
| 76 | +// CMap to struct or map[string]string or map[string]interface{} data |
| 77 | +func (v CMap) ToStruct(value interface{}) error { |
| 78 | + var m = make(map[string]interface{}) |
| 79 | + for k, v := range v { |
| 80 | + m[k] = v[0] |
| 81 | + } |
| 82 | + b, err := json.Marshal(m) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + t := reflect.TypeOf(value) |
| 87 | + if t.Kind() == reflect.Ptr { |
| 88 | + t = t.Elem() |
| 89 | + if t.Kind() == reflect.Struct || t.Kind() == reflect.Map { |
| 90 | + err = json.Unmarshal(b, value) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + } |
| 95 | + return nil |
| 96 | + } |
| 97 | + return errors.New("Type mismatch. Must be a pointer to Map or structure") |
| 98 | +} |
| 99 | + |
| 100 | +func (v CMap) ToValues() url.Values { |
| 101 | + return url.Values(v) |
| 102 | +} |
| 103 | + |
| 104 | +func (v CMap) ToMap() map[string]interface{} { |
| 105 | + param := make(map[string]interface{}) |
| 106 | + _ = v.ToStruct(¶m) |
| 107 | + return param |
| 108 | +} |
| 109 | + |
| 110 | +// new a CMap |
| 111 | +func New() CMap { |
| 112 | + return CMap{} |
| 113 | +} |
| 114 | + |
| 115 | +// new a CMap from url.Values |
| 116 | +func Values(values url.Values) CMap { |
| 117 | + return CMap(values) |
| 118 | +} |
| 119 | + |
| 120 | +// new a CMap from struct |
| 121 | +func Struct(v interface{}) (values CMap) { |
| 122 | + values = New() |
| 123 | + iVal := reflect.ValueOf(v) |
| 124 | + if iVal.Kind() == reflect.Ptr { |
| 125 | + iVal = iVal.Elem() |
| 126 | + } |
| 127 | + typ := iVal.Type() |
| 128 | + for i := 0; i < iVal.NumField(); i++ { |
| 129 | + fi := typ.Field(i) |
| 130 | + name := fi.Tag.Get("json") |
| 131 | + if name == "" { |
| 132 | + name = fi.Name |
| 133 | + } |
| 134 | + // add support slice |
| 135 | + if iVal.Field(i).Kind() == reflect.Slice { |
| 136 | + var buf bytes.Buffer |
| 137 | + buf.WriteString("[") |
| 138 | + iValArr := iVal.Field(i) |
| 139 | + for j := 0; j < iValArr.Len(); j++ { |
| 140 | + buf.WriteString(fmt.Sprint(`"`, iValArr.Index(j), `",`)) |
| 141 | + } |
| 142 | + val := string(buf.Bytes()[:buf.Len()-1]) |
| 143 | + val += "]" |
| 144 | + values.Set(name, val) |
| 145 | + continue |
| 146 | + } |
| 147 | + values.Set(name, fmt.Sprint(iVal.Field(i))) |
| 148 | + } |
| 149 | + return |
| 150 | +} |
0 commit comments