Skip to content

Commit 04ed968

Browse files
优化
1 parent 2f8ce9d commit 04ed968

7 files changed

+54
-2
lines changed

conv_any.go

+5
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ func GMap(i interface{}) map[string]interface{} {
138138
return toGMap(i)
139139
}
140140

141+
// SMap 任意类型转 map[string]string
142+
func SMap(i interface{}) map[string]string {
143+
return toSMap(i)
144+
}
145+
141146
// IMap 任意类型转 map[interface{}]interface{}
142147
func IMap(i interface{}) map[interface{}]interface{} {
143148
return toIMap(i)

conv_base.go

+25
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ func toBytes(i interface{}) []byte {
4848
}
4949
}
5050
return toBytes(result)
51+
case *Var:
52+
return toBytes(value.Val())
5153
}
5254
if IsNumber(i) {
5355
// int 类型无法解析
@@ -116,6 +118,8 @@ func toString(i interface{}) string {
116118
case io.Reader:
117119
bs, _ := ioutil.ReadAll(value)
118120
return string(bs)
121+
case *Var:
122+
return toString(value.Val())
119123
default:
120124
if value == nil {
121125
return ""
@@ -204,6 +208,8 @@ func toInt64(i interface{}) int64 {
204208
return int64(value.Int())
205209
case apiInt64:
206210
return value.Int64()
211+
case *Var:
212+
return toInt64(value.Val())
207213
default:
208214
s := toString(value)
209215
base := int64(1)
@@ -291,6 +297,8 @@ func toUint64(i interface{}) uint64 {
291297
return uint64(value.Uint())
292298
case apiUint64:
293299
return value.Uint64()
300+
case *Var:
301+
return toUint64(value.Val())
294302
default:
295303
s := toString(value)
296304
// HEX 十六进制
@@ -339,6 +347,8 @@ func toFloat64(i interface{}) float64 {
339347
return toFloat64(math.Float32frombits(binary.BigEndian.Uint32(padding(value, 4))))
340348
}
341349
return math.Float64frombits(binary.BigEndian.Uint64(padding(value, 8)))
350+
case *Var:
351+
return toFloat64(value.Val())
342352
default:
343353
v, _ := strconv.ParseFloat(toString(i), 64)
344354
return v
@@ -357,6 +367,8 @@ func toBool(i interface{}) bool {
357367
return emptyMap[strings.ToLower(toString(value))] == nil
358368
case apiBool:
359369
return value.Bool()
370+
case *Var:
371+
return toBool(value.Val())
360372
default:
361373
rv := reflect.ValueOf(i)
362374
switch rv.Kind() {
@@ -457,6 +469,13 @@ func toInterfaces(i interface{}) []interface{} {
457469
}
458470
case apiInterfaces:
459471
return value.Interfaces()
472+
case *Var:
473+
return []interface{}{value.Val()}
474+
case []*Var:
475+
array = make([]interface{}, len(value))
476+
for k, v := range value {
477+
array[k] = v.Val()
478+
}
460479
default:
461480
var (
462481
rv = reflect.ValueOf(i)
@@ -490,3 +509,9 @@ func toIMap(i interface{}) map[interface{}]interface{} {
490509
_ = json.Unmarshal(Bytes(i), &m)
491510
return m
492511
}
512+
513+
func toSMap(i interface{}) map[string]string {
514+
m := make(map[string]string)
515+
_ = json.Unmarshal(Bytes(i), &m)
516+
return m
517+
}

conv_init.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package conv

conv_map.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package conv
22

33
import (
4-
"github.com/json-iterator/go"
4+
json "github.com/json-iterator/go"
55
"regexp"
66
"strings"
77
)
@@ -93,7 +93,7 @@ func (this *Map) getList(idx int) *Map {
9393
}
9494

9595
func (this *Map) getParse() func(data []byte, v interface{}) error {
96-
parse := jsoniter.Unmarshal
96+
parse := json.Unmarshal
9797
switch this.model {
9898
case "json":
9999
case "xml":

conv_var.go

+7
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ func (this *Var) GMap(def ...map[string]interface{}) map[string]interface{} {
258258
return GMap(this.Value)
259259
}
260260

261+
func (this *Var) SMap(def ...map[string]string) map[string]string {
262+
if this.IsNil() && len(def) > 0 {
263+
return def[0]
264+
}
265+
return SMap(this.Value)
266+
}
267+
261268
func (this *Var) IMap(def ...map[interface{}]interface{}) map[interface{}]interface{} {
262269
if this.IsNil() && len(def) > 0 {
263270
return def[0]

conv_var_extend.go

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type Extend interface {
4646
GetHour(key string, def ...int) time.Duration
4747
GetErr(key string, def ...error) error
4848
GetGMap(key string, def ...map[string]interface{}) map[string]interface{}
49+
GetSMap(key string, def ...map[string]string) map[string]string
50+
GetIMap(key string, def ...map[interface{}]interface{}) map[interface{}]interface{}
4951
GetDMap(key string, def ...interface{}) *Map
5052
}
5153

@@ -229,6 +231,14 @@ func (this *extend) GetGMap(key string, def ...map[string]interface{}) map[strin
229231
return this.GetVar(key).GMap(def...)
230232
}
231233

234+
func (this *extend) GetSMap(key string, def ...map[string]string) map[string]string {
235+
return this.GetVar(key).SMap(def...)
236+
}
237+
238+
func (this *extend) GetIMap(key string, def ...map[interface{}]interface{}) map[interface{}]interface{} {
239+
return this.GetVar(key).IMap(def...)
240+
}
241+
232242
func (this *extend) GetDMap(key string, def ...interface{}) *Map {
233243
return this.GetVar(key).DMap(def...)
234244
}

conv_z_var_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ import (
77
func TestNew(t *testing.T) {
88
t.Log(String([]string{"a", "b"}))
99
t.Log(New("0xa0").Int())
10+
t.Log(String(New(100)))
11+
t.Log(Interfaces([]*Var{
12+
New(1), New("s"),
13+
}))
1014
}

0 commit comments

Comments
 (0)