Beispiel #1
0
func (s *SliceBase) SetData(data interface{}) error {
	if !toolkit.IsPointer(data) || !toolkit.IsSlice(data) {
		return errors.New("crowd.SliceBase.SetData: data is not pointer of slice")
	}

	s.data = data
	return nil
}
Beispiel #2
0
func NewSorter(data interface{}, fnsort FnCrowd) (s *Sorter, e error) {
	if !toolkit.IsPointer(data) || !toolkit.IsSlice(data) {
		e = errors.New("crowd.NewSorter: data is not pointer of slice")
		return
	}

	s = new(Sorter)
	s.data = data
	s.FnSort = _fn(fnsort)
	return
}
Beispiel #3
0
func (h *Hive) Populate(query string, m interface{}) (e error) {
	if !toolkit.IsPointer(m) {
		e = errorlib.Error("", "", "Fetch", "Model object should be pointer")
		return
	}
	hr, e := h.fetch(query)

	if len(hr.Header) != 0 && len(hr.Result) > 2 {
		Parse(hr.Header, hr.Result[1:], m, h.OutputType, "")
	}
	return
}
Beispiel #4
0
func (c *Cursor) Fetch(m interface{}, n int, closeWhenDone bool) error {

	if closeWhenDone {
		defer c.Close()
	}

	e := c.prepIter()
	if e != nil {
		return errorlib.Error(packageName, modCursor, "Fetch", e.Error())
	}

	if !toolkit.IsPointer(m) {
		return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer")
	}

	if n != 1 && reflect.ValueOf(m).Elem().Kind() != reflect.Slice {
		return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer of slice")
	}

	var v reflect.Type

	if n == 1 {
		v = reflect.TypeOf(m).Elem()
	} else {
		v = reflect.TypeOf(m).Elem().Elem()
	}

	ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)

	lineCount := 0

	//=============================
	// fmt.Println("Qursor 133 : ", c.ConditionVal.Find)
	for {
		iv := reflect.New(v).Interface()

		isAppend := true
		c.count += 1
		recData := toolkit.M{}
		appendData := toolkit.M{}

		dataTemp, e := c.reader.Read()

		for i, val := range dataTemp {
			orgname := c.headerColumn[i].name
			lowername := strings.ToLower(c.headerColumn[i].name)

			switch c.headerColumn[i].dataType {
			case "int":
				recData[lowername] = cast.ToInt(val, cast.RoundingAuto)
			case "float":
				recData[lowername] = cast.ToF64(val, 2, cast.RoundingAuto)
			default:
				recData[lowername] = val
			}

			if len(c.ConditionVal.Select) == 0 || c.ConditionVal.Select.Get("*", 0).(int) == 1 {
				appendData[orgname] = recData[lowername]
			} else {
				if c.ConditionVal.Select.Get(strings.ToLower(c.headerColumn[i].name), 0).(int) == 1 {
					appendData[orgname] = recData[lowername]
				}
			}
		}

		isAppend = c.ConditionVal.getCondition(recData)

		if c.count < c.ConditionVal.skip || (c.count > (c.ConditionVal.skip+c.ConditionVal.limit) && c.ConditionVal.limit > 0) {
			isAppend = false
		}

		if v.Kind() == reflect.Struct {
			for i := 0; i < v.NumField(); i++ {
				if appendData.Has(v.Field(i).Name) {
					switch v.Field(i).Type.Kind() {
					case reflect.Int:
						appendData.Set(v.Field(i).Name, cast.ToInt(appendData[v.Field(i).Name], cast.RoundingAuto))
					}
				}
			}
		}

		if e == io.EOF {
			if isAppend && len(appendData) > 0 {
				toolkit.Serde(appendData, iv, "json")
				ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
				lineCount += 1
			}
			break
		} else if e != nil {
			return errorlib.Error(packageName, modCursor,
				"Fetch", e.Error())
		}

		if isAppend && len(appendData) > 0 {
			toolkit.Serde(appendData, iv, "json")
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
			lineCount += 1
		}

		if n > 0 {
			if lineCount >= n {
				break
			}
		}
	}

	if e != nil {
		return errorlib.Error(packageName, modCursor, "Fetch", e.Error())
	}

	if n == 1 {
		if ivs.Len() > 0 {
			reflect.ValueOf(m).Elem().Set(ivs.Index(0))
		}
	} else {
		reflect.ValueOf(m).Elem().Set(ivs)
	}

	return nil
}
Beispiel #5
0
func Parse(header []string, in interface{}, m interface{}, outputType string, dateFormat string) (e error) {
	// log.Printf("start parse:\n")
	if !toolkit.IsPointer(m) {
		// log.Printf("not pointer\n")
		return errorlib.Error("", "", "Fetch", "Model object should be pointer")
	}
	// log.Printf("pointer\n")
	slice := false
	var ins []string
	if reflect.ValueOf(m).Elem().Kind() == reflect.Slice || toolkit.TypeName(in) == "[]string" {
		slice = true
		ins = in.([]string)
	} else {
		ins = append(ins, in.(string))
	}

	// log.Printf("outputType: %v\n", outputType)

	if outputType == CSV {
		var v reflect.Type

		if slice {
			v = reflect.TypeOf(m).Elem().Elem()
		} else {
			v = reflect.TypeOf(m).Elem()
		}

		ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)
		for _, data := range ins {
			appendData := toolkit.M{}
			iv := reflect.New(v).Interface()
			reader := csv.NewReader(strings.NewReader(""))
			if strings.Contains(data, "','") {
				reader = csv.NewReader(strings.NewReader("\"" + strings.Trim(strings.Replace(data, "','", "\",\"", -1), "'") + "\""))
			} else {
				reader = csv.NewReader(strings.NewReader(data))
			}
			record, e := reader.Read()

			if e != nil {
				return e
			}

			if v.Kind() == reflect.Struct {
				for i := 0; i < v.NumField(); i++ {
					appendData[v.Field(i).Name] = strings.TrimSpace(record[i])
				}

				for i := 0; i < v.NumField(); i++ {
					tag := v.Field(i).Tag

					if appendData.Has(v.Field(i).Name) || appendData.Has(tag.Get("tag_name")) {
						valthis := appendData[v.Field(i).Name]
						if valthis == nil {
							valthis = appendData[tag.Get("tag_name")]
						}

						switch v.Field(i).Type.Kind() {
						case reflect.Int:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Int16:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Int32:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Int64:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Float32:
							valf, _ := strconv.ParseFloat(valthis.(string), 32)
							appendData.Set(v.Field(i).Name, valf)
						case reflect.Float64:
							valf, _ := strconv.ParseFloat(valthis.(string), 64)
							appendData.Set(v.Field(i).Name, valf)
						}

						dtype := DetectFormat(valthis.(string), dateFormat)
						if dtype == "date" {
							valf := cast.String2Date(valthis.(string), dateFormat)
							appendData.Set(v.Field(i).Name, valf)
						} else if dtype == "bool" {
							valf, _ := strconv.ParseBool(valthis.(string))
							appendData.Set(v.Field(i).Name, valf)
						}
					}
				}
			} else {
				for i, val := range header {
					appendData[val] = strings.TrimSpace(record[i])
				}

				if len(header) == 0 {
					e = errorlib.Error("", "", "Parse Out", "Header cant be null because object is not struct")
					return e
				}

				for _, val := range header {
					valthis := appendData[val]
					dtype := DetectFormat(valthis.(string), dateFormat)
					if dtype == "int" {
						appendData.Set(val, cast.ToInt(valthis, cast.RoundingAuto))
					} else if dtype == "float" {
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(val, valf)
					} else if dtype == "date" {
						valf := cast.String2Date(valthis.(string), dateFormat)
						appendData.Set(val, valf)
					} else if dtype == "bool" {
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(val, valf)
					}
				}
			}

			toolkit.Serde(appendData, iv, JSON)
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
		}
		if slice {
			reflect.ValueOf(m).Elem().Set(ivs)
		} else {
			reflect.ValueOf(m).Elem().Set(ivs.Index(0))
		}
	} else if outputType == JSON {
		var temp interface{}
		ins = InspectJson(ins)

		//for catch multi json in one line
		if JsonPart != "" && slice {
			for {
				tempjsonpart := JsonPart
				JsonPart = ""
				tempIn := InspectJson([]string{tempjsonpart})
				if len(tempIn) == 0 {
					break
				} else {
					for _, tin := range tempIn {
						ins = append(ins, tin)
					}
				}
			}
		}

		forSerde := strings.Join(ins, ",")
		if slice {
			forSerde = fmt.Sprintf("[%s]", strings.Join(ins, ","))
		}
		if len(ins) > 0 {
			e := json.Unmarshal([]byte(forSerde), &temp)
			if e != nil {
				return e
			}
			e = toolkit.Serde(temp, m, "json")
			if e != nil {
				return e
			}
		}
	} else {
		var v reflect.Type

		if slice {
			v = reflect.TypeOf(m).Elem().Elem()
		} else {
			v = reflect.TypeOf(m).Elem()
		}

		// log.Printf("v: %v\n", v)

		ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)

		// log.Printf("ivs: %v\n", ivs)

		for _, data := range ins {
			appendData := toolkit.M{}
			iv := reflect.New(v).Interface()

			/*log.Printf("data: %v\n", data)
			log.Printf("iv: %v\n", iv)*/

			splitted := strings.Split(data, "\t")

			/*log.Printf("appendData: %v\n", appendData)
			log.Printf("kind: %v\n", v.Kind())
			log.Printf("test: %v", fmt.Sprintf("%v", v))
			//log.Printf("v.Name: %T\n", v)

			if fmt.Sprintf("%v", v) == "reflect.Value" {
				log.Printf("else: %v\n", "reflect.Value")
				for _, val := range header {
					log.Printf("val: %v\n", val)
					valthis := appendData[val]
					dtype := DetectFormat(valthis.(string), dateFormat)
					if dtype == "int" {
						appendData.Set(val, cast.ToInt(valthis, cast.RoundingAuto))
					} else if dtype == "float" {
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(val, valf)
					} else if dtype == "date" {
						valf := cast.String2Date(valthis.(string), dateFormat)
						appendData.Set(val, valf)
					} else if dtype == "bool" {
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(val, valf)
					}
				}
				log.Printf("appendData: %v\n", appendData)
			} else */
			if v.Kind() == reflect.Struct {
				for i := 0; i < v.NumField(); i++ {
					appendData[v.Field(i).Name] = strings.TrimSpace(strings.Trim(splitted[i], " '"))
				}

				// log.Printf("struct: %v\n", v.Kind())
				for i := 0; i < v.NumField(); i++ {
					tag := v.Field(i).Tag
					// log.Printf("i: %v\n", i)

					// log.Printf("name: (%v) tag: (%v)\n", appendData.Has(v.Field(i).Name), appendData.Has(tag.Get("tag_name")))

					if appendData.Has(v.Field(i).Name) || appendData.Has(tag.Get("tag_name")) {
						valthis := appendData[v.Field(i).Name]
						if valthis == nil {
							valthis = appendData[tag.Get("tag_name")]
						}
						// log.Printf("valthis: %v\n", valthis)
						switch v.Field(i).Type.Kind() {
						case reflect.Int:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Int16:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Int32:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Int64:
							appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
						case reflect.Float32:
							valf, _ := strconv.ParseFloat(valthis.(string), 32)
							appendData.Set(v.Field(i).Name, valf)
						case reflect.Float64:
							valf, _ := strconv.ParseFloat(valthis.(string), 64)
							appendData.Set(v.Field(i).Name, valf)
						}
						dtype := DetectFormat(valthis.(string), dateFormat)
						if dtype == "date" {
							valf := cast.String2Date(valthis.(string), dateFormat)
							appendData.Set(v.Field(i).Name, valf)
						} else if dtype == "bool" {
							valf, _ := strconv.ParseBool(valthis.(string))
							appendData.Set(v.Field(i).Name, valf)
						}
					}
				}

			} else {
				for i, val := range header {
					appendData[val] = strings.TrimSpace(strings.Trim(splitted[i], " '"))
				}

				if len(header) == 0 {
					e = errorlib.Error("", "", "Parse Out", "Header cant be null because object is not struct")
					return e
				}

				// log.Printf("else: %v\n", v.Kind())
				for _, val := range header {
					// log.Printf("val: %v\n", val)
					valthis := appendData[val]
					dtype := DetectFormat(valthis.(string), dateFormat)
					if dtype == "int" {
						appendData.Set(val, cast.ToInt(valthis, cast.RoundingAuto))
					} else if dtype == "float" {
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(val, valf)
					} else if dtype == "date" {
						valf := cast.String2Date(valthis.(string), dateFormat)
						appendData.Set(val, valf)
					} else if dtype == "bool" {
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(val, valf)
					}
				}
			}

			toolkit.Serde(appendData, iv, JSON)
			// log.Printf("iv result: %v\n", iv)
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
			// log.Printf("ivs result: %v\n", ivs)
		}

		if slice {
			reflect.ValueOf(m).Elem().Set(ivs)
		} else {
			reflect.ValueOf(m).Elem().Set(ivs.Index(0))
		}
		// log.Printf("result: %v\n", m)
	}
	return nil
}
Beispiel #6
0
func (c *Cursor) Fetch(m interface{}, n int, closeWhenDone bool) error {
	if closeWhenDone {
		defer c.Close()
	}

	if len(c.indexes) == 0 {
		return nil
	}

	var lower, upper int

	lower = c.currentIndex
	if lower < c.skip {
		lower = c.skip
	}

	upper = lower + n
	if upper > c.limit && c.limit > 0 {
		upper = c.limit
	}

	if !toolkit.IsPointer(m) {
		return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer")
	}

	if n != 1 && reflect.ValueOf(m).Elem().Kind() != reflect.Slice {
		return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer of slice")
	}

	var v reflect.Type

	if n == 1 {
		v = reflect.TypeOf(m).Elem()
	} else {
		v = reflect.TypeOf(m).Elem().Elem()
	}

	ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)

	for {
		appendData := toolkit.M{}
		iv := reflect.New(v).Interface()

		datatemp, ef := c.q.reader.Read()
		c.realIndex += 1
		if c.indexes[c.currentIndex] != c.realIndex {
			continue
		}

		c.currentIndex += 1
		for i, val := range datatemp {
			if len(c.fields) == 0 || c.fields.Has("*") || c.fields.Has(c.q.headerColumn[i].name) {
				appendData[c.q.headerColumn[i].name] = val
			}
		}

		if v.Kind() == reflect.Struct {
			for i := 0; i < v.NumField(); i++ {
				if appendData.Has(v.Field(i).Name) {
					switch v.Field(i).Type.Kind() {
					case reflect.Int:
						appendData.Set(v.Field(i).Name, cast.ToInt(appendData[v.Field(i).Name], cast.RoundingAuto))
					}
				}
			}
		}

		isAppend := lower < c.currentIndex && upper >= c.currentIndex

		if (ef == io.EOF || ef == nil) && isAppend && len(appendData) > 0 {
			toolkit.Serde(appendData, iv, "json")
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
		}

		if ef != nil && ef != io.EOF {
			return errorlib.Error(packageName, modCursor, "Fetch", ef.Error())
		} else if ef == io.EOF || (ivs.Len() >= n && n > 0) {
			break
		}
	}

	// if e != nil {
	// 	return errorlib.Error(packageName, modCursor, "Fetch", e.Error())
	// }

	if n == 1 {
		reflect.ValueOf(m).Elem().Set(ivs.Index(0))
	} else {
		reflect.ValueOf(m).Elem().Set(ivs)
	}

	return nil
}
Beispiel #7
0
func Parse(header []string, in interface{}, m interface{}, outputType string, dateFormat string) (e error) {
	if !toolkit.IsPointer(m) {
		return errorlib.Error("", "", "Fetch", "Model object should be pointer")
	}
	slice := false
	var ins []string
	if reflect.ValueOf(m).Elem().Kind() == reflect.Slice || toolkit.TypeName(in) == "[]string" {
		slice = true
		ins = in.([]string)
	} else {
		ins = append(ins, in.(string))
	}

	if outputType == CSV {
		var v reflect.Type

		if slice && toolkit.TypeName(m) != "*interface {}" {
			v = reflect.TypeOf(m).Elem().Elem()
		} else {
			v = reflect.TypeOf(m).Elem()
		}

		ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)
		for _, data := range ins {
			appendData := toolkit.M{}
			iv := reflect.New(v).Interface()
			reader := csv.NewReader(strings.NewReader(""))
			if strings.Contains(data, "','") {
				reader = csv.NewReader(strings.NewReader("\"" + strings.Trim(strings.Replace(data, "','", "\",\"", -1), "'") + "\""))
			} else {
				reader = csv.NewReader(strings.NewReader(data))
			}
			record, e := reader.Read()

			if e != nil {
				return e
			}

			if v.Kind() == reflect.Struct {
				for i := 0; i < v.NumField(); i++ {
					appendData[v.Field(i).Name] = strings.TrimSpace(record[i])
					valthis := appendData[v.Field(i).Name]
					switch v.Field(i).Type.Kind() {
					case reflect.Int:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Int16:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Int32:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Int64:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Float32:
						valf, _ := strconv.ParseFloat(valthis.(string), 32)
						appendData.Set(v.Field(i).Name, valf)
					case reflect.Float64:
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(v.Field(i).Name, valf)
					case reflect.Bool:
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(v.Field(i).Name, valf)
					default:
						dtype := DetectDataType(valthis.(string), dateFormat)
						if dtype == "date" {
							valf := cast.String2Date(valthis.(string), dateFormat)
							appendData.Set(v.Field(i).Name, valf)
						}
					}

				}

			} else {
				if len(header) == 0 {
					e = errorlib.Error("", "", "Parse Out", "Header cant be null because object is not struct")
					return e
				}

				for i, val := range header {
					appendData[val] = strings.TrimSpace(record[i])
					valthis := appendData[val]
					dtype := DetectDataType(valthis.(string), dateFormat)
					if dtype == "int" {
						appendData.Set(val, cast.ToInt(valthis, cast.RoundingAuto))
					} else if dtype == "float" {
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(val, valf)
					} else if dtype == "date" {
						valf := cast.String2Date(valthis.(string), dateFormat)
						appendData.Set(val, valf)
					} else if dtype == "bool" {
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(val, valf)
					}
				}
			}

			toolkit.Serde(appendData, iv, JSON)
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
		}
		if slice {
			reflect.ValueOf(m).Elem().Set(ivs)
		} else {
			reflect.ValueOf(m).Elem().Set(ivs.Index(0))
		}
	} else if outputType == JSON {
		var temp interface{}
		ins = InspectJson(ins)

		//for catch multi json in one line
		if JsonPart != "" && slice {
			for {
				tempjsonpart := JsonPart
				JsonPart = ""
				tempIn := InspectJson([]string{tempjsonpart})
				if len(tempIn) == 0 {
					break
				} else {
					for _, tin := range tempIn {
						ins = append(ins, tin)
					}
				}
			}
		}

		forSerde := strings.Join(ins, ",")
		if slice {
			forSerde = fmt.Sprintf("[%s]", strings.Join(ins, ","))
		}
		if len(ins) > 0 {
			e := json.Unmarshal([]byte(forSerde), &temp)
			if e != nil {
				return e
			}
			e = toolkit.Serde(temp, m, "json")
			if e != nil {
				return e
			}
		}
	} else {
		var v reflect.Type

		if slice && toolkit.TypeName(m) != "*interface {}" {
			v = reflect.TypeOf(m).Elem().Elem()
		} else {
			v = reflect.TypeOf(m).Elem()
		}

		ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)

		for _, data := range ins {
			appendData := toolkit.M{}
			iv := reflect.New(v).Interface()

			splitted := strings.Split(data, "\t")

			if v.Kind() == reflect.Struct {
				for i := 0; i < v.NumField(); i++ {
					appendData[v.Field(i).Name] = strings.TrimSpace(strings.Trim(splitted[i], " '"))
					valthis := appendData[v.Field(i).Name]
					switch v.Field(i).Type.Kind() {
					case reflect.Int:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Int16:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Int32:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Int64:
						appendData.Set(v.Field(i).Name, cast.ToInt(valthis, cast.RoundingAuto))
					case reflect.Float32:
						valf, _ := strconv.ParseFloat(valthis.(string), 32)
						appendData.Set(v.Field(i).Name, valf)
					case reflect.Float64:
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(v.Field(i).Name, valf)
					case reflect.Bool:
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(v.Field(i).Name, valf)
					default:
						dtype := DetectDataType(valthis.(string), dateFormat)
						if dtype == "date" {
							valf := cast.String2Date(valthis.(string), dateFormat)
							appendData.Set(v.Field(i).Name, valf)
						}
					}

				}

			} else {

				if len(header) == 0 {
					e = errorlib.Error("", "", "Parse Out", "Header cant be null because object is not struct")
					return e
				}

				for i, val := range header {
					appendData[val] = strings.TrimSpace(strings.Trim(splitted[i], " '"))
					valthis := appendData[val]
					dtype := DetectDataType(valthis.(string), dateFormat)
					if dtype == "int" {
						appendData.Set(val, cast.ToInt(valthis, cast.RoundingAuto))
					} else if dtype == "float" {
						valf, _ := strconv.ParseFloat(valthis.(string), 64)
						appendData.Set(val, valf)
					} else if dtype == "date" {
						valf := cast.String2Date(valthis.(string), dateFormat)
						appendData.Set(val, valf)
					} else if dtype == "bool" {
						valf, _ := strconv.ParseBool(valthis.(string))
						appendData.Set(val, valf)
					}
				}
			}

			toolkit.Serde(appendData, iv, JSON)
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
		}

		if slice {
			reflect.ValueOf(m).Elem().Set(ivs)
		} else {
			reflect.ValueOf(m).Elem().Set(ivs.Index(0))
		}
	}
	return nil
}
Beispiel #8
0
func (c *Cursor) Fetch(m interface{}, n int, closeWhenDone bool) error {

	if closeWhenDone {
		defer c.Close()
	}

	e := c.prepIter()
	if e != nil {
		return errorlib.Error(packageName, modCursor, "Fetch", e.Error())
	}

	if !toolkit.IsPointer(m) {
		return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer")
	}

	if n != 1 && reflect.ValueOf(m).Elem().Kind() != reflect.Slice {
		return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer of slice")
	}

	var v reflect.Type

	if n == 1 && reflect.ValueOf(m).Elem().Kind() != reflect.Slice {
		v = reflect.TypeOf(m).Elem()
	} else {
		v = reflect.TypeOf(m).Elem().Elem()
	}

	ivs := reflect.MakeSlice(reflect.SliceOf(v), 0, 0)

	lineCount := 0

	//=============================
	// fmt.Println("Qursor 191 : ", c.headerColumn)
	for {
		iv := reflect.New(v).Interface()

		isAppend := true
		// c.count += 1
		recData := toolkit.M{}
		appendData := toolkit.M{}

		dataTemp, e := c.reader.Read()

		for i, val := range dataTemp {
			orgname := c.headerColumn[i].name
			lowername := strings.ToLower(c.headerColumn[i].name)

			switch c.headerColumn[i].dataType {
			case "int":
				recData[lowername] = cast.ToInt(val, cast.RoundingAuto)
			case "float":
				decimalPoint := len(val) - (strings.Index(val, ".") + 1)
				recData[lowername] = cast.ToF64(val, decimalPoint, cast.RoundingAuto)
			case "date":
				recData[lowername] = toolkit.String2Date(val, c.headerColumn[i].format) // Just for test
				// fmt.Printf("FOR DEBUG : %v \n", c.headerColumn[i].format)
			default:
				recData[lowername] = val
			}

			if len(c.ConditionVal.Select) == 0 || c.ConditionVal.Select.Get("*", 0).(int) == 1 {
				appendData[orgname] = recData[lowername]
			} else {
				if c.ConditionVal.Select.Get(strings.ToLower(c.headerColumn[i].name), 0).(int) == 1 {
					appendData[orgname] = recData[lowername]
				}
			}
		}

		isAppend = c.ConditionVal.getCondition(recData)

		if isAppend {
			c.count += 1
		}

		if c.count <= c.ConditionVal.skip || (c.count > (c.ConditionVal.skip+c.ConditionVal.limit) && c.ConditionVal.limit > 0) {
			isAppend = false
		}
		// fmt.Printf("%v - %v \n", v.Elem().Kind(), toolkit.TypeName(v))
		if v.Kind() == reflect.Struct || v.Elem().Kind() == reflect.Struct {
			tv := v
			if v.Elem().Kind() == reflect.Struct {
				tv = v.Elem()
			}

			for i := 0; i < tv.NumField(); i++ {
				str := tv.Field(i).Name
				fcond := false
				if appendData.Has(str) {
					fcond = true
				} else if appendData.Has(strings.ToLower(str)) {
					fcond = true
					str = strings.ToLower(str)
				} else if strings.ToLower(str) == "id" && appendData.Has("_id") {
					str = "_id"
					fcond = true
				}

				if fcond {
					switch tv.Field(i).Type.Kind() {
					case reflect.Int:
						appendData.Set(str, cast.ToInt(appendData[str], cast.RoundingAuto))
					case reflect.String:
						appendData.Set(str, toolkit.ToString(appendData[str]))
					case reflect.Float64:
						tstr := toolkit.ToString(appendData[str])
						decimalPoint := len(tstr) - (strings.Index(tstr, ".") + 1)
						appendData.Set(str, toolkit.ToFloat64(tstr, decimalPoint, toolkit.RoundingAuto))
					}
				}
			}
		}

		if e == io.EOF {
			if isAppend && len(appendData) > 0 {
				toolkit.Serde(appendData, iv, "json")
				ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
				lineCount += 1
			}
			break
		} else if e != nil {
			return errorlib.Error(packageName, modCursor,
				"Fetch", e.Error())
		}

		if isAppend && len(appendData) > 0 {
			toolkit.Serde(appendData, iv, "json")
			ivs = reflect.Append(ivs, reflect.ValueOf(iv).Elem())
			lineCount += 1
		}

		if n > 0 {
			if lineCount >= n {
				break
			}
		}
	}

	if e != nil {
		return errorlib.Error(packageName, modCursor, "Fetch", e.Error())
	}

	if n == 1 && reflect.ValueOf(m).Elem().Kind() != reflect.Slice {
		if ivs.Len() > 0 {
			reflect.ValueOf(m).Elem().Set(ivs.Index(0))
		}
	} else {
		reflect.ValueOf(m).Elem().Set(ivs)
	}

	return nil
}