Пример #1
0
func TestPutPropertyLoadSaver(t *testing.T) {
	c, closeFunc := NewContext(t)
	defer closeFunc()

	type testEntity struct {
		IntVal int
	}

	te := &testEntity{2}
	pl, err := datastore.SaveStruct(te)
	if err != nil {
		t.Fatal(err)
	}

	keys := []*datastore.Key{datastore.NewKey(c, "Test", "", 1, nil)}

	pls := datastore.PropertyList(pl)
	if _, err := nds.PutMulti(c, keys,
		[]datastore.PropertyLoadSaver{&pls}); err != nil {
		t.Fatal(err)
	}

	getPl := datastore.PropertyList{}
	if err := nds.GetMulti(c,
		keys, []datastore.PropertyLoadSaver{&getPl}); err != nil {
		t.Fatal(err)
	}
	getTe := &testEntity{}
	if err := datastore.LoadStruct(getTe, getPl); err != nil {
		t.Fatal(err)
	}
	if te.IntVal != getTe.IntVal {
		t.Fatal("expected same IntVal", getTe.IntVal)
	}
}
Пример #2
0
// Run invokes handler with ctx.
// It returns an *httptest.ResponseRecorder containing the result of the invocation.
func (s *HandlerState) Run(ctx context.Context, handler kami.HandlerFunc) *httptest.ResponseRecorder {

	// marshal body and convert config
	var requestBody *bytes.Buffer
	var data []byte

	if s.body == nil {
		data = nil
	} else if d, err := json.Marshal(s.body); err != nil {
		panic(err)
	} else {
		data = d
	}

	requestBody = bytes.NewBuffer(data)

	w := httptest.NewRecorder()
	r, err := http.NewRequest("GET", "/", requestBody)
	if err != nil {
		panic(err)
	}

	var configPropList datastore.PropertyList

	if s.config != nil {

		props, err := datastore.SaveStruct(s.config)
		if err != nil {
			panic(err)
		}
		configPropList = props
	} else {
		configPropList = datastore.PropertyList{}
	}

	ctx = context.WithValue(ctx, internal.ConfigContextKey, &configPropList)
	ctx = context.WithValue(ctx, internal.AuthContextKey, s.account)
	if s.scope != "" {
		ctx = context.WithValue(ctx, internal.ClaimSetContextKey, &jws.ClaimSet{
			Scope: s.scope,
			Sub:   s.account.Email,
			Exp:   time.Now().AddDate(0, 0, 1).Unix(),
		})
	} else {
		ctx = context.WithValue(ctx, internal.ClaimSetContextKey, &jws.ClaimSet{
			Scope: strings.Join(s.account.Roles, ","),
			Sub:   s.account.Email,
			Exp:   time.Now().AddDate(0, 0, 1).Unix(),
		})
	}

	ctx = context.WithValue(ctx, internal.ParamContextKey, s.routeParams)

	handler(ctx, w, r)

	return w
}
Пример #3
0
func (ac *AppConfig) Save() ([]datastore.Property, error) {
	now := time.Now()
	ac.UpdatedAt = now

	if ac.CreatedAt.IsZero() {
		ac.CreatedAt = now
	}

	return datastore.SaveStruct(ac)
}
Пример #4
0
// Save implements the datastore PropertyLoadSaver imterface
func (s *shard) Save() ([]datastore.Property, error) {
	props, err := datastore.SaveStruct(s)

	cprops, err := s.common.Save()
	if err != nil {
		return nil, err
	}
	props = append(props, cprops...)

	return props, err
}
Пример #5
0
func TestLoadSaveStruct(t *testing.T) {
	type testEntity struct {
		IntValue      int `datastore:",noindex"`
		StringValue   string
		MultipleValue []int64
	}

	te := testEntity{10, "ten", []int64{1, 2, 3}}
	pl, err := datastore.SaveStruct(&te)
	if err != nil {
		t.Fatal(err)
	}

	tests := []struct {
		name     string
		value    interface{}
		noIndex  bool
		multiple bool
	}{
		{"IntValue", int64(10), true, false},
		{"StringValue", "ten", false, false},
		{"MultipleValue", int64(1), false, true},
		{"MultipleValue", int64(2), false, true},
		{"MultipleValue", int64(3), false, true},
	}

	for i, test := range tests {
		prop := pl[i]
		if prop.Name != test.name {
			t.Fatal("incorrect name")
		}
		if prop.Value != test.value {
			t.Fatalf("incorrect value required %+v got %+v",
				prop.Value, test.value)
		}
		if prop.NoIndex != test.noIndex {
			t.Fatal("incorrect no index")
		}
		if prop.Multiple != test.multiple {
			t.Fatal("incorrect multiple flag")
		}
	}

	loadTestEntity := testEntity{}
	if err := datastore.LoadStruct(&loadTestEntity, pl); err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(te, loadTestEntity) {
		t.Fatal("entities not equal")
	}
}
Пример #6
0
func (t *Task) Save() ([]datastore.Property, error) {
	var ps []datastore.Property

	tps, err := datastore.SaveStruct(&t.Assignment)
	if err != nil {
		return nil, err
	}
	ps = append(ps, tps...)

	tps, err = datastore.SaveStruct(&t.SkillWeights)
	if err != nil {
		return nil, err
	}
	ps = append(ps, tps...)

	buf := new(bytes.Buffer)

	if err := gob.NewEncoder(buf).Encode(t.Templates); err != nil {
		return nil, err
	}

	return append(ps, []datastore.Property{
		{
			Name:    "Tasker",
			Value:   int64(t.Tasker),
			NoIndex: true,
		},
		{
			Name:  "Languages",
			Value: strings.Join(t.Languages, " "),
		},
		{
			Name:    "Templates",
			Value:   buf.Bytes(),
			NoIndex: true,
		},
	}...), nil
}
Пример #7
0
// Save implements the datastore PropertyLoadSaver imterface
func (n *namespace) Save() ([]datastore.Property, error) {
	props, err := datastore.SaveStruct(n)
	if err != nil {
		return nil, err
	}

	cprops, err := n.common.Save()
	if err != nil {
		return nil, err
	}
	props = append(props, cprops...)

	return props, nil
}
Пример #8
0
// Save implements the datastore PropertyLoadSaver imterface
func (it *iterator) Save() ([]datastore.Property, error) {
	props, err := datastore.SaveStruct(it)
	if err != nil {
		return nil, err
	}

	cprops, err := it.common.Save()
	if err != nil {
		return nil, err
	}
	props = append(props, cprops...)

	return props, nil
}
Пример #9
0
// Save changes the application configuration to
// the values in conf. All HTTP requests subsequent to this one
// are guaranteed to use the new values in their configuration.
//
// Note that subsequent calls to Get with the same request context
// will continue to retrieve the old version of the configuration.
//
// As a special case, calling Save with a *config.Config will replace
// the entire contents of the configuration with the contents of Config.
func Save(ctx context.Context, conf interface{}) error {

	if typedConfig, ok := conf.(*Config); ok {
		pl := datastore.PropertyList(*typedConfig)
		replaceKey := datastore.NewKey(ctx, Entity, Entity, 0, nil)
		_, replaceErr := nds.Put(ctx, replaceKey, &pl)
		return replaceErr
	}

	return datastore.RunInTransaction(ctx, func(txCtx context.Context) error {

		props := datastore.PropertyList{}

		key := datastore.NewKey(txCtx, Entity, Entity, 0, nil)
		if err := nds.Get(txCtx, key, &props); err != nil && err != datastore.ErrNoSuchEntity {
			return err
		}

		// merge existing config with the new values
		if newProps, err := datastore.SaveStruct(conf); err != nil {
			return err
		} else {

			for _, newProp := range newProps {
				newProp.NoIndex = true
				replacing := false
				for _, prop := range props {
					// make sure NoIndex is set
					prop.NoIndex = true
					if prop.Name == newProp.Name {
						replacing = true
						prop.Value = newProp.Value
						break
					}
				}
				if !replacing {
					// append
					props = append(props, newProp)
				}

			}

		}

		_, err := nds.Put(txCtx, key, &props)
		return err

	}, nil)

}
Пример #10
0
// Save implements the datastore PropertyLoadSaver imterface
func (j *job) Save() ([]datastore.Property, error) {
	props, err := datastore.SaveStruct(j)
	if err != nil {
		return nil, err
	}

	jprops, err := j.common.Save()
	if err != nil {
		return nil, err
	}
	props = append(props, jprops...)

	payload := new(bytes.Buffer)
	enc := gob.NewEncoder(payload)
	if err := enc.Encode(&j.JobSpec); err == nil {
		props = append(props, datastore.Property{Name: "job_spec", Value: payload.Bytes(), NoIndex: true, Multiple: false})
	}

	return props, nil
}
Пример #11
0
func (c *common) Save() ([]datastore.Property, error) {
	c.ProcessTime += getTime().Sub(c.startTime)

	props, err := datastore.SaveStruct(c)
	if err != nil {
		return nil, err
	}

	for key, value := range c.Counters {
		props = append(props, datastore.Property{Name: "counters." + key, Value: value, NoIndex: true, Multiple: false})
	}

	b, err := c.Query.GobEncode()
	if err != nil {
		return nil, err
	}
	props = append(props, datastore.Property{Name: "query", Value: b, NoIndex: true, Multiple: false})

	return props, nil
}
Пример #12
0
func TestGetMultiPropertyLoadSaver(t *testing.T) {
	c, closeFunc := NewContext(t)
	defer closeFunc()

	type testEntity struct {
		IntVal int
	}

	keys := []*datastore.Key{}
	entities := []datastore.PropertyList{}

	for i := 1; i < 3; i++ {
		keys = append(keys, datastore.NewKey(c, "Entity", "", int64(i), nil))

		pl, err := datastore.SaveStruct(&testEntity{i})
		if err != nil {
			t.Fatal(err)
		}
		entities = append(entities, pl)
	}

	if _, err := nds.PutMulti(c, keys, entities); err != nil {
		t.Fatal(err)
	}

	// Prime the cache.
	uncachedEntities := make([]datastore.PropertyList, len(keys))
	if err := nds.GetMulti(c, keys, uncachedEntities); err != nil {
		t.Fatal(err)
	}

	for i, e := range entities {
		if !reflect.DeepEqual(e, uncachedEntities[i]) {
			t.Fatal("uncachedEntities not equal", e, uncachedEntities[i])
		}
	}

	// Use cache.
	cachedEntities := make([]datastore.PropertyList, len(keys))
	if err := nds.GetMulti(c, keys, cachedEntities); err != nil {
		t.Fatal(err)
	}

	for i, e := range entities {
		if !reflect.DeepEqual(e, cachedEntities[i]) {
			t.Fatal("cachedEntities not equal", e, cachedEntities[i])
		}
	}

	// We know the datastore supports property load saver but we need to make
	// sure that memcache does by ensuring memcache does not error when we
	// change to fetching with structs.
	// Do this by making sure the datastore is not called on this following
	// GetMulti as memcache should have worked.
	nds.SetDatastoreGetMulti(func(c context.Context,
		keys []*datastore.Key, vals interface{}) error {
		if len(keys) != 0 {
			return errors.New("should not be called")
		}
		return nil
	})
	defer func() {
		nds.SetDatastoreGetMulti(datastore.GetMulti)
	}()
	tes := make([]testEntity, len(entities))
	if err := nds.GetMulti(c, keys, tes); err != nil {
		t.Fatal(err)
	}
}
Пример #13
0
// SaveStruct wraps datastore.SaveStruct
func (d *Driver) SaveStruct(src interface{}) ([]datastore.Property, error) {
	return datastore.SaveStruct(src)
}
Пример #14
0
func (v *ValueGAE) Save(c chan<- Property) error {
	defer close(c)
	return datastore.SaveStruct(v, c)
}