0x4a52466c696e74 il y a 2 ans
Parent
commit
1bad2fb7b9
3 fichiers modifiés avec 88 ajouts et 4 suppressions
  1. 16 0
      value/tools.go
  2. 66 2
      value/value.go
  3. 6 2
      value/z_test.go

+ 16 - 0
value/tools.go

@@ -0,0 +1,16 @@
+package value
+
+import "reflect"
+
+func isKindNumber(k reflect.Kind) bool {
+	switch k {
+	case reflect.Float32, reflect.Float64:
+		return true
+	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+		return true
+	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
+		return true
+	default:
+		return false
+	}
+}

+ 66 - 2
value/value.go

@@ -65,7 +65,6 @@ func (s *Value) Setup(val interface{}) (res bool) {
 				}
 			case reflect.Slice:
 				{
-					log.Println("Slice")
 					slType := rType.Elem()
 					if rr.Kind() == reflect.Slice {
 						slRes := reflect.MakeSlice(slType, rr.Len(), rr.Cap())
@@ -79,8 +78,73 @@ func (s *Value) Setup(val interface{}) (res bool) {
 						rr.Elem().Set(slRes)
 						res = true
 					} else if rl.Kind() == reflect.String {
-
+						slType := rType.Elem()
+						sls := strings.Split(rl.String(), ",")
+						log.Println(slType, rr.Type())
+						slRes := reflect.MakeSlice(rr.Type().Elem(), len(sls), cap(sls))
+						for i, sVal := range sls {
+							sItem := reflect.ValueOf(strings.TrimSpace(sVal))
+							switch {
+							case isKindNumber(slType.Kind()):
+								{
+									numVal, err := strconv.ParseFloat(sItem.String(), 64)
+									if err != nil {
+										return false
+									}
+									sItem = reflect.ValueOf(numVal)
+									slRes.Index(i).Set(sItem.Convert(slType))
+								}
+							default:
+								{
+									if !sItem.CanConvert(slType) {
+										return false
+									}
+									slRes.Index(i).Set(sItem.Convert(slType))
+								}
+							}
+						}
+						rr.Elem().Set(slRes)
+						res = true
 					}
+					/*
+						log.Println("Slice")
+						slType := rType.Elem()
+						if rr.Kind() == reflect.Slice {
+							slRes := reflect.MakeSlice(slType, rr.Len(), rr.Cap())
+							for i := 0; i < rl.Len(); i++ {
+								if val := rl.Index(i); val.CanConvert(slType) {
+									slRes.Index(i).Set(val.Convert(slType))
+								} else {
+									return false
+								}
+							}
+							rr.Elem().Set(slRes)
+							res = true
+						} else if rl.Kind() == reflect.String {
+							log.Println("sl <===> str")
+							sls := strings.Split(rl.String(), ",")
+							log.Println(slType, rr.Type())
+							slRes := reflect.MakeSlice(slType, len(sls), cap(sls))
+							switch {
+							case isKindNumber(slType.Kind()):
+								log.Println("AAAAAAAAAAAAAAAAA")
+								for i, sVal := range sls {
+									numVal, err := strconv.ParseFloat(sVal, 64)
+									if err != nil {
+										return false
+									}
+									rl := reflect.ValueOf(numVal)
+									if val := rl.Index(i); val.CanConvert(slType) {
+										slRes.Index(i).Set(val.Convert(slType))
+									} else {
+										return false
+									}
+								}
+								rr.Elem().Set(slRes)
+								res = true
+							}
+						}
+					*/
 				}
 			default:
 				// json

+ 6 - 2
value/z_test.go

@@ -13,7 +13,11 @@ func TestValue(t *testing.T) {
 	val = ValueOf(100.55)
 	log.Println(val.Int())
 
-	val = ValueOf("one, two, three")
+	val = ValueOf([]string{"one", "two", "three"})
 	var sls []string
-	t.Log(val.Setup(&sls))
+	t.Log(val.Setup(&sls), sls)
+
+	var ils []int
+	val = ValueOf("10, 11, 12")
+	t.Log(val.Setup(&ils), ils)
 }