示例#1
0
文件: func.go 项目: speedland/apps
func iterateSearchKeyword(iter *yauction.AuctionSearchIterator, logger wcg.Logger) ([]*yauction.AuctionItem, error) {
	logger.Debug("Start iteration of %q", iter.Keyword)
	list := make([]*yauction.AuctionItem, 0)
	for {
		page := iter.Page
		logger.Debug("Search(%q, %d) ", iter.Keyword, page)
		found, err := iter.Next()
		if found != nil {
			logger.Debug(
				"Search(%q, %d): head = %q, (len: %d)",
				iter.Keyword,
				page,
				found[0].Id,
				len(found),
			)
			list = append(list, found...)
		}
		if err != nil {
			return list, err
		}
	}
}
示例#2
0
文件: fixture.go 项目: speedland/wcg
func loadJsonToDatastore(ctx appengine.Context, pkey *datastore.Key, data map[string]interface{}, logger wcg.Logger) error {
	var kind string
	var ns string
	var keyval interface{}
	var key *datastore.Key
	var ok bool
	var err error
	if _, ok = data["_kind"]; !ok {
		return fmt.Errorf("Missing key `_kind`")
	} else {
		kind = data["_kind"].(string)
	}
	if keyval, ok = data["_key"]; !ok {
		return fmt.Errorf("Missing key `_key`")
	}
	if _, ok = data["_ns"]; ok {
		ns = data["_ns"].(string)
		ctx, err = appengine.Namespace(ctx, ns)
		if err != nil {
			return err
		}
	}

	switch keyval.(type) {
	case int64:
		key = datastore.NewKey(ctx, kind, "", keyval.(int64), pkey)
	case string:
		key = datastore.NewKey(ctx, kind, keyval.(string), 0, pkey)
	default:
		return fmt.Errorf("Invalid `_key` type.")
	}
	if _, err := datastore.Put(ctx, key, JsonSaver(data)); err != nil {
		return err
	}
	// Check the data is actually stored.
	if err := wcg.RetryUntil(func() error {
		var v JsonSaver
		if err := datastore.Get(ctx, key, &v); err != nil {
			return fmt.Errorf(
				"fixture is not synched on '%s:[%s]': internal error?(%v) on ",
				kind, keyval, err,
			)
		}
		return nil
	}, 5*time.Second, 500*time.Millisecond); err != nil {
		return err
	}
	logger.Debug("[Fixture] %s%s -- %v", ns, key, data)

	for _, v := range data {
		// Child object
		if child, ok := v.(map[string]interface{}); ok {
			if err := loadJsonToDatastore(ctx, key, child, logger); err != nil {
				return err
			}
			continue
		}
		// Array
		if children, ok := v.([]interface{}); ok {
			for _, child := range children {
				if c, ok := child.(map[string]interface{}); ok {
					if err := loadJsonToDatastore(ctx, key, c, logger); err != nil {
						return err
					}
				}
			}
			continue
		}
	}
	return nil
}