Ejemplo n.º 1
0
func convertJsonValueToProperties(k string, v interface{}) []datastore.Property {
	propertyList := make([]datastore.Property, 0)
	switch v.(type) {
	case string:
		p := datastore.Property{Name: k}
		s := v.(string)
		if strings.HasPrefix(s, "[]") {
			p.Value = []byte(strings.TrimPrefix(s, "[]"))
			p.NoIndex = true
			propertyList = append(propertyList, p)
		} else {
			if dt, err := wcg.ParseDateTime(fmt.Sprintf("%s", v)); err == nil {
				p.Value = dt
				propertyList = append(propertyList, p)
			} else if d, err := wcg.ParseDate(fmt.Sprintf("%s", v)); err == nil {
				p.Value = d
				propertyList = append(propertyList, p)
			} else {
				p.Value = s
				propertyList = append(propertyList, p)
			}
		}
	case float64:
		str := []byte(fmt.Sprintf("%f", v))
		if _float_re.Match(str) {
			// should be int.
			propertyList = append(propertyList, datastore.Property{
				Name:  k,
				Value: int64(v.(float64)),
			})
		} else {
			propertyList = append(propertyList, datastore.Property{
				Name:  k,
				Value: v,
			})
		}
	case bool:
		propertyList = append(propertyList, datastore.Property{
			Name:  k,
			Value: v,
		})
	case []interface{}:
		for _, v1 := range v.([]interface{}) {
			propertyList = append(propertyList, datastore.Property{
				Name:     k,
				Value:    v1,
				Multiple: true,
			})
		}
	}
	return propertyList
}
Ejemplo n.º 2
0
func updateAttributesFromForm(res *wcg.Response, req *wcg.Request, p *blog.Post) error {
	var err error
	if err = postFormValidator.Eval(req.HttpRequest().PostForm); err != nil {
		return err
	}
	p.Title = req.Form("title")
	p.Content = req.Form("content")
	p.Tags = blog.ParseTagString(req.Form("tags"))
	p.IsDraft = req.Form("is_draft") == "true"
	p.IsHidden = req.Form("is_hidden") == "true"
	p.PublishAt, err = wcg.ParseDateTime(req.Form("publish_at"))
	return nil
}
Ejemplo n.º 3
0
func TestIepgBulkUpdate(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := wcg.NewAssert(t)
		d := NewIEpgDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))
		iepg1 := genTestIEpg()
		iepg1.ProgramTitle = "new"
		iepg1.StartAt, _ = wcg.ParseDateTime("2014/11/10 12:00")
		iepg1.EndAt, _ = wcg.ParseDateTime("2014/11/10 12:30")
		_, err := d.BulkUpdate([]*tv.IEpg{iepg1})
		assert.Nil(err, "IEpgDriver#BulkUpdate should not return an error (insert).")

		// insert
		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm Iepg entities has been stored within a timeout window.")

		// update
		iepg1.ProgramTitle = "update"
		_, err = d.BulkUpdate([]*tv.IEpg{iepg1})
		assert.Nil(err, "IEpgDriver#BulkUpdate should not return an error (update).")
		err = util.WaitFor(func() bool {
			found, err := d.Load(iepg1.Id)
			return err == nil && found.ProgramTitle == "update"
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm Iepg entities has been updated within a timeout window.")

		// Optout
		iepg1.Optout = true
		d.SyncSave(iepg1)

		iepg1.Optout = false
		iepg1.ProgramTitle = "optout"
		keys, _ := d.BulkUpdate([]*tv.IEpg{iepg1})
		assert.EqInt(0, len(keys), "IEpgDriver#BulkUpdate should not update optout entity.")
	})
}
Ejemplo n.º 4
0
func (ovt *FormValidationTarget) GetFieldValue(field string) interface{} {
	values := ovt.values[field]
	typed := ovt.fieldTypes[field]
	if values != nil {
		if len(values) > 0 {
			switch typed {
			case fieldTypeBool:
				return values[0] == "true" || values[0] == "1"
			case fieldTypeInt:
				v, _ := strconv.Atoi(values[0])
				return v
			case fieldTypeDate:
				v, e := wcg.ParseDate(values[0])
				if e != nil {
					return nil
				} else {
					return v
				}
			case fieldTypeDateTime:
				v, e := wcg.ParseDateTime(values[0])
				if e != nil {
					return nil
				} else {
					return v
				}
			default: // string
				return values[0]
			}
		} else {
			switch typed {
			case fieldTypeBool:
				return false
			case fieldTypeInt:
				return 0
			default: // string
				return ""
			}
		}
	} else {
		return nil
	}
}