Пример #1
0
func (p *Part) LoadForm(form url.Values) error {
	for key, value := range form {
		switch key {
		case "name":
			p.Name = value[0]
		case "description":
			p.Description = sql.NullString{
				String: value[0],
				Valid:  value[0] != "",
			}
		case "value":
			num, err := si.Parse(value[0])
			if err != nil && err != io.EOF {
				return err
			}
			p.Value = sql.NullFloat64{
				Float64: num.Value(),
				Valid:   value[0] != "",
			}
		case "category":
			val, err := strconv.Atoi(value[0])
			if err != nil {
				return err
			}
			p.CategoryId = int64(val)
		case "place":
			val, err := strconv.Atoi(value[0])
			if err != nil {
				return err
			}
			p.PlaceId = sql.NullInt64{
				Int64: int64(val),
				Valid: val != 0,
			}
		case "owner":
			val, err := strconv.Atoi(value[0])
			if err != nil {
				return err
			}
			p.OwnerId = sql.NullInt64{
				Int64: int64(val),
				Valid: true,
			}
		case "image_id":
			val, err := strconv.Atoi(value[0])
			if err != nil {
				return err
			}
			p.ImageId = sql.NullInt64{
				Int64: int64(val),
				Valid: val != 0,
			}
		}
	}
	return nil
}
Пример #2
0
func loadSearchQuery(c chan searchItem) (*searchQuery, error) {
	res := new(searchQuery)
	for item := range c {
		var err error
		switch item.typ {
		case searchItemUnit:
			res.Unit = item.val[1 : len(item.val)-1]
		case searchItemNumber:
			res.Value, err = si.Parse(strings.Replace(item.val, "_", " ", -1))
		case searchItemText:
			res.Keywords = append(res.Keywords, item.val)
		case searchItemStock:
			res.Stock, err = si.Parse(item.val[1 : len(item.val)-1])
		case searchItemString:
			res.Keywords = append(res.Keywords, item.val[1:len(item.val)-1])
		}
		if err != nil {
			return nil, err
		}
	}

	return res, nil
}